Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Nucleo_rtos_SPISlave_Test by
main.cpp@5:938e95903872, 2016-10-01 (annotated)
- Committer:
- ryood
- Date:
- Sat Oct 01 23:30:56 2016 +0000
- Revision:
- 5:938e95903872
- Parent:
- 2:46e25b11a043
SPI Slave Test for BaseMachine UI Controller
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 5:938e95903872 | 1 | /* |
ryood | 5:938e95903872 | 2 | * SPI Slave Test for BaseMachine UI Controller |
ryood | 5:938e95903872 | 3 | * |
ryood | 5:938e95903872 | 4 | * 2016.10.02 |
ryood | 5:938e95903872 | 5 | * |
ryood | 5:938e95903872 | 6 | */ |
ryood | 5: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 | 0:4cc5b11f7d91 | 12 | #define SPI_SPEED (10000000) |
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 | 1:ba17cd3b6ecf | 28 | // Slaveにinterruptをかける。 |
ryood | 1:ba17cd3b6ecf | 29 | StepChangePin.write(1); |
ryood | 1:ba17cd3b6ecf | 30 | StepChangePin.write(0); |
ryood | 1:ba17cd3b6ecf | 31 | } |
ryood | 1:ba17cd3b6ecf | 32 | |
ryood | 0:4cc5b11f7d91 | 33 | int main() |
ryood | 0:4cc5b11f7d91 | 34 | { |
ryood | 0:4cc5b11f7d91 | 35 | printf("\r\n\nNucleo rtos SPISlave Test..\r\n"); |
ryood | 0:4cc5b11f7d91 | 36 | |
ryood | 1:ba17cd3b6ecf | 37 | // Setup LED |
ryood | 0:4cc5b11f7d91 | 38 | for (int i = 0; i < 5; i++) { |
ryood | 0:4cc5b11f7d91 | 39 | Leds.write(0x3f); |
ryood | 0:4cc5b11f7d91 | 40 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 41 | Leds.write(0x00); |
ryood | 0:4cc5b11f7d91 | 42 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 43 | } |
ryood | 0:4cc5b11f7d91 | 44 | |
ryood | 1:ba17cd3b6ecf | 45 | // Setup SPISlave |
ryood | 0:4cc5b11f7d91 | 46 | SpiS.format(8, 0); |
ryood | 0:4cc5b11f7d91 | 47 | SpiS.frequency(SPI_SPEED); |
ryood | 0:4cc5b11f7d91 | 48 | |
ryood | 1:ba17cd3b6ecf | 49 | // RtosTimer |
ryood | 1:ba17cd3b6ecf | 50 | RtosTimer stepTimer(stepUp, osTimerPeriodic, (void *)0); |
ryood | 5:938e95903872 | 51 | stepTimer.start(125); // BPM:120 |
ryood | 1:ba17cd3b6ecf | 52 | |
ryood | 0:4cc5b11f7d91 | 53 | SpiS.reply(0); |
ryood | 0:4cc5b11f7d91 | 54 | while(1) { |
ryood | 0:4cc5b11f7d91 | 55 | if(SpiS.receive()) { |
ryood | 0:4cc5b11f7d91 | 56 | int v = SpiS.read(); // Read byte from master |
ryood | 0:4cc5b11f7d91 | 57 | Leds.write(v); |
ryood | 1:ba17cd3b6ecf | 58 | SpiS.reply(step % 16); |
ryood | 0:4cc5b11f7d91 | 59 | } |
ryood | 0:4cc5b11f7d91 | 60 | } |
ryood | 0:4cc5b11f7d91 | 61 | } |