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@6:438a6c0acbd4, 2013-10-20 (annotated)
- Committer:
- altasoul
- Date:
- Sun Oct 20 05:36:16 2013 +0000
- Revision:
- 6:438a6c0acbd4
- Parent:
- 5:5d388d1d7987
- Child:
- 7:dd892347b524
destructor
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:0547c8bf304f | 23 | #include "rtos_serial.h" |
altasoul | 0:0547c8bf304f | 24 | |
altasoul | 0:0547c8bf304f | 25 | RTOS_Serial::RTOS_Serial(PinName tx, PinName rx, const char *name) |
altasoul | 0:0547c8bf304f | 26 | : Serial(tx, rx, name) |
altasoul | 0:0547c8bf304f | 27 | { |
altasoul | 5:5d388d1d7987 | 28 | uart_number = get_index(); |
altasoul | 0:0547c8bf304f | 29 | const PinName leds[] = {LED1,LED2,LED3,LED4}; |
altasoul | 5:5d388d1d7987 | 30 | ledp = new DigitalOut(leds[uart_number]); |
altasoul | 5:5d388d1d7987 | 31 | rtos_serial_pointers_by_index[uart_number] = this; |
altasoul | 0:0547c8bf304f | 32 | tx_emitter_threadp = new Thread(tx_emitter, (void *) this); |
altasoul | 5:5d388d1d7987 | 33 | tx_tp[uart_number] = tx_emitter_threadp; |
altasoul | 6:438a6c0acbd4 | 34 | rx_isr_pFP = attach(rx_isr[uart_number], RxIrq); |
altasoul | 6:438a6c0acbd4 | 35 | tx_isr_pFP = attach(tx_isr[uart_number], TxIrq); |
altasoul | 2:891773cc33fd | 36 | tx_emitter_threadp->signal_set(0x01); // "prime the pump" of the tx-ready signals |
altasoul | 1:5a66fddad7c4 | 37 | } |
altasoul | 0:0547c8bf304f | 38 | |
altasoul | 6:438a6c0acbd4 | 39 | |
altasoul | 6:438a6c0acbd4 | 40 | RTOS_Serial::~RTOS_Serial() { |
altasoul | 6:438a6c0acbd4 | 41 | remove_handler(rx_isr_pFP, RxIrq); |
altasoul | 6:438a6c0acbd4 | 42 | remove_handler(tx_isr_pFP, TxIrq); |
altasoul | 6:438a6c0acbd4 | 43 | tx_emitter_threadp->terminate(); |
altasoul | 6:438a6c0acbd4 | 44 | tx_tp[uart_number] = NULL; |
altasoul | 6:438a6c0acbd4 | 45 | delete tx_emitter_threadp; |
altasoul | 6:438a6c0acbd4 | 46 | rtos_serial_pointers_by_index[uart_number] = NULL; |
altasoul | 6:438a6c0acbd4 | 47 | } |
altasoul | 6:438a6c0acbd4 | 48 | |
altasoul | 4:c7113cd0ac4b | 49 | RTOS_Serial* RTOS_Serial::rtos_serial_pointers_by_index[4] = { NULL, NULL, NULL, NULL }; |
altasoul | 2:891773cc33fd | 50 | |
altasoul | 1:5a66fddad7c4 | 51 | serial_t RTOS_Serial::get_serial() { return _serial; } |
altasoul | 0:0547c8bf304f | 52 | |
altasoul | 0:0547c8bf304f | 53 | int RTOS_Serial::get_index() { return _serial.index; } |
altasoul | 0:0547c8bf304f | 54 | |
altasoul | 0:0547c8bf304f | 55 | int RTOS_Serial::putc(int c) { |
altasoul | 2:891773cc33fd | 56 | //return Serial::putc(c); //DEBUG |
altasoul | 0:0547c8bf304f | 57 | //if (tx_q.put((int *)c, osWaitForever) == osOK) return c; else return EOF; |
altasoul | 0:0547c8bf304f | 58 | int status; |
altasoul | 0:0547c8bf304f | 59 | if ( (status = tx_q.put((int *)c, osWaitForever)) == osOK) return c; else { |
altasoul | 5:5d388d1d7987 | 60 | std::printf("\r\nRTOS_Serial::tx_q.put() returned %d\r\n", status); |
altasoul | 0:0547c8bf304f | 61 | return EOF; |
altasoul | 0:0547c8bf304f | 62 | } |
altasoul | 0:0547c8bf304f | 63 | } |
altasoul | 0:0547c8bf304f | 64 | |
altasoul | 2:891773cc33fd | 65 | int RTOS_Serial::puts(const char *s) { |
altasoul | 2:891773cc33fd | 66 | int rv = 0; |
altasoul | 2:891773cc33fd | 67 | while (*s) { |
altasoul | 2:891773cc33fd | 68 | if (putc(*s++) == EOF) { |
altasoul | 2:891773cc33fd | 69 | rv = EOF; |
altasoul | 2:891773cc33fd | 70 | break; |
altasoul | 2:891773cc33fd | 71 | } else { |
altasoul | 2:891773cc33fd | 72 | rv++; |
altasoul | 2:891773cc33fd | 73 | } |
altasoul | 2:891773cc33fd | 74 | } |
altasoul | 2:891773cc33fd | 75 | return rv; |
altasoul | 2:891773cc33fd | 76 | } |
altasoul | 2:891773cc33fd | 77 | |
altasoul | 0:0547c8bf304f | 78 | int RTOS_Serial::parent_putc(int c) { |
altasoul | 0:0547c8bf304f | 79 | return Serial::putc(c); |
altasoul | 0:0547c8bf304f | 80 | } |
altasoul | 0:0547c8bf304f | 81 | |
altasoul | 2:891773cc33fd | 82 | int RTOS_Serial::getc() { |
altasoul | 4:c7113cd0ac4b | 83 | int rv; |
altasoul | 4:c7113cd0ac4b | 84 | //return Serial::getc(); //FIXME: stand-in, which fails if we use our RX ISR |
altasoul | 4:c7113cd0ac4b | 85 | osEvent evt = rx_q.get(); |
altasoul | 4:c7113cd0ac4b | 86 | if (evt.status == osEventMessage) { |
altasoul | 4:c7113cd0ac4b | 87 | rv = (int) evt.value.v; |
altasoul | 4:c7113cd0ac4b | 88 | } else { //FIXME: find appropriate error reporting if any |
altasoul | 5:5d388d1d7987 | 89 | std::printf("\r\nRTOS_Serial::getc() evt.status %d\n", evt.status); |
altasoul | 4:c7113cd0ac4b | 90 | rv = EOF; |
altasoul | 4:c7113cd0ac4b | 91 | } |
altasoul | 4:c7113cd0ac4b | 92 | return rv; |
altasoul | 2:891773cc33fd | 93 | } |
altasoul | 2:891773cc33fd | 94 | |
altasoul | 2:891773cc33fd | 95 | |
altasoul | 2:891773cc33fd | 96 | // ISR's for transmitter interrupts |
altasoul | 0:0547c8bf304f | 97 | // class method |
altasoul | 0:0547c8bf304f | 98 | void RTOS_Serial::UART0_TX_ISR(){ |
altasoul | 2:891773cc33fd | 99 | //uint32_t IRR = LPC_UART0->IIR; |
altasoul | 0:0547c8bf304f | 100 | //tx_emitter_threadp->signal_set(0x1); |
altasoul | 0:0547c8bf304f | 101 | tx_tp[0]->signal_set(0x01); |
altasoul | 0:0547c8bf304f | 102 | } |
altasoul | 1:5a66fddad7c4 | 103 | // class method |
altasoul | 1:5a66fddad7c4 | 104 | void RTOS_Serial::UART1_TX_ISR(){ |
altasoul | 2:891773cc33fd | 105 | //uint32_t IRR = LPC_UART1->IIR; |
altasoul | 1:5a66fddad7c4 | 106 | //tx_emitter_threadp->signal_set(0x1); |
altasoul | 1:5a66fddad7c4 | 107 | tx_tp[1]->signal_set(0x01); |
altasoul | 1:5a66fddad7c4 | 108 | } |
altasoul | 1:5a66fddad7c4 | 109 | // class method |
altasoul | 1:5a66fddad7c4 | 110 | void RTOS_Serial::UART2_TX_ISR(){ |
altasoul | 2:891773cc33fd | 111 | //uint32_t IRR = LPC_UART2->IIR; |
altasoul | 1:5a66fddad7c4 | 112 | //tx_emitter_threadp->signal_set(0x1); |
altasoul | 1:5a66fddad7c4 | 113 | tx_tp[2]->signal_set(0x01); |
altasoul | 1:5a66fddad7c4 | 114 | } |
altasoul | 1:5a66fddad7c4 | 115 | // class method |
altasoul | 1:5a66fddad7c4 | 116 | void RTOS_Serial::UART3_TX_ISR(){ |
altasoul | 2:891773cc33fd | 117 | //uint32_t IRR = LPC_UART3->IIR; |
altasoul | 1:5a66fddad7c4 | 118 | //tx_emitter_threadp->signal_set(0x1); |
altasoul | 1:5a66fddad7c4 | 119 | tx_tp[3]->signal_set(0x01); |
altasoul | 1:5a66fddad7c4 | 120 | } |
altasoul | 1:5a66fddad7c4 | 121 | |
altasoul | 1:5a66fddad7c4 | 122 | func RTOS_Serial::tx_isr[4] = { |
altasoul | 1:5a66fddad7c4 | 123 | &RTOS_Serial::UART0_TX_ISR, |
altasoul | 1:5a66fddad7c4 | 124 | &RTOS_Serial::UART1_TX_ISR, |
altasoul | 1:5a66fddad7c4 | 125 | &RTOS_Serial::UART2_TX_ISR, |
altasoul | 1:5a66fddad7c4 | 126 | &RTOS_Serial::UART3_TX_ISR, |
altasoul | 1:5a66fddad7c4 | 127 | }; |
altasoul | 1:5a66fddad7c4 | 128 | |
altasoul | 2:891773cc33fd | 129 | /// ISR's for receiver interrupts |
altasoul | 2:891773cc33fd | 130 | // class method |
altasoul | 2:891773cc33fd | 131 | void RTOS_Serial::UART0_RX_ISR(){ |
altasoul | 4:c7113cd0ac4b | 132 | RTOS_Serial::rtos_serial_pointers_by_index[0]->rx_q.put((int *)LPC_UART0->RBR); |
altasoul | 2:891773cc33fd | 133 | } |
altasoul | 2:891773cc33fd | 134 | // class method |
altasoul | 2:891773cc33fd | 135 | void RTOS_Serial::UART1_RX_ISR(){ |
altasoul | 4:c7113cd0ac4b | 136 | RTOS_Serial::rtos_serial_pointers_by_index[1]->rx_q.put((int *)LPC_UART1->RBR); |
altasoul | 2:891773cc33fd | 137 | } |
altasoul | 2:891773cc33fd | 138 | // class method |
altasoul | 2:891773cc33fd | 139 | void RTOS_Serial::UART2_RX_ISR(){ |
altasoul | 4:c7113cd0ac4b | 140 | RTOS_Serial::rtos_serial_pointers_by_index[2]->rx_q.put((int *)LPC_UART2->RBR); |
altasoul | 2:891773cc33fd | 141 | } |
altasoul | 2:891773cc33fd | 142 | // class method |
altasoul | 2:891773cc33fd | 143 | void RTOS_Serial::UART3_RX_ISR(){ |
altasoul | 4:c7113cd0ac4b | 144 | RTOS_Serial::rtos_serial_pointers_by_index[3]->rx_q.put((int *)LPC_UART3->RBR); |
altasoul | 2:891773cc33fd | 145 | } |
altasoul | 2:891773cc33fd | 146 | |
altasoul | 2:891773cc33fd | 147 | func RTOS_Serial::rx_isr[4] = { |
altasoul | 2:891773cc33fd | 148 | &RTOS_Serial::UART0_RX_ISR, |
altasoul | 2:891773cc33fd | 149 | &RTOS_Serial::UART1_RX_ISR, |
altasoul | 2:891773cc33fd | 150 | &RTOS_Serial::UART2_RX_ISR, |
altasoul | 2:891773cc33fd | 151 | &RTOS_Serial::UART3_RX_ISR, |
altasoul | 2:891773cc33fd | 152 | }; |
altasoul | 2:891773cc33fd | 153 | |
altasoul | 1:5a66fddad7c4 | 154 | Thread* RTOS_Serial::tx_tp[4] = { NULL, NULL, NULL, NULL }; |
altasoul | 0:0547c8bf304f | 155 | |
altasoul | 0:0547c8bf304f | 156 | // tx_emitter is a class method |
altasoul | 0:0547c8bf304f | 157 | void RTOS_Serial::tx_emitter(void const *argument){ |
altasoul | 0:0547c8bf304f | 158 | RTOS_Serial *sp = (RTOS_Serial *) argument; |
altasoul | 0:0547c8bf304f | 159 | osEvent evt; |
altasoul | 2:891773cc33fd | 160 | //osStatus status; |
altasoul | 0:0547c8bf304f | 161 | while(true){ |
altasoul | 0:0547c8bf304f | 162 | evt = sp->tx_q.get(); |
altasoul | 0:0547c8bf304f | 163 | if (evt.status == osEventMessage) { |
altasoul | 2:891773cc33fd | 164 | // There is no TX interrupt until the first byte is sent out the port, |
altasoul | 2:891773cc33fd | 165 | // so we use a timeout on the signal from the interrupt service routine |
altasoul | 2:891773cc33fd | 166 | // and just proceed to transmit the character. This should happen only |
altasoul | 2:891773cc33fd | 167 | // once at most, to "prime the pump", but the timeout provides some |
altasoul | 2:891773cc33fd | 168 | // safety in case something goes wrong. |
altasoul | 2:891773cc33fd | 169 | // A first signal is sent by the RTOS_Serial constructor when the thread |
altasoul | 2:891773cc33fd | 170 | // is created, so normally this will not come into effect. |
altasoul | 2:891773cc33fd | 171 | // DEBUG: timeout omitted to search for instabilities |
altasoul | 2:891773cc33fd | 172 | Thread::signal_wait(0x1/*, 10*/); //FIXME: base the timeout on the baud rate |
altasoul | 0:0547c8bf304f | 173 | *(sp->ledp) = 1; |
altasoul | 0:0547c8bf304f | 174 | sp->parent_putc(evt.value.v); |
altasoul | 0:0547c8bf304f | 175 | *(sp->ledp) = 0; |
altasoul | 0:0547c8bf304f | 176 | } else { |
altasoul | 5:5d388d1d7987 | 177 | std::printf("\r\nRTOS_Serial::tx_emitter() evt.status %d\n", evt.status); |
altasoul | 0:0547c8bf304f | 178 | } |
altasoul | 0:0547c8bf304f | 179 | *(sp->ledp) = 0; |
altasoul | 0:0547c8bf304f | 180 | } |
altasoul | 3:5865277b7710 | 181 | } |