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.9 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_UART_H
  48. #define AM_HAL_UART_H
  49. #ifdef __cplusplus
  50. extern "C"
  51. {
  52. #endif
  53. //*****************************************************************************
  54. //
  55. //! @name UART Interrupts
  56. //! @brief Macro definitions for UART FIFO levels.
  57. //!
  58. //! They may be used with the \e am_hal_uart_fifo_config() function.
  59. //!
  60. //! @{
  61. //
  62. //*****************************************************************************
  63. #define AM_HAL_UART_INT_OVER_RUN AM_REG_UART_IER_OEIM_M
  64. #define AM_HAL_UART_INT_BREAK_ERR AM_REG_UART_IER_BEIM_M
  65. #define AM_HAL_UART_INT_PARITY_ERR AM_REG_UART_IER_PEIM_M
  66. #define AM_HAL_UART_INT_FRAME_ERR AM_REG_UART_IER_FEIM_M
  67. #define AM_HAL_UART_INT_RX_TMOUT AM_REG_UART_IER_RTIM_M
  68. #define AM_HAL_UART_INT_TX AM_REG_UART_IER_TXIM_M
  69. #define AM_HAL_UART_INT_RX AM_REG_UART_IER_RXIM_M
  70. #define AM_HAL_UART_INT_DSRM AM_REG_UART_IER_DSRMIM_M
  71. #define AM_HAL_UART_INT_DCDM AM_REG_UART_IER_DCDMIM_M
  72. #define AM_HAL_UART_INT_CTSM AM_REG_UART_IER_CTSMIM_M
  73. #define AM_HAL_UART_INT_TXCMP AM_REG_UART_IER_TXCMPMIM_M
  74. //! @}
  75. //*****************************************************************************
  76. //
  77. //! @name UART FIFO Levels
  78. //! @brief Macro definitions for RTV interrupt status bits.
  79. //!
  80. //! These macros correspond to the bits in the UART interrupt status register.
  81. //! They may be used with any of the \e am_hal_uart_int_x() functions.
  82. //!
  83. //! @{
  84. //
  85. //*****************************************************************************
  86. //TX
  87. #define AM_HAL_UART_TX_FIFO_1_8 AM_REG_UART_IFLS_TXIFLSEL(0)
  88. #define AM_HAL_UART_TX_FIFO_1_4 AM_REG_UART_IFLS_TXIFLSEL(1)
  89. #define AM_HAL_UART_TX_FIFO_1_2 AM_REG_UART_IFLS_TXIFLSEL(2)
  90. #define AM_HAL_UART_TX_FIFO_3_4 AM_REG_UART_IFLS_TXIFLSEL(3)
  91. #define AM_HAL_UART_TX_FIFO_7_8 AM_REG_UART_IFLS_TXIFLSEL(4)
  92. // RX
  93. #define AM_HAL_UART_RX_FIFO_1_8 AM_REG_UART_IFLS_RXIFLSEL(0)
  94. #define AM_HAL_UART_RX_FIFO_1_4 AM_REG_UART_IFLS_RXIFLSEL(1)
  95. #define AM_HAL_UART_RX_FIFO_1_2 AM_REG_UART_IFLS_RXIFLSEL(2)
  96. #define AM_HAL_UART_RX_FIFO_3_4 AM_REG_UART_IFLS_RXIFLSEL(3)
  97. #define AM_HAL_UART_RX_FIFO_7_8 AM_REG_UART_IFLS_RXIFLSEL(4)
  98. //! @}
  99. //*****************************************************************************
  100. //
  101. //! @name UART Status Register
  102. //! @brief Macro definitions for UART Status Register Bits.
  103. //!
  104. //! They may be used with the \e am_hal_uart_status_get() function.
  105. //!
  106. //! @{
  107. //
  108. //*****************************************************************************
  109. // This is the overrun error indicator.
  110. #define AM_HAL_UART_RSR_OVERRUN_NOERR AM_REG_UART_RSR_OESTAT_NOERR
  111. #define AM_HAL_UART_RSR_OVERRUN_ERROR AM_REG_UART_RSR_OESTAT_ERR
  112. // This is the break error indicator.
  113. #define AM_HAL_UART_RSR_BREAK_NOERR AM_REG_UART_RSR_BESTAT_NOERR
  114. #define AM_HAL_UART_RSR_BREAK_ERROR AM_REG_UART_RSR_BESTAT_ERR
  115. // This is the parity error indicator.
  116. #define AM_HAL_UART_RSR_PARITY_NOERR AM_REG_UART_RSR_PESTAT_NOERR
  117. #define AM_HAL_UART_RSR_PARITY_ERROR AM_REG_UART_RSR_PESTAT_ERR
  118. // This is the framing error indicator.
  119. #define AM_HAL_UART_RSR_FRAME_ERROR_NOERR AM_REG_UART_RSR_FESTAT_NOERR
  120. #define AM_HAL_UART_RSR_FRAME_ERROR_ERROR AM_REG_UART_RSR_FESTAT_ERR
  121. //! @}
  122. //*****************************************************************************
  123. //
  124. //! @name UART Flag Register
  125. //! @brief Macro definitions for UART Flag Register Bits.
  126. //!
  127. //! They may be used with the \e am_hal_uart_flags_get() function.
  128. //!
  129. //! @{
  130. //
  131. //*****************************************************************************
  132. #define AM_HAL_UART_FR_RING AM_REG_UART_FR_RI_M
  133. #define AM_HAL_UART_FR_TX_EMPTY AM_REG_UART_FR_TXFE_XMTFIFO_EMPTY
  134. #define AM_HAL_UART_FR_RX_FULL AM_REG_UART_FR_RXFF_RCVFIFO_FULL
  135. #define AM_HAL_UART_FR_TX_FULL AM_REG_UART_FR_TXFF_XMTFIFO_FULL
  136. #define AM_HAL_UART_FR_RX_EMPTY AM_REG_UART_FR_RXFE_RCVFIFO_EMPTY
  137. #define AM_HAL_UART_FR_BUSY AM_REG_UART_FR_BUSY_BUSY
  138. #define AM_HAL_UART_FR_DCD_DETECTED AM_REG_UART_FR_DCD_DETECTED
  139. #define AM_HAL_UART_FR_DSR_READY AM_REG_UART_FR_DSR_READY
  140. #define AM_HAL_UART_FR_CTS AM_REG_UART_FR_CTS_M
  141. //! @}
  142. //*****************************************************************************
  143. //
  144. //! @name UART Config Macros
  145. //! @brief Macro definitions for available Data bits.
  146. //!
  147. //! They may be used with the \e am_hal_uart_config_t structure used by \e
  148. //! am_hal_uart_config().
  149. //!
  150. //! @{
  151. //
  152. //*****************************************************************************
  153. //*****************************************************************************
  154. //
  155. // Data bits defines.
  156. //
  157. //*****************************************************************************
  158. #define AM_HAL_UART_DATA_BITS_8 AM_REG_UART_LCRH_WLEN(3)
  159. #define AM_HAL_UART_DATA_BITS_7 AM_REG_UART_LCRH_WLEN(2)
  160. #define AM_HAL_UART_DATA_BITS_6 AM_REG_UART_LCRH_WLEN(1)
  161. #define AM_HAL_UART_DATA_BITS_5 0
  162. //*****************************************************************************
  163. //
  164. // Parity defines.
  165. //
  166. //*****************************************************************************
  167. #define AM_HAL_UART_PARITY_NONE 0
  168. #define AM_HAL_UART_PARITY_ODD AM_REG_UART_LCRH_PEN_M
  169. #define AM_HAL_UART_PARITY_EVEN AM_REG_UART_LCRH_PEN_M | \
  170. AM_REG_UART_LCRH_EPS_M
  171. //*****************************************************************************
  172. //
  173. // Flow control defines.
  174. //
  175. //*****************************************************************************
  176. #define AM_HAL_UART_FLOW_CTRL_NONE 0
  177. #define AM_HAL_UART_FLOW_CTRL_RTS_CTS AM_REG_UART_CR_CTSEN_M | \
  178. AM_REG_UART_CR_RTSEN_M
  179. //! @}
  180. //*****************************************************************************
  181. //
  182. //! UART configuration structure
  183. //
  184. //*****************************************************************************
  185. typedef struct
  186. {
  187. //
  188. //! Desired Baudrate for the UART.
  189. //
  190. uint32_t ui32BaudRate;
  191. //
  192. //! Number of data bits.
  193. //!
  194. //! Valid values for ui32DataBits are:
  195. //!
  196. //! AM_HAL_UART_DATA_BITS_8
  197. //! AM_HAL_UART_DATA_BITS_7
  198. //! AM_HAL_UART_DATA_BITS_6
  199. //! AM_HAL_UART_DATA_BITS_5
  200. //
  201. uint32_t ui32DataBits;
  202. //
  203. //! Use two stop bits.
  204. //
  205. bool bTwoStopBits;
  206. //
  207. //! Parity.
  208. //!
  209. //! Valid values for ui32Parity are:
  210. //!
  211. //! AM_HAL_UART_PARITY_NONE
  212. //! AM_HAL_UART_PARITY_ODD
  213. //! AM_HAL_UART_PARITY_EVEN
  214. //
  215. uint32_t ui32Parity;
  216. //
  217. //! Flow control.
  218. //!
  219. //! Valid values for ui32FlowCtrl are:
  220. //!
  221. //! AM_HAL_UART_FLOW_CTRL_NONE
  222. //! AM_HAL_UART_FLOW_CTRL_RTS_CTS
  223. //
  224. uint32_t ui32FlowCtrl;
  225. }
  226. am_hal_uart_config_t;
  227. //*****************************************************************************
  228. //
  229. // Structure for containing information about the UART's configuration while
  230. // it is powered down.
  231. //
  232. //*****************************************************************************
  233. typedef struct
  234. {
  235. uint32_t ILPR;
  236. uint32_t IBRD;
  237. uint32_t FBRD;
  238. uint32_t LCRH;
  239. uint32_t CR;
  240. uint32_t IFLS;
  241. uint32_t IER;
  242. uint32_t UARTEN;
  243. uint32_t bValid;
  244. }
  245. am_hal_uart_pwrsave_t;
  246. //*****************************************************************************
  247. //
  248. // Global Variables
  249. //
  250. //*****************************************************************************
  251. extern am_hal_uart_pwrsave_t am_hal_uart_pwrsave[AM_REG_UART_NUM_MODULES];
  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. //*****************************************************************************