The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.
Dependents: hello SerialTestv11 SerialTestv12 Sierpinski ... more
mbed 2
This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.
SerialBase.h@122:f9eeca106725, 2016-07-07 (annotated)
- Committer:
- Kojto
- Date:
- Thu Jul 07 14:34:11 2016 +0100
- Revision:
- 122:f9eeca106725
- Parent:
- 102:da0ca467f8b5
- Child:
- 126:abea610beb85
Release 122 of the mbed library
Changes:
- new targets - Nucleo L432KC, Beetle, Nucleo F446ZE, Nucleo L011K4
- Thread safety addition - mbed API should contain a statement about thread safety
- critical section API addition
- CAS API (core_util_atomic_incr/decr)
- DEVICE_ are generated from targets.json file, device.h deprecated
- Callback replaces FunctionPointer to provide std like interface
- mbed HAL API docs improvements
- toolchain - prexif attributes with MBED_
- add new attributes - packed, weak, forcedinline, align
- target.json - contains targets definitions
- ST - L1XX - Cube update to 1.5
- SPI clock selection fix (clock from APB domain)
- F7 - Cube update v1.4.0
- L0 - baudrate init fix
- L1 - Cube update v1.5
- F3 - baudrate init fix, 3 targets CAN support
- F4 - Cube update v1.12.0, 3 targets CAN support
- L4XX - Cube update v1.5.1
- F0 - update Cube to v1.5.0
- L4 - 2 targets (L476RG/VG) CAN support
- NXP - pwm clock fix for KSDK2 MCU
- LPC2368 - remove ARM toolchain support - due to regression
- KSDK2 - fix SPI , I2C address and repeat start
- Silabs - some fixes backported from mbed 3
- Renesas - RZ_A1H - SystemCoreClockUpdate addition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 68:f37f3b9c9f0b | 1 | /* mbed Microcontroller Library |
bogdanm | 68:f37f3b9c9f0b | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 68:f37f3b9c9f0b | 3 | * |
bogdanm | 68:f37f3b9c9f0b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 68:f37f3b9c9f0b | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 68:f37f3b9c9f0b | 6 | * You may obtain a copy of the License at |
bogdanm | 68:f37f3b9c9f0b | 7 | * |
bogdanm | 68:f37f3b9c9f0b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 68:f37f3b9c9f0b | 9 | * |
bogdanm | 68:f37f3b9c9f0b | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 68:f37f3b9c9f0b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 68:f37f3b9c9f0b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 68:f37f3b9c9f0b | 13 | * See the License for the specific language governing permissions and |
bogdanm | 68:f37f3b9c9f0b | 14 | * limitations under the License. |
bogdanm | 68:f37f3b9c9f0b | 15 | */ |
bogdanm | 68:f37f3b9c9f0b | 16 | #ifndef MBED_SERIALBASE_H |
bogdanm | 68:f37f3b9c9f0b | 17 | #define MBED_SERIALBASE_H |
bogdanm | 68:f37f3b9c9f0b | 18 | |
bogdanm | 68:f37f3b9c9f0b | 19 | #include "platform.h" |
bogdanm | 68:f37f3b9c9f0b | 20 | |
bogdanm | 68:f37f3b9c9f0b | 21 | #if DEVICE_SERIAL |
bogdanm | 68:f37f3b9c9f0b | 22 | |
bogdanm | 68:f37f3b9c9f0b | 23 | #include "Stream.h" |
Kojto | 122:f9eeca106725 | 24 | #include "Callback.h" |
bogdanm | 68:f37f3b9c9f0b | 25 | #include "serial_api.h" |
bogdanm | 68:f37f3b9c9f0b | 26 | |
Kojto | 98:8ab26030e058 | 27 | #if DEVICE_SERIAL_ASYNCH |
Kojto | 98:8ab26030e058 | 28 | #include "CThunk.h" |
Kojto | 98:8ab26030e058 | 29 | #include "dma_api.h" |
Kojto | 98:8ab26030e058 | 30 | #endif |
Kojto | 98:8ab26030e058 | 31 | |
bogdanm | 68:f37f3b9c9f0b | 32 | namespace mbed { |
bogdanm | 68:f37f3b9c9f0b | 33 | |
bogdanm | 68:f37f3b9c9f0b | 34 | /** A base class for serial port implementations |
bogdanm | 68:f37f3b9c9f0b | 35 | * Can't be instantiated directly (use Serial or RawSerial) |
Kojto | 122:f9eeca106725 | 36 | * |
Kojto | 122:f9eeca106725 | 37 | * @Note Synchronization level: Set by subclass |
bogdanm | 68:f37f3b9c9f0b | 38 | */ |
bogdanm | 68:f37f3b9c9f0b | 39 | class SerialBase { |
bogdanm | 68:f37f3b9c9f0b | 40 | |
bogdanm | 68:f37f3b9c9f0b | 41 | public: |
bogdanm | 68:f37f3b9c9f0b | 42 | /** Set the baud rate of the serial port |
bogdanm | 68:f37f3b9c9f0b | 43 | * |
bogdanm | 68:f37f3b9c9f0b | 44 | * @param baudrate The baudrate of the serial port (default = 9600). |
bogdanm | 68:f37f3b9c9f0b | 45 | */ |
bogdanm | 68:f37f3b9c9f0b | 46 | void baud(int baudrate); |
bogdanm | 68:f37f3b9c9f0b | 47 | |
bogdanm | 68:f37f3b9c9f0b | 48 | enum Parity { |
bogdanm | 68:f37f3b9c9f0b | 49 | None = 0, |
bogdanm | 68:f37f3b9c9f0b | 50 | Odd, |
bogdanm | 68:f37f3b9c9f0b | 51 | Even, |
bogdanm | 68:f37f3b9c9f0b | 52 | Forced1, |
bogdanm | 68:f37f3b9c9f0b | 53 | Forced0 |
bogdanm | 68:f37f3b9c9f0b | 54 | }; |
bogdanm | 68:f37f3b9c9f0b | 55 | |
bogdanm | 68:f37f3b9c9f0b | 56 | enum IrqType { |
bogdanm | 68:f37f3b9c9f0b | 57 | RxIrq = 0, |
bogdanm | 68:f37f3b9c9f0b | 58 | TxIrq |
bogdanm | 68:f37f3b9c9f0b | 59 | }; |
bogdanm | 68:f37f3b9c9f0b | 60 | |
bogdanm | 75:dc225afb6914 | 61 | enum Flow { |
bogdanm | 75:dc225afb6914 | 62 | Disabled = 0, |
bogdanm | 75:dc225afb6914 | 63 | RTS, |
bogdanm | 75:dc225afb6914 | 64 | CTS, |
bogdanm | 75:dc225afb6914 | 65 | RTSCTS |
bogdanm | 75:dc225afb6914 | 66 | }; |
bogdanm | 75:dc225afb6914 | 67 | |
bogdanm | 68:f37f3b9c9f0b | 68 | /** Set the transmission format used by the serial port |
bogdanm | 68:f37f3b9c9f0b | 69 | * |
bogdanm | 68:f37f3b9c9f0b | 70 | * @param bits The number of bits in a word (5-8; default = 8) |
bogdanm | 68:f37f3b9c9f0b | 71 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
bogdanm | 68:f37f3b9c9f0b | 72 | * @param stop The number of stop bits (1 or 2; default = 1) |
bogdanm | 68:f37f3b9c9f0b | 73 | */ |
bogdanm | 68:f37f3b9c9f0b | 74 | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
bogdanm | 68:f37f3b9c9f0b | 75 | |
bogdanm | 68:f37f3b9c9f0b | 76 | /** Determine if there is a character available to read |
bogdanm | 68:f37f3b9c9f0b | 77 | * |
bogdanm | 68:f37f3b9c9f0b | 78 | * @returns |
bogdanm | 68:f37f3b9c9f0b | 79 | * 1 if there is a character available to read, |
bogdanm | 68:f37f3b9c9f0b | 80 | * 0 otherwise |
bogdanm | 68:f37f3b9c9f0b | 81 | */ |
bogdanm | 68:f37f3b9c9f0b | 82 | int readable(); |
bogdanm | 68:f37f3b9c9f0b | 83 | |
bogdanm | 68:f37f3b9c9f0b | 84 | /** Determine if there is space available to write a character |
bogdanm | 68:f37f3b9c9f0b | 85 | * |
bogdanm | 68:f37f3b9c9f0b | 86 | * @returns |
bogdanm | 68:f37f3b9c9f0b | 87 | * 1 if there is space to write a character, |
bogdanm | 68:f37f3b9c9f0b | 88 | * 0 otherwise |
bogdanm | 68:f37f3b9c9f0b | 89 | */ |
bogdanm | 68:f37f3b9c9f0b | 90 | int writeable(); |
bogdanm | 68:f37f3b9c9f0b | 91 | |
bogdanm | 68:f37f3b9c9f0b | 92 | /** Attach a function to call whenever a serial interrupt is generated |
bogdanm | 68:f37f3b9c9f0b | 93 | * |
Kojto | 122:f9eeca106725 | 94 | * @param func A pointer to a void function, or 0 to set as none |
bogdanm | 68:f37f3b9c9f0b | 95 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
bogdanm | 68:f37f3b9c9f0b | 96 | */ |
Kojto | 122:f9eeca106725 | 97 | void attach(Callback<void()> func, IrqType type=RxIrq); |
bogdanm | 68:f37f3b9c9f0b | 98 | |
bogdanm | 68:f37f3b9c9f0b | 99 | /** Attach a member function to call whenever a serial interrupt is generated |
bogdanm | 68:f37f3b9c9f0b | 100 | * |
Kojto | 122:f9eeca106725 | 101 | * @param obj pointer to the object to call the member function on |
Kojto | 122:f9eeca106725 | 102 | * @param method pointer to the member function to be called |
bogdanm | 68:f37f3b9c9f0b | 103 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
bogdanm | 68:f37f3b9c9f0b | 104 | */ |
bogdanm | 68:f37f3b9c9f0b | 105 | template<typename T> |
Kojto | 122:f9eeca106725 | 106 | void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { |
Kojto | 122:f9eeca106725 | 107 | attach(Callback<void()>(obj, method), type); |
Kojto | 122:f9eeca106725 | 108 | } |
Kojto | 122:f9eeca106725 | 109 | |
Kojto | 122:f9eeca106725 | 110 | /** Attach a member function to call whenever a serial interrupt is generated |
Kojto | 122:f9eeca106725 | 111 | * |
Kojto | 122:f9eeca106725 | 112 | * @param obj pointer to the object to call the member function on |
Kojto | 122:f9eeca106725 | 113 | * @param method pointer to the member function to be called |
Kojto | 122:f9eeca106725 | 114 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Kojto | 122:f9eeca106725 | 115 | */ |
Kojto | 122:f9eeca106725 | 116 | template<typename T> |
Kojto | 122:f9eeca106725 | 117 | void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { |
Kojto | 122:f9eeca106725 | 118 | attach(Callback<void()>(obj, method), type); |
bogdanm | 68:f37f3b9c9f0b | 119 | } |
bogdanm | 68:f37f3b9c9f0b | 120 | |
bogdanm | 68:f37f3b9c9f0b | 121 | /** Generate a break condition on the serial line |
bogdanm | 68:f37f3b9c9f0b | 122 | */ |
bogdanm | 68:f37f3b9c9f0b | 123 | void send_break(); |
bogdanm | 85:024bf7f99721 | 124 | |
Kojto | 122:f9eeca106725 | 125 | protected: |
Kojto | 122:f9eeca106725 | 126 | |
Kojto | 122:f9eeca106725 | 127 | /** Acquire exclusive access to this serial port |
Kojto | 122:f9eeca106725 | 128 | */ |
Kojto | 122:f9eeca106725 | 129 | virtual void lock(void); |
Kojto | 122:f9eeca106725 | 130 | |
Kojto | 122:f9eeca106725 | 131 | /** Release exclusive access to this serial port |
Kojto | 122:f9eeca106725 | 132 | */ |
Kojto | 122:f9eeca106725 | 133 | virtual void unlock(void); |
Kojto | 122:f9eeca106725 | 134 | |
Kojto | 122:f9eeca106725 | 135 | public: |
Kojto | 122:f9eeca106725 | 136 | |
bogdanm | 75:dc225afb6914 | 137 | #if DEVICE_SERIAL_FC |
bogdanm | 75:dc225afb6914 | 138 | /** Set the flow control type on the serial port |
bogdanm | 75:dc225afb6914 | 139 | * |
bogdanm | 85:024bf7f99721 | 140 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
bogdanm | 75:dc225afb6914 | 141 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
bogdanm | 75:dc225afb6914 | 142 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
bogdanm | 75:dc225afb6914 | 143 | */ |
bogdanm | 75:dc225afb6914 | 144 | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); |
bogdanm | 75:dc225afb6914 | 145 | #endif |
bogdanm | 68:f37f3b9c9f0b | 146 | |
bogdanm | 68:f37f3b9c9f0b | 147 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
bogdanm | 68:f37f3b9c9f0b | 148 | |
Kojto | 98:8ab26030e058 | 149 | #if DEVICE_SERIAL_ASYNCH |
Kojto | 98:8ab26030e058 | 150 | |
Kojto | 98:8ab26030e058 | 151 | /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback |
Kojto | 98:8ab26030e058 | 152 | * |
Kojto | 98:8ab26030e058 | 153 | * @param buffer The buffer where received data will be stored |
Kojto | 102:da0ca467f8b5 | 154 | * @param length The buffer length in bytes |
Kojto | 98:8ab26030e058 | 155 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 156 | * @param event The logical OR of TX events |
Kojto | 98:8ab26030e058 | 157 | */ |
Kojto | 102:da0ca467f8b5 | 158 | int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
Kojto | 98:8ab26030e058 | 159 | |
Kojto | 98:8ab26030e058 | 160 | /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback |
Kojto | 98:8ab26030e058 | 161 | * |
Kojto | 98:8ab26030e058 | 162 | * @param buffer The buffer where received data will be stored |
Kojto | 102:da0ca467f8b5 | 163 | * @param length The buffer length in bytes |
Kojto | 98:8ab26030e058 | 164 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 165 | * @param event The logical OR of TX events |
Kojto | 98:8ab26030e058 | 166 | */ |
Kojto | 102:da0ca467f8b5 | 167 | int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
Kojto | 98:8ab26030e058 | 168 | |
Kojto | 98:8ab26030e058 | 169 | /** Abort the on-going write transfer |
Kojto | 98:8ab26030e058 | 170 | */ |
Kojto | 98:8ab26030e058 | 171 | void abort_write(); |
Kojto | 98:8ab26030e058 | 172 | |
Kojto | 98:8ab26030e058 | 173 | /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. |
Kojto | 98:8ab26030e058 | 174 | * |
Kojto | 98:8ab26030e058 | 175 | * @param buffer The buffer where received data will be stored |
Kojto | 102:da0ca467f8b5 | 176 | * @param length The buffer length in bytes |
Kojto | 98:8ab26030e058 | 177 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 178 | * @param event The logical OR of RX events |
Kojto | 98:8ab26030e058 | 179 | * @param char_match The matching character |
Kojto | 98:8ab26030e058 | 180 | */ |
Kojto | 98:8ab26030e058 | 181 | 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); |
Kojto | 98:8ab26030e058 | 182 | |
Kojto | 98:8ab26030e058 | 183 | /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. |
Kojto | 98:8ab26030e058 | 184 | * |
Kojto | 98:8ab26030e058 | 185 | * @param buffer The buffer where received data will be stored |
Kojto | 102:da0ca467f8b5 | 186 | * @param length The buffer length in bytes |
Kojto | 98:8ab26030e058 | 187 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 188 | * @param event The logical OR of RX events |
Kojto | 98:8ab26030e058 | 189 | * @param char_match The matching character |
Kojto | 98:8ab26030e058 | 190 | */ |
Kojto | 98:8ab26030e058 | 191 | 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); |
Kojto | 98:8ab26030e058 | 192 | |
Kojto | 98:8ab26030e058 | 193 | /** Abort the on-going read transfer |
Kojto | 98:8ab26030e058 | 194 | */ |
Kojto | 98:8ab26030e058 | 195 | void abort_read(); |
Kojto | 98:8ab26030e058 | 196 | |
Kojto | 98:8ab26030e058 | 197 | /** Configure DMA usage suggestion for non-blocking TX transfers |
Kojto | 98:8ab26030e058 | 198 | * |
Kojto | 98:8ab26030e058 | 199 | * @param usage The usage DMA hint for peripheral |
Kojto | 98:8ab26030e058 | 200 | * @return Zero if the usage was set, -1 if a transaction is on-going |
Kojto | 98:8ab26030e058 | 201 | */ |
Kojto | 98:8ab26030e058 | 202 | int set_dma_usage_tx(DMAUsage usage); |
Kojto | 98:8ab26030e058 | 203 | |
Kojto | 98:8ab26030e058 | 204 | /** Configure DMA usage suggestion for non-blocking RX transfers |
Kojto | 98:8ab26030e058 | 205 | * |
Kojto | 98:8ab26030e058 | 206 | * @param usage The usage DMA hint for peripheral |
Kojto | 98:8ab26030e058 | 207 | * @return Zero if the usage was set, -1 if a transaction is on-going |
Kojto | 98:8ab26030e058 | 208 | */ |
Kojto | 98:8ab26030e058 | 209 | int set_dma_usage_rx(DMAUsage usage); |
Kojto | 98:8ab26030e058 | 210 | |
Kojto | 98:8ab26030e058 | 211 | protected: |
Kojto | 98:8ab26030e058 | 212 | void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match); |
Kojto | 102:da0ca467f8b5 | 213 | void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); |
Kojto | 98:8ab26030e058 | 214 | void interrupt_handler_asynch(void); |
Kojto | 98:8ab26030e058 | 215 | #endif |
Kojto | 98:8ab26030e058 | 216 | |
bogdanm | 68:f37f3b9c9f0b | 217 | protected: |
bogdanm | 68:f37f3b9c9f0b | 218 | SerialBase(PinName tx, PinName rx); |
bogdanm | 85:024bf7f99721 | 219 | virtual ~SerialBase() { |
bogdanm | 85:024bf7f99721 | 220 | } |
bogdanm | 85:024bf7f99721 | 221 | |
bogdanm | 68:f37f3b9c9f0b | 222 | int _base_getc(); |
bogdanm | 68:f37f3b9c9f0b | 223 | int _base_putc(int c); |
bogdanm | 68:f37f3b9c9f0b | 224 | |
Kojto | 98:8ab26030e058 | 225 | #if DEVICE_SERIAL_ASYNCH |
Kojto | 98:8ab26030e058 | 226 | CThunk<SerialBase> _thunk_irq; |
Kojto | 98:8ab26030e058 | 227 | event_callback_t _tx_callback; |
Kojto | 98:8ab26030e058 | 228 | event_callback_t _rx_callback; |
Kojto | 98:8ab26030e058 | 229 | DMAUsage _tx_usage; |
Kojto | 98:8ab26030e058 | 230 | DMAUsage _rx_usage; |
Kojto | 98:8ab26030e058 | 231 | #endif |
Kojto | 98:8ab26030e058 | 232 | |
Kojto | 122:f9eeca106725 | 233 | serial_t _serial; |
Kojto | 122:f9eeca106725 | 234 | Callback<void()> _irq[2]; |
Kojto | 122:f9eeca106725 | 235 | int _baud; |
Kojto | 98:8ab26030e058 | 236 | |
bogdanm | 68:f37f3b9c9f0b | 237 | }; |
bogdanm | 68:f37f3b9c9f0b | 238 | |
bogdanm | 68:f37f3b9c9f0b | 239 | } // namespace mbed |
bogdanm | 68:f37f3b9c9f0b | 240 | |
bogdanm | 68:f37f3b9c9f0b | 241 | #endif |
bogdanm | 68:f37f3b9c9f0b | 242 | |
bogdanm | 68:f37f3b9c9f0b | 243 | #endif |