test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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