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:
5:5d388d1d7987
Parent:
4:c7113cd0ac4b
Child:
6:438a6c0acbd4
diff -r c7113cd0ac4b -r 5d388d1d7987 rtos_serial.cpp
--- a/rtos_serial.cpp	Sat Oct 19 05:40:43 2013 +0000
+++ b/rtos_serial.cpp	Sat Oct 19 06:47:12 2013 +0000
@@ -25,13 +25,14 @@
 RTOS_Serial::RTOS_Serial(PinName tx, PinName rx, const char *name)
  : Serial(tx, rx, name)
 {
+    uart_number = get_index();
     const PinName leds[] = {LED1,LED2,LED3,LED4};
-    ledp = new DigitalOut(leds[get_index()]);
-    rtos_serial_pointers_by_index[get_index()] = this;
+    ledp = new DigitalOut(leds[uart_number]);
+    rtos_serial_pointers_by_index[uart_number] = this;
     tx_emitter_threadp = new Thread(tx_emitter, (void *) this);
-    tx_tp[get_index()] = tx_emitter_threadp;
-    attach(rx_isr[get_index()], RxIrq);
-    attach(tx_isr[get_index()], TxIrq);
+    tx_tp[uart_number] = tx_emitter_threadp;
+    attach(rx_isr[uart_number], RxIrq);
+    attach(tx_isr[uart_number], TxIrq);
     tx_emitter_threadp->signal_set(0x01);   // "prime the pump" of the tx-ready signals
 }
 
@@ -46,7 +47,7 @@
     //if (tx_q.put((int *)c, osWaitForever) == osOK) return c; else return EOF;
     int status;
     if ( (status = tx_q.put((int *)c, osWaitForever)) == osOK) return c; else {
-        std::printf("tx_q.put() returned %d\r\n", status);
+        std::printf("\r\nRTOS_Serial::tx_q.put() returned %d\r\n", status);
         return EOF;
     }
 }   
@@ -75,7 +76,7 @@
     if (evt.status == osEventMessage) {
         rv = (int) evt.value.v;
     } else {    //FIXME: find appropriate error reporting if any
-        std::printf("rx_dequeuer() evt.status %d\n", evt.status);
+        std::printf("\r\nRTOS_Serial::getc() evt.status %d\n", evt.status);
         rv = EOF;
     }
     return rv;
@@ -163,7 +164,7 @@
             sp->parent_putc(evt.value.v);
             *(sp->ledp) = 0;
         } else {
-            std::printf("tx_emitter() evt.status %d\n", evt.status);
+            std::printf("\r\nRTOS_Serial::tx_emitter() evt.status %d\n", evt.status);
         }
         *(sp->ledp) = 0;
     }