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@3:f911d11682f3, 2015-05-13 (annotated)
- Committer:
- stevew817
- Date:
- Wed May 13 12:09:20 2015 +0000
- Revision:
- 3:f911d11682f3
- Parent:
- 0:074157529cc4
- Child:
- 4:bbf422ee69e4
bugfixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Steven Cooreman |
0:074157529cc4 | 1 | /***************************************************************************//** |
Steven Cooreman |
0:074157529cc4 | 2 | * @file main.cpp |
Steven Cooreman |
0:074157529cc4 | 3 | * @brief Demo program for the mbed asynchronous serial API. |
Steven Cooreman |
0:074157529cc4 | 4 | ******************************************************************************* |
Steven Cooreman |
0:074157529cc4 | 5 | * @section License |
Steven Cooreman |
0:074157529cc4 | 6 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
Steven Cooreman |
0:074157529cc4 | 7 | ******************************************************************************* |
Steven Cooreman |
0:074157529cc4 | 8 | * |
Steven Cooreman |
0:074157529cc4 | 9 | * Permission is granted to anyone to use this software for any purpose, |
Steven Cooreman |
0:074157529cc4 | 10 | * including commercial applications, and to alter it and redistribute it |
Steven Cooreman |
0:074157529cc4 | 11 | * freely, subject to the following restrictions: |
Steven Cooreman |
0:074157529cc4 | 12 | * |
Steven Cooreman |
0:074157529cc4 | 13 | * 1. The origin of this software must not be misrepresented; you must not |
Steven Cooreman |
0:074157529cc4 | 14 | * claim that you wrote the original software. |
Steven Cooreman |
0:074157529cc4 | 15 | * 2. Altered source versions must be plainly marked as such, and must not be |
Steven Cooreman |
0:074157529cc4 | 16 | * misrepresented as being the original software. |
Steven Cooreman |
0:074157529cc4 | 17 | * 3. This notice may not be removed or altered from any source distribution. |
Steven Cooreman |
0:074157529cc4 | 18 | * |
Steven Cooreman |
0:074157529cc4 | 19 | * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no |
Steven Cooreman |
0:074157529cc4 | 20 | * obligation to support this Software. Silicon Labs is providing the |
Steven Cooreman |
0:074157529cc4 | 21 | * Software "AS IS", with no express or implied warranties of any kind, |
Steven Cooreman |
0:074157529cc4 | 22 | * including, but not limited to, any implied warranties of merchantability |
Steven Cooreman |
0:074157529cc4 | 23 | * or fitness for any particular purpose or warranties against infringement |
Steven Cooreman |
0:074157529cc4 | 24 | * of any proprietary rights of a third party. |
Steven Cooreman |
0:074157529cc4 | 25 | * |
Steven Cooreman |
0:074157529cc4 | 26 | * Silicon Labs will not be liable for any consequential, incidental, or |
Steven Cooreman |
0:074157529cc4 | 27 | * special damages, or any other relief, or for any claim by any third party, |
Steven Cooreman |
0:074157529cc4 | 28 | * arising from your use of this Software. |
Steven Cooreman |
0:074157529cc4 | 29 | * |
Steven Cooreman |
0:074157529cc4 | 30 | ******************************************************************************/ |
Steven Cooreman |
0:074157529cc4 | 31 | |
Steven Cooreman |
0:074157529cc4 | 32 | #include "mbed.h" |
Steven Cooreman |
0:074157529cc4 | 33 | |
Steven Cooreman |
0:074157529cc4 | 34 | /*------------ Constant definitions --------------*/ |
Steven Cooreman |
0:074157529cc4 | 35 | #define TX_PIN USBTX |
Steven Cooreman |
0:074157529cc4 | 36 | #define RX_PIN USBRX |
Steven Cooreman |
0:074157529cc4 | 37 | #define BRATE 115200 |
Steven Cooreman |
0:074157529cc4 | 38 | #define LED_PIN LED0 |
Steven Cooreman |
0:074157529cc4 | 39 | #define TOGGLE_RATE (0.5f) |
Steven Cooreman |
0:074157529cc4 | 40 | #define BUFF_LENGTH 5 |
Steven Cooreman |
0:074157529cc4 | 41 | |
Steven Cooreman |
0:074157529cc4 | 42 | /*-------- Check if platform compatible ----------*/ |
Steven Cooreman |
0:074157529cc4 | 43 | #if DEVICE_SERIAL_ASYNCH |
Steven Cooreman |
0:074157529cc4 | 44 | Serial test_connection(USBTX, USBRX); |
Steven Cooreman |
0:074157529cc4 | 45 | #else |
Steven Cooreman |
0:074157529cc4 | 46 | #error "Platform not compatible with Low Power APIs for Serial" |
Steven Cooreman |
0:074157529cc4 | 47 | #endif |
Steven Cooreman |
0:074157529cc4 | 48 | |
Steven Cooreman |
0:074157529cc4 | 49 | /*------------------ Variables -------------------*/ |
Steven Cooreman |
0:074157529cc4 | 50 | LowPowerTicker blinker; |
Steven Cooreman |
0:074157529cc4 | 51 | bool blinking = false; |
Steven Cooreman |
0:074157529cc4 | 52 | event_callback_t serialEventCb; |
stevew817 | 3:f911d11682f3 | 53 | DigitalOut LED(LED_PIN); |
stevew817 | 3:f911d11682f3 | 54 | uint8_t rx_buf[BUFF_LENGTH + 1]; |
Steven Cooreman |
0:074157529cc4 | 55 | |
Steven Cooreman |
0:074157529cc4 | 56 | /*------------------ Callbacks -------------------*/ |
Steven Cooreman |
0:074157529cc4 | 57 | void blink(void) { |
stevew817 | 3:f911d11682f3 | 58 | LED = !LED; |
Steven Cooreman |
0:074157529cc4 | 59 | } |
Steven Cooreman |
0:074157529cc4 | 60 | |
Steven Cooreman |
0:074157529cc4 | 61 | void serialCb(int events) { |
Steven Cooreman |
0:074157529cc4 | 62 | /* Something triggered the callback, either buffer is full or 'S' is received */ |
Steven Cooreman |
0:074157529cc4 | 63 | unsigned char i = 0; |
Steven Cooreman |
0:074157529cc4 | 64 | if(events & SERIAL_EVENT_RX_CHARACTER_MATCH) { |
Steven Cooreman |
0:074157529cc4 | 65 | //Received 'S', check for buffer length |
Steven Cooreman |
0:074157529cc4 | 66 | for(unsigned char i; i < BUFF_LENGTH; i++) { |
Steven Cooreman |
0:074157529cc4 | 67 | //Found the length! |
Steven Cooreman |
0:074157529cc4 | 68 | if(rx_buf[i] == 'S') break; |
Steven Cooreman |
0:074157529cc4 | 69 | } |
Steven Cooreman |
0:074157529cc4 | 70 | |
Steven Cooreman |
0:074157529cc4 | 71 | // Toggle blinking |
Steven Cooreman |
0:074157529cc4 | 72 | if(blinking) { |
Steven Cooreman |
0:074157529cc4 | 73 | blinker.detach(); |
Steven Cooreman |
0:074157529cc4 | 74 | blinking = false; |
Steven Cooreman |
0:074157529cc4 | 75 | } else { |
Steven Cooreman |
0:074157529cc4 | 76 | blinker.attach(blink, TOGGLE_RATE); |
Steven Cooreman |
0:074157529cc4 | 77 | blinking = true; |
Steven Cooreman |
0:074157529cc4 | 78 | } |
Steven Cooreman |
0:074157529cc4 | 79 | } else if (events & SERIAL_EVENT_RX_COMPLETE) { |
Steven Cooreman |
0:074157529cc4 | 80 | i = BUFF_LENGTH - 1; |
Steven Cooreman |
0:074157529cc4 | 81 | } else { |
Steven Cooreman |
0:074157529cc4 | 82 | rx_buf[0] = 'E'; |
Steven Cooreman |
0:074157529cc4 | 83 | rx_buf[1] = 'R'; |
Steven Cooreman |
0:074157529cc4 | 84 | rx_buf[2] = 'R'; |
Steven Cooreman |
0:074157529cc4 | 85 | rx_buf[3] = '!'; |
Steven Cooreman |
0:074157529cc4 | 86 | rx_buf[4] = 0; |
Steven Cooreman |
0:074157529cc4 | 87 | i = 3; |
Steven Cooreman |
0:074157529cc4 | 88 | } |
Steven Cooreman |
0:074157529cc4 | 89 | |
Steven Cooreman |
0:074157529cc4 | 90 | // Echo string, no callback |
stevew817 | 3:f911d11682f3 | 91 | test_connection.write(rx_buf, i+1, 0, 0); |
Steven Cooreman |
0:074157529cc4 | 92 | |
Steven Cooreman |
0:074157529cc4 | 93 | // Reset serial reception |
stevew817 | 3:f911d11682f3 | 94 | test_connection.read(rx_buf, BUFF_LENGTH, serialEventCb, SERIAL_EVENT_RX_ALL, 'S'); |
Steven Cooreman |
0:074157529cc4 | 95 | } |
Steven Cooreman |
0:074157529cc4 | 96 | |
Steven Cooreman |
0:074157529cc4 | 97 | /*-------------------- Main ----------------------*/ |
Steven Cooreman |
0:074157529cc4 | 98 | int main() { |
Steven Cooreman |
0:074157529cc4 | 99 | /* Very Simple Main (tm) */ |
Steven Cooreman |
0:074157529cc4 | 100 | serialEventCb.attach(serialCb); |
Steven Cooreman |
0:074157529cc4 | 101 | |
Steven Cooreman |
0:074157529cc4 | 102 | /* Setup serial connection */ |
Steven Cooreman |
0:074157529cc4 | 103 | test_connection.baud(BRATE); |
Steven Cooreman |
0:074157529cc4 | 104 | test_connection.printf("Low Power API test\n\nSend 'S' to toggle blinking\n"); |
stevew817 | 3:f911d11682f3 | 105 | test_connection.read(rx_buf, BUFF_LENGTH, serialEventCb, SERIAL_EVENT_RX_ALL, 'S'); |
Steven Cooreman |
0:074157529cc4 | 106 | |
Steven Cooreman |
0:074157529cc4 | 107 | /* Let the callbacks take care of everything */ |
Steven Cooreman |
0:074157529cc4 | 108 | while(1) sleep(); |
Steven Cooreman |
0:074157529cc4 | 109 | } |