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:
- 13:2fb32235253c
- Parent:
- 12:be7883573c91
- Child:
- 14:33d60e4eb215
--- a/rtos_serial.cpp Thu Oct 24 17:30:52 2013 +0000 +++ b/rtos_serial.cpp Thu Oct 24 18:25:14 2013 +0000 @@ -28,18 +28,22 @@ */ #include "rtos_serial.h" -#define START_THREAD 0x80 +#define RTOS_SERIAL_START_THREAD 0x80 +#define RTOS_SERIAL_USE_LED_INDICATOR 0 RTOS_Serial::RTOS_Serial(PinName tx, PinName rx, const char *name) - : RawSerial(tx, rx), name(name), + : RawSerial(tx, rx), + name(name), _tx_thread(&RTOS_Serial::threadStarter, (void *) this, osPriorityNormal, RTOS_SERIAL_TX_THREAD_STACK_SIZE) { uart_number = get_index(); - const PinName leds[] = {LED1,LED2,LED3,LED4}; +#if RTOS_SERIAL_USE_LED_INDICATOR + static const PinName leds[] = {LED1,LED2,LED3,LED4}; ledp = new DigitalOut(leds[uart_number]); for (int i=0; i<1; i++) { *ledp=1; wait(0.1); *ledp=0; wait(0.1); }; wait(0.5); - _tx_thread.signal_set(START_THREAD); +#endif + _tx_thread.signal_set(RTOS_SERIAL_START_THREAD); } #if 0 @@ -120,23 +124,28 @@ void RTOS_Serial::threadStarter(void const *p) { RTOS_Serial *instance = (RTOS_Serial*)p; - instance->_tx_thread.signal_set(0x01); // "prime the pump" of the tx-ready signals - instance->tx_emitter(instance); + instance->tx_emitter(); } -void RTOS_Serial::tx_emitter(void const *argument){ - RTOS_Serial *sp = (RTOS_Serial *) argument; +void RTOS_Serial::tx_emitter(){ osEvent evt; //osStatus status; - _tx_thread.signal_wait(START_THREAD); + _tx_thread.signal_wait(RTOS_SERIAL_START_THREAD); +#if RTOS_SERIAL_USE_LED_INDICATOR for (int i=0; i<2; i++) { *ledp=1; wait(0.1); *ledp=0; wait(0.1); }; wait(0.5); +#endif attach(this, &RTOS_Serial::rx_isr, RxIrq); +#if RTOS_SERIAL_USE_LED_INDICATOR for (int i=0; i<3; i++) { *ledp=1; wait(0.1); *ledp=0; wait(0.1); }; wait(0.5); +#endif attach(this, &RTOS_Serial::tx_isr, TxIrq); +#if RTOS_SERIAL_USE_LED_INDICATOR for (int i=0; i<4; i++) { *ledp=1; wait(0.1); *ledp=0; wait(0.1); }; wait(0.5); - +#endif + _tx_thread.signal_set(0x01); // "prime the pump" of the tx-ready signals + while(true){ - evt = sp->tx_q.get(); + evt = tx_q.get(); if (evt.status == osEventMessage) { // There is no TX interrupt until the first byte is sent out the port, // so we use a timeout on the signal from the interrupt service routine @@ -147,12 +156,18 @@ // is created, so normally this will not come into effect. // DEBUG: timeout omitted to search for instabilities Thread::signal_wait(0x1/*, 10*/); //FIXME: base the timeout on the baud rate - *(sp->ledp) = 1; - sp->parent_putc(evt.value.v); - *(sp->ledp) = 0; +#if RTOS_SERIAL_USE_LED_INDICATOR + *ledp = 1; +#endif + parent_putc(evt.value.v); +#if RTOS_SERIAL_USE_LED_INDICATOR + *ledp = 0; +#endif } else { std::printf("\r\nRTOS_Serial::tx_emitter() evt.status %d\n", evt.status); } - *(sp->ledp) = 0; +#if RTOS_SERIAL_USE_LED_INDICATOR + *ledp = 0; +#endif } }