Helmut Tschemernjak
/
Nucleo-mbed-os-example-blinky
Test
Fork of Nucleo-mbed-os-example-blinky by
main.cpp@6:a40d570cbe50, 2017-01-29 (annotated)
- Committer:
- Helmut64
- Date:
- Sun Jan 29 10:18:53 2017 +0000
- Revision:
- 6:a40d570cbe50
- Parent:
- 5:7db87ff5ed38
Added HAL SPI testing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helmut64 | 0:681c27ac21c0 | 1 | #include "mbed.h" |
Helmut64 | 0:681c27ac21c0 | 2 | |
Helmut64 | 4:634ee710cadb | 3 | DigitalOut myled(LED1); |
Helmut64 | 4:634ee710cadb | 4 | InterruptIn event(USER_BUTTON); |
Helmut64 | 4:634ee710cadb | 5 | |
Helmut64 | 6:a40d570cbe50 | 6 | event_callback_t spiCb; |
Helmut64 | 6:a40d570cbe50 | 7 | |
Helmut64 | 6:a40d570cbe50 | 8 | #if DEVICE_SPI_ASYNCH |
Helmut64 | 6:a40d570cbe50 | 9 | #define SPI_S(obj) (( struct spi_s *)(&(obj->spi))) |
Helmut64 | 6:a40d570cbe50 | 10 | #else |
Helmut64 | 6:a40d570cbe50 | 11 | #define SPI_S(obj) (( struct spi_s *)(obj)) |
Helmut64 | 6:a40d570cbe50 | 12 | #endif |
Helmut64 | 6:a40d570cbe50 | 13 | |
Helmut64 | 4:634ee710cadb | 14 | bool pressed = false; |
Helmut64 | 6:a40d570cbe50 | 15 | bool transferDone = false; |
Helmut64 | 3:a6caeb32839c | 16 | |
Helmut64 | 4:634ee710cadb | 17 | void KeyPressed() |
Helmut64 | 4:634ee710cadb | 18 | { |
Helmut64 | 4:634ee710cadb | 19 | pressed = true; |
Helmut64 | 4:634ee710cadb | 20 | } |
Helmut64 | 0:681c27ac21c0 | 21 | |
Helmut64 | 6:a40d570cbe50 | 22 | void SPIDone(int events) |
Helmut64 | 6:a40d570cbe50 | 23 | { |
Helmut64 | 6:a40d570cbe50 | 24 | transferDone = true; |
Helmut64 | 6:a40d570cbe50 | 25 | } |
Helmut64 | 6:a40d570cbe50 | 26 | |
Helmut64 | 4:634ee710cadb | 27 | int main() { |
Helmut64 | 4:634ee710cadb | 28 | event.fall(&KeyPressed); |
Helmut64 | 6:a40d570cbe50 | 29 | |
Helmut64 | 6:a40d570cbe50 | 30 | spiCb.attach(SPIDone); |
Helmut64 | 6:a40d570cbe50 | 31 | |
Helmut64 | 4:634ee710cadb | 32 | while(1) { |
Helmut64 | 4:634ee710cadb | 33 | myled = 1; // LED is ON |
Helmut64 | 4:634ee710cadb | 34 | wait(0.2); // 200 ms |
Helmut64 | 4:634ee710cadb | 35 | myled = 0; // LED is OFF |
Helmut64 | 4:634ee710cadb | 36 | wait(1.0); // 1 sec |
Helmut64 | 4:634ee710cadb | 37 | if (pressed) { |
Helmut64 | 6:a40d570cbe50 | 38 | myled = 1; |
Helmut64 | 6:a40d570cbe50 | 39 | SPI *s = new SPI(PB_5, PB_4, PB_3); |
Helmut64 | 6:a40d570cbe50 | 40 | s->frequency(100000); |
Helmut64 | 6:a40d570cbe50 | 41 | |
Helmut64 | 6:a40d570cbe50 | 42 | struct spi_s *spiobj = SPI_S(((spi_t *)( (char *)s+4) )); |
Helmut64 | 6:a40d570cbe50 | 43 | SPI_HandleTypeDef *handle = &(spiobj->handle); |
Helmut64 | 6:a40d570cbe50 | 44 | |
Helmut64 | 6:a40d570cbe50 | 45 | |
Helmut64 | 6:a40d570cbe50 | 46 | for(int i = 0; i < 400000000; i++) { |
Helmut64 | 6:a40d570cbe50 | 47 | uint8_t send[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x00 }; |
Helmut64 | 6:a40d570cbe50 | 48 | uint8_t rcv[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
Helmut64 | 6:a40d570cbe50 | 49 | //s->write(0xff); |
Helmut64 | 6:a40d570cbe50 | 50 | HAL_SPI_Transmit(handle, send, 10, 10); |
Helmut64 | 6:a40d570cbe50 | 51 | //HAL_SPI_Receive(handle, rcv, 10, 10); |
Helmut64 | 6:a40d570cbe50 | 52 | |
Helmut64 | 6:a40d570cbe50 | 53 | } |
Helmut64 | 6:a40d570cbe50 | 54 | //s->transfer((uint8_t*)"aaa", 3, (uint8_t*)NULL, 0, spiCb, SPI_EVENT_COMPLETE); |
Helmut64 | 6:a40d570cbe50 | 55 | // s->transfer((uint8_t*)"aaa", 3, (uint8_t*)NULL, 0, spiCb); |
Helmut64 | 5:7db87ff5ed38 | 56 | delete s; |
Helmut64 | 4:634ee710cadb | 57 | deepsleep(); |
Helmut64 | 4:634ee710cadb | 58 | pressed = false; |
Helmut64 | 4:634ee710cadb | 59 | } |
Helmut64 | 0:681c27ac21c0 | 60 | } |
Helmut64 | 6:a40d570cbe50 | 61 | } |