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