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:
- 6:438a6c0acbd4
- Parent:
- 5:5d388d1d7987
- Child:
- 7:dd892347b524
--- a/rtos_serial.cpp Sat Oct 19 06:47:12 2013 +0000 +++ b/rtos_serial.cpp Sun Oct 20 05:36:16 2013 +0000 @@ -31,11 +31,21 @@ rtos_serial_pointers_by_index[uart_number] = this; tx_emitter_threadp = new Thread(tx_emitter, (void *) this); tx_tp[uart_number] = tx_emitter_threadp; - attach(rx_isr[uart_number], RxIrq); - attach(tx_isr[uart_number], TxIrq); + rx_isr_pFP = attach(rx_isr[uart_number], RxIrq); + tx_isr_pFP = attach(tx_isr[uart_number], TxIrq); tx_emitter_threadp->signal_set(0x01); // "prime the pump" of the tx-ready signals } + +RTOS_Serial::~RTOS_Serial() { + remove_handler(rx_isr_pFP, RxIrq); + remove_handler(tx_isr_pFP, TxIrq); + tx_emitter_threadp->terminate(); + tx_tp[uart_number] = NULL; + delete tx_emitter_threadp; + rtos_serial_pointers_by_index[uart_number] = NULL; +} + RTOS_Serial* RTOS_Serial::rtos_serial_pointers_by_index[4] = { NULL, NULL, NULL, NULL }; serial_t RTOS_Serial::get_serial() { return _serial; }