The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anna Bridge 169:a7c7b631e539 1 /*******************************************************************************
Anna Bridge 169:a7c7b631e539 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
Anna Bridge 169:a7c7b631e539 3 *
Anna Bridge 169:a7c7b631e539 4 * Permission is hereby granted, free of charge, to any person obtaining a
Anna Bridge 169:a7c7b631e539 5 * copy of this software and associated documentation files (the "Software"),
Anna Bridge 169:a7c7b631e539 6 * to deal in the Software without restriction, including without limitation
Anna Bridge 169:a7c7b631e539 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Anna Bridge 169:a7c7b631e539 8 * and/or sell copies of the Software, and to permit persons to whom the
Anna Bridge 169:a7c7b631e539 9 * Software is furnished to do so, subject to the following conditions:
Anna Bridge 169:a7c7b631e539 10 *
Anna Bridge 169:a7c7b631e539 11 * The above copyright notice and this permission notice shall be included
Anna Bridge 169:a7c7b631e539 12 * in all copies or substantial portions of the Software.
Anna Bridge 169:a7c7b631e539 13 *
Anna Bridge 169:a7c7b631e539 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Anna Bridge 169:a7c7b631e539 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Anna Bridge 169:a7c7b631e539 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Anna Bridge 169:a7c7b631e539 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
Anna Bridge 169:a7c7b631e539 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Anna Bridge 169:a7c7b631e539 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Anna Bridge 169:a7c7b631e539 20 * OTHER DEALINGS IN THE SOFTWARE.
Anna Bridge 169:a7c7b631e539 21 *
Anna Bridge 169:a7c7b631e539 22 * Except as contained in this notice, the name of Maxim Integrated
Anna Bridge 169:a7c7b631e539 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
Anna Bridge 169:a7c7b631e539 24 * Products, Inc. Branding Policy.
Anna Bridge 169:a7c7b631e539 25 *
Anna Bridge 169:a7c7b631e539 26 * The mere transfer of this software does not imply any licenses
Anna Bridge 169:a7c7b631e539 27 * of trade secrets, proprietary technology, copyrights, patents,
Anna Bridge 169:a7c7b631e539 28 * trademarks, maskwork rights, or any other form of intellectual
Anna Bridge 169:a7c7b631e539 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
Anna Bridge 169:a7c7b631e539 30 * ownership rights.
Anna Bridge 169:a7c7b631e539 31 *
Anna Bridge 169:a7c7b631e539 32 * $Date: 2016-06-21 16:16:33 -0500 (Tue, 21 Jun 2016) $
Anna Bridge 169:a7c7b631e539 33 * $Revision: 23447 $
Anna Bridge 169:a7c7b631e539 34 *
Anna Bridge 169:a7c7b631e539 35 ******************************************************************************/
Anna Bridge 169:a7c7b631e539 36
Anna Bridge 169:a7c7b631e539 37 /**
Anna Bridge 169:a7c7b631e539 38 * @file uart.h
Anna Bridge 169:a7c7b631e539 39 * @brief UART driver header file.
Anna Bridge 169:a7c7b631e539 40 */
Anna Bridge 169:a7c7b631e539 41
Anna Bridge 169:a7c7b631e539 42 #include "mxc_config.h"
Anna Bridge 169:a7c7b631e539 43 #include "mxc_sys.h"
Anna Bridge 169:a7c7b631e539 44 #include "uart_regs.h"
Anna Bridge 169:a7c7b631e539 45
Anna Bridge 169:a7c7b631e539 46 #ifndef _UART_H_
Anna Bridge 169:a7c7b631e539 47 #define _UART_H_
Anna Bridge 169:a7c7b631e539 48
Anna Bridge 169:a7c7b631e539 49 #ifdef __cplusplus
Anna Bridge 169:a7c7b631e539 50 extern "C" {
Anna Bridge 169:a7c7b631e539 51 #endif
Anna Bridge 169:a7c7b631e539 52
Anna Bridge 169:a7c7b631e539 53 /***** Definitions *****/
Anna Bridge 169:a7c7b631e539 54
Anna Bridge 169:a7c7b631e539 55 /// @brief Defines number of data bits per transmission/reception
Anna Bridge 169:a7c7b631e539 56 typedef enum {
Anna Bridge 169:a7c7b631e539 57 UART_DATA_SIZE_5_BITS = MXC_V_UART_CTRL_DATA_SIZE_5_BITS,
Anna Bridge 169:a7c7b631e539 58 UART_DATA_SIZE_6_BITS = MXC_V_UART_CTRL_DATA_SIZE_6_BITS,
Anna Bridge 169:a7c7b631e539 59 UART_DATA_SIZE_7_BITS = MXC_V_UART_CTRL_DATA_SIZE_7_BITS,
Anna Bridge 169:a7c7b631e539 60 UART_DATA_SIZE_8_BITS = MXC_V_UART_CTRL_DATA_SIZE_8_BITS
Anna Bridge 169:a7c7b631e539 61 }
Anna Bridge 169:a7c7b631e539 62 uart_data_size_t;
Anna Bridge 169:a7c7b631e539 63
Anna Bridge 169:a7c7b631e539 64 /// @brief Defines number of data bits per transmission/reception
Anna Bridge 169:a7c7b631e539 65 typedef enum {
Anna Bridge 169:a7c7b631e539 66 UART_PARITY_DISABLE = MXC_V_UART_CTRL_PARITY_DISABLE,
Anna Bridge 169:a7c7b631e539 67 UART_PARITY_ODD = MXC_V_UART_CTRL_PARITY_ODD,
Anna Bridge 169:a7c7b631e539 68 UART_PARITY_EVEN = MXC_V_UART_CTRL_PARITY_EVEN,
Anna Bridge 169:a7c7b631e539 69 UART_PARITY_MARK = MXC_V_UART_CTRL_PARITY_MARK
Anna Bridge 169:a7c7b631e539 70 } uart_parity_t;
Anna Bridge 169:a7c7b631e539 71
Anna Bridge 169:a7c7b631e539 72 /// @brief UART configuration type.
Anna Bridge 169:a7c7b631e539 73 typedef struct {
Anna Bridge 169:a7c7b631e539 74 uint8_t extra_stop; ///< 0 for one stop bit, 1 for two stop bits.
Anna Bridge 169:a7c7b631e539 75 uint8_t cts; ///< 1 to enable CTS.
Anna Bridge 169:a7c7b631e539 76 uint8_t rts; ///< 1 to enable RTS.
Anna Bridge 169:a7c7b631e539 77 uint32_t baud; ///< Baud rate in Hz.
Anna Bridge 169:a7c7b631e539 78 uart_data_size_t size; ///< Number of bits in each character.
Anna Bridge 169:a7c7b631e539 79 uart_parity_t parity; ///< Even or odd parity.
Anna Bridge 169:a7c7b631e539 80 } uart_cfg_t;
Anna Bridge 169:a7c7b631e539 81
Anna Bridge 169:a7c7b631e539 82 /**
Anna Bridge 169:a7c7b631e539 83 * @brief Type alias for UART request structure
Anna Bridge 169:a7c7b631e539 84 */
Anna Bridge 169:a7c7b631e539 85 typedef struct uart_req uart_req_t;
Anna Bridge 169:a7c7b631e539 86
Anna Bridge 169:a7c7b631e539 87 /**
Anna Bridge 169:a7c7b631e539 88 * @brief Type alias \c uart_async_callback for function signature: \code void callback)(uart_req_t* , int error_code) \endcode
Anna Bridge 169:a7c7b631e539 89 * @param uart_req_t* Pointer to the transaction request.
Anna Bridge 169:a7c7b631e539 90 * @param error_code Return code for the UART request. @see mxc_errors.h.
Anna Bridge 169:a7c7b631e539 91 */
Anna Bridge 169:a7c7b631e539 92 typedef void (*uart_async_callback)(uart_req_t*, int);
Anna Bridge 169:a7c7b631e539 93
Anna Bridge 169:a7c7b631e539 94 /// @brief UART Transaction request, must remain allocated until callback has completed.
Anna Bridge 169:a7c7b631e539 95 struct uart_req {
Anna Bridge 169:a7c7b631e539 96 uint8_t *data; ///< Data buffer for characters.
Anna Bridge 169:a7c7b631e539 97 unsigned len; ///< Length of characters in data to send or receive.
Anna Bridge 169:a7c7b631e539 98 unsigned num; ///< Number of characters actually sent or received.
Anna Bridge 169:a7c7b631e539 99 uart_async_callback callback; ///< A callback function pointer with alias signature \c uart_async_callback
Anna Bridge 169:a7c7b631e539 100 };
Anna Bridge 169:a7c7b631e539 101
Anna Bridge 169:a7c7b631e539 102
Anna Bridge 169:a7c7b631e539 103 /***** Globals *****/
Anna Bridge 169:a7c7b631e539 104
Anna Bridge 169:a7c7b631e539 105 /***** Function Prototypes *****/
Anna Bridge 169:a7c7b631e539 106
Anna Bridge 169:a7c7b631e539 107 /**
Anna Bridge 169:a7c7b631e539 108 * @brief Initialize and enable UART module.
Anna Bridge 169:a7c7b631e539 109 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 110 * @param cfg Pointer to UART configuration.
Anna Bridge 169:a7c7b631e539 111 * @param sys_cfg Pointer to system configuration object
Anna Bridge 169:a7c7b631e539 112 * @returns #E_NO_ERROR if everything is successful
Anna Bridge 169:a7c7b631e539 113 */
Anna Bridge 169:a7c7b631e539 114 int UART_Init(mxc_uart_regs_t *uart, const uart_cfg_t *cfg, const sys_cfg_uart_t *sys_cfg);
Anna Bridge 169:a7c7b631e539 115
Anna Bridge 169:a7c7b631e539 116 /**
Anna Bridge 169:a7c7b631e539 117 * @brief Shutdown UART module.
Anna Bridge 169:a7c7b631e539 118 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 119 * @returns #E_NO_ERROR if everything is successful
Anna Bridge 169:a7c7b631e539 120 */
Anna Bridge 169:a7c7b631e539 121 int UART_Shutdown(mxc_uart_regs_t *uart);
Anna Bridge 169:a7c7b631e539 122
Anna Bridge 169:a7c7b631e539 123 /**
Anna Bridge 169:a7c7b631e539 124 * @brief Write UART data. Will block until transaction is complete.
Anna Bridge 169:a7c7b631e539 125 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 126 * @param data Pointer to buffer for write data.
Anna Bridge 169:a7c7b631e539 127 * @param len Number of bytes to write.
Anna Bridge 169:a7c7b631e539 128 * @note Will return once data has been put into FIFO, not necessarily transmitted.
Anna Bridge 169:a7c7b631e539 129 * @returns Number of bytes written if successful, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 130 */
Anna Bridge 169:a7c7b631e539 131 int UART_Write(mxc_uart_regs_t *uart, uint8_t* data, int len);
Anna Bridge 169:a7c7b631e539 132
Anna Bridge 169:a7c7b631e539 133 /**
Anna Bridge 169:a7c7b631e539 134 * @brief Read UART data. Will block until transaction is complete.
Anna Bridge 169:a7c7b631e539 135 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 136 * @param data Pointer to buffer for read data.
Anna Bridge 169:a7c7b631e539 137 * @param len Number of bytes to read.
Anna Bridge 169:a7c7b631e539 138 * @param num Optional pointer to number of bytes actually read.
Anna Bridge 169:a7c7b631e539 139 * Pass NULL if undesired.
Anna Bridge 169:a7c7b631e539 140 * @returns Number of bytes read is successful, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 141 */
Anna Bridge 169:a7c7b631e539 142 int UART_Read(mxc_uart_regs_t *uart, uint8_t* data, int len, int *num);
Anna Bridge 169:a7c7b631e539 143
Anna Bridge 169:a7c7b631e539 144 /**
Anna Bridge 169:a7c7b631e539 145 * @brief Asynchronously Write UART data.
Anna Bridge 169:a7c7b631e539 146 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 147 * @param req Request for a UART transaction.
Anna Bridge 169:a7c7b631e539 148 * @note Request struct must remain allocated until callback.
Anna Bridge 169:a7c7b631e539 149 * @returns #E_NO_ERROR if everything is successful, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 150 */
Anna Bridge 169:a7c7b631e539 151 int UART_WriteAsync(mxc_uart_regs_t *uart, uart_req_t *req);
Anna Bridge 169:a7c7b631e539 152
Anna Bridge 169:a7c7b631e539 153 /**
Anna Bridge 169:a7c7b631e539 154 * @brief Asynchronously Read UART data.
Anna Bridge 169:a7c7b631e539 155 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 156 * @param req Pointer to request for a UART transaction.
Anna Bridge 169:a7c7b631e539 157 * @note Request struct must remain allocated until callback.
Anna Bridge 169:a7c7b631e539 158 * @returns #E_NO_ERROR if everything is successful, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 159 */
Anna Bridge 169:a7c7b631e539 160 int UART_ReadAsync(mxc_uart_regs_t *uart, uart_req_t *req);
Anna Bridge 169:a7c7b631e539 161
Anna Bridge 169:a7c7b631e539 162 /**
Anna Bridge 169:a7c7b631e539 163 * @brief Abort asynchronous request.
Anna Bridge 169:a7c7b631e539 164 * @param req Pointer to request for a UART transaction.
Anna Bridge 169:a7c7b631e539 165 * @returns #E_NO_ERROR if request aborted, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 166 */
Anna Bridge 169:a7c7b631e539 167 int UART_AbortAsync(uart_req_t *req);
Anna Bridge 169:a7c7b631e539 168
Anna Bridge 169:a7c7b631e539 169 /**
Anna Bridge 169:a7c7b631e539 170 * @brief UART interrupt handler.
Anna Bridge 169:a7c7b631e539 171 * @details This function should be called by the application from the interrupt
Anna Bridge 169:a7c7b631e539 172 * handler if UART interrupts are enabled. Alternately, this function
Anna Bridge 169:a7c7b631e539 173 * can be periodically called by the application if UART interrupts are
Anna Bridge 169:a7c7b631e539 174 * disabled. Only necessary to call this when using asynchronous functions.
Anna Bridge 169:a7c7b631e539 175 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 176 */
Anna Bridge 169:a7c7b631e539 177 void UART_Handler(mxc_uart_regs_t *uart);
Anna Bridge 169:a7c7b631e539 178
Anna Bridge 169:a7c7b631e539 179 /**
Anna Bridge 169:a7c7b631e539 180 * @brief Check to see if the UART is busy.
Anna Bridge 169:a7c7b631e539 181 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 182 * @returns #E_NO_ERROR if idle, #E_BUSY if in use.
Anna Bridge 169:a7c7b631e539 183 */
Anna Bridge 169:a7c7b631e539 184 int UART_Busy(mxc_uart_regs_t *uart);
Anna Bridge 169:a7c7b631e539 185
Anna Bridge 169:a7c7b631e539 186 /**
Anna Bridge 169:a7c7b631e539 187 * @brief Attempt to prepare the UART for sleep.
Anna Bridge 169:a7c7b631e539 188 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 189 * @details Checks for any ongoing transactions. Disables interrupts if the I2CM
Anna Bridge 169:a7c7b631e539 190 is idle.
Anna Bridge 169:a7c7b631e539 191 * @returns #E_NO_ERROR if ready to sleep, #E_BUSY if not ready for sleep.
Anna Bridge 169:a7c7b631e539 192 */
Anna Bridge 169:a7c7b631e539 193 int UART_PrepForSleep(mxc_uart_regs_t *uart);
Anna Bridge 169:a7c7b631e539 194
Anna Bridge 169:a7c7b631e539 195 /**
Anna Bridge 169:a7c7b631e539 196 * @brief Enables the UART without overwriting existing configuration.
Anna Bridge 169:a7c7b631e539 197 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 198 */
Anna Bridge 169:a7c7b631e539 199 __STATIC_INLINE void UART_Enable(mxc_uart_regs_t *uart)
Anna Bridge 169:a7c7b631e539 200 {
Anna Bridge 169:a7c7b631e539 201 uart->ctrl |= (MXC_F_UART_CTRL_UART_EN | MXC_F_UART_CTRL_TX_FIFO_EN |
Anna Bridge 169:a7c7b631e539 202 MXC_F_UART_CTRL_RX_FIFO_EN);
Anna Bridge 169:a7c7b631e539 203 }
Anna Bridge 169:a7c7b631e539 204
Anna Bridge 169:a7c7b631e539 205 /**
Anna Bridge 169:a7c7b631e539 206 * @brief Drain all of the data in the RXFIFO.
Anna Bridge 169:a7c7b631e539 207 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 208 */
Anna Bridge 169:a7c7b631e539 209 __STATIC_INLINE void UART_DrainRX(mxc_uart_regs_t *uart)
Anna Bridge 169:a7c7b631e539 210 {
Anna Bridge 169:a7c7b631e539 211 uint32_t ctrl_save = uart->ctrl;
Anna Bridge 169:a7c7b631e539 212 uart->ctrl = (ctrl_save & ~MXC_F_UART_CTRL_RX_FIFO_EN);
Anna Bridge 169:a7c7b631e539 213 uart->ctrl = ctrl_save;
Anna Bridge 169:a7c7b631e539 214 }
Anna Bridge 169:a7c7b631e539 215
Anna Bridge 169:a7c7b631e539 216 /**
Anna Bridge 169:a7c7b631e539 217 * @brief Drain all of the data in the TXFIFO.
Anna Bridge 169:a7c7b631e539 218 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 219 */
Anna Bridge 169:a7c7b631e539 220 __STATIC_INLINE void UART_DrainTX(mxc_uart_regs_t *uart)
Anna Bridge 169:a7c7b631e539 221 {
Anna Bridge 169:a7c7b631e539 222 uint32_t ctrl_save = uart->ctrl;
Anna Bridge 169:a7c7b631e539 223 uart->ctrl = (ctrl_save & ~MXC_F_UART_CTRL_TX_FIFO_EN);
Anna Bridge 169:a7c7b631e539 224 uart->ctrl = ctrl_save;
Anna Bridge 169:a7c7b631e539 225 }
Anna Bridge 169:a7c7b631e539 226
Anna Bridge 169:a7c7b631e539 227 /**
Anna Bridge 169:a7c7b631e539 228 * @brief Write FIFO availability.
Anna Bridge 169:a7c7b631e539 229 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 230 * @returns Number of empty bytes available in write FIFO.
Anna Bridge 169:a7c7b631e539 231 */
Anna Bridge 169:a7c7b631e539 232 __STATIC_INLINE unsigned UART_NumWriteAvail(mxc_uart_regs_t *uart)
Anna Bridge 169:a7c7b631e539 233 {
Anna Bridge 169:a7c7b631e539 234 return (MXC_UART_FIFO_DEPTH - (uart->tx_fifo_ctrl & MXC_F_UART_TX_FIFO_CTRL_FIFO_ENTRY));
Anna Bridge 169:a7c7b631e539 235 }
Anna Bridge 169:a7c7b631e539 236
Anna Bridge 169:a7c7b631e539 237 /**
Anna Bridge 169:a7c7b631e539 238 * @brief Read FIFO availability.
Anna Bridge 169:a7c7b631e539 239 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 240 * @returns Number of bytes in read FIFO.
Anna Bridge 169:a7c7b631e539 241 */
Anna Bridge 169:a7c7b631e539 242 __STATIC_INLINE unsigned UART_NumReadAvail(mxc_uart_regs_t *uart)
Anna Bridge 169:a7c7b631e539 243 {
Anna Bridge 169:a7c7b631e539 244 return (uart->rx_fifo_ctrl & MXC_F_UART_RX_FIFO_CTRL_FIFO_ENTRY);
Anna Bridge 169:a7c7b631e539 245 }
Anna Bridge 169:a7c7b631e539 246
Anna Bridge 169:a7c7b631e539 247 /**
Anna Bridge 169:a7c7b631e539 248 * @brief Clear interrupt flags.
Anna Bridge 169:a7c7b631e539 249 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 250 * @param mask Mask of interrupts to clear.
Anna Bridge 169:a7c7b631e539 251 */
Anna Bridge 169:a7c7b631e539 252 __STATIC_INLINE void UART_ClearFlags(mxc_uart_regs_t *uart, uint32_t mask)
Anna Bridge 169:a7c7b631e539 253 {
Anna Bridge 169:a7c7b631e539 254 uart->intfl = mask;
Anna Bridge 169:a7c7b631e539 255 }
Anna Bridge 169:a7c7b631e539 256
Anna Bridge 169:a7c7b631e539 257 /**
Anna Bridge 169:a7c7b631e539 258 * @brief Get interrupt flags.
Anna Bridge 169:a7c7b631e539 259 * @param uart Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 260 * @returns Mask of active flags.
Anna Bridge 169:a7c7b631e539 261 */
Anna Bridge 169:a7c7b631e539 262 __STATIC_INLINE unsigned UART_GetFlags(mxc_uart_regs_t *uart)
Anna Bridge 169:a7c7b631e539 263 {
Anna Bridge 169:a7c7b631e539 264 return (uart->intfl);
Anna Bridge 169:a7c7b631e539 265 }
Anna Bridge 169:a7c7b631e539 266
Anna Bridge 169:a7c7b631e539 267 #ifdef __cplusplus
Anna Bridge 169:a7c7b631e539 268 }
Anna Bridge 169:a7c7b631e539 269 #endif
Anna Bridge 169:a7c7b631e539 270
Anna Bridge 169:a7c7b631e539 271 #endif /* _UART_H_ */