testing for serial low power mbed on gecko board
Fork of Serial-LowPower-Demo by
main.cpp@6:942e0f133842, 2016-03-29 (annotated)
- Committer:
- shakeeb
- Date:
- Tue Mar 29 11:54:35 2016 +0000
- Revision:
- 6:942e0f133842
- Parent:
- 5:997e92a78a3b
serial low power demo example
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 | |
stevew817 | 5:997e92a78a3b | 61 | /** |
stevew817 | 5:997e92a78a3b | 62 | * This is a callback! Do not call frequency-dependent operations here. |
stevew817 | 5:997e92a78a3b | 63 | * |
stevew817 | 5:997e92a78a3b | 64 | * For a more thorough explanation, go here: |
stevew817 | 5:997e92a78a3b | 65 | * https://developer.mbed.org/teams/SiliconLabs/wiki/Using-the-improved-mbed-sleep-API#mixing-sleep-with-synchronous-code |
stevew817 | 5:997e92a78a3b | 66 | **/ |
Steven Cooreman |
0:074157529cc4 | 67 | void serialCb(int events) { |
Steven Cooreman |
0:074157529cc4 | 68 | /* Something triggered the callback, either buffer is full or 'S' is received */ |
srodk | 4:bbf422ee69e4 | 69 | unsigned char i; |
Steven Cooreman |
0:074157529cc4 | 70 | if(events & SERIAL_EVENT_RX_CHARACTER_MATCH) { |
Steven Cooreman |
0:074157529cc4 | 71 | //Received 'S', check for buffer length |
srodk | 4:bbf422ee69e4 | 72 | for(i = 0; i < BUFF_LENGTH; i++) { |
Steven Cooreman |
0:074157529cc4 | 73 | //Found the length! |
Steven Cooreman |
0:074157529cc4 | 74 | if(rx_buf[i] == 'S') break; |
Steven Cooreman |
0:074157529cc4 | 75 | } |
Steven Cooreman |
0:074157529cc4 | 76 | |
Steven Cooreman |
0:074157529cc4 | 77 | // Toggle blinking |
Steven Cooreman |
0:074157529cc4 | 78 | if(blinking) { |
Steven Cooreman |
0:074157529cc4 | 79 | blinker.detach(); |
Steven Cooreman |
0:074157529cc4 | 80 | blinking = false; |
Steven Cooreman |
0:074157529cc4 | 81 | } else { |
Steven Cooreman |
0:074157529cc4 | 82 | blinker.attach(blink, TOGGLE_RATE); |
Steven Cooreman |
0:074157529cc4 | 83 | blinking = true; |
Steven Cooreman |
0:074157529cc4 | 84 | } |
Steven Cooreman |
0:074157529cc4 | 85 | } else if (events & SERIAL_EVENT_RX_COMPLETE) { |
Steven Cooreman |
0:074157529cc4 | 86 | i = BUFF_LENGTH - 1; |
Steven Cooreman |
0:074157529cc4 | 87 | } else { |
Steven Cooreman |
0:074157529cc4 | 88 | rx_buf[0] = 'E'; |
Steven Cooreman |
0:074157529cc4 | 89 | rx_buf[1] = 'R'; |
Steven Cooreman |
0:074157529cc4 | 90 | rx_buf[2] = 'R'; |
Steven Cooreman |
0:074157529cc4 | 91 | rx_buf[3] = '!'; |
Steven Cooreman |
0:074157529cc4 | 92 | rx_buf[4] = 0; |
Steven Cooreman |
0:074157529cc4 | 93 | i = 3; |
Steven Cooreman |
0:074157529cc4 | 94 | } |
Steven Cooreman |
0:074157529cc4 | 95 | |
Steven Cooreman |
0:074157529cc4 | 96 | // Echo string, no callback |
stevew817 | 3:f911d11682f3 | 97 | test_connection.write(rx_buf, i+1, 0, 0); |
Steven Cooreman |
0:074157529cc4 | 98 | |
Steven Cooreman |
0:074157529cc4 | 99 | // Reset serial reception |
stevew817 | 3:f911d11682f3 | 100 | test_connection.read(rx_buf, BUFF_LENGTH, serialEventCb, SERIAL_EVENT_RX_ALL, 'S'); |
Steven Cooreman |
0:074157529cc4 | 101 | } |
Steven Cooreman |
0:074157529cc4 | 102 | |
Steven Cooreman |
0:074157529cc4 | 103 | /*-------------------- Main ----------------------*/ |
Steven Cooreman |
0:074157529cc4 | 104 | int main() { |
Steven Cooreman |
0:074157529cc4 | 105 | /* Very Simple Main (tm) */ |
Steven Cooreman |
0:074157529cc4 | 106 | serialEventCb.attach(serialCb); |
Steven Cooreman |
0:074157529cc4 | 107 | |
Steven Cooreman |
0:074157529cc4 | 108 | /* Setup serial connection */ |
Steven Cooreman |
0:074157529cc4 | 109 | test_connection.baud(BRATE); |
Steven Cooreman |
0:074157529cc4 | 110 | test_connection.printf("Low Power API test\n\nSend 'S' to toggle blinking\n"); |
stevew817 | 3:f911d11682f3 | 111 | test_connection.read(rx_buf, BUFF_LENGTH, serialEventCb, SERIAL_EVENT_RX_ALL, 'S'); |
Steven Cooreman |
0:074157529cc4 | 112 | |
Steven Cooreman |
0:074157529cc4 | 113 | /* Let the callbacks take care of everything */ |
Steven Cooreman |
0:074157529cc4 | 114 | while(1) sleep(); |
Steven Cooreman |
0:074157529cc4 | 115 | } |