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@2:46e25b11a043, 2016-10-01 (annotated)
- Committer:
- ryood
- Date:
- Sat Oct 01 12:33:07 2016 +0000
- Revision:
- 2:46e25b11a043
- Parent:
- 1:ba17cd3b6ecf
- Child:
- 3:e35c6a9ad906
- Child:
- 5:938e95903872
Change SPISlave to SPI2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:4cc5b11f7d91 | 1 | #include "mbed.h" |
ryood | 0:4cc5b11f7d91 | 2 | #include "rtos.h" |
ryood | 0:4cc5b11f7d91 | 3 | #include "SPISlave.h" |
ryood | 0:4cc5b11f7d91 | 4 | |
ryood | 0:4cc5b11f7d91 | 5 | #define SPI_SPEED (10000000) |
ryood | 0:4cc5b11f7d91 | 6 | |
ryood | 0:4cc5b11f7d91 | 7 | BusOut Leds(PA_10, PB_3, PB_5, PB_4, PB_10, PA_8); |
ryood | 2:46e25b11a043 | 8 | //DigitalOut StepChangePin(PC_7); |
ryood | 2:46e25b11a043 | 9 | DigitalOut StepChangePin(PB_1); |
ryood | 0:4cc5b11f7d91 | 10 | |
ryood | 2:46e25b11a043 | 11 | //SPISlave SpiS(PA_7, PA_6, PA_5, PA_4); // mosi, miso, sclk, ssel |
ryood | 2:46e25b11a043 | 12 | // SPI2 |
ryood | 2:46e25b11a043 | 13 | SPISlave SpiS(PB_15, PB_14, PB_13, PB_12); // mosi, miso, sclk, ssel |
ryood | 0:4cc5b11f7d91 | 14 | |
ryood | 1:ba17cd3b6ecf | 15 | unsigned int step = 0; |
ryood | 1:ba17cd3b6ecf | 16 | |
ryood | 1:ba17cd3b6ecf | 17 | void stepUp(void const* arg) |
ryood | 1:ba17cd3b6ecf | 18 | { |
ryood | 1:ba17cd3b6ecf | 19 | step++; |
ryood | 1:ba17cd3b6ecf | 20 | |
ryood | 1:ba17cd3b6ecf | 21 | // Slaveにinterruptをかける。 |
ryood | 1:ba17cd3b6ecf | 22 | StepChangePin.write(1); |
ryood | 1:ba17cd3b6ecf | 23 | StepChangePin.write(0); |
ryood | 1:ba17cd3b6ecf | 24 | } |
ryood | 1:ba17cd3b6ecf | 25 | |
ryood | 0:4cc5b11f7d91 | 26 | int main() |
ryood | 0:4cc5b11f7d91 | 27 | { |
ryood | 0:4cc5b11f7d91 | 28 | printf("\r\n\nNucleo rtos SPISlave Test..\r\n"); |
ryood | 0:4cc5b11f7d91 | 29 | |
ryood | 1:ba17cd3b6ecf | 30 | // Setup LED |
ryood | 0:4cc5b11f7d91 | 31 | for (int i = 0; i < 5; i++) { |
ryood | 0:4cc5b11f7d91 | 32 | Leds.write(0x3f); |
ryood | 0:4cc5b11f7d91 | 33 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 34 | Leds.write(0x00); |
ryood | 0:4cc5b11f7d91 | 35 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 36 | } |
ryood | 0:4cc5b11f7d91 | 37 | |
ryood | 1:ba17cd3b6ecf | 38 | // Setup SPISlave |
ryood | 0:4cc5b11f7d91 | 39 | SpiS.format(8, 0); |
ryood | 0:4cc5b11f7d91 | 40 | SpiS.frequency(SPI_SPEED); |
ryood | 0:4cc5b11f7d91 | 41 | |
ryood | 1:ba17cd3b6ecf | 42 | // RtosTimer |
ryood | 1:ba17cd3b6ecf | 43 | RtosTimer stepTimer(stepUp, osTimerPeriodic, (void *)0); |
ryood | 1:ba17cd3b6ecf | 44 | stepTimer.start(250); // BPM:60 |
ryood | 1:ba17cd3b6ecf | 45 | |
ryood | 0:4cc5b11f7d91 | 46 | SpiS.reply(0); |
ryood | 0:4cc5b11f7d91 | 47 | while(1) { |
ryood | 0:4cc5b11f7d91 | 48 | if(SpiS.receive()) { |
ryood | 0:4cc5b11f7d91 | 49 | int v = SpiS.read(); // Read byte from master |
ryood | 0:4cc5b11f7d91 | 50 | Leds.write(v); |
ryood | 1:ba17cd3b6ecf | 51 | SpiS.reply(step % 16); |
ryood | 0:4cc5b11f7d91 | 52 | } |
ryood | 0:4cc5b11f7d91 | 53 | } |
ryood | 0:4cc5b11f7d91 | 54 | } |