Created
April 1, 2020 14:49
-
-
Save lonesometraveler/70c910eb66db8f0c30c26eb7700b1984 to your computer and use it in GitHub Desktop.
Mbed EventQueue example 3: Mbed OS6 UnbufferedSerial Interrupt Callback and deferred printf()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mbed.h" | |
// EventQueue | |
EventQueue *queue = mbed_event_queue(); | |
// Create a DigitalOutput object to toggle an LED whenever data is received. | |
static DigitalOut led(LED1); | |
// Create a UnbufferedSerial object with a default baud rate. | |
static UnbufferedSerial serial_port(USBTX, USBRX); | |
void process_in_user_context(char c) { | |
printf("Printing in a user context. Received [%c].\r\n", c); | |
} | |
void on_rx_interrupt() { | |
char c; | |
// Toggle the LED. | |
led = !led; | |
// Read the data to clear the receive interrupt. | |
if (serial_port.read(&c, 1)) { | |
// defer the execution to a different context | |
queue->call(&process_in_user_context, c); | |
} | |
} | |
int main(void) { | |
// Set desired properties (9600-8-N-1). | |
serial_port.baud(9600); | |
serial_port.format( | |
/* bits */ 8, | |
/* parity */ SerialBase::None, | |
/* stop bit */ 1 | |
); | |
// Register a callback to process a Rx (receive) interrupt. | |
serial_port.attach(&on_rx_interrupt, SerialBase::RxIrq); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment