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:
Fri Oct 18 02:55:07 2013 +0000
Revision:
0:0547c8bf304f
Child:
1:5a66fddad7c4
checkpoint in draft

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 0:0547c8bf304f 23 #include "rtos_serial.h"
altasoul 0:0547c8bf304f 24
altasoul 0:0547c8bf304f 25 //RTOS_Serial::tx_mail_t blah;
altasoul 0:0547c8bf304f 26
altasoul 0:0547c8bf304f 27 RTOS_Serial::RTOS_Serial(PinName tx, PinName rx, const char *name)
altasoul 0:0547c8bf304f 28 : Serial(tx, rx, name)
altasoul 0:0547c8bf304f 29 {
altasoul 0:0547c8bf304f 30 const PinName leds[] = {LED1,LED2,LED3,LED4};
altasoul 0:0547c8bf304f 31 ledp = new DigitalOut(leds[get_index()]);
altasoul 0:0547c8bf304f 32 tx_emitter_threadp = new Thread(tx_emitter, (void *) this);
altasoul 0:0547c8bf304f 33 };
altasoul 0:0547c8bf304f 34
altasoul 0:0547c8bf304f 35 Thread* RTOS_Serial::tx_tp[4] = { NULL, NULL, NULL, NULL };
altasoul 0:0547c8bf304f 36
altasoul 0:0547c8bf304f 37 serial_t RTOS_Serial::get_serial() { return _serial; };
altasoul 0:0547c8bf304f 38
altasoul 0:0547c8bf304f 39 int RTOS_Serial::get_index() { return _serial.index; }
altasoul 0:0547c8bf304f 40
altasoul 0:0547c8bf304f 41 int RTOS_Serial::putc(int c) {
altasoul 0:0547c8bf304f 42 //return Serial::putc(c);
altasoul 0:0547c8bf304f 43 //if (tx_q.put((int *)c, osWaitForever) == osOK) return c; else return EOF;
altasoul 0:0547c8bf304f 44 int status;
altasoul 0:0547c8bf304f 45 if ( (status = tx_q.put((int *)c, osWaitForever)) == osOK) return c; else {
altasoul 0:0547c8bf304f 46 std::printf("tx_q.put() returned %d\r\n", status);
altasoul 0:0547c8bf304f 47 return EOF;
altasoul 0:0547c8bf304f 48 }
altasoul 0:0547c8bf304f 49 }
altasoul 0:0547c8bf304f 50
altasoul 0:0547c8bf304f 51 int RTOS_Serial::parent_putc(int c) {
altasoul 0:0547c8bf304f 52 return Serial::putc(c);
altasoul 0:0547c8bf304f 53 }
altasoul 0:0547c8bf304f 54
altasoul 0:0547c8bf304f 55 // class method
altasoul 0:0547c8bf304f 56 void RTOS_Serial::UART0_TX_ISR(){
altasoul 0:0547c8bf304f 57 uint32_t IRR = LPC_UART0->IIR;
altasoul 0:0547c8bf304f 58 //tx_emitter_threadp->signal_set(0x1);
altasoul 0:0547c8bf304f 59 tx_tp[0]->signal_set(0x01);
altasoul 0:0547c8bf304f 60 }
altasoul 0:0547c8bf304f 61
altasoul 0:0547c8bf304f 62 // tx_emitter is a class method
altasoul 0:0547c8bf304f 63 void RTOS_Serial::tx_emitter(void const *argument){
altasoul 0:0547c8bf304f 64 RTOS_Serial *sp = (RTOS_Serial *) argument;
altasoul 0:0547c8bf304f 65 osEvent evt;
altasoul 0:0547c8bf304f 66 while(true){
altasoul 0:0547c8bf304f 67 #if 0
altasoul 0:0547c8bf304f 68 *(sp->ledp) = 0;
altasoul 0:0547c8bf304f 69 osDelay(200);
altasoul 0:0547c8bf304f 70 *(sp->ledp) = 1;
altasoul 0:0547c8bf304f 71 osDelay(200);
altasoul 0:0547c8bf304f 72 #endif
altasoul 0:0547c8bf304f 73 #if 1
altasoul 0:0547c8bf304f 74 evt = sp->tx_q.get();
altasoul 0:0547c8bf304f 75 if (evt.status == osEventMessage) {
altasoul 0:0547c8bf304f 76 //Thread::signal_wait(0x1);
altasoul 0:0547c8bf304f 77 *(sp->ledp) = 1;
altasoul 0:0547c8bf304f 78 sp->parent_putc(evt.value.v);
altasoul 0:0547c8bf304f 79 *(sp->ledp) = 0;
altasoul 0:0547c8bf304f 80 } else {
altasoul 0:0547c8bf304f 81 std::printf("tx_emitter() evt.status %d\n", evt.status);
altasoul 0:0547c8bf304f 82 }
altasoul 0:0547c8bf304f 83 #endif
altasoul 0:0547c8bf304f 84 *(sp->ledp) = 0;
altasoul 0:0547c8bf304f 85 }
altasoul 0:0547c8bf304f 86 }