Ryo Od
/
Nucleo_rtos_SPISlave_Test
SPI Slave Test.
main.cpp@5:ca0be3b8b8a6, 2016-10-07 (annotated)
- Committer:
- ryood
- Date:
- Fri Oct 07 09:57:56 2016 +0000
- Revision:
- 5:ca0be3b8b8a6
- Parent:
- 4:0051fa8b5475
for BaseMachine_UI_Controller Revision:45
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 3:938e95903872 | 1 | /* |
ryood | 3:938e95903872 | 2 | * SPI Slave Test for BaseMachine UI Controller |
ryood | 3:938e95903872 | 3 | * |
ryood | 3:938e95903872 | 4 | * 2016.10.02 |
ryood | 3:938e95903872 | 5 | * |
ryood | 3:938e95903872 | 6 | */ |
ryood | 3:938e95903872 | 7 | |
ryood | 0:4cc5b11f7d91 | 8 | #include "mbed.h" |
ryood | 0:4cc5b11f7d91 | 9 | #include "rtos.h" |
ryood | 0:4cc5b11f7d91 | 10 | #include "SPISlave.h" |
ryood | 0:4cc5b11f7d91 | 11 | |
ryood | 4:0051fa8b5475 | 12 | #define SPI_SPEED (3000000) |
ryood | 0:4cc5b11f7d91 | 13 | |
ryood | 0:4cc5b11f7d91 | 14 | BusOut Leds(PA_10, PB_3, PB_5, PB_4, PB_10, PA_8); |
ryood | 2:46e25b11a043 | 15 | //DigitalOut StepChangePin(PC_7); |
ryood | 2:46e25b11a043 | 16 | DigitalOut StepChangePin(PB_1); |
ryood | 0:4cc5b11f7d91 | 17 | |
ryood | 2:46e25b11a043 | 18 | //SPISlave SpiS(PA_7, PA_6, PA_5, PA_4); // mosi, miso, sclk, ssel |
ryood | 2:46e25b11a043 | 19 | // SPI2 |
ryood | 2:46e25b11a043 | 20 | SPISlave SpiS(PB_15, PB_14, PB_13, PB_12); // mosi, miso, sclk, ssel |
ryood | 0:4cc5b11f7d91 | 21 | |
ryood | 5:ca0be3b8b8a6 | 22 | enum SequencerCmd { |
ryood | 5:ca0be3b8b8a6 | 23 | CMD_RCV_PLAYING_STEP = 0x01, |
ryood | 5:ca0be3b8b8a6 | 24 | CMD_RUN = 0x11, |
ryood | 5:ca0be3b8b8a6 | 25 | CMD_BPM = 0x12, |
ryood | 5:ca0be3b8b8a6 | 26 | CMD_ACCENT_LEVEL = 0x13, |
ryood | 5:ca0be3b8b8a6 | 27 | CMD_WAVE_SHAPE = 0x21, |
ryood | 5:ca0be3b8b8a6 | 28 | CMD_PULSE_WIDTH = 0x22, |
ryood | 5:ca0be3b8b8a6 | 29 | CMD_CUTOFF = 0x31, |
ryood | 5:ca0be3b8b8a6 | 30 | CMD_RESONANCE = 0x32, |
ryood | 5:ca0be3b8b8a6 | 31 | CMD_LEVEL = 0x41, |
ryood | 5:ca0be3b8b8a6 | 32 | CMD_DURATION = 0x42, |
ryood | 5:ca0be3b8b8a6 | 33 | CMD_DECAY = 0x43, |
ryood | 5:ca0be3b8b8a6 | 34 | CMD_SUSTAIN = 0x44, |
ryood | 5:ca0be3b8b8a6 | 35 | CMD_NOTE = 0x51, |
ryood | 5:ca0be3b8b8a6 | 36 | CMD_PITCH = 0x52, |
ryood | 5:ca0be3b8b8a6 | 37 | }; |
ryood | 5:ca0be3b8b8a6 | 38 | |
ryood | 1:ba17cd3b6ecf | 39 | unsigned int step = 0; |
ryood | 1:ba17cd3b6ecf | 40 | |
ryood | 1:ba17cd3b6ecf | 41 | void stepUp(void const* arg) |
ryood | 1:ba17cd3b6ecf | 42 | { |
ryood | 1:ba17cd3b6ecf | 43 | step++; |
ryood | 1:ba17cd3b6ecf | 44 | |
ryood | 4:0051fa8b5475 | 45 | |
ryood | 4:0051fa8b5475 | 46 | // Masterにinterruptをかける。 |
ryood | 1:ba17cd3b6ecf | 47 | StepChangePin.write(1); |
ryood | 1:ba17cd3b6ecf | 48 | StepChangePin.write(0); |
ryood | 1:ba17cd3b6ecf | 49 | } |
ryood | 1:ba17cd3b6ecf | 50 | |
ryood | 0:4cc5b11f7d91 | 51 | int main() |
ryood | 0:4cc5b11f7d91 | 52 | { |
ryood | 0:4cc5b11f7d91 | 53 | printf("\r\n\nNucleo rtos SPISlave Test..\r\n"); |
ryood | 0:4cc5b11f7d91 | 54 | |
ryood | 1:ba17cd3b6ecf | 55 | // Setup LED |
ryood | 0:4cc5b11f7d91 | 56 | for (int i = 0; i < 5; i++) { |
ryood | 0:4cc5b11f7d91 | 57 | Leds.write(0x3f); |
ryood | 0:4cc5b11f7d91 | 58 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 59 | Leds.write(0x00); |
ryood | 0:4cc5b11f7d91 | 60 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 61 | } |
ryood | 0:4cc5b11f7d91 | 62 | |
ryood | 1:ba17cd3b6ecf | 63 | // Setup SPISlave |
ryood | 4:0051fa8b5475 | 64 | SpiS.format(16, 0); |
ryood | 0:4cc5b11f7d91 | 65 | SpiS.frequency(SPI_SPEED); |
ryood | 0:4cc5b11f7d91 | 66 | |
ryood | 1:ba17cd3b6ecf | 67 | // RtosTimer |
ryood | 1:ba17cd3b6ecf | 68 | RtosTimer stepTimer(stepUp, osTimerPeriodic, (void *)0); |
ryood | 5:ca0be3b8b8a6 | 69 | //stepTimer.start(125); |
ryood | 1:ba17cd3b6ecf | 70 | |
ryood | 0:4cc5b11f7d91 | 71 | while(1) { |
ryood | 0:4cc5b11f7d91 | 72 | if(SpiS.receive()) { |
ryood | 4:0051fa8b5475 | 73 | SpiS.reply(step % 16); |
ryood | 5:ca0be3b8b8a6 | 74 | uint16_t v = SpiS.read(); |
ryood | 0:4cc5b11f7d91 | 75 | Leds.write(v); |
ryood | 5:ca0be3b8b8a6 | 76 | |
ryood | 5:ca0be3b8b8a6 | 77 | if (v >> 8 == CMD_RUN) { |
ryood | 5:ca0be3b8b8a6 | 78 | printf("CMD_RUN\r\n"); |
ryood | 5:ca0be3b8b8a6 | 79 | if ((v & 0xff) == 1) { |
ryood | 5:ca0be3b8b8a6 | 80 | stepTimer.start(125); |
ryood | 5:ca0be3b8b8a6 | 81 | } else { |
ryood | 5:ca0be3b8b8a6 | 82 | stepTimer.stop(); |
ryood | 5:ca0be3b8b8a6 | 83 | } |
ryood | 5:ca0be3b8b8a6 | 84 | } |
ryood | 5:ca0be3b8b8a6 | 85 | |
ryood | 4:0051fa8b5475 | 86 | printf("%04x\r\n", v); |
ryood | 0:4cc5b11f7d91 | 87 | } |
ryood | 0:4cc5b11f7d91 | 88 | } |
ryood | 0:4cc5b11f7d91 | 89 | } |