n32g45x_usart.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_usart.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __N32G45X_USART_H__
  35. #define __N32G45X_USART_H__
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #include "n32g45x.h"
  40. /** @addtogroup N32G45X_StdPeriph_Driver
  41. * @{
  42. */
  43. /** @addtogroup USART
  44. * @{
  45. */
  46. /** @addtogroup USART_Exported_Types
  47. * @{
  48. */
  49. /**
  50. * @brief USART Init Structure definition
  51. */
  52. typedef struct
  53. {
  54. uint32_t BaudRate; /*!< This member configures the USART communication baud rate.
  55. The baud rate is computed using the following formula:
  56. - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->BaudRate)))
  57. - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
  58. uint16_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  59. This parameter can be a value of @ref USART_Word_Length */
  60. uint16_t StopBits; /*!< Specifies the number of stop bits transmitted.
  61. This parameter can be a value of @ref USART_Stop_Bits */
  62. uint16_t Parity; /*!< Specifies the parity mode.
  63. This parameter can be a value of @ref Parity
  64. @note When parity is enabled, the computed parity is inserted
  65. at the MSB position of the transmitted data (9th bit when
  66. the word length is set to 9 data bits; 8th bit when the
  67. word length is set to 8 data bits). */
  68. uint16_t Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
  69. This parameter can be a value of @ref Mode */
  70. uint16_t HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
  71. or disabled.
  72. This parameter can be a value of @ref USART_Hardware_Flow_Control */
  73. } USART_InitType;
  74. /**
  75. * @brief USART Clock Init Structure definition
  76. */
  77. typedef struct
  78. {
  79. uint16_t Clock; /*!< Specifies whether the USART clock is enabled or disabled.
  80. This parameter can be a value of @ref Clock */
  81. uint16_t Polarity; /*!< Specifies the steady state value of the serial clock.
  82. This parameter can be a value of @ref USART_Clock_Polarity */
  83. uint16_t Phase; /*!< Specifies the clock transition on which the bit capture is made.
  84. This parameter can be a value of @ref USART_Clock_Phase */
  85. uint16_t LastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
  86. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  87. This parameter can be a value of @ref USART_Last_Bit */
  88. } USART_ClockInitType;
  89. /**
  90. * @}
  91. */
  92. /** @addtogroup USART_Exported_Constants
  93. * @{
  94. */
  95. #define IS_USART_ALL_PERIPH(PERIPH) \
  96. (((PERIPH) == USART1) || ((PERIPH) == USART2) || ((PERIPH) == USART3) || ((PERIPH) == UART4) \
  97. || ((PERIPH) == UART5) || ((PERIPH) == UART6) || ((PERIPH) == UART7))
  98. #define IS_USART_123_PERIPH(PERIPH) (((PERIPH) == USART1) || ((PERIPH) == USART2) || ((PERIPH) == USART3))
  99. #define IS_USART_1234_PERIPH(PERIPH) \
  100. (((PERIPH) == USART1) || ((PERIPH) == USART2) || ((PERIPH) == USART3) || ((PERIPH) == UART4))
  101. /** @addtogroup USART_Word_Length
  102. * @{
  103. */
  104. #define USART_WL_8B ((uint16_t)0x0000)
  105. #define USART_WL_9B ((uint16_t)0x1000)
  106. #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == USART_WL_8B) || ((LENGTH) == USART_WL_9B))
  107. /**
  108. * @}
  109. */
  110. /** @addtogroup USART_Stop_Bits
  111. * @{
  112. */
  113. #define USART_STPB_1 ((uint16_t)0x0000)
  114. #define USART_STPB_0_5 ((uint16_t)0x1000)
  115. #define USART_STPB_2 ((uint16_t)0x2000)
  116. #define USART_STPB_1_5 ((uint16_t)0x3000)
  117. #define IS_USART_STOPBITS(STOPBITS) \
  118. (((STOPBITS) == USART_STPB_1) || ((STOPBITS) == USART_STPB_0_5) || ((STOPBITS) == USART_STPB_2) \
  119. || ((STOPBITS) == USART_STPB_1_5))
  120. /**
  121. * @}
  122. */
  123. /** @addtogroup Parity
  124. * @{
  125. */
  126. #define USART_PE_NO ((uint16_t)0x0000)
  127. #define USART_PE_EVEN ((uint16_t)0x0400)
  128. #define USART_PE_ODD ((uint16_t)0x0600)
  129. #define IS_USART_PARITY(PARITY) (((PARITY) == USART_PE_NO) || ((PARITY) == USART_PE_EVEN) || ((PARITY) == USART_PE_ODD))
  130. /**
  131. * @}
  132. */
  133. /** @addtogroup Mode
  134. * @{
  135. */
  136. #define USART_MODE_RX ((uint16_t)0x0004)
  137. #define USART_MODE_TX ((uint16_t)0x0008)
  138. #define IS_USART_MODE(MODE) ((((MODE) & (uint16_t)0xFFF3) == 0x00) && ((MODE) != (uint16_t)0x00))
  139. /**
  140. * @}
  141. */
  142. /** @addtogroup USART_Hardware_Flow_Control
  143. * @{
  144. */
  145. #define USART_HFCTRL_NONE ((uint16_t)0x0000)
  146. #define USART_HFCTRL_RTS ((uint16_t)0x0100)
  147. #define USART_HFCTRL_CTS ((uint16_t)0x0200)
  148. #define USART_HFCTRL_RTS_CTS ((uint16_t)0x0300)
  149. #define IS_USART_HARDWARE_FLOW_CONTROL(CONTROL) \
  150. (((CONTROL) == USART_HFCTRL_NONE) || ((CONTROL) == USART_HFCTRL_RTS) || ((CONTROL) == USART_HFCTRL_CTS) \
  151. || ((CONTROL) == USART_HFCTRL_RTS_CTS))
  152. /**
  153. * @}
  154. */
  155. /** @addtogroup Clock
  156. * @{
  157. */
  158. #define USART_CLK_DISABLE ((uint16_t)0x0000)
  159. #define USART_CLK_ENABLE ((uint16_t)0x0800)
  160. #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_CLK_DISABLE) || ((CLOCK) == USART_CLK_ENABLE))
  161. /**
  162. * @}
  163. */
  164. /** @addtogroup USART_Clock_Polarity
  165. * @{
  166. */
  167. #define USART_CLKPOL_LOW ((uint16_t)0x0000)
  168. #define USART_CLKPOL_HIGH ((uint16_t)0x0400)
  169. #define IS_USART_CPOL(CPOL) (((CPOL) == USART_CLKPOL_LOW) || ((CPOL) == USART_CLKPOL_HIGH))
  170. /**
  171. * @}
  172. */
  173. /** @addtogroup USART_Clock_Phase
  174. * @{
  175. */
  176. #define USART_CLKPHA_1EDGE ((uint16_t)0x0000)
  177. #define USART_CLKPHA_2EDGE ((uint16_t)0x0200)
  178. #define IS_USART_CPHA(CPHA) (((CPHA) == USART_CLKPHA_1EDGE) || ((CPHA) == USART_CLKPHA_2EDGE))
  179. /**
  180. * @}
  181. */
  182. /** @addtogroup USART_Last_Bit
  183. * @{
  184. */
  185. #define USART_CLKLB_DISABLE ((uint16_t)0x0000)
  186. #define USART_CLKLB_ENABLE ((uint16_t)0x0100)
  187. #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_CLKLB_DISABLE) || ((LASTBIT) == USART_CLKLB_ENABLE))
  188. /**
  189. * @}
  190. */
  191. /** @addtogroup USART_Interrupt_definition
  192. * @{
  193. */
  194. #define USART_INT_PEF ((uint16_t)0x0028)
  195. #define USART_INT_TXDE ((uint16_t)0x0727)
  196. #define USART_INT_TXC ((uint16_t)0x0626)
  197. #define USART_INT_RXDNE ((uint16_t)0x0525)
  198. #define USART_INT_IDLEF ((uint16_t)0x0424)
  199. #define USART_INT_LINBD ((uint16_t)0x0846)
  200. #define USART_INT_CTSF ((uint16_t)0x096A)
  201. #define USART_INT_ERRF ((uint16_t)0x0060)
  202. #define USART_INT_OREF ((uint16_t)0x0360)
  203. #define USART_INT_NEF ((uint16_t)0x0260)
  204. #define USART_INT_FEF ((uint16_t)0x0160)
  205. #define IS_USART_CFG_INT(IT) \
  206. (((IT) == USART_INT_PEF) || ((IT) == USART_INT_TXDE) || ((IT) == USART_INT_TXC) || ((IT) == USART_INT_RXDNE) \
  207. || ((IT) == USART_INT_IDLEF) || ((IT) == USART_INT_LINBD) || ((IT) == USART_INT_CTSF) \
  208. || ((IT) == USART_INT_ERRF))
  209. #define IS_USART_GET_INT(IT) \
  210. (((IT) == USART_INT_PEF) || ((IT) == USART_INT_TXDE) || ((IT) == USART_INT_TXC) || ((IT) == USART_INT_RXDNE) \
  211. || ((IT) == USART_INT_IDLEF) || ((IT) == USART_INT_LINBD) || ((IT) == USART_INT_CTSF) || ((IT) == USART_INT_OREF) \
  212. || ((IT) == USART_INT_NEF) || ((IT) == USART_INT_FEF))
  213. #define IS_USART_CLR_INT(IT) \
  214. (((IT) == USART_INT_TXC) || ((IT) == USART_INT_RXDNE) || ((IT) == USART_INT_LINBD) || ((IT) == USART_INT_CTSF))
  215. /**
  216. * @}
  217. */
  218. /** @addtogroup USART_DMA_Requests
  219. * @{
  220. */
  221. #define USART_DMAREQ_TX ((uint16_t)0x0080)
  222. #define USART_DMAREQ_RX ((uint16_t)0x0040)
  223. #define IS_USART_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFF3F) == 0x00) && ((DMAREQ) != (uint16_t)0x00))
  224. /**
  225. * @}
  226. */
  227. /** @addtogroup USART_WakeUp_methods
  228. * @{
  229. */
  230. #define USART_WUM_IDLELINE ((uint16_t)0x0000)
  231. #define USART_WUM_ADDRMASK ((uint16_t)0x0800)
  232. #define IS_USART_WAKEUP(WAKEUP) (((WAKEUP) == USART_WUM_IDLELINE) || ((WAKEUP) == USART_WUM_ADDRMASK))
  233. /**
  234. * @}
  235. */
  236. /** @addtogroup USART_LIN_Break_Detection_Length
  237. * @{
  238. */
  239. #define USART_LINBDL_10B ((uint16_t)0x0000)
  240. #define USART_LINBDL_11B ((uint16_t)0x0020)
  241. #define IS_USART_LIN_BREAK_DETECT_LENGTH(LENGTH) (((LENGTH) == USART_LINBDL_10B) || ((LENGTH) == USART_LINBDL_11B))
  242. /**
  243. * @}
  244. */
  245. /** @addtogroup USART_IrDA_Low_Power
  246. * @{
  247. */
  248. #define USART_IRDAMODE_LOWPPWER ((uint16_t)0x0004)
  249. #define USART_IRDAMODE_NORMAL ((uint16_t)0x0000)
  250. #define IS_USART_IRDA_MODE(MODE) (((MODE) == USART_IRDAMODE_LOWPPWER) || ((MODE) == USART_IRDAMODE_NORMAL))
  251. /**
  252. * @}
  253. */
  254. /** @addtogroup USART_Flags
  255. * @{
  256. */
  257. #define USART_FLAG_CTSF ((uint16_t)0x0200)
  258. #define USART_FLAG_LINBD ((uint16_t)0x0100)
  259. #define USART_FLAG_TXDE ((uint16_t)0x0080)
  260. #define USART_FLAG_TXC ((uint16_t)0x0040)
  261. #define USART_FLAG_RXDNE ((uint16_t)0x0020)
  262. #define USART_FLAG_IDLEF ((uint16_t)0x0010)
  263. #define USART_FLAG_OREF ((uint16_t)0x0008)
  264. #define USART_FLAG_NEF ((uint16_t)0x0004)
  265. #define USART_FLAG_FEF ((uint16_t)0x0002)
  266. #define USART_FLAG_PEF ((uint16_t)0x0001)
  267. #define IS_USART_FLAG(FLAG) \
  268. (((FLAG) == USART_FLAG_PEF) || ((FLAG) == USART_FLAG_TXDE) || ((FLAG) == USART_FLAG_TXC) \
  269. || ((FLAG) == USART_FLAG_RXDNE) || ((FLAG) == USART_FLAG_IDLEF) || ((FLAG) == USART_FLAG_LINBD) \
  270. || ((FLAG) == USART_FLAG_CTSF) || ((FLAG) == USART_FLAG_OREF) || ((FLAG) == USART_FLAG_NEF) \
  271. || ((FLAG) == USART_FLAG_FEF))
  272. #define IS_USART_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFC9F) == 0x00) && ((FLAG) != (uint16_t)0x00))
  273. #define IS_USART_PERIPH_FLAG(PERIPH, USART_FLAG) \
  274. ((((*(uint32_t*)&(PERIPH)) != UART4_BASE) && ((*(uint32_t*)&(PERIPH)) != UART5_BASE)) \
  275. || ((USART_FLAG) != USART_FLAG_CTSF))
  276. #define IS_USART_BAUDRATE(BAUDRATE) (((BAUDRATE) > 0) && ((BAUDRATE) < 0x0044AA21))
  277. #define IS_USART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xF)
  278. #define IS_USART_DATA(DATA) ((DATA) <= 0x1FF)
  279. /**
  280. * @}
  281. */
  282. /**
  283. * @}
  284. */
  285. /** @addtogroup USART_Exported_Macros
  286. * @{
  287. */
  288. /**
  289. * @}
  290. */
  291. /** @addtogroup USART_Exported_Functions
  292. * @{
  293. */
  294. void USART_DeInit(USART_Module* USARTx);
  295. void USART_Init(USART_Module* USARTx, USART_InitType* USART_InitStruct);
  296. void USART_StructInit(USART_InitType* USART_InitStruct);
  297. void USART_ClockInit(USART_Module* USARTx, USART_ClockInitType* USART_ClockInitStruct);
  298. void USART_ClockStructInit(USART_ClockInitType* USART_ClockInitStruct);
  299. void USART_Enable(USART_Module* USARTx, FunctionalState Cmd);
  300. void USART_ConfigInt(USART_Module* USARTx, uint16_t USART_INT, FunctionalState Cmd);
  301. void USART_EnableDMA(USART_Module* USARTx, uint16_t USART_DMAReq, FunctionalState Cmd);
  302. void USART_SetAddr(USART_Module* USARTx, uint8_t USART_Addr);
  303. void USART_ConfigWakeUpMode(USART_Module* USARTx, uint16_t USART_WakeUpMode);
  304. void USART_EnableRcvWakeUp(USART_Module* USARTx, FunctionalState Cmd);
  305. void USART_ConfigLINBreakDetectLength(USART_Module* USARTx, uint16_t USART_LINBreakDetectLength);
  306. void USART_EnableLIN(USART_Module* USARTx, FunctionalState Cmd);
  307. void USART_SendData(USART_Module* USARTx, uint16_t Data);
  308. uint16_t USART_ReceiveData(USART_Module* USARTx);
  309. void USART_SendBreak(USART_Module* USARTx);
  310. void USART_SetGuardTime(USART_Module* USARTx, uint8_t USART_GuardTime);
  311. void USART_SetPrescaler(USART_Module* USARTx, uint8_t USART_Prescaler);
  312. void USART_EnableSmartCard(USART_Module* USARTx, FunctionalState Cmd);
  313. void USART_SetSmartCardNACK(USART_Module* USARTx, FunctionalState Cmd);
  314. void USART_EnableHalfDuplex(USART_Module* USARTx, FunctionalState Cmd);
  315. void USART_ConfigIrDAMode(USART_Module* USARTx, uint16_t USART_IrDAMode);
  316. void USART_EnableIrDA(USART_Module* USARTx, FunctionalState Cmd);
  317. FlagStatus USART_GetFlagStatus(USART_Module* USARTx, uint16_t USART_FLAG);
  318. void USART_ClrFlag(USART_Module* USARTx, uint16_t USART_FLAG);
  319. INTStatus USART_GetIntStatus(USART_Module* USARTx, uint16_t USART_INT);
  320. void USART_ClrIntPendingBit(USART_Module* USARTx, uint16_t USART_INT);
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif /* __N32G45X_USART_H__ */
  325. /**
  326. * @}
  327. */
  328. /**
  329. * @}
  330. */
  331. /**
  332. * @}
  333. */