A code sample showing the use of the asynchronous Serial APIs.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /***************************************************************************//**
00002  * @file main.cpp
00003  * @brief Demo program for the mbed asynchronous serial API.
00004  *******************************************************************************
00005  * @section License
00006  * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
00007  *******************************************************************************
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  * 1. The origin of this software must not be misrepresented; you must not
00014  *    claim that you wrote the original software.
00015  * 2. Altered source versions must be plainly marked as such, and must not be
00016  *    misrepresented as being the original software.
00017  * 3. This notice may not be removed or altered from any source distribution.
00018  *
00019  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
00020  * obligation to support this Software. Silicon Labs is providing the
00021  * Software "AS IS", with no express or implied warranties of any kind,
00022  * including, but not limited to, any implied warranties of merchantability
00023  * or fitness for any particular purpose or warranties against infringement
00024  * of any proprietary rights of a third party.
00025  *
00026  * Silicon Labs will not be liable for any consequential, incidental, or
00027  * special damages, or any other relief, or for any claim by any third party,
00028  * arising from your use of this Software.
00029  *
00030  ******************************************************************************/
00031  
00032 #include "mbed.h"
00033 
00034 /*------------ Constant definitions --------------*/
00035 #define TX_PIN          USBTX
00036 #define RX_PIN          USBRX
00037 #define BRATE           115200
00038 #define LED_PIN         LED0
00039 #define TOGGLE_RATE     (0.5f)
00040 #define BUFF_LENGTH     5
00041 
00042 /*-------- Check if platform compatible ----------*/
00043 #if DEVICE_SERIAL_ASYNCH
00044 Serial test_connection(USBTX, USBRX);
00045 #else
00046 #error "Platform not compatible with Low Power APIs for Serial"
00047 #endif
00048 
00049 /*------------------ Variables -------------------*/
00050 LowPowerTicker      blinker;
00051 bool                blinking = false;
00052 event_callback_t    serialEventCb;
00053 DigitalOut          LED(LED_PIN);
00054 uint8_t             rx_buf[BUFF_LENGTH + 1];
00055 
00056 /*------------------ Callbacks -------------------*/
00057 void blink(void) {
00058     LED = !LED;
00059 }
00060 
00061 /**
00062 * This is a callback! Do not call frequency-dependent operations here.
00063 *
00064 * For a more thorough explanation, go here: 
00065 * https://developer.mbed.org/teams/SiliconLabs/wiki/Using-the-improved-mbed-sleep-API#mixing-sleep-with-synchronous-code
00066 **/
00067 void serialCb(int events) {
00068     /* Something triggered the callback, either buffer is full or 'S' is received */
00069     unsigned char i;
00070     if(events & SERIAL_EVENT_RX_CHARACTER_MATCH) {
00071         //Received 'S', check for buffer length
00072         for(i = 0; i < BUFF_LENGTH; i++) {
00073             //Found the length!
00074             if(rx_buf[i] == 'S') break;
00075         }
00076         
00077         // Toggle blinking
00078         if(blinking) {
00079             blinker.detach();
00080             blinking = false;
00081         } else {
00082             blinker.attach(blink, TOGGLE_RATE);
00083             blinking = true;
00084         }
00085     } else if (events & SERIAL_EVENT_RX_COMPLETE) {
00086         i = BUFF_LENGTH - 1;
00087     } else {
00088         rx_buf[0] = 'E';
00089         rx_buf[1] = 'R';
00090         rx_buf[2] = 'R';
00091         rx_buf[3] = '!';
00092         rx_buf[4] = 0;
00093         i = 3;
00094     }
00095     
00096     // Echo string, no callback
00097     test_connection.write(rx_buf, i+1, 0, 0);
00098     
00099     // Reset serial reception
00100     test_connection.read(rx_buf, BUFF_LENGTH, serialEventCb, SERIAL_EVENT_RX_ALL, 'S');
00101 }
00102 
00103 /*-------------------- Main ----------------------*/
00104 int main() {
00105     /* Very Simple Main (tm) */
00106     serialEventCb.attach(serialCb);
00107     
00108     /* Setup serial connection */
00109     test_connection.baud(BRATE);
00110     test_connection.printf("Low Power API test\n\nSend 'S' to toggle blinking\n");
00111     test_connection.read(rx_buf, BUFF_LENGTH, serialEventCb, SERIAL_EVENT_RX_ALL, 'S');
00112     
00113     /* Let the callbacks take care of everything */
00114     while(1) sleep();
00115 }