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@1:ba17cd3b6ecf, 2016-09-27 (annotated)
- Committer:
- ryood
- Date:
- Tue Sep 27 09:46:20 2016 +0000
- Revision:
- 1:ba17cd3b6ecf
- Parent:
- 0:4cc5b11f7d91
- Child:
- 2:46e25b11a043
impl. RtosTimer
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 | 1:ba17cd3b6ecf | 8 | DigitalOut StepChangePin(PC_7); |
ryood | 0:4cc5b11f7d91 | 9 | |
ryood | 0:4cc5b11f7d91 | 10 | SPISlave SpiS(PA_7, PA_6, PA_5, PA_4); // mosi, miso, sclk, ssel |
ryood | 0:4cc5b11f7d91 | 11 | |
ryood | 1:ba17cd3b6ecf | 12 | unsigned int step = 0; |
ryood | 1:ba17cd3b6ecf | 13 | |
ryood | 1:ba17cd3b6ecf | 14 | void stepUp(void const* arg) |
ryood | 1:ba17cd3b6ecf | 15 | { |
ryood | 1:ba17cd3b6ecf | 16 | step++; |
ryood | 1:ba17cd3b6ecf | 17 | |
ryood | 1:ba17cd3b6ecf | 18 | // Slaveにinterruptをかける。 |
ryood | 1:ba17cd3b6ecf | 19 | StepChangePin.write(1); |
ryood | 1:ba17cd3b6ecf | 20 | StepChangePin.write(0); |
ryood | 1:ba17cd3b6ecf | 21 | } |
ryood | 1:ba17cd3b6ecf | 22 | |
ryood | 0:4cc5b11f7d91 | 23 | int main() |
ryood | 0:4cc5b11f7d91 | 24 | { |
ryood | 0:4cc5b11f7d91 | 25 | printf("\r\n\nNucleo rtos SPISlave Test..\r\n"); |
ryood | 0:4cc5b11f7d91 | 26 | |
ryood | 1:ba17cd3b6ecf | 27 | // Setup LED |
ryood | 0:4cc5b11f7d91 | 28 | for (int i = 0; i < 5; i++) { |
ryood | 0:4cc5b11f7d91 | 29 | Leds.write(0x3f); |
ryood | 0:4cc5b11f7d91 | 30 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 31 | Leds.write(0x00); |
ryood | 0:4cc5b11f7d91 | 32 | Thread::wait(100); |
ryood | 0:4cc5b11f7d91 | 33 | } |
ryood | 0:4cc5b11f7d91 | 34 | |
ryood | 1:ba17cd3b6ecf | 35 | // Setup SPISlave |
ryood | 0:4cc5b11f7d91 | 36 | SpiS.format(8, 0); |
ryood | 0:4cc5b11f7d91 | 37 | SpiS.frequency(SPI_SPEED); |
ryood | 0:4cc5b11f7d91 | 38 | |
ryood | 1:ba17cd3b6ecf | 39 | // RtosTimer |
ryood | 1:ba17cd3b6ecf | 40 | RtosTimer stepTimer(stepUp, osTimerPeriodic, (void *)0); |
ryood | 1:ba17cd3b6ecf | 41 | stepTimer.start(250); // BPM:60 |
ryood | 1:ba17cd3b6ecf | 42 | |
ryood | 0:4cc5b11f7d91 | 43 | SpiS.reply(0); |
ryood | 0:4cc5b11f7d91 | 44 | while(1) { |
ryood | 0:4cc5b11f7d91 | 45 | if(SpiS.receive()) { |
ryood | 0:4cc5b11f7d91 | 46 | int v = SpiS.read(); // Read byte from master |
ryood | 0:4cc5b11f7d91 | 47 | Leds.write(v); |
ryood | 1:ba17cd3b6ecf | 48 | SpiS.reply(step % 16); |
ryood | 0:4cc5b11f7d91 | 49 | } |
ryood | 0:4cc5b11f7d91 | 50 | } |
ryood | 0:4cc5b11f7d91 | 51 | } |