apm32f4xx_usart.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*!
  2. * @file apm32f4xx_usart.h
  3. *
  4. * @brief This file contains all the functions prototypes for the USART firmware library
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-06-23
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2021-2022 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be usefull and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. /* Define to prevent recursive inclusion */
  26. #ifndef __APM32F4XX_USART_H
  27. #define __APM32F4XX_USART_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Includes */
  32. #include "apm32f4xx.h"
  33. /** @addtogroup APM32F4xx_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup USART_Driver
  37. @{
  38. */
  39. /** @defgroup USART_Enumerations
  40. @{
  41. */
  42. /**
  43. * @brief USART Word Length
  44. */
  45. typedef enum
  46. {
  47. USART_WORD_LEN_8B, /*!< 8-bit data length */
  48. USART_WORD_LEN_9B /*!< 9-bit data length */
  49. } USART_WORD_LEN_T;
  50. /**
  51. * @brief USART Stop bits
  52. */
  53. typedef enum
  54. {
  55. USART_STOP_BIT_1, /*!< 1-bit stop bit */
  56. USART_STOP_BIT_0_5, /*!< 0.5-bit stop bit */
  57. USART_STOP_BIT_2, /*!< 2-bit stop bit */
  58. USART_STOP_BIT_1_5 /*!< 1.5-bit stop bit */
  59. } USART_STOP_BIT_T;
  60. /**
  61. * @brief USART Parity
  62. */
  63. typedef enum
  64. {
  65. USART_PARITY_NONE = (uint8_t)0x00, /*!< Disable parity control */
  66. USART_PARITY_EVEN = (uint8_t)0x01, /*!< Enable even parity control */
  67. USART_PARITY_ODD = (uint8_t)0x03 /*!< Enable odd parity control*/
  68. } USART_PARITY_T;
  69. /**
  70. * @brief USART mode
  71. */
  72. typedef enum
  73. {
  74. USART_MODE_RX = (uint8_t)0x01, /*!< Enable USART Receive mode */
  75. USART_MODE_TX = (uint8_t)0x02, /*!< Enable USART transmit mode */
  76. USART_MODE_TX_RX = (uint8_t)0x03 /*!< Enable USART receive and transmit mode */
  77. } USART_MODE_T;
  78. /**
  79. * @brief USART hardware flow control
  80. */
  81. typedef enum
  82. {
  83. USART_HARDWARE_FLOW_NONE, /*!< Disable hardware flow control */
  84. USART_HARDWARE_FLOW_RTS, /*!< Enable RTS hardware flow control */
  85. USART_HARDWARE_FLOW_CTS, /*!< Enable CTS hardware flow control */
  86. USART_HARDWARE_FLOW_RTS_CTS /*!< Enable RTS and CTS hardware flow control */
  87. } USART_HARDWARE_FLOW_T;
  88. /**
  89. * @brief USART Clock enable
  90. */
  91. typedef enum
  92. {
  93. USART_CLKEN_DISABLE, /*!< Disable usart clock */
  94. USART_CLKEN_ENABLE /*!< Enable usart clock */
  95. } USART_CLKEN_T;
  96. /**
  97. * @brief USART Clock polarity
  98. */
  99. typedef enum
  100. {
  101. USART_CLKPOL_LOW, /*!< Set clock polarity to low */
  102. USART_CLKPOL_HIGH /*!< Set clock polarity to high */
  103. } USART_CLKPOL_T;
  104. /**
  105. * @brief USART Clock phase
  106. */
  107. typedef enum
  108. {
  109. USART_CLKPHA_1EDGE, /*!< Set usart to sample at the edge of the first clock */
  110. USART_CLKPHA_2EDGE /*!< Set usart to sample at the edge of the second clock */
  111. } USART_CLKPHA_T;
  112. /**
  113. * @brief USART Last bit clock pulse
  114. */
  115. typedef enum
  116. {
  117. USART_LBCP_DISABLE, /*!< Enable output last bit clock pulse */
  118. USART_LBCP_ENABLE, /*!< Disable output last bit clock pulse */
  119. } USART_LBCP_T;
  120. /**
  121. * @brief USART Interrupt Source
  122. */
  123. typedef enum
  124. {
  125. USART_INT_PE = 0x0010100, /*!< Parity error interrupt */
  126. USART_INT_TXBE = 0x7010080, /*!< Tansmit data buffer empty interrupt */
  127. USART_INT_TXC = 0x6010040, /*!< Transmission complete interrupt */
  128. USART_INT_RXBNE = 0x5010020, /*!< Receive data buffer not empty interrupt */
  129. USART_INT_IDLE = 0x4010010, /*!< Idle line detection interrupt */
  130. USART_INT_OVRE_RX = 0x3010008, /*!< OverRun Error interruptpt if the RXBNFLG bit is set */
  131. USART_INT_LBD = 0x8020040, /*!< LIN break detection interrupt */
  132. USART_INT_CTS = 0x9040400, /*!< CTS change interrupt */
  133. USART_INT_ERR = 0x0040001, /*!< Error interrupt(Frame error, noise error, overrun error) */
  134. USART_INT_OVRE_ER = 0x3040001, /*!< OverRun Error interruptpt if the EIE bit is set */
  135. USART_INT_NE = 0x2040001, /*!< Noise Error interrupt */
  136. USART_INT_FE = 0x1040001 /*!< Framing Error interrupt */
  137. } USART_INT_T;
  138. /**
  139. * @brief USART DMA enable
  140. */
  141. typedef enum
  142. {
  143. USART_DMA_RX = (uint8_t)0x01, /*!< USART DMA transmit request */
  144. USART_DMA_TX = (uint8_t)0x02, /*!< USART DMA receive request */
  145. USART_DMA_TX_RX = (uint8_t)0x03 /*!< USART DMA transmit/receive request */
  146. } USART_DMA_T;
  147. /**
  148. * @brief USART Wakeup method
  149. */
  150. typedef enum
  151. {
  152. USART_WAKEUP_IDLE_LINE, /*!< WakeUp by an idle line detection */
  153. USART_WAKEUP_ADDRESS_MARK /*!< WakeUp by an address mark */
  154. } USART_WAKEUP_T;
  155. /**
  156. * @brief USART LIN break detection length
  157. */
  158. typedef enum
  159. {
  160. USART_LBDL_10B, /*!< 10-bit break detection */
  161. USART_LBDL_11B /*!< 11-bit break detection */
  162. } USART_LBDL_T;
  163. /**
  164. * @brief USART IrDA low-power
  165. */
  166. typedef enum
  167. {
  168. USART_IRDALP_NORMAL, /*!< Normal */
  169. USART_IRDALP_LOWPOWER /*!< Low-Power */
  170. } USART_IRDALP_T;
  171. /**
  172. * @brief USART flag
  173. */
  174. typedef enum
  175. {
  176. USART_FLAG_CTS = 0x0200, /*!< CTS Change flag (not available for UART4 and UART5) */
  177. USART_FLAG_LBD = 0x0100, /*!< LIN Break detection flag */
  178. USART_FLAG_TXBE = 0x0080, /*!< Transmit data buffer empty flag */
  179. USART_FLAG_TXC = 0x0040, /*!< Transmission Complete flag */
  180. USART_FLAG_RXBNE = 0x0020, /*!< Receive data buffer not empty flag */
  181. USART_FLAG_IDLE = 0x0010, /*!< Idle Line detection flag */
  182. USART_FLAG_OVRE = 0x0008, /*!< OverRun Error flag */
  183. USART_FLAG_NE = 0x0004, /*!< Noise Error flag */
  184. USART_FLAG_FE = 0x0002, /*!< Framing Error flag */
  185. USART_FLAG_PE = 0x0001 /*!< Parity Error flag */
  186. } USART_FLAG_T;
  187. /**@} end of group USART_Enumerations*/
  188. /** @addtogroup USART_Structure Data Structure
  189. @{
  190. */
  191. /**
  192. * @brief USART Config struct definition
  193. */
  194. typedef struct
  195. {
  196. uint32_t baudRate; /*!< Specifies the baud rate */
  197. USART_WORD_LEN_T wordLength; /*!< Specifies the word length */
  198. USART_STOP_BIT_T stopBits; /*!< Specifies the stop bits */
  199. USART_PARITY_T parity; /*!< Specifies the parity */
  200. USART_MODE_T mode; /*!< Specifies the mode */
  201. USART_HARDWARE_FLOW_T hardwareFlow; /*!< Specifies the hardware flow control */
  202. } USART_Config_T;
  203. /**
  204. * @brief USART synchronous communication clock config struct definition
  205. */
  206. typedef struct
  207. {
  208. USART_CLKEN_T clock; /*!< Enable or Disable Clock */
  209. USART_CLKPOL_T polarity; /*!< Specifies the clock polarity */
  210. USART_CLKPHA_T phase; /*!< Specifies the clock phase */
  211. USART_LBCP_T lastBit; /*!< Enable or Disable last bit clock */
  212. } USART_ClockConfig_T;
  213. /**@} end of group USART_Structure*/
  214. /** @defgroup USART_Functions
  215. @{
  216. */
  217. /* USART Reset */
  218. void USART_Reset(USART_T* usart);
  219. /* USART Configuration */
  220. void USART_Config(USART_T* uart, USART_Config_T* usartConfig);
  221. void USART_ConfigStructInit(USART_Config_T* usartConfig);
  222. void USART_ConfigClock(USART_T* usart, USART_ClockConfig_T* clockConfig);
  223. void USART_ConfigClockStructInit(USART_ClockConfig_T* clockConfig);
  224. void USART_Enable(USART_T* usart);
  225. void USART_Disable(USART_T* usart);
  226. void USART_ConfigPrescaler(USART_T* usart, uint8_t div);
  227. void USART_EnableOverSampling8(USART_T* usart);
  228. void USART_DisableOverSampling8(USART_T* usart);
  229. void USART_EnableOverSampling(USART_T* usart);
  230. void USART_DisableOverSampling(USART_T* usart);
  231. /* Data transfers */
  232. void USART_TxData(USART_T* usart, uint16_t data);
  233. uint16_t USART_RxData(USART_T* usart);
  234. /* Multi-Processor Communication */
  235. void USART_Address(USART_T* usart, uint8_t address);
  236. void USART_EnableMuteMode(USART_T* usart);
  237. void USART_DisableMuteMode(USART_T* usart);
  238. void USART_ConfigWakeUp(USART_T* usart, USART_WAKEUP_T wakeup);
  239. /* LIN mode */
  240. void USART_ConfigLINBreakDetectLength(USART_T* usart, USART_LBDL_T length);
  241. void USART_EnableLIN(USART_T* usart);
  242. void USART_DisableLIN(USART_T* usart);
  243. void USART_TxBreak(USART_T* usart);
  244. /* Half-duplex mode */
  245. void USART_EnableHalfDuplex(USART_T* usart);
  246. void USART_DisableHalfDuplex(USART_T* usart);
  247. /* Smartcard mode */
  248. void USART_ConfigGuardTime(USART_T* usart, uint8_t guardTime);
  249. void USART_EnableSmartCard(USART_T* usart);
  250. void USART_DisableSmartCard(USART_T* usart);
  251. void USART_EnableSmartCardNACK(USART_T* usart);
  252. void USART_DisableSmartCardNACK(USART_T* usart);
  253. /* IrDA mode */
  254. void USART_ConfigIrDA(USART_T* usart, USART_IRDALP_T IrDAMode);
  255. void USART_EnableIrDA(USART_T* usart);
  256. void USART_DisableIrDA(USART_T* usart);
  257. /* DMA transfers management */
  258. void USART_EnableDMA(USART_T* usart, USART_DMA_T dmaReq);
  259. void USART_DisableDMA(USART_T* usart, USART_DMA_T dmaReq);
  260. /* Interrupts and flags management */
  261. void USART_EnableInterrupt(USART_T* usart, USART_INT_T interrupt);
  262. void USART_DisableInterrupt(USART_T* usart, USART_INT_T interrupt);
  263. uint8_t USART_ReadStatusFlag(USART_T* usart, USART_FLAG_T flag);
  264. void USART_ClearStatusFlag(USART_T* usart, USART_FLAG_T flag);
  265. uint8_t USART_ReadIntFlag(USART_T* usart, USART_INT_T flag);
  266. void USART_ClearIntFlag(USART_T* usart, USART_INT_T flag);
  267. #ifdef __cplusplus
  268. }
  269. #endif
  270. #endif /* __APM32F4XX_USART_H */
  271. /**@} end of group USART_Enumerations */
  272. /**@} end of group USART_Driver */
  273. /**@} end of group APM32F4xx_StdPeriphDriver */