Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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