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

Committer:
altasoul
Date:
Thu Oct 24 05:34:47 2013 +0000
Revision:
11:bc067b42f8e0
Parent:
10:d5adca63e94a
Child:
12:be7883573c91
get_baud(), emitter thread stack size

Who changed what in which revision?

UserRevisionLine numberNew contents of line
altasoul 0:0547c8bf304f 1 /*
altasoul 0:0547c8bf304f 2 * Copyright (c) 2013 Tom Soulanille
altasoul 0:0547c8bf304f 3 *
altasoul 0:0547c8bf304f 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
altasoul 0:0547c8bf304f 5 * of this software and associated documentation files (the "Software"), to deal
altasoul 0:0547c8bf304f 6 * in the Software without restriction, including without limitation the rights
altasoul 0:0547c8bf304f 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
altasoul 0:0547c8bf304f 8 * copies of the Software, and to permit persons to whom the Software is
altasoul 0:0547c8bf304f 9 * furnished to do so, subject to the following conditions:
altasoul 0:0547c8bf304f 10 *
altasoul 0:0547c8bf304f 11 * The above copyright notice and this permission notice shall be included in
altasoul 0:0547c8bf304f 12 * all copies or substantial portions of the Software.
altasoul 0:0547c8bf304f 13 *
altasoul 0:0547c8bf304f 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
altasoul 0:0547c8bf304f 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
altasoul 0:0547c8bf304f 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
altasoul 0:0547c8bf304f 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
altasoul 0:0547c8bf304f 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
altasoul 0:0547c8bf304f 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
altasoul 0:0547c8bf304f 20 * SOFTWARE.
altasoul 0:0547c8bf304f 21 */
altasoul 0:0547c8bf304f 22
altasoul 10:d5adca63e94a 23 /* TODO:
altasoul 10:d5adca63e94a 24 * - size the tx thread stack
altasoul 10:d5adca63e94a 25 * - make an rx buffer thread
altasoul 10:d5adca63e94a 26 * - implement readable()
altasoul 10:d5adca63e94a 27 * - implement writeable() somehow
altasoul 10:d5adca63e94a 28 */
altasoul 0:0547c8bf304f 29 #include "rtos_serial.h"
altasoul 0:0547c8bf304f 30
altasoul 0:0547c8bf304f 31 RTOS_Serial::RTOS_Serial(PinName tx, PinName rx, const char *name)
altasoul 0:0547c8bf304f 32 : Serial(tx, rx, name)
altasoul 0:0547c8bf304f 33 {
altasoul 5:5d388d1d7987 34 uart_number = get_index();
altasoul 0:0547c8bf304f 35 const PinName leds[] = {LED1,LED2,LED3,LED4};
altasoul 5:5d388d1d7987 36 ledp = new DigitalOut(leds[uart_number]);
altasoul 8:3644d12758da 37 #ifdef RTOS_SERIAL_TX_THREAD
altasoul 11:bc067b42f8e0 38 tx_emitter_threadp = new Thread(tx_emitter, (void *) this,
altasoul 11:bc067b42f8e0 39 osPriorityNormal, RTOS_SERIAL_TX_THREAD_STACK_SIZE);
altasoul 8:3644d12758da 40 #endif
altasoul 10:d5adca63e94a 41 // rx_isr_pFP = attach(this, &RTOS_Serial::rx_isr, RxIrq);
altasoul 10:d5adca63e94a 42 // tx_isr_pFP = attach(this, &RTOS_Serial::tx_isr, TxIrq);
altasoul 10:d5adca63e94a 43 attach(this, &RTOS_Serial::rx_isr, RxIrq);
altasoul 10:d5adca63e94a 44 attach(this, &RTOS_Serial::tx_isr, TxIrq);
altasoul 8:3644d12758da 45 #ifdef RTOS_SERIAL_TX_THREAD
altasoul 2:891773cc33fd 46 tx_emitter_threadp->signal_set(0x01); // "prime the pump" of the tx-ready signals
altasoul 8:3644d12758da 47 #else
altasoul 8:3644d12758da 48 Serial::_putc('*'); //DEBUG
altasoul 8:3644d12758da 49 #endif
altasoul 1:5a66fddad7c4 50 }
altasoul 0:0547c8bf304f 51
altasoul 10:d5adca63e94a 52 #if 0
altasoul 6:438a6c0acbd4 53 RTOS_Serial::~RTOS_Serial() {
altasoul 10:d5adca63e94a 54 std::printf("[destroying RTOS_Serial 0x%x]", this); std::fflush(stdout);
altasoul 10:d5adca63e94a 55 bool b;
altasoul 10:d5adca63e94a 56 osDelay(200); //DEBUG
altasoul 10:d5adca63e94a 57 #if 0
altasoul 10:d5adca63e94a 58 std::printf("[remove rx handler 0x%x", rx_isr_pFP); std::fflush(stdout);
altasoul 10:d5adca63e94a 59 // returns false as expected: b = remove_handler(NULL, RxIrq);
altasoul 10:d5adca63e94a 60 b = remove_handler(rx_isr_pFP, RxIrq);
altasoul 10:d5adca63e94a 61 std::printf("returned %d]", b); std::fflush(stdout);
altasoul 10:d5adca63e94a 62 std::printf("[remove tx handler 0x%x", tx_isr_pFP); std::fflush(stdout);
altasoul 10:d5adca63e94a 63 b = remove_handler(tx_isr_pFP, TxIrq);
altasoul 10:d5adca63e94a 64 std::printf("returned %d]", b); std::fflush(stdout);
altasoul 10:d5adca63e94a 65 #endif
altasoul 8:3644d12758da 66 #ifdef RTOS_SERIAL_TX_THREAD
altasoul 6:438a6c0acbd4 67 tx_emitter_threadp->terminate();
altasoul 10:d5adca63e94a 68 std::printf("[tx_emitter_thread 0x%x terminated]", tx_emitter_threadp); std::fflush(stdout);
altasoul 6:438a6c0acbd4 69 delete tx_emitter_threadp;
altasoul 10:d5adca63e94a 70 std::printf("[tx_emitter_threadp deleted]"); std::fflush(stdout);
altasoul 8:3644d12758da 71 #endif
altasoul 6:438a6c0acbd4 72 }
altasoul 10:d5adca63e94a 73 #endif
altasoul 6:438a6c0acbd4 74
altasoul 0:0547c8bf304f 75 int RTOS_Serial::get_index() { return _serial.index; }
altasoul 0:0547c8bf304f 76
altasoul 11:bc067b42f8e0 77 int RTOS_Serial::get_baud() { return _baud; }
altasoul 11:bc067b42f8e0 78
altasoul 0:0547c8bf304f 79 int RTOS_Serial::putc(int c) {
altasoul 2:891773cc33fd 80 //return Serial::putc(c); //DEBUG
altasoul 0:0547c8bf304f 81 //if (tx_q.put((int *)c, osWaitForever) == osOK) return c; else return EOF;
altasoul 0:0547c8bf304f 82 int status;
altasoul 0:0547c8bf304f 83 if ( (status = tx_q.put((int *)c, osWaitForever)) == osOK) return c; else {
altasoul 5:5d388d1d7987 84 std::printf("\r\nRTOS_Serial::tx_q.put() returned %d\r\n", status);
altasoul 0:0547c8bf304f 85 return EOF;
altasoul 0:0547c8bf304f 86 }
altasoul 0:0547c8bf304f 87 }
altasoul 0:0547c8bf304f 88
altasoul 2:891773cc33fd 89 int RTOS_Serial::puts(const char *s) {
altasoul 2:891773cc33fd 90 int rv = 0;
altasoul 2:891773cc33fd 91 while (*s) {
altasoul 2:891773cc33fd 92 if (putc(*s++) == EOF) {
altasoul 2:891773cc33fd 93 rv = EOF;
altasoul 2:891773cc33fd 94 break;
altasoul 2:891773cc33fd 95 } else {
altasoul 2:891773cc33fd 96 rv++;
altasoul 2:891773cc33fd 97 }
altasoul 2:891773cc33fd 98 }
altasoul 2:891773cc33fd 99 return rv;
altasoul 2:891773cc33fd 100 }
altasoul 2:891773cc33fd 101
altasoul 0:0547c8bf304f 102 int RTOS_Serial::parent_putc(int c) {
altasoul 7:dd892347b524 103 return Serial::_putc(c);
altasoul 0:0547c8bf304f 104 }
altasoul 0:0547c8bf304f 105
altasoul 2:891773cc33fd 106 int RTOS_Serial::getc() {
altasoul 4:c7113cd0ac4b 107 int rv;
altasoul 4:c7113cd0ac4b 108 //return Serial::getc(); //FIXME: stand-in, which fails if we use our RX ISR
altasoul 4:c7113cd0ac4b 109 osEvent evt = rx_q.get();
altasoul 4:c7113cd0ac4b 110 if (evt.status == osEventMessage) {
altasoul 4:c7113cd0ac4b 111 rv = (int) evt.value.v;
altasoul 4:c7113cd0ac4b 112 } else { //FIXME: find appropriate error reporting if any
altasoul 5:5d388d1d7987 113 std::printf("\r\nRTOS_Serial::getc() evt.status %d\n", evt.status);
altasoul 4:c7113cd0ac4b 114 rv = EOF;
altasoul 4:c7113cd0ac4b 115 }
altasoul 4:c7113cd0ac4b 116 return rv;
altasoul 2:891773cc33fd 117 }
altasoul 2:891773cc33fd 118
altasoul 8:3644d12758da 119 void RTOS_Serial::rx_isr(){
altasoul 11:bc067b42f8e0 120 rx_q.put((int *) _getc()); // returns immediately even if queue was full
altasoul 1:5a66fddad7c4 121 }
altasoul 1:5a66fddad7c4 122
altasoul 8:3644d12758da 123 void RTOS_Serial::tx_isr(){
altasoul 8:3644d12758da 124 #ifdef RTOS_SERIAL_TX_THREAD
altasoul 8:3644d12758da 125 tx_emitter_threadp->signal_set(0x1);
altasoul 8:3644d12758da 126 #else
altasoul 8:3644d12758da 127 osEvent evt;
altasoul 10:d5adca63e94a 128 evt = tx_q.get(); // BUG: This will wait forever in ISR!
altasoul 8:3644d12758da 129 if (evt.status == osEventMessage) {
altasoul 8:3644d12758da 130 *ledp = 1;
altasoul 8:3644d12758da 131 parent_putc(evt.value.v);
altasoul 8:3644d12758da 132 *ledp = 0;
altasoul 8:3644d12758da 133 } /*else {
altasoul 8:3644d12758da 134 std::printf("\r\nRTOS_Serial::tx_emitter() evt.status %d\n", evt.status);
altasoul 8:3644d12758da 135 }*/
altasoul 8:3644d12758da 136 #endif
altasoul 2:891773cc33fd 137 }
altasoul 2:891773cc33fd 138
altasoul 8:3644d12758da 139 #ifdef RTOS_SERIAL_TX_THREAD
altasoul 0:0547c8bf304f 140 // tx_emitter is a class method
altasoul 0:0547c8bf304f 141 void RTOS_Serial::tx_emitter(void const *argument){
altasoul 0:0547c8bf304f 142 RTOS_Serial *sp = (RTOS_Serial *) argument;
altasoul 0:0547c8bf304f 143 osEvent evt;
altasoul 2:891773cc33fd 144 //osStatus status;
altasoul 0:0547c8bf304f 145 while(true){
altasoul 0:0547c8bf304f 146 evt = sp->tx_q.get();
altasoul 0:0547c8bf304f 147 if (evt.status == osEventMessage) {
altasoul 2:891773cc33fd 148 // There is no TX interrupt until the first byte is sent out the port,
altasoul 2:891773cc33fd 149 // so we use a timeout on the signal from the interrupt service routine
altasoul 2:891773cc33fd 150 // and just proceed to transmit the character. This should happen only
altasoul 2:891773cc33fd 151 // once at most, to "prime the pump", but the timeout provides some
altasoul 2:891773cc33fd 152 // safety in case something goes wrong.
altasoul 2:891773cc33fd 153 // A first signal is sent by the RTOS_Serial constructor when the thread
altasoul 2:891773cc33fd 154 // is created, so normally this will not come into effect.
altasoul 2:891773cc33fd 155 // DEBUG: timeout omitted to search for instabilities
altasoul 2:891773cc33fd 156 Thread::signal_wait(0x1/*, 10*/); //FIXME: base the timeout on the baud rate
altasoul 0:0547c8bf304f 157 *(sp->ledp) = 1;
altasoul 0:0547c8bf304f 158 sp->parent_putc(evt.value.v);
altasoul 0:0547c8bf304f 159 *(sp->ledp) = 0;
altasoul 0:0547c8bf304f 160 } else {
altasoul 5:5d388d1d7987 161 std::printf("\r\nRTOS_Serial::tx_emitter() evt.status %d\n", evt.status);
altasoul 0:0547c8bf304f 162 }
altasoul 0:0547c8bf304f 163 *(sp->ledp) = 0;
altasoul 0:0547c8bf304f 164 }
altasoul 3:5865277b7710 165 }
altasoul 8:3644d12758da 166 #endif