Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
ethaderu
Date:
Tue Mar 05 02:34:44 2019 +0000
Revision:
3:78f223d34f36
Publish 1

Who changed what in which revision?

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