am_hal_uart.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //*****************************************************************************
  2. //
  3. // am_hal_uart.h
  4. //! @file
  5. //!
  6. //! @brief Functions for accessing and configuring the UART.
  7. //!
  8. //! @addtogroup uart2 UART
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_UART_H
  48. #define AM_HAL_UART_H
  49. //*****************************************************************************
  50. //
  51. //! @name UART Interrupts
  52. //! @brief Macro definitions for UART FIFO levels.
  53. //!
  54. //! They may be used with the \e am_hal_uart_fifo_config() function.
  55. //!
  56. //! @{
  57. //
  58. //*****************************************************************************
  59. #define AM_HAL_UART_INT_OVER_RUN AM_REG_UART_IER_OEIM_M
  60. #define AM_HAL_UART_INT_BREAK_ERR AM_REG_UART_IER_BEIM_M
  61. #define AM_HAL_UART_INT_PARITY_ERR AM_REG_UART_IER_PEIM_M
  62. #define AM_HAL_UART_INT_FRAME_ERR AM_REG_UART_IER_FEIM_M
  63. #define AM_HAL_UART_INT_RX_TMOUT AM_REG_UART_IER_RTIM_M
  64. #define AM_HAL_UART_INT_TX AM_REG_UART_IER_TXIM_M
  65. #define AM_HAL_UART_INT_RX AM_REG_UART_IER_RXIM_M
  66. #define AM_HAL_UART_INT_DSRM AM_REG_UART_IER_DSRMIM_M
  67. #define AM_HAL_UART_INT_DCDM AM_REG_UART_IER_DCDMIM_M
  68. #define AM_HAL_UART_INT_CTSM AM_REG_UART_IER_CTSMIM_M
  69. #define AM_HAL_UART_INT_TXCMP AM_REG_UART_IER_TXCMPMIM_M
  70. //! @}
  71. //*****************************************************************************
  72. //
  73. //! @name UART FIFO Levels
  74. //! @brief Macro definitions for RTV interrupt status bits.
  75. //!
  76. //! These macros correspond to the bits in the UART interrupt status register.
  77. //! They may be used with any of the \e am_hal_uart_int_x() functions.
  78. //!
  79. //! @{
  80. //
  81. //*****************************************************************************
  82. //TX
  83. #define AM_HAL_UART_TX_FIFO_1_8 AM_REG_UART_IFLS_TXIFLSEL(0)
  84. #define AM_HAL_UART_TX_FIFO_1_4 AM_REG_UART_IFLS_TXIFLSEL(1)
  85. #define AM_HAL_UART_TX_FIFO_1_2 AM_REG_UART_IFLS_TXIFLSEL(2)
  86. #define AM_HAL_UART_TX_FIFO_3_4 AM_REG_UART_IFLS_TXIFLSEL(3)
  87. #define AM_HAL_UART_TX_FIFO_7_8 AM_REG_UART_IFLS_TXIFLSEL(4)
  88. // RX
  89. #define AM_HAL_UART_RX_FIFO_1_8 AM_REG_UART_IFLS_RXIFLSEL(0)
  90. #define AM_HAL_UART_RX_FIFO_1_4 AM_REG_UART_IFLS_RXIFLSEL(1)
  91. #define AM_HAL_UART_RX_FIFO_1_2 AM_REG_UART_IFLS_RXIFLSEL(2)
  92. #define AM_HAL_UART_RX_FIFO_3_4 AM_REG_UART_IFLS_RXIFLSEL(3)
  93. #define AM_HAL_UART_RX_FIFO_7_8 AM_REG_UART_IFLS_RXIFLSEL(4)
  94. //! @}
  95. //*****************************************************************************
  96. //
  97. //! @name UART Status Register
  98. //! @brief Macro definitions for UART Status Register Bits.
  99. //!
  100. //! They may be used with the \e am_hal_uart_status_get() function.
  101. //!
  102. //! @{
  103. //
  104. //*****************************************************************************
  105. // This is the overrun error indicator.
  106. #define AM_HAL_UART_RSR_OVERRUN_NOERR AM_REG_UART_RSR_OESTAT_NOERR
  107. #define AM_HAL_UART_RSR_OVERRUN_ERROR AM_REG_UART_RSR_OESTAT_ERR
  108. // This is the break error indicator.
  109. #define AM_HAL_UART_RSR_BREAK_NOERR AM_REG_UART_RSR_BESTAT_NOERR
  110. #define AM_HAL_UART_RSR_BREAK_ERROR AM_REG_UART_RSR_BESTAT_ERR
  111. // This is the parity error indicator.
  112. #define AM_HAL_UART_RSR_PARITY_NOERR AM_REG_UART_RSR_PESTAT_NOERR
  113. #define AM_HAL_UART_RSR_PARITY_ERROR AM_REG_UART_RSR_PESTAT_ERR
  114. // This is the framing error indicator.
  115. #define AM_HAL_UART_RSR_FRAME_ERROR_NOERR AM_REG_UART_RSR_FESTAT_NOERR
  116. #define AM_HAL_UART_RSR_FRAME_ERROR_ERROR AM_REG_UART_RSR_FESTAT_ERR
  117. //! @}
  118. //*****************************************************************************
  119. //
  120. //! @name UART Flag Register
  121. //! @brief Macro definitions for UART Flag Register Bits.
  122. //!
  123. //! They may be used with the \e am_hal_uart_flags_get() function.
  124. //!
  125. //! @{
  126. //
  127. //*****************************************************************************
  128. #define AM_HAL_UART_FR_RING AM_REG_UART_FR_RI_M
  129. #define AM_HAL_UART_FR_TX_EMPTY AM_REG_UART_FR_TXFE_XMTFIFO_EMPTY
  130. #define AM_HAL_UART_FR_RX_FULL AM_REG_UART_FR_RXFF_RCVFIFO_FULL
  131. #define AM_HAL_UART_FR_TX_FULL AM_REG_UART_FR_TXFF_XMTFIFO_FULL
  132. #define AM_HAL_UART_FR_RX_EMPTY AM_REG_UART_FR_RXFE_RCVFIFO_EMPTY
  133. #define AM_HAL_UART_FR_BUSY AM_REG_UART_FR_BUSY_BUSY
  134. #define AM_HAL_UART_FR_DCD_DETECTED AM_REG_UART_FR_DCD_DETECTED
  135. #define AM_HAL_UART_FR_DSR_READY AM_REG_UART_FR_DSR_READY
  136. #define AM_HAL_UART_FR_CTS AM_REG_UART_FR_CTS_M
  137. //! @}
  138. //*****************************************************************************
  139. //
  140. //! @name UART Config Macros
  141. //! @brief Macro definitions for available Data bits.
  142. //!
  143. //! They may be used with the \e am_hal_uart_config_t structure used by \e
  144. //! am_hal_uart_config().
  145. //!
  146. //! @{
  147. //
  148. //*****************************************************************************
  149. //*****************************************************************************
  150. //
  151. // Data bits defines.
  152. //
  153. //*****************************************************************************
  154. #define AM_HAL_UART_DATA_BITS_8 AM_REG_UART_LCRH_WLEN(3)
  155. #define AM_HAL_UART_DATA_BITS_7 AM_REG_UART_LCRH_WLEN(2)
  156. #define AM_HAL_UART_DATA_BITS_6 AM_REG_UART_LCRH_WLEN(1)
  157. #define AM_HAL_UART_DATA_BITS_5 0
  158. //*****************************************************************************
  159. //
  160. // Parity defines.
  161. //
  162. //*****************************************************************************
  163. #define AM_HAL_UART_PARITY_NONE 0
  164. #define AM_HAL_UART_PARITY_ODD AM_REG_UART_LCRH_PEN_M
  165. #define AM_HAL_UART_PARITY_EVEN AM_REG_UART_LCRH_PEN_M | \
  166. AM_REG_UART_LCRH_EPS_M
  167. //*****************************************************************************
  168. //
  169. // Flow control defines.
  170. //
  171. //*****************************************************************************
  172. #define AM_HAL_UART_FLOW_CTRL_NONE 0
  173. #define AM_HAL_UART_FLOW_CTRL_RTS_CTS AM_REG_UART_CR_CTSEN_M | \
  174. AM_REG_UART_CR_RTSEN_M
  175. //! @}
  176. //*****************************************************************************
  177. //
  178. //! UART configuration structure
  179. //
  180. //*****************************************************************************
  181. typedef struct
  182. {
  183. //
  184. //! Desired Baudrate for the UART.
  185. //
  186. uint32_t ui32BaudRate;
  187. //
  188. //! Number of data bits.
  189. //!
  190. //! Valid values for ui32DataBits are:
  191. //!
  192. //! AM_HAL_UART_DATA_BITS_8
  193. //! AM_HAL_UART_DATA_BITS_7
  194. //! AM_HAL_UART_DATA_BITS_6
  195. //! AM_HAL_UART_DATA_BITS_5
  196. //
  197. uint32_t ui32DataBits;
  198. //
  199. //! Use two stop bits.
  200. //
  201. bool bTwoStopBits;
  202. //
  203. //! Parity.
  204. //!
  205. //! Valid values for ui32Parity are:
  206. //!
  207. //! AM_HAL_UART_PARITY_NONE
  208. //! AM_HAL_UART_PARITY_ODD
  209. //! AM_HAL_UART_PARITY_EVEN
  210. //
  211. uint32_t ui32Parity;
  212. //
  213. //! Flow control.
  214. //!
  215. //! Valid values for ui32FlowCtrl are:
  216. //!
  217. //! AM_HAL_UART_FLOW_CTRL_NONE
  218. //! AM_HAL_UART_FLOW_CTRL_RTS_CTS
  219. //
  220. uint32_t ui32FlowCtrl;
  221. }
  222. am_hal_uart_config_t;
  223. //*****************************************************************************
  224. //
  225. // Structure for containing information about the UART's configuration while
  226. // it is powered down.
  227. //
  228. //*****************************************************************************
  229. typedef struct
  230. {
  231. uint32_t ILPR;
  232. uint32_t IBRD;
  233. uint32_t FBRD;
  234. uint32_t LCRH;
  235. uint32_t CR;
  236. uint32_t IFLS;
  237. uint32_t IER;
  238. uint32_t UARTEN;
  239. uint32_t bValid;
  240. }
  241. am_hal_uart_pwrsave_t;
  242. //*****************************************************************************
  243. //
  244. // Global Variables
  245. //
  246. //*****************************************************************************
  247. extern am_hal_uart_pwrsave_t am_hal_uart_pwrsave[AM_REG_UART_NUM_MODULES];
  248. #ifdef __cplusplus
  249. extern "C"
  250. {
  251. #endif
  252. //*****************************************************************************
  253. //
  254. // External function definitions
  255. //
  256. //*****************************************************************************
  257. extern void am_hal_uart_pwrctrl_enable(uint32_t ui32Module);
  258. extern void am_hal_uart_pwrctrl_disable(uint32_t ui32Module);
  259. extern void am_hal_uart_power_on_restore(uint32_t ui32Module);
  260. extern void am_hal_uart_power_off_save(uint32_t ui32Module);
  261. extern void am_hal_uart_config(uint32_t ui32Module,
  262. am_hal_uart_config_t *psConfig);
  263. extern uint32_t am_hal_uart_status_get(uint32_t ui32Module);
  264. extern uint32_t am_hal_uart_int_status_get(uint32_t ui32Module,
  265. bool bEnabledOnly);
  266. extern void am_hal_uart_int_clear(uint32_t ui32Module,
  267. uint32_t ui32Interrupt);
  268. extern void am_hal_uart_int_disable(uint32_t ui32Module,
  269. uint32_t ui32Interrupt);
  270. extern void am_hal_uart_int_enable(uint32_t ui32Module,
  271. uint32_t ui32Interrupt);
  272. extern uint32_t am_hal_uart_int_enable_get(uint32_t ui32Module);
  273. extern void am_hal_uart_enable(uint32_t ui32Module);
  274. extern void am_hal_uart_disable(uint32_t ui32Module);
  275. extern void am_hal_uart_clock_enable(uint32_t ui32Module);
  276. extern void am_hal_uart_clock_disable(uint32_t ui32Module);
  277. extern void am_hal_uart_fifo_config(uint32_t ui32Module, uint32_t ui32LvlCfg);
  278. extern uint32_t am_hal_uart_flags_get(uint32_t ui32Module);
  279. // rx/tx polled
  280. extern void am_hal_uart_char_transmit_polled(uint32_t ui32Module,
  281. char cChar);
  282. extern void am_hal_uart_string_transmit_polled(uint32_t ui32Module,
  283. char *pcString);
  284. extern void am_hal_uart_char_receive_polled(uint32_t ui32Module,
  285. char *pcChar);
  286. extern void am_hal_uart_line_receive_polled(uint32_t ui32Module,
  287. uint32_t ui32MaxChars,
  288. char *pcChar);
  289. // rx/tx buffered
  290. extern void am_hal_uart_init_buffered(uint32_t ui32Module,
  291. uint8_t *pui8RxArray,
  292. uint32_t ui32RxSize,
  293. uint8_t *pui8TxArray,
  294. uint32_t ui32TxSize);
  295. extern void am_hal_uart_get_status_buffered(uint32_t ui32Module,
  296. uint32_t *pui32RxSize,
  297. uint32_t *pui32TxSize);
  298. extern void am_hal_uart_service_buffered(uint32_t ui32Module,
  299. uint32_t ui32Status);
  300. extern void am_hal_uart_service_buffered_timeout_save(uint32_t ui32Module,
  301. uint32_t ui32Status);
  302. extern void am_hal_uart_char_transmit_buffered(uint32_t ui32Module,
  303. char cChar);
  304. extern void am_hal_uart_string_transmit_buffered(uint32_t ui32Module,
  305. char *pcString);
  306. extern uint32_t am_hal_uart_char_receive_buffered(uint32_t ui32Module,
  307. char *pcString,
  308. uint32_t ui32MaxChars);
  309. #ifdef __cplusplus
  310. }
  311. #endif
  312. #endif // AM_HAL_UART_H
  313. //*****************************************************************************
  314. //
  315. // End Doxygen group.
  316. //! @}
  317. //
  318. //*****************************************************************************