DHT11

Committer:
jhon309
Date:
Thu Aug 13 00:21:57 2015 +0000
Revision:
0:c52df770855b
DHT11

Who changed what in which revision?

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