1 /** 2 * @file 3 * @brief This files defines the driver API including definitions, data types 4 * and function prototypes. 5 */ 6 7 /* **************************************************************************** 8 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 9 * 10 * Permission is hereby granted, free of charge, to any person obtaining a 11 * copy of this software and associated documentation files (the "Software"), 12 * to deal in the Software without restriction, including without limitation 13 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 * and/or sell copies of the Software, and to permit persons to whom the 15 * Software is furnished to do so, subject to the following conditions: 16 * 17 * The above copyright notice and this permission notice shall be included 18 * in all copies or substantial portions of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 23 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 24 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 * OTHER DEALINGS IN THE SOFTWARE. 27 * 28 * Except as contained in this notice, the name of Maxim Integrated 29 * Products, Inc. shall not be used except as stated in the Maxim Integrated 30 * Products, Inc. Branding Policy. 31 * 32 * The mere transfer of this software does not imply any licenses 33 * of trade secrets, proprietary technology, copyrights, patents, 34 * trademarks, maskwork rights, or any other form of intellectual 35 * property whatsoever. Maxim Integrated Products, Inc. retains all 36 * ownership rights. 37 * 38 * $Date: 2019-10-07 11:05:30 -0500 (Mon, 07 Oct 2019) $ 39 * $Revision: 47429 $ 40 * 41 *************************************************************************** */ 42 43 44 #ifndef _UART_H_ 45 #define _UART_H_ 46 47 /***** Includes *****/ 48 #include <stdint.h> 49 #include "uart_regs.h" 50 #include "mxc_sys.h" 51 52 /***** Definitions *****/ 53 54 /** 55 * @brief Alternate clock rate. (7.3728MHz) */ 56 #define UART_ALTERNATE_CLOCK_HZ 7372800 57 /** 58 * @defgroup uart UART 59 * @ingroup periphlibs 60 * @{ 61 */ 62 /** 63 * @brief Parity settings type */ 64 typedef enum { 65 UART_PARITY_DISABLE = 0, /**< Parity disabled */ 66 UART_PARITY_EVEN_0 = (MXC_F_UART_CTRL_PARITY_EN | 67 MXC_S_UART_CTRL_PARITY_EVEN | 68 MXC_F_UART_CTRL_PARMD), /**< Use for even parity 0 */ 69 UART_PARITY_EVEN_1 = (MXC_F_UART_CTRL_PARITY_EN | 70 MXC_S_UART_CTRL_PARITY_EVEN), /**< Use for even parity 1 */ 71 UART_PARITY_EVEN = UART_PARITY_EVEN_1, /**< Conventional even parity */ 72 UART_PARITY_ODD_0 = (MXC_F_UART_CTRL_PARITY_EN | 73 MXC_S_UART_CTRL_PARITY_ODD | 74 MXC_F_UART_CTRL_PARMD), /**< Use for odd parity 0 */ 75 UART_PARITY_ODD_1 = (MXC_F_UART_CTRL_PARITY_EN | 76 MXC_S_UART_CTRL_PARITY_ODD), /**< Use for odd parity 1 */ 77 UART_PARITY_ODD = UART_PARITY_ODD_1, /**< Conventional odd parity */ 78 UART_PARITY_MARK_0 = (MXC_F_UART_CTRL_PARITY_EN | 79 MXC_S_UART_CTRL_PARITY_MARK | 80 MXC_F_UART_CTRL_PARMD), /**< Use for mark parity 0 */ 81 UART_PARITY_MARK_1 = (MXC_F_UART_CTRL_PARITY_EN | 82 MXC_S_UART_CTRL_PARITY_MARK), /**< Use for mark parity 1 */ 83 UART_PARITY_MARK = UART_PARITY_MARK_1, /**< Conventional mark parity */ 84 UART_PARITY_SPACE_0 = (MXC_F_UART_CTRL_PARITY_EN | 85 MXC_S_UART_CTRL_PARITY_SPACE | 86 MXC_F_UART_CTRL_PARMD), /**< Use for space parity 0 */ 87 UART_PARITY_SPACE_1 = (MXC_F_UART_CTRL_PARITY_EN | 88 MXC_S_UART_CTRL_PARITY_SPACE), /**< Use for space parity 1 */ 89 UART_PARITY_SPACE = UART_PARITY_SPACE_1, /**< Conventional space parity */ 90 } uart_parity_t; 91 92 /** 93 * @brief Message size settings */ 94 typedef enum { 95 UART_DATA_SIZE_5_BITS = MXC_S_UART_CTRL_CHAR_SIZE_5, /**< Data Size 5 Bits */ 96 UART_DATA_SIZE_6_BITS = MXC_S_UART_CTRL_CHAR_SIZE_6, /**< Data Size 6 Bits */ 97 UART_DATA_SIZE_7_BITS = MXC_S_UART_CTRL_CHAR_SIZE_7, /**< Data Size 7 Bits */ 98 UART_DATA_SIZE_8_BITS = MXC_S_UART_CTRL_CHAR_SIZE_8, /**< Data Size 8 Bits */ 99 } uart_size_t; 100 101 /** 102 * @brief Stop bit settings */ 103 typedef enum { 104 UART_STOP_1 = 0, /**< UART Stop 1 clock cycle */ 105 UART_STOP_1P5 = MXC_F_UART_CTRL_STOPBITS, /**< UART Stop 1.5 clock cycle */ 106 UART_STOP_2 = MXC_F_UART_CTRL_STOPBITS, /**< UART Stop 2 clock cycle */ 107 } uart_stop_t; 108 109 /** 110 * @brief Flow control */ 111 typedef enum { 112 UART_FLOW_CTRL_DIS = 0, /**< RTS/CTS flow is disabled */ 113 UART_FLOW_CTRL_EN = MXC_F_UART_CTRL_FLOW_CTRL, /**< RTS/CTS flow is enabled */ 114 } uart_flow_ctrl_t; 115 116 /** 117 * @brief Flow control Polarity */ 118 typedef enum { 119 UART_FLOW_POL_DIS = 0, /**< RTS/CTS asserted is low */ 120 UART_FLOW_POL_EN = MXC_F_UART_CTRL_FLOW_POL, /**< RTS/CTS asserted is high */ 121 } uart_flow_pol_t; 122 123 #if (TARGET != 32660) 124 /** 125 * @brief Clock Source Select */ 126 typedef enum { 127 UART_CLKSEL_SYSTEM = 0, /**< Peripheral clock will be used as the bit rate clock */ 128 UART_CLKSEL_ALTERNATE = MXC_F_UART_CTRL_CLKSEL, /**< Use the device's alternate UART bit rate clock. */ 129 } uart_clksel_t; 130 #endif 131 132 /** 133 * @brief UART configuration type. */ 134 typedef struct { 135 uart_parity_t parity; /** Configure parity checking */ 136 uart_size_t size; /** Configure character size */ 137 uart_stop_t stop; /** Configure the number of stop bits to use */ 138 uart_flow_ctrl_t flow; /** Configure hardware flow control */ 139 uart_flow_pol_t pol; /** Configure hardware flow control */ 140 uint32_t baud; /** Configure baud rate */ 141 #if (TARGET != 32660) 142 uart_clksel_t clksel; /** Configure hardware clock source */ 143 #endif 144 } uart_cfg_t; 145 146 /** 147 * @brief Non-blocking UART transaction request. */ 148 typedef struct uart_req uart_req_t; 149 struct uart_req { 150 uint8_t *data; /** Data buffer for characters */ 151 int len; /** Length of characters in data to send or receive */ 152 int num; /** Number of characters actually sent or received */ 153 154 /** 155 * @brief Callback for asynchronous request. 156 * 157 * @param uart_req_t* Pointer to the transaction request. 158 * @param int Error code. 159 * 160 */ 161 void(*callback)(uart_req_t*, int); 162 }; 163 164 165 /***** Functions Prototypes *****/ 166 167 168 /** 169 * @brief Initialize and enable UART module. 170 * @param uart Pointer to the UART registers. 171 * @param cfg Pointer to UART configuration. 172 * @param sys_cfg Pointer to system configuration object 173 * @returns #E_NO_ERROR UART initialized successfully, @ref MXC_Error_Codes "error" if 174 * unsuccessful. 175 */ 176 int UART_Init(mxc_uart_regs_t *uart, const uart_cfg_t *cfg, const sys_cfg_uart_t* sys_cfg); 177 178 /** 179 * @brief Shutdown UART module. 180 * @param uart Pointer to the UART registers. 181 * @returns #E_NO_ERROR UART shutdown successfully, @ref MXC_Error_Codes "error" if 182 * unsuccessful. 183 */ 184 int UART_Shutdown(mxc_uart_regs_t *uart); 185 186 /** 187 * @brief UART interrupt handler. 188 * @details This function should be called by the application from the 189 * interrupt handler if UART interrupts are enabled. Alternately, 190 * this function can be periodically called by the application if 191 * UART interrupts are disabled. It is only necessary to call this 192 * when using asynchronous functions. 193 * 194 * @param uart Pointer to the UART registers. 195 */ 196 void UART_Handler(mxc_uart_regs_t *uart); 197 198 /** 199 * @brief Read UART data, <em>blocking</em> until transaction is complete. 200 * 201 * @param uart Pointer to the UART registers. 202 * @param data Pointer to buffer to save the data read. 203 * @param len Number of bytes to read. 204 * @param num Pointer to store the number of bytes actually read, pass NULL if not needed. 205 * 206 * @return Number of bytes read, @ref MXC_Error_Codes "error" if unsuccessful. 207 */ 208 int UART_Read(mxc_uart_regs_t *uart, uint8_t *data, int len, int *num); 209 210 /** 211 * @brief Write UART data. This function blocks until the write transaction 212 * is complete. 213 * @param uart Pointer to the UART registers. 214 * @param data Pointer to buffer for write data. 215 * @param len Number of bytes to write. 216 * @note This function will return once data has been put into FIFO, not necessarily 217 * transmitted. 218 * @return Number of bytes written if successful, @ref MXC_Error_Codes "error" if unsuccessful. 219 */ 220 int UART_Write(mxc_uart_regs_t *uart, const uint8_t *data, int len); 221 222 /** 223 * @brief Asynchronously read UART data. 224 * 225 * @param uart Pointer to the UART registers. 226 * @param req Pointer to request for a UART transaction, see #uart_req. 227 * @note Request struct must remain allocated until callback function specified in 'req' is called. 228 * 229 * @return #E_NO_ERROR Asynchronous read successfully started, @ref MXC_Error_Codes "error" if unsuccessful. 230 */ 231 int UART_ReadAsync(mxc_uart_regs_t *uart, uart_req_t *req); 232 233 /** 234 * @brief Asynchronously write/transmit UART data. 235 * 236 * @param uart Pointer to the UART registers. 237 * @param req Request for a UART transaction, see #uart_req. 238 * @note Request struct must remain allocated until callback function specified in 'req' is called. 239 * 240 * @return #E_NO_ERROR Asynchronous write successfully started, @ref 241 * MXC_Error_Codes "error" if unsuccessful. 242 */ 243 int UART_WriteAsync(mxc_uart_regs_t *uart, uart_req_t *req); 244 245 /** 246 * @brief Read a single byte from the UART. 247 * @note This function will block until a character is available. 248 * 249 * @param uart Pointer to the UART registers. 250 * @return The byte read. 251 */ 252 uint8_t UART_ReadByte(mxc_uart_regs_t *uart); 253 254 /** 255 * @brief Write one byte at a time to the UART. 256 * @note This function will block until the character has been placed in the transmit FIFO. 257 * It may return before the character is actually transmitted. 258 * 259 * @param uart Pointer to the UART registers. 260 * @param data The byte to write. 261 */ 262 void UART_WriteByte(mxc_uart_regs_t *uart, uint8_t data); 263 264 /** 265 * @brief Check to see if the UART is busy. 266 * 267 * @param uart Pointer to the UART registers. 268 * 269 * @return #E_NO_ERROR if the UART is idle, #E_BUSY if the UART is in use. 270 */ 271 int UART_Busy(mxc_uart_regs_t *uart); 272 273 /** 274 * @brief Prepare the UART for entry into a Low-Power mode (DEEPSLEEP/BACKUP). 275 * @details Checks for any ongoing transactions. Disables interrupts if the 276 * UART is idle. 277 * 278 * @param uart Pointer to the UART registers. 279 * @return #E_NO_ERROR UART is ready to enter Low-Power modes (DEEPSLEEP/BACKUP). 280 * @return #E_BUSY UART is active and busy and not ready to enter a 281 * Low-Power mode (DEEPSLEEP/BACKUP). 282 * 283 */ 284 int UART_PrepForSleep(mxc_uart_regs_t *uart); 285 286 /** 287 * @brief Abort asynchronous request. 288 * 289 * @param req Pointer to the request to abort. See #uart_req. 290 * 291 * @return #E_NO_ERROR if the asynchronous request aborted successfully started, @ref 292 * MXC_Error_Codes "error" if unsuccessful. 293 */ 294 int UART_AbortAsync(uart_req_t *req); 295 296 /** 297 * @brief Returns the number of bytes still pending transmission in the UART TX FIFO. 298 * 299 * @param uart Pointer to the UART registers. 300 * 301 * @return Number of unused bytes in the TX FIFO. 302 */ 303 unsigned UART_NumWriteAvail(mxc_uart_regs_t *uart); 304 305 /** 306 * @brief Returns the number of bytes available to be read from the RX FIFO. 307 * 308 * @param uart Pointer to the UART registers. 309 * 310 * @return The number of bytes available to read in the RX FIFO. 311 */ 312 unsigned UART_NumReadAvail(mxc_uart_regs_t *uart); 313 314 /** 315 * @brief Clears the specified interrupt flags. 316 * 317 * @param uart Pointer to the UART registers. 318 * @param mask Mask of the UART interrupts to clear, see 319 * @ref UART_INT_FL Register. 320 */ 321 void UART_ClearFlags(mxc_uart_regs_t *uart, uint32_t mask); 322 323 /** 324 * @brief Get the UART interrupt flags. 325 * 326 * @param uart Pointer to the UART registers. 327 * 328 * @return Mask of active flags. 329 */ 330 unsigned UART_GetFlags(mxc_uart_regs_t *uart); 331 332 /** 333 * @brief Enables the UART. 334 * @note This function does not change the existing UART configuration. 335 * 336 * @param uart Pointer to the UART registers. 337 */ 338 void UART_Enable(mxc_uart_regs_t *uart); 339 340 /** 341 * @brief Disables the UART. 342 * @note This function does not change the existing UART configuration. 343 * 344 * @param uart Pointer to the UART registers. 345 */ 346 void UART_Disable(mxc_uart_regs_t *uart); 347 348 /** 349 * @brief Drains/empties and data in the RX FIFO, discarding any bytes not yet consumed. 350 * 351 * @param uart Pointer to the UART registers. 352 */ 353 void UART_DrainRX(mxc_uart_regs_t *uart); 354 355 /** 356 * @brief Drains/empties any data in the TX FIFO, discarding any bytes not yet transmitted. 357 * 358 * @param uart Pointer to the UART registers. 359 */ 360 void UART_DrainTX(mxc_uart_regs_t *uart); 361 362 /**@} end of group uart */ 363 364 #endif /* _UART_H_ */ 365