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