An RTOS-friendly Serial interface Its primary benefit is that it never hogs the CPU. An amusing alternative to the traditional ring-bufferd interrupt-serviced systems, it uses short mbed-rtos queues to buffer characters to and from the UART, and a thread to service the transmitter. Short interrupt service routines enqueue received characters and signal the transmit thread when the transmitter is available. WARNING: Do not create RTOS-Serial objects before the RTOS is running! Put them inside your main() block or another function, not in the global initialization.

Dependents:   Test_RDM880_rfid_reader

Revision:
14:33d60e4eb215
Parent:
13:2fb32235253c
Child:
15:5f38a747ba08
--- a/rtos_serial.cpp	Thu Oct 24 18:25:14 2013 +0000
+++ b/rtos_serial.cpp	Thu Oct 24 20:31:25 2013 +0000
@@ -73,6 +73,8 @@
 
 int  RTOS_Serial::get_baud() { return _baud; }
 
+//int RTOS_Serial::writeable() { return true; } //FIXME: implement
+
 int RTOS_Serial::putc(int c) {
     //return Serial::putc(c); //DEBUG
     //if (tx_q.put((int *)c, osWaitForever) == osOK) return c; else return EOF;
@@ -100,12 +102,16 @@
     return RawSerial::putc(c);
 }
 
-int RTOS_Serial::getc() {
+//int RTOS_Serial::readable() { return true; } //FIXME: implement
+
+int RTOS_Serial::getc(int timeout) {
     int rv;
     //return Serial::getc();  //FIXME: stand-in, which fails if we use our RX ISR
-    osEvent evt = rx_q.get();
+    osEvent evt = rx_q.get(timeout);
     if (evt.status == osEventMessage) {
         rv = (int) evt.value.v;
+    } else if (evt.status == osOK) {
+        rv = EOF;
     } else {    //FIXME: find appropriate error reporting if any
         std::printf("\r\nRTOS_Serial::getc() evt.status %d\n", evt.status);
         rv = EOF;