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_SERIAL_API_H
jhon309 0:c52df770855b 17 #define MBED_SERIAL_API_H
jhon309 0:c52df770855b 18
jhon309 0:c52df770855b 19 #include "device.h"
jhon309 0:c52df770855b 20 #include "buffer.h"
jhon309 0:c52df770855b 21 #include "dma_api.h"
jhon309 0:c52df770855b 22
jhon309 0:c52df770855b 23 #if DEVICE_SERIAL
jhon309 0:c52df770855b 24
jhon309 0:c52df770855b 25 #define SERIAL_EVENT_TX_SHIFT (2)
jhon309 0:c52df770855b 26 #define SERIAL_EVENT_RX_SHIFT (8)
jhon309 0:c52df770855b 27
jhon309 0:c52df770855b 28 #define SERIAL_EVENT_TX_MASK (0x00FC)
jhon309 0:c52df770855b 29 #define SERIAL_EVENT_RX_MASK (0x3F00)
jhon309 0:c52df770855b 30
jhon309 0:c52df770855b 31 #define SERIAL_EVENT_ERROR (1 << 1)
jhon309 0:c52df770855b 32
jhon309 0:c52df770855b 33 /**
jhon309 0:c52df770855b 34 * @defgroup SerialTXEvents Serial TX Events Macros
jhon309 0:c52df770855b 35 *
jhon309 0:c52df770855b 36 * @{
jhon309 0:c52df770855b 37 */
jhon309 0:c52df770855b 38 #define SERIAL_EVENT_TX_COMPLETE (1 << (SERIAL_EVENT_TX_SHIFT + 0))
jhon309 0:c52df770855b 39 #define SERIAL_EVENT_TX_ALL (SERIAL_EVENT_TX_COMPLETE)
jhon309 0:c52df770855b 40 /**@}*/
jhon309 0:c52df770855b 41
jhon309 0:c52df770855b 42 /**
jhon309 0:c52df770855b 43 * @defgroup SerialRXEvents Serial RX Events Macros
jhon309 0:c52df770855b 44 *
jhon309 0:c52df770855b 45 * @{
jhon309 0:c52df770855b 46 */
jhon309 0:c52df770855b 47 #define SERIAL_EVENT_RX_COMPLETE (1 << (SERIAL_EVENT_RX_SHIFT + 0))
jhon309 0:c52df770855b 48 #define SERIAL_EVENT_RX_OVERRUN_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 1))
jhon309 0:c52df770855b 49 #define SERIAL_EVENT_RX_FRAMING_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 2))
jhon309 0:c52df770855b 50 #define SERIAL_EVENT_RX_PARITY_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 3))
jhon309 0:c52df770855b 51 #define SERIAL_EVENT_RX_OVERFLOW (1 << (SERIAL_EVENT_RX_SHIFT + 4))
jhon309 0:c52df770855b 52 #define SERIAL_EVENT_RX_CHARACTER_MATCH (1 << (SERIAL_EVENT_RX_SHIFT + 5))
jhon309 0:c52df770855b 53 #define SERIAL_EVENT_RX_ALL (SERIAL_EVENT_RX_OVERFLOW | SERIAL_EVENT_RX_PARITY_ERROR | \
jhon309 0:c52df770855b 54 SERIAL_EVENT_RX_FRAMING_ERROR | SERIAL_EVENT_RX_OVERRUN_ERROR | \
jhon309 0:c52df770855b 55 SERIAL_EVENT_RX_COMPLETE | SERIAL_EVENT_RX_CHARACTER_MATCH)
jhon309 0:c52df770855b 56 /**@}*/
jhon309 0:c52df770855b 57
jhon309 0:c52df770855b 58 #define SERIAL_RESERVED_CHAR_MATCH (255)
jhon309 0:c52df770855b 59
jhon309 0:c52df770855b 60 typedef enum {
jhon309 0:c52df770855b 61 ParityNone = 0,
jhon309 0:c52df770855b 62 ParityOdd = 1,
jhon309 0:c52df770855b 63 ParityEven = 2,
jhon309 0:c52df770855b 64 ParityForced1 = 3,
jhon309 0:c52df770855b 65 ParityForced0 = 4
jhon309 0:c52df770855b 66 } SerialParity;
jhon309 0:c52df770855b 67
jhon309 0:c52df770855b 68 typedef enum {
jhon309 0:c52df770855b 69 RxIrq,
jhon309 0:c52df770855b 70 TxIrq
jhon309 0:c52df770855b 71 } SerialIrq;
jhon309 0:c52df770855b 72
jhon309 0:c52df770855b 73 typedef enum {
jhon309 0:c52df770855b 74 FlowControlNone,
jhon309 0:c52df770855b 75 FlowControlRTS,
jhon309 0:c52df770855b 76 FlowControlCTS,
jhon309 0:c52df770855b 77 FlowControlRTSCTS
jhon309 0:c52df770855b 78 } FlowControl;
jhon309 0:c52df770855b 79
jhon309 0:c52df770855b 80 typedef void (*uart_irq_handler)(uint32_t id, SerialIrq event);
jhon309 0:c52df770855b 81
jhon309 0:c52df770855b 82 #if DEVICE_SERIAL_ASYNCH
jhon309 0:c52df770855b 83 /** Asynch serial hal structure
jhon309 0:c52df770855b 84 */
jhon309 0:c52df770855b 85 typedef struct {
jhon309 0:c52df770855b 86 struct serial_s serial; /**< Target specific serial structure */
jhon309 0:c52df770855b 87 struct buffer_s tx_buff; /**< Tx buffer */
jhon309 0:c52df770855b 88 struct buffer_s rx_buff; /**< Rx buffer */
jhon309 0:c52df770855b 89 uint8_t char_match; /**< Character to be matched */
jhon309 0:c52df770855b 90 uint8_t char_found; /**< State of the matched character */
jhon309 0:c52df770855b 91 } serial_t;
jhon309 0:c52df770855b 92
jhon309 0:c52df770855b 93 #else
jhon309 0:c52df770855b 94 /** Non-asynch serial hal structure
jhon309 0:c52df770855b 95 */
jhon309 0:c52df770855b 96 typedef struct serial_s serial_t;
jhon309 0:c52df770855b 97
jhon309 0:c52df770855b 98 #endif
jhon309 0:c52df770855b 99
jhon309 0:c52df770855b 100 #ifdef __cplusplus
jhon309 0:c52df770855b 101 extern "C" {
jhon309 0:c52df770855b 102 #endif
jhon309 0:c52df770855b 103
jhon309 0:c52df770855b 104 /**
jhon309 0:c52df770855b 105 * \defgroup GeneralSerial Serial Configuration Functions
jhon309 0:c52df770855b 106 * @{
jhon309 0:c52df770855b 107 */
jhon309 0:c52df770855b 108
jhon309 0:c52df770855b 109 /** Initialize the serial peripheral. It sets the default parameters for serial
jhon309 0:c52df770855b 110 * peripheral, and configure its specifieds pins.
jhon309 0:c52df770855b 111 *
jhon309 0:c52df770855b 112 * @param obj The serial object
jhon309 0:c52df770855b 113 * @param tx The TX pin
jhon309 0:c52df770855b 114 * @param rx The RX pin
jhon309 0:c52df770855b 115 */
jhon309 0:c52df770855b 116 void serial_init(serial_t *obj, PinName tx, PinName rx);
jhon309 0:c52df770855b 117
jhon309 0:c52df770855b 118 /** Release the serial peripheral, not currently invoked. It requires further
jhon309 0:c52df770855b 119 * resource management.
jhon309 0:c52df770855b 120 *
jhon309 0:c52df770855b 121 * @param obj The serial object
jhon309 0:c52df770855b 122 */
jhon309 0:c52df770855b 123 void serial_free(serial_t *obj);
jhon309 0:c52df770855b 124
jhon309 0:c52df770855b 125 /** Configure the baud rate
jhon309 0:c52df770855b 126 *
jhon309 0:c52df770855b 127 * @param obj The serial object
jhon309 0:c52df770855b 128 * @param baudrate The baud rate to be configured
jhon309 0:c52df770855b 129 */
jhon309 0:c52df770855b 130 void serial_baud(serial_t *obj, int baudrate);
jhon309 0:c52df770855b 131
jhon309 0:c52df770855b 132 /** Configure the format. Set the number of bits, parity and the number of stop bits
jhon309 0:c52df770855b 133 *
jhon309 0:c52df770855b 134 * @param obj The serial object
jhon309 0:c52df770855b 135 * @param data_bits The number of data bits
jhon309 0:c52df770855b 136 * @param parity The parity
jhon309 0:c52df770855b 137 * @param stop_bits The number of stop bits
jhon309 0:c52df770855b 138 */
jhon309 0:c52df770855b 139 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits);
jhon309 0:c52df770855b 140
jhon309 0:c52df770855b 141 /** The serial interrupt handler registration.
jhon309 0:c52df770855b 142 *
jhon309 0:c52df770855b 143 * @param obj The serial object
jhon309 0:c52df770855b 144 * @param handler The interrupt handler which will be invoked when interrupt fires.
jhon309 0:c52df770855b 145 * @param id The SerialBase object
jhon309 0:c52df770855b 146 */
jhon309 0:c52df770855b 147 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id);
jhon309 0:c52df770855b 148
jhon309 0:c52df770855b 149 /** Configure serial interrupt. This function is used for word-approach
jhon309 0:c52df770855b 150 *
jhon309 0:c52df770855b 151 * @param obj The serial object
jhon309 0:c52df770855b 152 * @param irq The serial IRQ type (RX or TX)
jhon309 0:c52df770855b 153 * @param enable Set to non-zero to enable events, or zero to disable them
jhon309 0:c52df770855b 154 */
jhon309 0:c52df770855b 155 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable);
jhon309 0:c52df770855b 156
jhon309 0:c52df770855b 157 /** Get character. This is a blocking call, waiting for a character
jhon309 0:c52df770855b 158 *
jhon309 0:c52df770855b 159 * @param obj The serial object
jhon309 0:c52df770855b 160 */
jhon309 0:c52df770855b 161 int serial_getc(serial_t *obj);
jhon309 0:c52df770855b 162
jhon309 0:c52df770855b 163 /** Put a character. This is a blocking call, waiting for a peripheral to be available
jhon309 0:c52df770855b 164 * for writing
jhon309 0:c52df770855b 165 *
jhon309 0:c52df770855b 166 * @param obj The serial object
jhon309 0:c52df770855b 167 * @param c The character to be sent
jhon309 0:c52df770855b 168 */
jhon309 0:c52df770855b 169 void serial_putc(serial_t *obj, int c);
jhon309 0:c52df770855b 170
jhon309 0:c52df770855b 171 /** Check if the serial peripheral is readable
jhon309 0:c52df770855b 172 *
jhon309 0:c52df770855b 173 * @param obj The serial object
jhon309 0:c52df770855b 174 * @return Non-zero value if a character can be read, 0 if nothing to read.
jhon309 0:c52df770855b 175 */
jhon309 0:c52df770855b 176 int serial_readable(serial_t *obj);
jhon309 0:c52df770855b 177
jhon309 0:c52df770855b 178 /** Check if the serial peripheral is writable
jhon309 0:c52df770855b 179 *
jhon309 0:c52df770855b 180 * @param obj The serial object
jhon309 0:c52df770855b 181 * @return Non-zero value if a character can be written, 0 otherwise.
jhon309 0:c52df770855b 182 */
jhon309 0:c52df770855b 183 int serial_writable(serial_t *obj);
jhon309 0:c52df770855b 184
jhon309 0:c52df770855b 185 /** Clear the serial peripheral
jhon309 0:c52df770855b 186 *
jhon309 0:c52df770855b 187 * @param obj The serial object
jhon309 0:c52df770855b 188 */
jhon309 0:c52df770855b 189 void serial_clear(serial_t *obj);
jhon309 0:c52df770855b 190
jhon309 0:c52df770855b 191 /** Set the break
jhon309 0:c52df770855b 192 *
jhon309 0:c52df770855b 193 * @param obj The serial object
jhon309 0:c52df770855b 194 */
jhon309 0:c52df770855b 195 void serial_break_set(serial_t *obj);
jhon309 0:c52df770855b 196
jhon309 0:c52df770855b 197 /** Clear the break
jhon309 0:c52df770855b 198 *
jhon309 0:c52df770855b 199 * @param obj The serial object
jhon309 0:c52df770855b 200 */
jhon309 0:c52df770855b 201 void serial_break_clear(serial_t *obj);
jhon309 0:c52df770855b 202
jhon309 0:c52df770855b 203 /** Configure the TX pin for UART function.
jhon309 0:c52df770855b 204 *
jhon309 0:c52df770855b 205 * @param tx The pin used for TX
jhon309 0:c52df770855b 206 */
jhon309 0:c52df770855b 207 void serial_pinout_tx(PinName tx);
jhon309 0:c52df770855b 208
jhon309 0:c52df770855b 209 /** Configure the serial for the flow control. It sets flow control in the hardware
jhon309 0:c52df770855b 210 * if a serial peripheral supports it, otherwise software emulation is used.
jhon309 0:c52df770855b 211 *
jhon309 0:c52df770855b 212 * @param obj The serial object
jhon309 0:c52df770855b 213 * @param type The type of the flow control. Look at the available FlowControl types.
jhon309 0:c52df770855b 214 * @param rxflow The tx pin
jhon309 0:c52df770855b 215 * @param txflow The rx pin
jhon309 0:c52df770855b 216 */
jhon309 0:c52df770855b 217 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow);
jhon309 0:c52df770855b 218
jhon309 0:c52df770855b 219 #if DEVICE_SERIAL_ASYNCH
jhon309 0:c52df770855b 220
jhon309 0:c52df770855b 221 /**@}*/
jhon309 0:c52df770855b 222
jhon309 0:c52df770855b 223 /**
jhon309 0:c52df770855b 224 * \defgroup AsynchSerial Asynchronous Serial Hardware Abstraction Layer
jhon309 0:c52df770855b 225 * @{
jhon309 0:c52df770855b 226 */
jhon309 0:c52df770855b 227
jhon309 0:c52df770855b 228 /** Begin asynchronous TX transfer. The used buffer is specified in the serial object,
jhon309 0:c52df770855b 229 * tx_buff
jhon309 0:c52df770855b 230 *
jhon309 0:c52df770855b 231 * @param obj The serial object
jhon309 0:c52df770855b 232 * @param tx The buffer for sending
jhon309 0:c52df770855b 233 * @param tx_length The number of words to transmit
jhon309 0:c52df770855b 234 * @param tx_width The bit width of buffer word
jhon309 0:c52df770855b 235 * @param handler The serial handler
jhon309 0:c52df770855b 236 * @param event The logical OR of events to be registered
jhon309 0:c52df770855b 237 * @param hint A suggestion for how to use DMA with this transfer
jhon309 0:c52df770855b 238 * @return Returns number of data transfered, or 0 otherwise
jhon309 0:c52df770855b 239 */
jhon309 0:c52df770855b 240 int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint);
jhon309 0:c52df770855b 241
jhon309 0:c52df770855b 242 /** Begin asynchronous RX transfer (enable interrupt for data collecting)
jhon309 0:c52df770855b 243 * The used buffer is specified in the serial object - rx_buff
jhon309 0:c52df770855b 244 *
jhon309 0:c52df770855b 245 * @param obj The serial object
jhon309 0:c52df770855b 246 * @param rx The buffer for sending
jhon309 0:c52df770855b 247 * @param rx_length The number of words to transmit
jhon309 0:c52df770855b 248 * @param rx_width The bit width of buffer word
jhon309 0:c52df770855b 249 * @param handler The serial handler
jhon309 0:c52df770855b 250 * @param event The logical OR of events to be registered
jhon309 0:c52df770855b 251 * @param handler The serial handler
jhon309 0:c52df770855b 252 * @param char_match A character in range 0-254 to be matched
jhon309 0:c52df770855b 253 * @param hint A suggestion for how to use DMA with this transfer
jhon309 0:c52df770855b 254 */
jhon309 0:c52df770855b 255 void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint);
jhon309 0:c52df770855b 256
jhon309 0:c52df770855b 257 /** Attempts to determine if the serial peripheral is already in use for TX
jhon309 0:c52df770855b 258 *
jhon309 0:c52df770855b 259 * @param obj The serial object
jhon309 0:c52df770855b 260 * @return Non-zero if the RX transaction is ongoing, 0 otherwise
jhon309 0:c52df770855b 261 */
jhon309 0:c52df770855b 262 uint8_t serial_tx_active(serial_t *obj);
jhon309 0:c52df770855b 263
jhon309 0:c52df770855b 264 /** Attempts to determine if the serial peripheral is already in use for RX
jhon309 0:c52df770855b 265 *
jhon309 0:c52df770855b 266 * @param obj The serial object
jhon309 0:c52df770855b 267 * @return Non-zero if the RX transaction is ongoing, 0 otherwise
jhon309 0:c52df770855b 268 */
jhon309 0:c52df770855b 269 uint8_t serial_rx_active(serial_t *obj);
jhon309 0:c52df770855b 270
jhon309 0:c52df770855b 271 /** The asynchronous TX and RX handler.
jhon309 0:c52df770855b 272 *
jhon309 0:c52df770855b 273 * @param obj The serial object
jhon309 0:c52df770855b 274 * @return Returns event flags if a RX transfer termination condition was met or 0 otherwise
jhon309 0:c52df770855b 275 */
jhon309 0:c52df770855b 276 int serial_irq_handler_asynch(serial_t *obj);
jhon309 0:c52df770855b 277
jhon309 0:c52df770855b 278 /** Abort the ongoing TX transaction. It disables the enabled interupt for TX and
jhon309 0:c52df770855b 279 * flush TX hardware buffer if TX FIFO is used
jhon309 0:c52df770855b 280 *
jhon309 0:c52df770855b 281 * @param obj The serial object
jhon309 0:c52df770855b 282 */
jhon309 0:c52df770855b 283 void serial_tx_abort_asynch(serial_t *obj);
jhon309 0:c52df770855b 284
jhon309 0:c52df770855b 285 /** Abort the ongoing RX transaction It disables the enabled interrupt for RX and
jhon309 0:c52df770855b 286 * flush RX hardware buffer if RX FIFO is used
jhon309 0:c52df770855b 287 *
jhon309 0:c52df770855b 288 * @param obj The serial object
jhon309 0:c52df770855b 289 */
jhon309 0:c52df770855b 290 void serial_rx_abort_asynch(serial_t *obj);
jhon309 0:c52df770855b 291
jhon309 0:c52df770855b 292 /**@}*/
jhon309 0:c52df770855b 293
jhon309 0:c52df770855b 294 #endif
jhon309 0:c52df770855b 295
jhon309 0:c52df770855b 296 #ifdef __cplusplus
jhon309 0:c52df770855b 297 }
jhon309 0:c52df770855b 298 #endif
jhon309 0:c52df770855b 299
jhon309 0:c52df770855b 300 #endif
jhon309 0:c52df770855b 301
jhon309 0:c52df770855b 302 #endif