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:
Wed Oct 30 05:56:17 2013 +0000
Revision:
19:d974f46f6882
Parent:
16:2d3937773625
example needed @endcode

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