uart.h 14 KB

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