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