A code sample showing the use of the asynchronous Serial APIs.
Dependencies: mbed
Beware: this example is only compatible with boards supporting the asynchronous APIs!
Information
All examples in this repo are considered EXPERIMENTAL QUALITY, meaning this code has been created as one-off proof-of-concept and is suitable as a demonstration for experimental purposes only. This code will not be regularly maintained by Silicon Labs and there is no guarantee that these projects will work across all environments, SDK versions and hardware.
main.cpp@1:f6715f1bf6a6, 2015-05-13 (annotated)
- Committer:
- stevew817
- Date:
- Wed May 13 09:01:35 2015 +0000
- Revision:
- 1:f6715f1bf6a6
switch to prebuilt mbed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stevew817 | 1:f6715f1bf6a6 | 1 | #include "mbed.h" |
stevew817 | 1:f6715f1bf6a6 | 2 | |
stevew817 | 1:f6715f1bf6a6 | 3 | /*------------ Constant definitions --------------*/ |
stevew817 | 1:f6715f1bf6a6 | 4 | #define TX_PIN USBTX |
stevew817 | 1:f6715f1bf6a6 | 5 | #define RX_PIN USBRX |
stevew817 | 1:f6715f1bf6a6 | 6 | #define BRATE 115200 |
stevew817 | 1:f6715f1bf6a6 | 7 | #define LED_PIN LED0 |
stevew817 | 1:f6715f1bf6a6 | 8 | #define TOGGLE_RATE (0.5f) |
stevew817 | 1:f6715f1bf6a6 | 9 | #define BUFF_LENGTH 5 |
stevew817 | 1:f6715f1bf6a6 | 10 | |
stevew817 | 1:f6715f1bf6a6 | 11 | /*-------- Check if platform compatible ----------*/ |
stevew817 | 1:f6715f1bf6a6 | 12 | #if DEVICE_SERIAL_ASYNCH |
stevew817 | 1:f6715f1bf6a6 | 13 | Serial test_connection(USBTX, USBRX); |
stevew817 | 1:f6715f1bf6a6 | 14 | #else |
stevew817 | 1:f6715f1bf6a6 | 15 | #error "Platform not compatible with Low Power APIs for Serial" |
stevew817 | 1:f6715f1bf6a6 | 16 | #endif |
stevew817 | 1:f6715f1bf6a6 | 17 | |
stevew817 | 1:f6715f1bf6a6 | 18 | /*------------------ Variables -------------------*/ |
stevew817 | 1:f6715f1bf6a6 | 19 | LowPowerTicker blinker; |
stevew817 | 1:f6715f1bf6a6 | 20 | bool blinking = false; |
stevew817 | 1:f6715f1bf6a6 | 21 | event_callback_t serialEventCb; |
stevew817 | 1:f6715f1bf6a6 | 22 | DigitalOut LED; |
stevew817 | 1:f6715f1bf6a6 | 23 | char rx_buf[BUFF_LENGTH + 1]; |
stevew817 | 1:f6715f1bf6a6 | 24 | |
stevew817 | 1:f6715f1bf6a6 | 25 | /*------------------ Callbacks -------------------*/ |
stevew817 | 1:f6715f1bf6a6 | 26 | void blink(void) { |
stevew817 | 1:f6715f1bf6a6 | 27 | LED = ~LED; |
stevew817 | 1:f6715f1bf6a6 | 28 | } |
stevew817 | 1:f6715f1bf6a6 | 29 | |
stevew817 | 1:f6715f1bf6a6 | 30 | void serialCb(int events) { |
stevew817 | 1:f6715f1bf6a6 | 31 | /* Something triggered the callback, either buffer is full or 'S' is received */ |
stevew817 | 1:f6715f1bf6a6 | 32 | unsigned char i = 0; |
stevew817 | 1:f6715f1bf6a6 | 33 | if(events & SERIAL_EVENT_RX_CHARACTER_MATCH) { |
stevew817 | 1:f6715f1bf6a6 | 34 | //Received 'S', check for buffer length |
stevew817 | 1:f6715f1bf6a6 | 35 | for(unsigned char i; i < BUFF_LENGTH; i++) { |
stevew817 | 1:f6715f1bf6a6 | 36 | //Found the length! |
stevew817 | 1:f6715f1bf6a6 | 37 | if(rx_buf[i] == 'S') break; |
stevew817 | 1:f6715f1bf6a6 | 38 | } |
stevew817 | 1:f6715f1bf6a6 | 39 | |
stevew817 | 1:f6715f1bf6a6 | 40 | // Toggle blinking |
stevew817 | 1:f6715f1bf6a6 | 41 | if(blinking) { |
stevew817 | 1:f6715f1bf6a6 | 42 | blinker.detach(); |
stevew817 | 1:f6715f1bf6a6 | 43 | blinking = false; |
stevew817 | 1:f6715f1bf6a6 | 44 | } else { |
stevew817 | 1:f6715f1bf6a6 | 45 | blinker.attach(blink, TOGGLE_RATE); |
stevew817 | 1:f6715f1bf6a6 | 46 | blinking = true; |
stevew817 | 1:f6715f1bf6a6 | 47 | } |
stevew817 | 1:f6715f1bf6a6 | 48 | } else if (events & SERIAL_EVENT_RX_COMPLETE) { |
stevew817 | 1:f6715f1bf6a6 | 49 | i = BUFF_LENGTH - 1; |
stevew817 | 1:f6715f1bf6a6 | 50 | } else { |
stevew817 | 1:f6715f1bf6a6 | 51 | rx_buf[0] = 'E'; |
stevew817 | 1:f6715f1bf6a6 | 52 | rx_buf[1] = 'R'; |
stevew817 | 1:f6715f1bf6a6 | 53 | rx_buf[2] = 'R'; |
stevew817 | 1:f6715f1bf6a6 | 54 | rx_buf[3] = '!'; |
stevew817 | 1:f6715f1bf6a6 | 55 | rx_buf[4] = 0; |
stevew817 | 1:f6715f1bf6a6 | 56 | i = 3; |
stevew817 | 1:f6715f1bf6a6 | 57 | } |
stevew817 | 1:f6715f1bf6a6 | 58 | |
stevew817 | 1:f6715f1bf6a6 | 59 | // Echo string, no callback |
stevew817 | 1:f6715f1bf6a6 | 60 | test_connection.write(rx_buf, i+1, (event_callback_t*)0, 0); |
stevew817 | 1:f6715f1bf6a6 | 61 | |
stevew817 | 1:f6715f1bf6a6 | 62 | // Reset serial reception |
stevew817 | 1:f6715f1bf6a6 | 63 | test_connection.read(rx_buf, BUFF_LENGTH, &serialEventCb, SERIAL_EVENT_RX_ALL, 'S'); |
stevew817 | 1:f6715f1bf6a6 | 64 | } |
stevew817 | 1:f6715f1bf6a6 | 65 | |
stevew817 | 1:f6715f1bf6a6 | 66 | /*-------------------- Main ----------------------*/ |
stevew817 | 1:f6715f1bf6a6 | 67 | int main() { |
stevew817 | 1:f6715f1bf6a6 | 68 | /* Very Simple Main (tm) */ |
stevew817 | 1:f6715f1bf6a6 | 69 | serialEventCb.attach(serialCb); |
stevew817 | 1:f6715f1bf6a6 | 70 | |
stevew817 | 1:f6715f1bf6a6 | 71 | /* Setup serial connection */ |
stevew817 | 1:f6715f1bf6a6 | 72 | test_connection.baud(BRATE); |
stevew817 | 1:f6715f1bf6a6 | 73 | test_connection.printf("Low Power API test\n\nSend 'S' to toggle blinking\n"); |
stevew817 | 1:f6715f1bf6a6 | 74 | test_connection.read(rx_buf, BUFF_LENGTH, &serialEventCb, SERIAL_EVENT_RX_ALL, 'S'); |
stevew817 | 1:f6715f1bf6a6 | 75 | |
stevew817 | 1:f6715f1bf6a6 | 76 | /* Let the callbacks take care of everything */ |
stevew817 | 1:f6715f1bf6a6 | 77 | while(1) sleep(); |
stevew817 | 1:f6715f1bf6a6 | 78 | } |