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 20:31:25 2013 +0000
Revision:
14:33d60e4eb215
Parent:
13:2fb32235253c
Child:
15:5f38a747ba08
getc(timeout) added

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 13:2fb32235253c 31 #define RTOS_SERIAL_START_THREAD 0x80
altasoul 13:2fb32235253c 32 #define RTOS_SERIAL_USE_LED_INDICATOR 0
altasoul 12:be7883573c91 33
altasoul 0:0547c8bf304f 34 RTOS_Serial::RTOS_Serial(PinName tx, PinName rx, const char *name)
altasoul 13:2fb32235253c 35 : RawSerial(tx, rx),
altasoul 13:2fb32235253c 36 name(name),
altasoul 12:be7883573c91 37 _tx_thread(&RTOS_Serial::threadStarter, (void *) this,
altasoul 12:be7883573c91 38 osPriorityNormal, RTOS_SERIAL_TX_THREAD_STACK_SIZE)
altasoul 0:0547c8bf304f 39 {
altasoul 5:5d388d1d7987 40 uart_number = get_index();
altasoul 13:2fb32235253c 41 #if RTOS_SERIAL_USE_LED_INDICATOR
altasoul 13:2fb32235253c 42 static const PinName leds[] = {LED1,LED2,LED3,LED4};
altasoul 5:5d388d1d7987 43 ledp = new DigitalOut(leds[uart_number]);
altasoul 12:be7883573c91 44 for (int i=0; i<1; i++) { *ledp=1; wait(0.1); *ledp=0; wait(0.1); }; wait(0.5);
altasoul 13:2fb32235253c 45 #endif
altasoul 13:2fb32235253c 46 _tx_thread.signal_set(RTOS_SERIAL_START_THREAD);
altasoul 1:5a66fddad7c4 47 }
altasoul 0:0547c8bf304f 48
altasoul 10:d5adca63e94a 49 #if 0
altasoul 6:438a6c0acbd4 50 RTOS_Serial::~RTOS_Serial() {
altasoul 10:d5adca63e94a 51 std::printf("[destroying RTOS_Serial 0x%x]", this); std::fflush(stdout);
altasoul 10:d5adca63e94a 52 bool b;
altasoul 10:d5adca63e94a 53 osDelay(200); //DEBUG
altasoul 10:d5adca63e94a 54 #if 0
altasoul 10:d5adca63e94a 55 std::printf("[remove rx handler 0x%x", rx_isr_pFP); std::fflush(stdout);
altasoul 10:d5adca63e94a 56 // returns false as expected: b = remove_handler(NULL, RxIrq);
altasoul 10:d5adca63e94a 57 b = remove_handler(rx_isr_pFP, RxIrq);
altasoul 10:d5adca63e94a 58 std::printf("returned %d]", b); std::fflush(stdout);
altasoul 10:d5adca63e94a 59 std::printf("[remove tx handler 0x%x", tx_isr_pFP); std::fflush(stdout);
altasoul 10:d5adca63e94a 60 b = remove_handler(tx_isr_pFP, TxIrq);
altasoul 10:d5adca63e94a 61 std::printf("returned %d]", b); std::fflush(stdout);
altasoul 10:d5adca63e94a 62 #endif
altasoul 8:3644d12758da 63 #ifdef RTOS_SERIAL_TX_THREAD
altasoul 12:be7883573c91 64 _tx_thread.terminate();
altasoul 12:be7883573c91 65 std::printf("[tx_emitter_thread 0x%x terminated]", &_tx_thread); std::fflush(stdout);
altasoul 6:438a6c0acbd4 66 delete tx_emitter_threadp;
altasoul 10:d5adca63e94a 67 std::printf("[tx_emitter_threadp deleted]"); std::fflush(stdout);
altasoul 8:3644d12758da 68 #endif
altasoul 6:438a6c0acbd4 69 }
altasoul 10:d5adca63e94a 70 #endif
altasoul 6:438a6c0acbd4 71
altasoul 0:0547c8bf304f 72 int RTOS_Serial::get_index() { return _serial.index; }
altasoul 0:0547c8bf304f 73
altasoul 11:bc067b42f8e0 74 int RTOS_Serial::get_baud() { return _baud; }
altasoul 11:bc067b42f8e0 75
altasoul 14:33d60e4eb215 76 //int RTOS_Serial::writeable() { return true; } //FIXME: implement
altasoul 14:33d60e4eb215 77
altasoul 0:0547c8bf304f 78 int RTOS_Serial::putc(int c) {
altasoul 2:891773cc33fd 79 //return Serial::putc(c); //DEBUG
altasoul 0:0547c8bf304f 80 //if (tx_q.put((int *)c, osWaitForever) == osOK) return c; else return EOF;
altasoul 0:0547c8bf304f 81 int status;
altasoul 0:0547c8bf304f 82 if ( (status = tx_q.put((int *)c, osWaitForever)) == osOK) return c; else {
altasoul 5:5d388d1d7987 83 std::printf("\r\nRTOS_Serial::tx_q.put() returned %d\r\n", status);
altasoul 0:0547c8bf304f 84 return EOF;
altasoul 0:0547c8bf304f 85 }
altasoul 0:0547c8bf304f 86 }
altasoul 0:0547c8bf304f 87
altasoul 2:891773cc33fd 88 int RTOS_Serial::puts(const char *s) {
altasoul 2:891773cc33fd 89 int rv = 0;
altasoul 2:891773cc33fd 90 while (*s) {
altasoul 2:891773cc33fd 91 if (putc(*s++) == EOF) {
altasoul 2:891773cc33fd 92 rv = EOF;
altasoul 2:891773cc33fd 93 break;
altasoul 2:891773cc33fd 94 } else {
altasoul 2:891773cc33fd 95 rv++;
altasoul 2:891773cc33fd 96 }
altasoul 2:891773cc33fd 97 }
altasoul 2:891773cc33fd 98 return rv;
altasoul 2:891773cc33fd 99 }
altasoul 2:891773cc33fd 100
altasoul 0:0547c8bf304f 101 int RTOS_Serial::parent_putc(int c) {
altasoul 12:be7883573c91 102 return RawSerial::putc(c);
altasoul 0:0547c8bf304f 103 }
altasoul 0:0547c8bf304f 104
altasoul 14:33d60e4eb215 105 //int RTOS_Serial::readable() { return true; } //FIXME: implement
altasoul 14:33d60e4eb215 106
altasoul 14:33d60e4eb215 107 int RTOS_Serial::getc(int timeout) {
altasoul 4:c7113cd0ac4b 108 int rv;
altasoul 4:c7113cd0ac4b 109 //return Serial::getc(); //FIXME: stand-in, which fails if we use our RX ISR
altasoul 14:33d60e4eb215 110 osEvent evt = rx_q.get(timeout);
altasoul 4:c7113cd0ac4b 111 if (evt.status == osEventMessage) {
altasoul 4:c7113cd0ac4b 112 rv = (int) evt.value.v;
altasoul 14:33d60e4eb215 113 } else if (evt.status == osOK) {
altasoul 14:33d60e4eb215 114 rv = EOF;
altasoul 4:c7113cd0ac4b 115 } else { //FIXME: find appropriate error reporting if any
altasoul 5:5d388d1d7987 116 std::printf("\r\nRTOS_Serial::getc() evt.status %d\n", evt.status);
altasoul 4:c7113cd0ac4b 117 rv = EOF;
altasoul 4:c7113cd0ac4b 118 }
altasoul 4:c7113cd0ac4b 119 return rv;
altasoul 2:891773cc33fd 120 }
altasoul 2:891773cc33fd 121
altasoul 8:3644d12758da 122 void RTOS_Serial::rx_isr(){
altasoul 12:be7883573c91 123 rx_q.put((int *) RawSerial::getc()); // returns immediately even if queue was full
altasoul 1:5a66fddad7c4 124 }
altasoul 1:5a66fddad7c4 125
altasoul 8:3644d12758da 126 void RTOS_Serial::tx_isr(){
altasoul 12:be7883573c91 127 _tx_thread.signal_set(0x1);
altasoul 2:891773cc33fd 128 }
altasoul 2:891773cc33fd 129
altasoul 12:be7883573c91 130
altasoul 12:be7883573c91 131 void RTOS_Serial::threadStarter(void const *p) {
altasoul 12:be7883573c91 132 RTOS_Serial *instance = (RTOS_Serial*)p;
altasoul 13:2fb32235253c 133 instance->tx_emitter();
altasoul 12:be7883573c91 134 }
altasoul 12:be7883573c91 135
altasoul 13:2fb32235253c 136 void RTOS_Serial::tx_emitter(){
altasoul 0:0547c8bf304f 137 osEvent evt;
altasoul 2:891773cc33fd 138 //osStatus status;
altasoul 13:2fb32235253c 139 _tx_thread.signal_wait(RTOS_SERIAL_START_THREAD);
altasoul 13:2fb32235253c 140 #if RTOS_SERIAL_USE_LED_INDICATOR
altasoul 12:be7883573c91 141 for (int i=0; i<2; i++) { *ledp=1; wait(0.1); *ledp=0; wait(0.1); }; wait(0.5);
altasoul 13:2fb32235253c 142 #endif
altasoul 12:be7883573c91 143 attach(this, &RTOS_Serial::rx_isr, RxIrq);
altasoul 13:2fb32235253c 144 #if RTOS_SERIAL_USE_LED_INDICATOR
altasoul 12:be7883573c91 145 for (int i=0; i<3; i++) { *ledp=1; wait(0.1); *ledp=0; wait(0.1); }; wait(0.5);
altasoul 13:2fb32235253c 146 #endif
altasoul 12:be7883573c91 147 attach(this, &RTOS_Serial::tx_isr, TxIrq);
altasoul 13:2fb32235253c 148 #if RTOS_SERIAL_USE_LED_INDICATOR
altasoul 12:be7883573c91 149 for (int i=0; i<4; i++) { *ledp=1; wait(0.1); *ledp=0; wait(0.1); }; wait(0.5);
altasoul 13:2fb32235253c 150 #endif
altasoul 13:2fb32235253c 151 _tx_thread.signal_set(0x01); // "prime the pump" of the tx-ready signals
altasoul 13:2fb32235253c 152
altasoul 0:0547c8bf304f 153 while(true){
altasoul 13:2fb32235253c 154 evt = tx_q.get();
altasoul 0:0547c8bf304f 155 if (evt.status == osEventMessage) {
altasoul 2:891773cc33fd 156 // There is no TX interrupt until the first byte is sent out the port,
altasoul 2:891773cc33fd 157 // so we use a timeout on the signal from the interrupt service routine
altasoul 2:891773cc33fd 158 // and just proceed to transmit the character. This should happen only
altasoul 2:891773cc33fd 159 // once at most, to "prime the pump", but the timeout provides some
altasoul 2:891773cc33fd 160 // safety in case something goes wrong.
altasoul 2:891773cc33fd 161 // A first signal is sent by the RTOS_Serial constructor when the thread
altasoul 2:891773cc33fd 162 // is created, so normally this will not come into effect.
altasoul 2:891773cc33fd 163 // DEBUG: timeout omitted to search for instabilities
altasoul 2:891773cc33fd 164 Thread::signal_wait(0x1/*, 10*/); //FIXME: base the timeout on the baud rate
altasoul 13:2fb32235253c 165 #if RTOS_SERIAL_USE_LED_INDICATOR
altasoul 13:2fb32235253c 166 *ledp = 1;
altasoul 13:2fb32235253c 167 #endif
altasoul 13:2fb32235253c 168 parent_putc(evt.value.v);
altasoul 13:2fb32235253c 169 #if RTOS_SERIAL_USE_LED_INDICATOR
altasoul 13:2fb32235253c 170 *ledp = 0;
altasoul 13:2fb32235253c 171 #endif
altasoul 0:0547c8bf304f 172 } else {
altasoul 5:5d388d1d7987 173 std::printf("\r\nRTOS_Serial::tx_emitter() evt.status %d\n", evt.status);
altasoul 0:0547c8bf304f 174 }
altasoul 13:2fb32235253c 175 #if RTOS_SERIAL_USE_LED_INDICATOR
altasoul 13:2fb32235253c 176 *ledp = 0;
altasoul 13:2fb32235253c 177 #endif
altasoul 0:0547c8bf304f 178 }
altasoul 3:5865277b7710 179 }