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.h@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 | #ifndef RTOS_SERIAL_H |
altasoul | 0:0547c8bf304f | 24 | #define RTOS_SERIAL_H |
altasoul | 0:0547c8bf304f | 25 | |
altasoul | 0:0547c8bf304f | 26 | #include "mbed.h" |
altasoul | 0:0547c8bf304f | 27 | #include "rtos.h" |
altasoul | 0:0547c8bf304f | 28 | |
altasoul | 0:0547c8bf304f | 29 | #define MBED_RTOS_SERIAL_VERSION_MAJOR 0 |
altasoul | 0:0547c8bf304f | 30 | #define MBED_RTOS_SERIAL_VERSION_MINOR 0 |
altasoul | 0:0547c8bf304f | 31 | #define MBED_RTOS_SERIAL_VERSION_EDIT 3 |
altasoul | 0:0547c8bf304f | 32 | |
altasoul | 0:0547c8bf304f | 33 | #define N_TX_MSGS 5 /* FIXME: parameterize */ |
altasoul | 0:0547c8bf304f | 34 | #define OPTION_USE_MAIL_FOR_TX 1 |
altasoul | 0:0547c8bf304f | 35 | |
altasoul | 0:0547c8bf304f | 36 | /* |
altasoul | 0:0547c8bf304f | 37 | from Serial.h: |
altasoul | 0:0547c8bf304f | 38 | class Serial : public Stream { |
altasoul | 0:0547c8bf304f | 39 | |
altasoul | 0:0547c8bf304f | 40 | public: |
altasoul | 0:0547c8bf304f | 41 | Serial(PinName tx, PinName rx, const char *name=NULL); |
altasoul | 0:0547c8bf304f | 42 | ... |
altasoul | 0:0547c8bf304f | 43 | protected: |
altasoul | 0:0547c8bf304f | 44 | ... |
altasoul | 0:0547c8bf304f | 45 | serial_t _serial; |
altasoul | 0:0547c8bf304f | 46 | |
altasoul | 0:0547c8bf304f | 47 | from serial_api.h: |
altasoul | 0:0547c8bf304f | 48 | typedef struct serial_s serial_t; |
altasoul | 0:0547c8bf304f | 49 | |
altasoul | 0:0547c8bf304f | 50 | from objects.h: |
altasoul | 0:0547c8bf304f | 51 | struct serial_s { |
altasoul | 0:0547c8bf304f | 52 | LPC_UART_TypeDef *uart; |
altasoul | 0:0547c8bf304f | 53 | int index; |
altasoul | 0:0547c8bf304f | 54 | }; |
altasoul | 0:0547c8bf304f | 55 | |
altasoul | 0:0547c8bf304f | 56 | from LPC17xx.h: |
altasoul | 0:0547c8bf304f | 57 | typedef struct |
altasoul | 0:0547c8bf304f | 58 | { |
altasoul | 0:0547c8bf304f | 59 | union { |
altasoul | 0:0547c8bf304f | 60 | __I uint8_t RBR; |
altasoul | 0:0547c8bf304f | 61 | __O uint8_t THR; |
altasoul | 0:0547c8bf304f | 62 | __IO uint8_t DLL; |
altasoul | 0:0547c8bf304f | 63 | uint32_t RESERVED0; |
altasoul | 0:0547c8bf304f | 64 | }; |
altasoul | 0:0547c8bf304f | 65 | union { |
altasoul | 0:0547c8bf304f | 66 | __IO uint8_t DLM; |
altasoul | 0:0547c8bf304f | 67 | __IO uint32_t IER; |
altasoul | 0:0547c8bf304f | 68 | }; |
altasoul | 0:0547c8bf304f | 69 | union { |
altasoul | 0:0547c8bf304f | 70 | __I uint32_t IIR; |
altasoul | 0:0547c8bf304f | 71 | __O uint8_t FCR; |
altasoul | 0:0547c8bf304f | 72 | }; |
altasoul | 0:0547c8bf304f | 73 | __IO uint8_t LCR; |
altasoul | 0:0547c8bf304f | 74 | uint8_t RESERVED1[7]; |
altasoul | 0:0547c8bf304f | 75 | __I uint8_t LSR; |
altasoul | 0:0547c8bf304f | 76 | uint8_t RESERVED2[7]; |
altasoul | 0:0547c8bf304f | 77 | __IO uint8_t SCR; |
altasoul | 0:0547c8bf304f | 78 | uint8_t RESERVED3[3]; |
altasoul | 0:0547c8bf304f | 79 | __IO uint32_t ACR; |
altasoul | 0:0547c8bf304f | 80 | __IO uint8_t ICR; |
altasoul | 0:0547c8bf304f | 81 | uint8_t RESERVED4[3]; |
altasoul | 0:0547c8bf304f | 82 | __IO uint8_t FDR; |
altasoul | 0:0547c8bf304f | 83 | uint8_t RESERVED5[7]; |
altasoul | 0:0547c8bf304f | 84 | __IO uint8_t TER; |
altasoul | 0:0547c8bf304f | 85 | uint8_t RESERVED6[39]; |
altasoul | 0:0547c8bf304f | 86 | __IO uint32_t FIFOLVL; |
altasoul | 0:0547c8bf304f | 87 | } LPC_UART_TypeDef; |
altasoul | 0:0547c8bf304f | 88 | |
altasoul | 0:0547c8bf304f | 89 | */ |
altasoul | 0:0547c8bf304f | 90 | |
altasoul | 1:5a66fddad7c4 | 91 | //FIXME: |
altasoul | 1:5a66fddad7c4 | 92 | typedef void (*func)(); |
altasoul | 0:0547c8bf304f | 93 | |
altasoul | 0:0547c8bf304f | 94 | class RTOS_Serial : public Serial { |
altasoul | 0:0547c8bf304f | 95 | public: |
altasoul | 0:0547c8bf304f | 96 | |
altasoul | 0:0547c8bf304f | 97 | RTOS_Serial(PinName tx, PinName rx, const char *name=NULL); |
altasoul | 6:438a6c0acbd4 | 98 | virtual ~RTOS_Serial(); |
altasoul | 0:0547c8bf304f | 99 | int putc(int c); |
altasoul | 2:891773cc33fd | 100 | int puts(const char *s); |
altasoul | 2:891773cc33fd | 101 | int getc(); |
altasoul | 0:0547c8bf304f | 102 | int get_index(); |
altasoul | 2:891773cc33fd | 103 | Queue<int, 16> rx_q; |
altasoul | 4:c7113cd0ac4b | 104 | static RTOS_Serial* rtos_serial_pointers_by_index[4]; |
altasoul | 0:0547c8bf304f | 105 | |
altasoul | 0:0547c8bf304f | 106 | protected: |
altasoul | 0:0547c8bf304f | 107 | |
altasoul | 0:0547c8bf304f | 108 | typedef struct { |
altasoul | 0:0547c8bf304f | 109 | char *p; |
altasoul | 0:0547c8bf304f | 110 | int len; |
altasoul | 0:0547c8bf304f | 111 | } tx_mail_t; |
altasoul | 0:0547c8bf304f | 112 | |
altasoul | 0:0547c8bf304f | 113 | DigitalOut *ledp; |
altasoul | 5:5d388d1d7987 | 114 | int uart_number; |
altasoul | 0:0547c8bf304f | 115 | Queue<int, 16> tx_q; |
altasoul | 0:0547c8bf304f | 116 | int parent_putc(int); |
altasoul | 1:5a66fddad7c4 | 117 | |
altasoul | 6:438a6c0acbd4 | 118 | pFunctionPointer_t tx_isr_pFP, rx_isr_pFP; |
altasoul | 0:0547c8bf304f | 119 | static void tx_emitter(void const *argument); |
altasoul | 4:c7113cd0ac4b | 120 | Thread* tx_emitter_threadp; |
altasoul | 4:c7113cd0ac4b | 121 | static Thread* tx_tp[4]; |
altasoul | 1:5a66fddad7c4 | 122 | static void UART0_TX_ISR(), UART1_TX_ISR(), UART2_TX_ISR(), UART3_TX_ISR(); |
altasoul | 1:5a66fddad7c4 | 123 | static func tx_isr[4]; |
altasoul | 4:c7113cd0ac4b | 124 | static void UART0_RX_ISR(), UART1_RX_ISR(), UART2_RX_ISR(), UART3_RX_ISR(); |
altasoul | 2:891773cc33fd | 125 | static func rx_isr[4]; |
altasoul | 0:0547c8bf304f | 126 | uint32_t _debug_increment_me; |
altasoul | 0:0547c8bf304f | 127 | serial_t get_serial(); |
altasoul | 0:0547c8bf304f | 128 | }; |
altasoul | 0:0547c8bf304f | 129 | |
altasoul | 0:0547c8bf304f | 130 | #endif |