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
Diff: rtos_serial.cpp
- Revision:
- 11:bc067b42f8e0
- Parent:
- 10:d5adca63e94a
- Child:
- 12:be7883573c91
--- a/rtos_serial.cpp Wed Oct 23 21:06:59 2013 +0000 +++ b/rtos_serial.cpp Thu Oct 24 05:34:47 2013 +0000 @@ -21,8 +21,6 @@ */ /* TODO: - * - assert rx overrun failure - * - fix rx overrun failure * - size the tx thread stack * - make an rx buffer thread * - implement readable() @@ -37,7 +35,8 @@ const PinName leds[] = {LED1,LED2,LED3,LED4}; ledp = new DigitalOut(leds[uart_number]); #ifdef RTOS_SERIAL_TX_THREAD - tx_emitter_threadp = new Thread(tx_emitter, (void *) this); + tx_emitter_threadp = new Thread(tx_emitter, (void *) this, + osPriorityNormal, RTOS_SERIAL_TX_THREAD_STACK_SIZE); #endif // rx_isr_pFP = attach(this, &RTOS_Serial::rx_isr, RxIrq); // tx_isr_pFP = attach(this, &RTOS_Serial::tx_isr, TxIrq); @@ -75,6 +74,8 @@ int RTOS_Serial::get_index() { return _serial.index; } +int RTOS_Serial::get_baud() { return _baud; } + int RTOS_Serial::putc(int c) { //return Serial::putc(c); //DEBUG //if (tx_q.put((int *)c, osWaitForever) == osOK) return c; else return EOF; @@ -116,7 +117,7 @@ } void RTOS_Serial::rx_isr(){ - rx_q.put((int *) _getc()); // FIXME: won't this wedge on overrun? + rx_q.put((int *) _getc()); // returns immediately even if queue was full } void RTOS_Serial::tx_isr(){