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