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