test
SerialBase.h@0:44931ff4a3ed, 2020-01-14 (annotated)
- Committer:
- juansal12
- Date:
- Tue Jan 14 19:14:29 2020 +0000
- Revision:
- 0:44931ff4a3ed
Sofi 7 code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
juansal12 | 0:44931ff4a3ed | 1 | /* mbed Microcontroller Library |
juansal12 | 0:44931ff4a3ed | 2 | * Copyright (c) 2006-2013 ARM Limited |
juansal12 | 0:44931ff4a3ed | 3 | * |
juansal12 | 0:44931ff4a3ed | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
juansal12 | 0:44931ff4a3ed | 5 | * you may not use this file except in compliance with the License. |
juansal12 | 0:44931ff4a3ed | 6 | * You may obtain a copy of the License at |
juansal12 | 0:44931ff4a3ed | 7 | * |
juansal12 | 0:44931ff4a3ed | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
juansal12 | 0:44931ff4a3ed | 9 | * |
juansal12 | 0:44931ff4a3ed | 10 | * Unless required by applicable law or agreed to in writing, software |
juansal12 | 0:44931ff4a3ed | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
juansal12 | 0:44931ff4a3ed | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
juansal12 | 0:44931ff4a3ed | 13 | * See the License for the specific language governing permissions and |
juansal12 | 0:44931ff4a3ed | 14 | * limitations under the License. |
juansal12 | 0:44931ff4a3ed | 15 | */ |
juansal12 | 0:44931ff4a3ed | 16 | #ifndef MBED_SERIALBASE_H |
juansal12 | 0:44931ff4a3ed | 17 | #define MBED_SERIALBASE_H |
juansal12 | 0:44931ff4a3ed | 18 | |
juansal12 | 0:44931ff4a3ed | 19 | #include "platform.h" |
juansal12 | 0:44931ff4a3ed | 20 | |
juansal12 | 0:44931ff4a3ed | 21 | #if DEVICE_SERIAL |
juansal12 | 0:44931ff4a3ed | 22 | |
juansal12 | 0:44931ff4a3ed | 23 | #include "Stream.h" |
juansal12 | 0:44931ff4a3ed | 24 | #include "FunctionPointer.h" |
juansal12 | 0:44931ff4a3ed | 25 | #include "serial_api.h" |
juansal12 | 0:44931ff4a3ed | 26 | |
juansal12 | 0:44931ff4a3ed | 27 | #if DEVICE_SERIAL_ASYNCH |
juansal12 | 0:44931ff4a3ed | 28 | #include "CThunk.h" |
juansal12 | 0:44931ff4a3ed | 29 | #include "dma_api.h" |
juansal12 | 0:44931ff4a3ed | 30 | #endif |
juansal12 | 0:44931ff4a3ed | 31 | |
juansal12 | 0:44931ff4a3ed | 32 | namespace mbed { |
juansal12 | 0:44931ff4a3ed | 33 | |
juansal12 | 0:44931ff4a3ed | 34 | /** A base class for serial port implementations |
juansal12 | 0:44931ff4a3ed | 35 | * Can't be instantiated directly (use Serial or RawSerial) |
juansal12 | 0:44931ff4a3ed | 36 | */ |
juansal12 | 0:44931ff4a3ed | 37 | class SerialBase { |
juansal12 | 0:44931ff4a3ed | 38 | |
juansal12 | 0:44931ff4a3ed | 39 | public: |
juansal12 | 0:44931ff4a3ed | 40 | /** Set the baud rate of the serial port |
juansal12 | 0:44931ff4a3ed | 41 | * |
juansal12 | 0:44931ff4a3ed | 42 | * @param baudrate The baudrate of the serial port (default = 9600). |
juansal12 | 0:44931ff4a3ed | 43 | */ |
juansal12 | 0:44931ff4a3ed | 44 | void baud(int baudrate); |
juansal12 | 0:44931ff4a3ed | 45 | |
juansal12 | 0:44931ff4a3ed | 46 | enum Parity { |
juansal12 | 0:44931ff4a3ed | 47 | None = 0, |
juansal12 | 0:44931ff4a3ed | 48 | Odd, |
juansal12 | 0:44931ff4a3ed | 49 | Even, |
juansal12 | 0:44931ff4a3ed | 50 | Forced1, |
juansal12 | 0:44931ff4a3ed | 51 | Forced0 |
juansal12 | 0:44931ff4a3ed | 52 | }; |
juansal12 | 0:44931ff4a3ed | 53 | |
juansal12 | 0:44931ff4a3ed | 54 | enum IrqType { |
juansal12 | 0:44931ff4a3ed | 55 | RxIrq = 0, |
juansal12 | 0:44931ff4a3ed | 56 | TxIrq |
juansal12 | 0:44931ff4a3ed | 57 | }; |
juansal12 | 0:44931ff4a3ed | 58 | |
juansal12 | 0:44931ff4a3ed | 59 | enum Flow { |
juansal12 | 0:44931ff4a3ed | 60 | Disabled = 0, |
juansal12 | 0:44931ff4a3ed | 61 | RTS, |
juansal12 | 0:44931ff4a3ed | 62 | CTS, |
juansal12 | 0:44931ff4a3ed | 63 | RTSCTS |
juansal12 | 0:44931ff4a3ed | 64 | }; |
juansal12 | 0:44931ff4a3ed | 65 | |
juansal12 | 0:44931ff4a3ed | 66 | /** Set the transmission format used by the serial port |
juansal12 | 0:44931ff4a3ed | 67 | * |
juansal12 | 0:44931ff4a3ed | 68 | * @param bits The number of bits in a word (5-8; default = 8) |
juansal12 | 0:44931ff4a3ed | 69 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
juansal12 | 0:44931ff4a3ed | 70 | * @param stop The number of stop bits (1 or 2; default = 1) |
juansal12 | 0:44931ff4a3ed | 71 | */ |
juansal12 | 0:44931ff4a3ed | 72 | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
juansal12 | 0:44931ff4a3ed | 73 | |
juansal12 | 0:44931ff4a3ed | 74 | /** Determine if there is a character available to read |
juansal12 | 0:44931ff4a3ed | 75 | * |
juansal12 | 0:44931ff4a3ed | 76 | * @returns |
juansal12 | 0:44931ff4a3ed | 77 | * 1 if there is a character available to read, |
juansal12 | 0:44931ff4a3ed | 78 | * 0 otherwise |
juansal12 | 0:44931ff4a3ed | 79 | */ |
juansal12 | 0:44931ff4a3ed | 80 | int readable(); |
juansal12 | 0:44931ff4a3ed | 81 | |
juansal12 | 0:44931ff4a3ed | 82 | /** Determine if there is space available to write a character |
juansal12 | 0:44931ff4a3ed | 83 | * |
juansal12 | 0:44931ff4a3ed | 84 | * @returns |
juansal12 | 0:44931ff4a3ed | 85 | * 1 if there is space to write a character, |
juansal12 | 0:44931ff4a3ed | 86 | * 0 otherwise |
juansal12 | 0:44931ff4a3ed | 87 | */ |
juansal12 | 0:44931ff4a3ed | 88 | int writeable(); |
juansal12 | 0:44931ff4a3ed | 89 | |
juansal12 | 0:44931ff4a3ed | 90 | /** Attach a function to call whenever a serial interrupt is generated |
juansal12 | 0:44931ff4a3ed | 91 | * |
juansal12 | 0:44931ff4a3ed | 92 | * @param fptr A pointer to a void function, or 0 to set as none |
juansal12 | 0:44931ff4a3ed | 93 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
juansal12 | 0:44931ff4a3ed | 94 | */ |
juansal12 | 0:44931ff4a3ed | 95 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
juansal12 | 0:44931ff4a3ed | 96 | |
juansal12 | 0:44931ff4a3ed | 97 | /** Attach a member function to call whenever a serial interrupt is generated |
juansal12 | 0:44931ff4a3ed | 98 | * |
juansal12 | 0:44931ff4a3ed | 99 | * @param tptr pointer to the object to call the member function on |
juansal12 | 0:44931ff4a3ed | 100 | * @param mptr pointer to the member function to be called |
juansal12 | 0:44931ff4a3ed | 101 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
juansal12 | 0:44931ff4a3ed | 102 | */ |
juansal12 | 0:44931ff4a3ed | 103 | template<typename T> |
juansal12 | 0:44931ff4a3ed | 104 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
juansal12 | 0:44931ff4a3ed | 105 | if((mptr != NULL) && (tptr != NULL)) { |
juansal12 | 0:44931ff4a3ed | 106 | _irq[type].attach(tptr, mptr); |
juansal12 | 0:44931ff4a3ed | 107 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
juansal12 | 0:44931ff4a3ed | 108 | } else { |
juansal12 | 0:44931ff4a3ed | 109 | serial_irq_set(&_serial, (SerialIrq)type, 0); |
juansal12 | 0:44931ff4a3ed | 110 | } |
juansal12 | 0:44931ff4a3ed | 111 | } |
juansal12 | 0:44931ff4a3ed | 112 | |
juansal12 | 0:44931ff4a3ed | 113 | /** Generate a break condition on the serial line |
juansal12 | 0:44931ff4a3ed | 114 | */ |
juansal12 | 0:44931ff4a3ed | 115 | void send_break(); |
juansal12 | 0:44931ff4a3ed | 116 | |
juansal12 | 0:44931ff4a3ed | 117 | #if DEVICE_SERIAL_FC |
juansal12 | 0:44931ff4a3ed | 118 | /** Set the flow control type on the serial port |
juansal12 | 0:44931ff4a3ed | 119 | * |
juansal12 | 0:44931ff4a3ed | 120 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
juansal12 | 0:44931ff4a3ed | 121 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
juansal12 | 0:44931ff4a3ed | 122 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
juansal12 | 0:44931ff4a3ed | 123 | */ |
juansal12 | 0:44931ff4a3ed | 124 | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); |
juansal12 | 0:44931ff4a3ed | 125 | #endif |
juansal12 | 0:44931ff4a3ed | 126 | |
juansal12 | 0:44931ff4a3ed | 127 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
juansal12 | 0:44931ff4a3ed | 128 | |
juansal12 | 0:44931ff4a3ed | 129 | #if DEVICE_SERIAL_ASYNCH |
juansal12 | 0:44931ff4a3ed | 130 | |
juansal12 | 0:44931ff4a3ed | 131 | /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback |
juansal12 | 0:44931ff4a3ed | 132 | * |
juansal12 | 0:44931ff4a3ed | 133 | * @param buffer The buffer where received data will be stored |
juansal12 | 0:44931ff4a3ed | 134 | * @param length The buffer length |
juansal12 | 0:44931ff4a3ed | 135 | * @param callback The event callback function |
juansal12 | 0:44931ff4a3ed | 136 | * @param event The logical OR of TX events |
juansal12 | 0:44931ff4a3ed | 137 | */ |
juansal12 | 0:44931ff4a3ed | 138 | int write(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
juansal12 | 0:44931ff4a3ed | 139 | |
juansal12 | 0:44931ff4a3ed | 140 | /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback |
juansal12 | 0:44931ff4a3ed | 141 | * |
juansal12 | 0:44931ff4a3ed | 142 | * @param buffer The buffer where received data will be stored |
juansal12 | 0:44931ff4a3ed | 143 | * @param length The buffer length |
juansal12 | 0:44931ff4a3ed | 144 | * @param callback The event callback function |
juansal12 | 0:44931ff4a3ed | 145 | * @param event The logical OR of TX events |
juansal12 | 0:44931ff4a3ed | 146 | */ |
juansal12 | 0:44931ff4a3ed | 147 | int write(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
juansal12 | 0:44931ff4a3ed | 148 | |
juansal12 | 0:44931ff4a3ed | 149 | /** Abort the on-going write transfer |
juansal12 | 0:44931ff4a3ed | 150 | */ |
juansal12 | 0:44931ff4a3ed | 151 | void abort_write(); |
juansal12 | 0:44931ff4a3ed | 152 | |
juansal12 | 0:44931ff4a3ed | 153 | /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. |
juansal12 | 0:44931ff4a3ed | 154 | * |
juansal12 | 0:44931ff4a3ed | 155 | * @param buffer The buffer where received data will be stored |
juansal12 | 0:44931ff4a3ed | 156 | * @param length The buffer length |
juansal12 | 0:44931ff4a3ed | 157 | * @param callback The event callback function |
juansal12 | 0:44931ff4a3ed | 158 | * @param event The logical OR of RX events |
juansal12 | 0:44931ff4a3ed | 159 | * @param char_match The matching character |
juansal12 | 0:44931ff4a3ed | 160 | */ |
juansal12 | 0:44931ff4a3ed | 161 | int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); |
juansal12 | 0:44931ff4a3ed | 162 | |
juansal12 | 0:44931ff4a3ed | 163 | /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. |
juansal12 | 0:44931ff4a3ed | 164 | * |
juansal12 | 0:44931ff4a3ed | 165 | * @param buffer The buffer where received data will be stored |
juansal12 | 0:44931ff4a3ed | 166 | * @param length The buffer length |
juansal12 | 0:44931ff4a3ed | 167 | * @param callback The event callback function |
juansal12 | 0:44931ff4a3ed | 168 | * @param event The logical OR of RX events |
juansal12 | 0:44931ff4a3ed | 169 | * @param char_match The matching character |
juansal12 | 0:44931ff4a3ed | 170 | */ |
juansal12 | 0:44931ff4a3ed | 171 | int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); |
juansal12 | 0:44931ff4a3ed | 172 | |
juansal12 | 0:44931ff4a3ed | 173 | /** Abort the on-going read transfer |
juansal12 | 0:44931ff4a3ed | 174 | */ |
juansal12 | 0:44931ff4a3ed | 175 | void abort_read(); |
juansal12 | 0:44931ff4a3ed | 176 | |
juansal12 | 0:44931ff4a3ed | 177 | /** Configure DMA usage suggestion for non-blocking TX transfers |
juansal12 | 0:44931ff4a3ed | 178 | * |
juansal12 | 0:44931ff4a3ed | 179 | * @param usage The usage DMA hint for peripheral |
juansal12 | 0:44931ff4a3ed | 180 | * @return Zero if the usage was set, -1 if a transaction is on-going |
juansal12 | 0:44931ff4a3ed | 181 | */ |
juansal12 | 0:44931ff4a3ed | 182 | int set_dma_usage_tx(DMAUsage usage); |
juansal12 | 0:44931ff4a3ed | 183 | |
juansal12 | 0:44931ff4a3ed | 184 | /** Configure DMA usage suggestion for non-blocking RX transfers |
juansal12 | 0:44931ff4a3ed | 185 | * |
juansal12 | 0:44931ff4a3ed | 186 | * @param usage The usage DMA hint for peripheral |
juansal12 | 0:44931ff4a3ed | 187 | * @return Zero if the usage was set, -1 if a transaction is on-going |
juansal12 | 0:44931ff4a3ed | 188 | */ |
juansal12 | 0:44931ff4a3ed | 189 | int set_dma_usage_rx(DMAUsage usage); |
juansal12 | 0:44931ff4a3ed | 190 | |
juansal12 | 0:44931ff4a3ed | 191 | protected: |
juansal12 | 0:44931ff4a3ed | 192 | void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match); |
juansal12 | 0:44931ff4a3ed | 193 | void start_write(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); |
juansal12 | 0:44931ff4a3ed | 194 | void interrupt_handler_asynch(void); |
juansal12 | 0:44931ff4a3ed | 195 | #endif |
juansal12 | 0:44931ff4a3ed | 196 | |
juansal12 | 0:44931ff4a3ed | 197 | protected: |
juansal12 | 0:44931ff4a3ed | 198 | SerialBase(PinName tx, PinName rx); |
juansal12 | 0:44931ff4a3ed | 199 | virtual ~SerialBase() { |
juansal12 | 0:44931ff4a3ed | 200 | } |
juansal12 | 0:44931ff4a3ed | 201 | |
juansal12 | 0:44931ff4a3ed | 202 | int _base_getc(); |
juansal12 | 0:44931ff4a3ed | 203 | int _base_putc(int c); |
juansal12 | 0:44931ff4a3ed | 204 | |
juansal12 | 0:44931ff4a3ed | 205 | #if DEVICE_SERIAL_ASYNCH |
juansal12 | 0:44931ff4a3ed | 206 | CThunk<SerialBase> _thunk_irq; |
juansal12 | 0:44931ff4a3ed | 207 | event_callback_t _tx_callback; |
juansal12 | 0:44931ff4a3ed | 208 | event_callback_t _rx_callback; |
juansal12 | 0:44931ff4a3ed | 209 | DMAUsage _tx_usage; |
juansal12 | 0:44931ff4a3ed | 210 | DMAUsage _rx_usage; |
juansal12 | 0:44931ff4a3ed | 211 | #endif |
juansal12 | 0:44931ff4a3ed | 212 | |
juansal12 | 0:44931ff4a3ed | 213 | serial_t _serial; |
juansal12 | 0:44931ff4a3ed | 214 | FunctionPointer _irq[2]; |
juansal12 | 0:44931ff4a3ed | 215 | int _baud; |
juansal12 | 0:44931ff4a3ed | 216 | |
juansal12 | 0:44931ff4a3ed | 217 | }; |
juansal12 | 0:44931ff4a3ed | 218 | |
juansal12 | 0:44931ff4a3ed | 219 | } // namespace mbed |
juansal12 | 0:44931ff4a3ed | 220 | |
juansal12 | 0:44931ff4a3ed | 221 | #endif |
juansal12 | 0:44931ff4a3ed | 222 | |
juansal12 | 0:44931ff4a3ed | 223 | #endif |