MCU driver/HAL for the Picocell Gateway concentrator board. The firmware implements either a USB CDC protocol or a UART protocol to bridge commands coming from host to the SX1308 SPI interface.

Committer:
dgabino
Date:
Wed Apr 11 14:42:47 2018 +0000
Revision:
0:c76361bd82e8
Initial commit

Who changed what in which revision?

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