The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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