Ryo Od
/
Nucleo_rtos_SPISlave_Test
SPI Slave Test.
main.cpp@4:0051fa8b5475, 2016-10-07 (annotated)
- Committer:
- ryood
- Date:
- Fri Oct 07 09:03:07 2016 +0000
- Revision:
- 4:0051fa8b5475
- Parent:
- 3:938e95903872
- Child:
- 5:ca0be3b8b8a6
UART print SPI recieved parameter
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 | 1:ba17cd3b6ecf | 22 | unsigned int step = 0; |
ryood | 1:ba17cd3b6ecf | 23 | |
ryood | 1:ba17cd3b6ecf | 24 | void stepUp(void const* arg) |
ryood | 1:ba17cd3b6ecf | 25 | { |
ryood | 1:ba17cd3b6ecf | 26 | step++; |
ryood | 1:ba17cd3b6ecf | 27 | |
ryood | 4:0051fa8b5475 | 28 | |
ryood | 4:0051fa8b5475 | 29 | // Masterにinterruptをかける。 |
ryood | 1:ba17cd3b6ecf | 30 | StepChangePin.write(1); |
ryood | 1:ba17cd3b6ecf | 31 | StepChangePin.write(0); |
ryood | 1:ba17cd3b6ecf | 32 | } |
ryood | 1:ba17cd3b6ecf | 33 | |
ryood | 0:4cc5b11f7d91 | 34 | int main() |
ryood | 0:4cc5b11f7d91 | 35 | { |
ryood | 0:4cc5b11f7d91 | 36 | printf("\r\n\nNucleo rtos SPISlave Test..\r\n"); |
ryood | 0:4cc5b11f7d91 | 37 | |
ryood | 1:ba17cd3b6ecf | 38 | // Setup LED |
ryood | 0:4cc5b11f7d91 | 39 | for (int i = 0; i < 5; i++) { |
ryood | 0:4cc5b11f7d91 | 40 | Leds.write(0x3f); |
ryood | 0:4cc5b11f7d91 | 41 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 42 | Leds.write(0x00); |
ryood | 0:4cc5b11f7d91 | 43 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 44 | } |
ryood | 0:4cc5b11f7d91 | 45 | |
ryood | 1:ba17cd3b6ecf | 46 | // Setup SPISlave |
ryood | 4:0051fa8b5475 | 47 | SpiS.format(16, 0); |
ryood | 0:4cc5b11f7d91 | 48 | SpiS.frequency(SPI_SPEED); |
ryood | 0:4cc5b11f7d91 | 49 | |
ryood | 1:ba17cd3b6ecf | 50 | // RtosTimer |
ryood | 1:ba17cd3b6ecf | 51 | RtosTimer stepTimer(stepUp, osTimerPeriodic, (void *)0); |
ryood | 4:0051fa8b5475 | 52 | stepTimer.start(125); |
ryood | 1:ba17cd3b6ecf | 53 | |
ryood | 0:4cc5b11f7d91 | 54 | SpiS.reply(0); |
ryood | 0:4cc5b11f7d91 | 55 | while(1) { |
ryood | 0:4cc5b11f7d91 | 56 | if(SpiS.receive()) { |
ryood | 4:0051fa8b5475 | 57 | SpiS.reply(step % 16); |
ryood | 0:4cc5b11f7d91 | 58 | int v = SpiS.read(); // Read byte from master |
ryood | 0:4cc5b11f7d91 | 59 | Leds.write(v); |
ryood | 4:0051fa8b5475 | 60 | printf("%04x\r\n", v); |
ryood | 0:4cc5b11f7d91 | 61 | } |
ryood | 0:4cc5b11f7d91 | 62 | } |
ryood | 0:4cc5b11f7d91 | 63 | } |