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