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@114:252557024ec3, 2016-02-16 (annotated)
- Committer:
- Kojto
- Date:
- Tue Feb 16 14:28:01 2016 +0000
- Revision:
- 114:252557024ec3
- Parent:
- 102:da0ca467f8b5
- Child:
- 122:f9eeca106725
Release 114 of the mbed library
Changes:
- Atmel SAM - warnings fixes
- B96B F446VE - hw control flow addition
- Remove of GCC CW which was not active
- Remove GCC CS, not released anymore - deprecated
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" |
bogdanm | 68:f37f3b9c9f0b | 24 | #include "FunctionPointer.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) |
bogdanm | 68:f37f3b9c9f0b | 36 | */ |
bogdanm | 68:f37f3b9c9f0b | 37 | class SerialBase { |
bogdanm | 68:f37f3b9c9f0b | 38 | |
bogdanm | 68:f37f3b9c9f0b | 39 | public: |
bogdanm | 68:f37f3b9c9f0b | 40 | /** Set the baud rate of the serial port |
bogdanm | 68:f37f3b9c9f0b | 41 | * |
bogdanm | 68:f37f3b9c9f0b | 42 | * @param baudrate The baudrate of the serial port (default = 9600). |
bogdanm | 68:f37f3b9c9f0b | 43 | */ |
bogdanm | 68:f37f3b9c9f0b | 44 | void baud(int baudrate); |
bogdanm | 68:f37f3b9c9f0b | 45 | |
bogdanm | 68:f37f3b9c9f0b | 46 | enum Parity { |
bogdanm | 68:f37f3b9c9f0b | 47 | None = 0, |
bogdanm | 68:f37f3b9c9f0b | 48 | Odd, |
bogdanm | 68:f37f3b9c9f0b | 49 | Even, |
bogdanm | 68:f37f3b9c9f0b | 50 | Forced1, |
bogdanm | 68:f37f3b9c9f0b | 51 | Forced0 |
bogdanm | 68:f37f3b9c9f0b | 52 | }; |
bogdanm | 68:f37f3b9c9f0b | 53 | |
bogdanm | 68:f37f3b9c9f0b | 54 | enum IrqType { |
bogdanm | 68:f37f3b9c9f0b | 55 | RxIrq = 0, |
bogdanm | 68:f37f3b9c9f0b | 56 | TxIrq |
bogdanm | 68:f37f3b9c9f0b | 57 | }; |
bogdanm | 68:f37f3b9c9f0b | 58 | |
bogdanm | 75:dc225afb6914 | 59 | enum Flow { |
bogdanm | 75:dc225afb6914 | 60 | Disabled = 0, |
bogdanm | 75:dc225afb6914 | 61 | RTS, |
bogdanm | 75:dc225afb6914 | 62 | CTS, |
bogdanm | 75:dc225afb6914 | 63 | RTSCTS |
bogdanm | 75:dc225afb6914 | 64 | }; |
bogdanm | 75:dc225afb6914 | 65 | |
bogdanm | 68:f37f3b9c9f0b | 66 | /** Set the transmission format used by the serial port |
bogdanm | 68:f37f3b9c9f0b | 67 | * |
bogdanm | 68:f37f3b9c9f0b | 68 | * @param bits The number of bits in a word (5-8; default = 8) |
bogdanm | 68:f37f3b9c9f0b | 69 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
bogdanm | 68:f37f3b9c9f0b | 70 | * @param stop The number of stop bits (1 or 2; default = 1) |
bogdanm | 68:f37f3b9c9f0b | 71 | */ |
bogdanm | 68:f37f3b9c9f0b | 72 | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
bogdanm | 68:f37f3b9c9f0b | 73 | |
bogdanm | 68:f37f3b9c9f0b | 74 | /** Determine if there is a character available to read |
bogdanm | 68:f37f3b9c9f0b | 75 | * |
bogdanm | 68:f37f3b9c9f0b | 76 | * @returns |
bogdanm | 68:f37f3b9c9f0b | 77 | * 1 if there is a character available to read, |
bogdanm | 68:f37f3b9c9f0b | 78 | * 0 otherwise |
bogdanm | 68:f37f3b9c9f0b | 79 | */ |
bogdanm | 68:f37f3b9c9f0b | 80 | int readable(); |
bogdanm | 68:f37f3b9c9f0b | 81 | |
bogdanm | 68:f37f3b9c9f0b | 82 | /** Determine if there is space available to write a character |
bogdanm | 68:f37f3b9c9f0b | 83 | * |
bogdanm | 68:f37f3b9c9f0b | 84 | * @returns |
bogdanm | 68:f37f3b9c9f0b | 85 | * 1 if there is space to write a character, |
bogdanm | 68:f37f3b9c9f0b | 86 | * 0 otherwise |
bogdanm | 68:f37f3b9c9f0b | 87 | */ |
bogdanm | 68:f37f3b9c9f0b | 88 | int writeable(); |
bogdanm | 68:f37f3b9c9f0b | 89 | |
bogdanm | 68:f37f3b9c9f0b | 90 | /** Attach a function to call whenever a serial interrupt is generated |
bogdanm | 68:f37f3b9c9f0b | 91 | * |
bogdanm | 68:f37f3b9c9f0b | 92 | * @param fptr A pointer to a void function, or 0 to set as none |
bogdanm | 68:f37f3b9c9f0b | 93 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
bogdanm | 68:f37f3b9c9f0b | 94 | */ |
bogdanm | 68:f37f3b9c9f0b | 95 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
bogdanm | 68:f37f3b9c9f0b | 96 | |
bogdanm | 68:f37f3b9c9f0b | 97 | /** Attach a member function to call whenever a serial interrupt is generated |
bogdanm | 68:f37f3b9c9f0b | 98 | * |
bogdanm | 68:f37f3b9c9f0b | 99 | * @param tptr pointer to the object to call the member function on |
bogdanm | 68:f37f3b9c9f0b | 100 | * @param mptr pointer to the member function to be called |
bogdanm | 68:f37f3b9c9f0b | 101 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
bogdanm | 68:f37f3b9c9f0b | 102 | */ |
bogdanm | 68:f37f3b9c9f0b | 103 | template<typename T> |
bogdanm | 68:f37f3b9c9f0b | 104 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
bogdanm | 68:f37f3b9c9f0b | 105 | if((mptr != NULL) && (tptr != NULL)) { |
bogdanm | 68:f37f3b9c9f0b | 106 | _irq[type].attach(tptr, mptr); |
bogdanm | 68:f37f3b9c9f0b | 107 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
Kojto | 99:dbbf35b96557 | 108 | } else { |
Kojto | 99:dbbf35b96557 | 109 | serial_irq_set(&_serial, (SerialIrq)type, 0); |
bogdanm | 68:f37f3b9c9f0b | 110 | } |
bogdanm | 68:f37f3b9c9f0b | 111 | } |
bogdanm | 68:f37f3b9c9f0b | 112 | |
bogdanm | 68:f37f3b9c9f0b | 113 | /** Generate a break condition on the serial line |
bogdanm | 68:f37f3b9c9f0b | 114 | */ |
bogdanm | 68:f37f3b9c9f0b | 115 | void send_break(); |
bogdanm | 85:024bf7f99721 | 116 | |
bogdanm | 75:dc225afb6914 | 117 | #if DEVICE_SERIAL_FC |
bogdanm | 75:dc225afb6914 | 118 | /** Set the flow control type on the serial port |
bogdanm | 75:dc225afb6914 | 119 | * |
bogdanm | 85:024bf7f99721 | 120 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
bogdanm | 75:dc225afb6914 | 121 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
bogdanm | 75:dc225afb6914 | 122 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
bogdanm | 75:dc225afb6914 | 123 | */ |
bogdanm | 75:dc225afb6914 | 124 | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); |
bogdanm | 75:dc225afb6914 | 125 | #endif |
bogdanm | 68:f37f3b9c9f0b | 126 | |
bogdanm | 68:f37f3b9c9f0b | 127 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
bogdanm | 68:f37f3b9c9f0b | 128 | |
Kojto | 98:8ab26030e058 | 129 | #if DEVICE_SERIAL_ASYNCH |
Kojto | 98:8ab26030e058 | 130 | |
Kojto | 98:8ab26030e058 | 131 | /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback |
Kojto | 98:8ab26030e058 | 132 | * |
Kojto | 98:8ab26030e058 | 133 | * @param buffer The buffer where received data will be stored |
Kojto | 102:da0ca467f8b5 | 134 | * @param length The buffer length in bytes |
Kojto | 98:8ab26030e058 | 135 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 136 | * @param event The logical OR of TX events |
Kojto | 98:8ab26030e058 | 137 | */ |
Kojto | 102:da0ca467f8b5 | 138 | int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
Kojto | 98:8ab26030e058 | 139 | |
Kojto | 98:8ab26030e058 | 140 | /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback |
Kojto | 98:8ab26030e058 | 141 | * |
Kojto | 98:8ab26030e058 | 142 | * @param buffer The buffer where received data will be stored |
Kojto | 102:da0ca467f8b5 | 143 | * @param length The buffer length in bytes |
Kojto | 98:8ab26030e058 | 144 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 145 | * @param event The logical OR of TX events |
Kojto | 98:8ab26030e058 | 146 | */ |
Kojto | 102:da0ca467f8b5 | 147 | int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
Kojto | 98:8ab26030e058 | 148 | |
Kojto | 98:8ab26030e058 | 149 | /** Abort the on-going write transfer |
Kojto | 98:8ab26030e058 | 150 | */ |
Kojto | 98:8ab26030e058 | 151 | void abort_write(); |
Kojto | 98:8ab26030e058 | 152 | |
Kojto | 98:8ab26030e058 | 153 | /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. |
Kojto | 98:8ab26030e058 | 154 | * |
Kojto | 98:8ab26030e058 | 155 | * @param buffer The buffer where received data will be stored |
Kojto | 102:da0ca467f8b5 | 156 | * @param length The buffer length in bytes |
Kojto | 98:8ab26030e058 | 157 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 158 | * @param event The logical OR of RX events |
Kojto | 98:8ab26030e058 | 159 | * @param char_match The matching character |
Kojto | 98:8ab26030e058 | 160 | */ |
Kojto | 98:8ab26030e058 | 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); |
Kojto | 98:8ab26030e058 | 162 | |
Kojto | 98:8ab26030e058 | 163 | /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. |
Kojto | 98:8ab26030e058 | 164 | * |
Kojto | 98:8ab26030e058 | 165 | * @param buffer The buffer where received data will be stored |
Kojto | 102:da0ca467f8b5 | 166 | * @param length The buffer length in bytes |
Kojto | 98:8ab26030e058 | 167 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 168 | * @param event The logical OR of RX events |
Kojto | 98:8ab26030e058 | 169 | * @param char_match The matching character |
Kojto | 98:8ab26030e058 | 170 | */ |
Kojto | 98:8ab26030e058 | 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); |
Kojto | 98:8ab26030e058 | 172 | |
Kojto | 98:8ab26030e058 | 173 | /** Abort the on-going read transfer |
Kojto | 98:8ab26030e058 | 174 | */ |
Kojto | 98:8ab26030e058 | 175 | void abort_read(); |
Kojto | 98:8ab26030e058 | 176 | |
Kojto | 98:8ab26030e058 | 177 | /** Configure DMA usage suggestion for non-blocking TX transfers |
Kojto | 98:8ab26030e058 | 178 | * |
Kojto | 98:8ab26030e058 | 179 | * @param usage The usage DMA hint for peripheral |
Kojto | 98:8ab26030e058 | 180 | * @return Zero if the usage was set, -1 if a transaction is on-going |
Kojto | 98:8ab26030e058 | 181 | */ |
Kojto | 98:8ab26030e058 | 182 | int set_dma_usage_tx(DMAUsage usage); |
Kojto | 98:8ab26030e058 | 183 | |
Kojto | 98:8ab26030e058 | 184 | /** Configure DMA usage suggestion for non-blocking RX transfers |
Kojto | 98:8ab26030e058 | 185 | * |
Kojto | 98:8ab26030e058 | 186 | * @param usage The usage DMA hint for peripheral |
Kojto | 98:8ab26030e058 | 187 | * @return Zero if the usage was set, -1 if a transaction is on-going |
Kojto | 98:8ab26030e058 | 188 | */ |
Kojto | 98:8ab26030e058 | 189 | int set_dma_usage_rx(DMAUsage usage); |
Kojto | 98:8ab26030e058 | 190 | |
Kojto | 98:8ab26030e058 | 191 | protected: |
Kojto | 98:8ab26030e058 | 192 | 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 | 193 | void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); |
Kojto | 98:8ab26030e058 | 194 | void interrupt_handler_asynch(void); |
Kojto | 98:8ab26030e058 | 195 | #endif |
Kojto | 98:8ab26030e058 | 196 | |
bogdanm | 68:f37f3b9c9f0b | 197 | protected: |
bogdanm | 68:f37f3b9c9f0b | 198 | SerialBase(PinName tx, PinName rx); |
bogdanm | 85:024bf7f99721 | 199 | virtual ~SerialBase() { |
bogdanm | 85:024bf7f99721 | 200 | } |
bogdanm | 85:024bf7f99721 | 201 | |
bogdanm | 68:f37f3b9c9f0b | 202 | int _base_getc(); |
bogdanm | 68:f37f3b9c9f0b | 203 | int _base_putc(int c); |
bogdanm | 68:f37f3b9c9f0b | 204 | |
Kojto | 98:8ab26030e058 | 205 | #if DEVICE_SERIAL_ASYNCH |
Kojto | 98:8ab26030e058 | 206 | CThunk<SerialBase> _thunk_irq; |
Kojto | 98:8ab26030e058 | 207 | event_callback_t _tx_callback; |
Kojto | 98:8ab26030e058 | 208 | event_callback_t _rx_callback; |
Kojto | 98:8ab26030e058 | 209 | DMAUsage _tx_usage; |
Kojto | 98:8ab26030e058 | 210 | DMAUsage _rx_usage; |
Kojto | 98:8ab26030e058 | 211 | #endif |
Kojto | 98:8ab26030e058 | 212 | |
bogdanm | 68:f37f3b9c9f0b | 213 | serial_t _serial; |
bogdanm | 68:f37f3b9c9f0b | 214 | FunctionPointer _irq[2]; |
bogdanm | 68:f37f3b9c9f0b | 215 | int _baud; |
Kojto | 98:8ab26030e058 | 216 | |
bogdanm | 68:f37f3b9c9f0b | 217 | }; |
bogdanm | 68:f37f3b9c9f0b | 218 | |
bogdanm | 68:f37f3b9c9f0b | 219 | } // namespace mbed |
bogdanm | 68:f37f3b9c9f0b | 220 | |
bogdanm | 68:f37f3b9c9f0b | 221 | #endif |
bogdanm | 68:f37f3b9c9f0b | 222 | |
bogdanm | 68:f37f3b9c9f0b | 223 | #endif |