test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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