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
rtos_serial.cpp
- Committer:
- altasoul
- Date:
- 2013-10-22
- Revision:
- 8:3644d12758da
- Parent:
- 7:dd892347b524
- Child:
- 10:d5adca63e94a
File content as of revision 8:3644d12758da:
/* * Copyright (c) 2013 Tom Soulanille * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include "rtos_serial.h" 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[uart_number]); #ifdef RTOS_SERIAL_TX_THREAD tx_emitter_threadp = new Thread(tx_emitter, (void *) this); #endif rx_isr_pFP = attach(this, &RTOS_Serial::rx_isr, RxIrq); tx_isr_pFP = attach(this, &RTOS_Serial::tx_isr, TxIrq); #ifdef RTOS_SERIAL_TX_THREAD tx_emitter_threadp->signal_set(0x01); // "prime the pump" of the tx-ready signals #else Serial::_putc('*'); //DEBUG #endif } RTOS_Serial::~RTOS_Serial() { remove_handler(rx_isr_pFP, RxIrq); remove_handler(tx_isr_pFP, TxIrq); #ifdef RTOS_SERIAL_TX_THREAD tx_emitter_threadp->terminate(); delete tx_emitter_threadp; #endif } int RTOS_Serial::get_index() { return _serial.index; } int RTOS_Serial::putc(int c) { //return Serial::putc(c); //DEBUG //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("\r\nRTOS_Serial::tx_q.put() returned %d\r\n", status); return EOF; } } int RTOS_Serial::puts(const char *s) { int rv = 0; while (*s) { if (putc(*s++) == EOF) { rv = EOF; break; } else { rv++; } } return rv; } int RTOS_Serial::parent_putc(int c) { return Serial::_putc(c); } int RTOS_Serial::getc() { int rv; //return Serial::getc(); //FIXME: stand-in, which fails if we use our RX ISR osEvent evt = rx_q.get(); if (evt.status == osEventMessage) { rv = (int) evt.value.v; } else { //FIXME: find appropriate error reporting if any std::printf("\r\nRTOS_Serial::getc() evt.status %d\n", evt.status); rv = EOF; } return rv; } void RTOS_Serial::rx_isr(){ rx_q.put((int *) _getc()); } void RTOS_Serial::tx_isr(){ #ifdef RTOS_SERIAL_TX_THREAD tx_emitter_threadp->signal_set(0x1); #else osEvent evt; evt = tx_q.get(); if (evt.status == osEventMessage) { *ledp = 1; parent_putc(evt.value.v); *ledp = 0; } /*else { std::printf("\r\nRTOS_Serial::tx_emitter() evt.status %d\n", evt.status); }*/ #endif } #ifdef RTOS_SERIAL_TX_THREAD // tx_emitter is a class method void RTOS_Serial::tx_emitter(void const *argument){ RTOS_Serial *sp = (RTOS_Serial *) argument; osEvent evt; //osStatus status; while(true){ evt = sp->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 // and just proceed to transmit the character. This should happen only // once at most, to "prime the pump", but the timeout provides some // safety in case something goes wrong. // A first signal is sent by the RTOS_Serial constructor when the thread // 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; } else { std::printf("\r\nRTOS_Serial::tx_emitter() evt.status %d\n", evt.status); } *(sp->ledp) = 0; } } #endif