py32f0xx_hal_uart.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_hal_uart.h
  4. * @author MCU Application Team
  5. * @brief Header file of UART HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. /* Define to prevent recursive inclusion -------------------------------------*/
  31. #ifndef __PY32F0xx_HAL_UART_H
  32. #define __PY32F0xx_HAL_UART_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "py32f0xx_hal_def.h"
  38. /** @addtogroup PY32F0xx_HAL_Driver
  39. * @{
  40. */
  41. /** @addtogroup UART
  42. * @{
  43. */
  44. /* Exported types ------------------------------------------------------------*/
  45. /** @defgroup UART_Exported_Types UART Exported Types
  46. * @{
  47. */
  48. /**
  49. * @brief UART Init Structure definition
  50. */
  51. typedef struct
  52. {
  53. uint32_t BaudRate; /*!< This member configures the UART communication baud rate.
  54. The baud rate is computed using the following formula:
  55. - IntegerDivider = ((PCLKx) / (16 * (huart->Init.BaudRate)))
  56. - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 16) + 0.5 */
  57. uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  58. This parameter can be a value of @ref UART_Word_Length */
  59. uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
  60. This parameter can be a value of @ref UART_Stop_Bits */
  61. uint32_t Parity; /*!< Specifies the parity mode.
  62. This parameter can be a value of @ref UART_Parity
  63. @note When parity is enabled, the computed parity is inserted
  64. at the MSB position of the transmitted data (9th bit when
  65. the word length is set to 9 data bits; 8th bit when the
  66. word length is set to 8 data bits). */
  67. uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
  68. This parameter can be a value of @ref UART_Mode */
  69. uint32_t HwFlowCtl; /*!< Specifies whether the hardware flow control mode is enabled or disabled.
  70. This parameter can be a value of @ref UART_Hardware_Flow_Control */
  71. uint32_t OverSampling; /*!< Specifies whether the Over sampling 8 or Over sampling 16.
  72. This parameter can be a value of @ref UART_Over_Sampling. */
  73. } UART_InitTypeDef;
  74. /**
  75. * @brief HAL UART State structures definition
  76. * @note HAL UART State value is a combination of 2 different substates: gState and RxState.
  77. * - gState contains UART state information related to global Handle management
  78. * and also information related to Tx operations.
  79. * gState value coding follow below described bitmap :
  80. * b7-b6 Error information
  81. * 00 : No Error
  82. * 01 : (Not Used)
  83. * 10 : Timeout
  84. * 11 : Error
  85. * b5 Peripheral initialization status
  86. * 0 : Reset (Peripheral not initialized)
  87. * 1 : Init done (Peripheral not initialized. HAL UART Init function already called)
  88. * b4-b3 (not used)
  89. * xx : Should be set to 00
  90. * b2 Intrinsic process state
  91. * 0 : Ready
  92. * 1 : Busy (Peripheral busy with some configuration or internal operations)
  93. * b1 (not used)
  94. * x : Should be set to 0
  95. * b0 Tx state
  96. * 0 : Ready (no Tx operation ongoing)
  97. * 1 : Busy (Tx operation ongoing)
  98. * - RxState contains information related to Rx operations.
  99. * RxState value coding follow below described bitmap :
  100. * b7-b6 (not used)
  101. * xx : Should be set to 00
  102. * b5 Peripheral initialization status
  103. * 0 : Reset (Peripheral not initialized)
  104. * 1 : Init done (Peripheral not initialized)
  105. * b4-b2 (not used)
  106. * xxx : Should be set to 000
  107. * b1 Rx state
  108. * 0 : Ready (no Rx operation ongoing)
  109. * 1 : Busy (Rx operation ongoing)
  110. * b0 (not used)
  111. * x : Should be set to 0.
  112. */
  113. typedef enum
  114. {
  115. HAL_UART_STATE_RESET = 0x00U, /*!< Peripheral is not yet Initialized
  116. Value is allowed for gState and RxState */
  117. HAL_UART_STATE_READY = 0x20U, /*!< Peripheral Initialized and ready for use
  118. Value is allowed for gState and RxState */
  119. HAL_UART_STATE_BUSY = 0x24U, /*!< an internal process is ongoing
  120. Value is allowed for gState only */
  121. HAL_UART_STATE_BUSY_TX = 0x21U, /*!< Data Transmission process is ongoing
  122. Value is allowed for gState only */
  123. HAL_UART_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing
  124. Value is allowed for RxState only */
  125. HAL_UART_STATE_BUSY_TX_RX = 0x23U, /*!< Data Transmission and Reception process is ongoing
  126. Not to be used for neither gState nor RxState.
  127. Value is result of combination (Or) between gState and RxState values */
  128. HAL_UART_STATE_TIMEOUT = 0xA0U, /*!< Timeout state
  129. Value is allowed for gState only */
  130. HAL_UART_STATE_ERROR = 0xE0U /*!< Error
  131. Value is allowed for gState only */
  132. } HAL_UART_StateTypeDef;
  133. /* @brief UART Advanced Features initialization structure definition */
  134. typedef struct
  135. {
  136. uint32_t AdvFeatureInit; /*!< Specifies which advanced UART features is initialized. Several
  137. Advanced Features may be initialized at the same time .
  138. This parameter can be a value of @ref UART_Advanced_Features_Initialization_Type. */
  139. uint32_t AutoBaudRateEnable; /*!< Specifies whether auto Baud rate detection is enabled.
  140. This parameter can be a value of @ref UART_AutoBaudRate_Enable. */
  141. uint32_t AutoBaudRateMode; /*!< If auto Baud rate detection is enabled, specifies how the rate
  142. detection is carried out.
  143. This parameter can be a value of @ref UART_AutoBaud_Rate_Mode. */
  144. } UART_AdvFeatureInitTypeDef;
  145. /**
  146. * @brief UART handle Structure definition
  147. */
  148. typedef struct __UART_HandleTypeDef
  149. {
  150. USART_TypeDef *Instance; /*!< UART registers base address */
  151. UART_InitTypeDef Init; /*!< UART communication parameters */
  152. UART_AdvFeatureInitTypeDef AdvancedInit; /*!< UART Advanced Features initialization parameters */
  153. uint8_t *pTxBuffPtr; /*!< Pointer to UART Tx transfer Buffer */
  154. uint16_t TxXferSize; /*!< UART Tx Transfer size */
  155. __IO uint16_t TxXferCount; /*!< UART Tx Transfer Counter */
  156. uint8_t *pRxBuffPtr; /*!< Pointer to UART Rx transfer Buffer */
  157. uint16_t RxXferSize; /*!< UART Rx Transfer size */
  158. __IO uint16_t RxXferCount; /*!< UART Rx Transfer Counter */
  159. #if (defined(DMA1) || defined(DMA))
  160. DMA_HandleTypeDef *hdmatx; /*!< UART Tx DMA Handle parameters */
  161. DMA_HandleTypeDef *hdmarx; /*!< UART Rx DMA Handle parameters */
  162. #endif /* DMA1 or DMA */
  163. HAL_LockTypeDef Lock; /*!< Locking object */
  164. __IO HAL_UART_StateTypeDef gState; /*!< UART state information related to global Handle management
  165. and also related to Tx operations.
  166. This parameter can be a value of @ref HAL_UART_StateTypeDef */
  167. __IO HAL_UART_StateTypeDef RxState; /*!< UART state information related to Rx operations.
  168. This parameter can be a value of @ref HAL_UART_StateTypeDef */
  169. __IO uint32_t ErrorCode; /*!< UART Error code */
  170. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  171. void (* TxHalfCpltCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Tx Half Complete Callback */
  172. void (* TxCpltCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Tx Complete Callback */
  173. void (* RxHalfCpltCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Rx Half Complete Callback */
  174. void (* RxCpltCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Rx Complete Callback */
  175. void (* ErrorCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Error Callback */
  176. void (* AbortCpltCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Abort Complete Callback */
  177. void (* AbortTransmitCpltCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Abort Transmit Complete Callback */
  178. void (* AbortReceiveCpltCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Abort Receive Complete Callback */
  179. void (* WakeupCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Wakeup Callback */
  180. void (* MspInitCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Msp Init callback */
  181. void (* MspDeInitCallback)(struct __UART_HandleTypeDef *huart); /*!< UART Msp DeInit callback */
  182. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  183. } UART_HandleTypeDef;
  184. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  185. /**
  186. * @brief HAL UART Callback ID enumeration definition
  187. */
  188. typedef enum
  189. {
  190. HAL_UART_TX_HALFCOMPLETE_CB_ID = 0x00U, /*!< UART Tx Half Complete Callback ID */
  191. HAL_UART_TX_COMPLETE_CB_ID = 0x01U, /*!< UART Tx Complete Callback ID */
  192. HAL_UART_RX_HALFCOMPLETE_CB_ID = 0x02U, /*!< UART Rx Half Complete Callback ID */
  193. HAL_UART_RX_COMPLETE_CB_ID = 0x03U, /*!< UART Rx Complete Callback ID */
  194. HAL_UART_ERROR_CB_ID = 0x04U, /*!< UART Error Callback ID */
  195. HAL_UART_ABORT_COMPLETE_CB_ID = 0x05U, /*!< UART Abort Complete Callback ID */
  196. HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID = 0x06U, /*!< UART Abort Transmit Complete Callback ID */
  197. HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID = 0x07U, /*!< UART Abort Receive Complete Callback ID */
  198. HAL_UART_WAKEUP_CB_ID = 0x08U, /*!< UART Wakeup Callback ID */
  199. HAL_UART_MSPINIT_CB_ID = 0x0BU, /*!< UART MspInit callback ID */
  200. HAL_UART_MSPDEINIT_CB_ID = 0x0CU /*!< UART MspDeInit callback ID */
  201. } HAL_UART_CallbackIDTypeDef;
  202. /**
  203. * @brief HAL UART Callback pointer definition
  204. */
  205. typedef void (*pUART_CallbackTypeDef)(UART_HandleTypeDef *huart); /*!< pointer to an UART callback function */
  206. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  207. /**
  208. * @}
  209. */
  210. /* Exported constants --------------------------------------------------------*/
  211. /** @defgroup UART_Exported_Constants UART Exported Constants
  212. * @{
  213. */
  214. /** @defgroup UART_Error_Code UART Error Code
  215. * @{
  216. */
  217. #define HAL_UART_ERROR_NONE 0x00000000U /*!< No error */
  218. #define HAL_UART_ERROR_PE 0x00000001U /*!< Parity error */
  219. #define HAL_UART_ERROR_NE 0x00000002U /*!< Noise error */
  220. #define HAL_UART_ERROR_FE 0x00000004U /*!< Frame error */
  221. #define HAL_UART_ERROR_ORE 0x00000008U /*!< Overrun error */
  222. #if (defined(DMA1) || defined(DMA))
  223. #define HAL_UART_ERROR_DMA 0x00000010U /*!< DMA transfer error */
  224. #endif /* DMA1 or DMA */
  225. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  226. #define HAL_UART_ERROR_INVALID_CALLBACK 0x00000020U /*!< Invalid Callback error */
  227. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  228. /**
  229. * @}
  230. */
  231. /** @defgroup UART_Word_Length UART Word Length
  232. * @{
  233. */
  234. #define UART_WORDLENGTH_8B 0x00000000U
  235. #define UART_WORDLENGTH_9B ((uint32_t)USART_CR1_M)
  236. /**
  237. * @}
  238. */
  239. /** @defgroup UART_Stop_Bits UART Number of Stop Bits
  240. * @{
  241. */
  242. #define UART_STOPBITS_1 0x00000000U
  243. #define UART_STOPBITS_2 ((uint32_t)USART_CR2_STOP)
  244. /**
  245. * @}
  246. */
  247. /** @defgroup UART_Parity UART Parity
  248. * @{
  249. */
  250. #define UART_PARITY_NONE 0x00000000U
  251. #define UART_PARITY_EVEN ((uint32_t)USART_CR1_PCE)
  252. #define UART_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
  253. /**
  254. * @}
  255. */
  256. /** @defgroup UART_Hardware_Flow_Control UART Hardware Flow Control
  257. * @{
  258. */
  259. #define UART_HWCONTROL_NONE 0x00000000U
  260. #define UART_HWCONTROL_RTS ((uint32_t)USART_CR3_RTSE)
  261. #define UART_HWCONTROL_CTS ((uint32_t)USART_CR3_CTSE)
  262. #define UART_HWCONTROL_RTS_CTS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
  263. /**
  264. * @}
  265. */
  266. /** @defgroup UART_Mode UART Transfer Mode
  267. * @{
  268. */
  269. #define UART_MODE_RX ((uint32_t)USART_CR1_RE)
  270. #define UART_MODE_TX ((uint32_t)USART_CR1_TE)
  271. #define UART_MODE_TX_RX ((uint32_t)(USART_CR1_TE | USART_CR1_RE))
  272. /**
  273. * @}
  274. */
  275. /** @defgroup UART_State UART State
  276. * @{
  277. */
  278. #define UART_STATE_DISABLE 0x00000000U
  279. #define UART_STATE_ENABLE ((uint32_t)USART_CR1_UE)
  280. /**
  281. * @}
  282. */
  283. /** @defgroup UART_Over_Sampling UART Over Sampling
  284. * @{
  285. */
  286. #define UART_OVERSAMPLING_16 0x00000000U
  287. #if defined(USART_CR3_OVER8)
  288. #define UART_OVERSAMPLING_8 ((uint32_t)USART_CR3_OVER8)
  289. #endif /* USART_CR3_OVER8 */
  290. /**
  291. * @}
  292. */
  293. /** @defgroup UART_LIN_Break_Detection_Length UART LIN Break Detection Length
  294. * @{
  295. */
  296. #define UART_LINBREAKDETECTLENGTH_10B 0x00000000U
  297. #define UART_LINBREAKDETECTLENGTH_11B ((uint32_t)USART_CR2_LBDL)
  298. /**
  299. * @}
  300. */
  301. /** @defgroup UART_WakeUp_functions UART Wakeup Functions
  302. * @{
  303. */
  304. #define UART_WAKEUPMETHOD_IDLELINE 0x00000000U
  305. #define UART_WAKEUPMETHOD_ADDRESSMARK ((uint32_t)USART_CR1_WAKE)
  306. /**
  307. * @}
  308. */
  309. /** @defgroup UART_Flags UART FLags
  310. * Elements values convention: 0xXXXX
  311. * - 0xXXXX : Flag mask in the SR register
  312. * @{
  313. */
  314. #define UART_FLAG_ABRF ((uint32_t)USART_SR_ABRF) /*!< UART auto Baud rate flag */
  315. #define UART_FLAG_ABRE ((uint32_t)USART_SR_ABRE) /*!< UART auto Baud rate error */
  316. #define UART_FLAG_CTS ((uint32_t)USART_SR_CTS)
  317. #define UART_FLAG_TXE ((uint32_t)USART_SR_TXE)
  318. #define UART_FLAG_TC ((uint32_t)USART_SR_TC)
  319. #define UART_FLAG_RXNE ((uint32_t)USART_SR_RXNE)
  320. #define UART_FLAG_IDLE ((uint32_t)USART_SR_IDLE)
  321. #define UART_FLAG_ORE ((uint32_t)USART_SR_ORE)
  322. #define UART_FLAG_NE ((uint32_t)USART_SR_NE)
  323. #define UART_FLAG_FE ((uint32_t)USART_SR_FE)
  324. #define UART_FLAG_PE ((uint32_t)USART_SR_PE)
  325. /**
  326. * @}
  327. */
  328. /** @defgroup UART_Interrupt_definition UART Interrupt Definitions
  329. * Elements values convention: 0xY000XXXX
  330. * - XXXX : Interrupt mask (16 bits) in the Y register
  331. * - Y : Interrupt source register (2bits)
  332. * - 0001: CR1 register
  333. * - 0010: CR2 register
  334. * - 0011: CR3 register
  335. * @{
  336. */
  337. #define UART_IT_PE ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_PEIE))
  338. #define UART_IT_TXE ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_TXEIE))
  339. #define UART_IT_TC ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_TCIE))
  340. #define UART_IT_RXNE ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_RXNEIE))
  341. #define UART_IT_IDLE ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_IDLEIE))
  342. #define UART_IT_CTS ((uint32_t)(UART_CR3_REG_INDEX << 28U | USART_CR3_CTSIE))
  343. #define UART_IT_ERR ((uint32_t)(UART_CR3_REG_INDEX << 28U | USART_CR3_EIE))
  344. /**
  345. * @}
  346. */
  347. /** @defgroup UART_Advanced_Features_Initialization_Type UART Advanced Feature Initialization Type
  348. * @{
  349. */
  350. #define UART_ADVFEATURE_NO_INIT 0x00000000U /*!< No advanced feature initialization */
  351. #define UART_ADVFEATURE_AUTOBAUDRATE_INIT 0x00000040U /*!< Auto Baud rate detection initialization */
  352. /**
  353. * @}
  354. */
  355. /** @defgroup UART_AutoBaudRate_Enable UART Advanced Feature Auto BaudRate Enable
  356. * @{
  357. */
  358. #define UART_ADVFEATURE_AUTOBAUDRATE_DISABLE 0x00000000U /*!< RX Auto Baud rate detection enable */
  359. #define UART_ADVFEATURE_AUTOBAUDRATE_ENABLE USART_CR3_ABREN /*!< RX Auto Baud rate detection disable */
  360. /**
  361. * @}
  362. */
  363. /** @defgroup UART_AutoBaud_Rate_Mode UART Advanced Feature AutoBaud Rate Mode
  364. * @{
  365. */
  366. #define UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT 0x00000000U /*!< Auto Baud rate detection on start bit */
  367. #define UART_ADVFEATURE_AUTOBAUDRATE_ONFALLINGEDGE USART_CR3_ABRMODE_0 /*!< Auto Baud rate detection on falling edge */
  368. /**
  369. * @}
  370. */
  371. /**
  372. * @}
  373. */
  374. /* Exported macro ------------------------------------------------------------*/
  375. /** @defgroup UART_Exported_Macros UART Exported Macros
  376. * @{
  377. */
  378. /** @brief Reset UART handle gstate & RxState
  379. * @param __HANDLE__ specifies the UART Handle.
  380. * UART Handle selects the USARTx or UARTy peripheral
  381. * (USART,UART availability and x,y values depending on device).
  382. * @retval None
  383. */
  384. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  385. #define __HAL_UART_RESET_HANDLE_STATE(__HANDLE__) do{ \
  386. (__HANDLE__)->gState = HAL_UART_STATE_RESET; \
  387. (__HANDLE__)->RxState = HAL_UART_STATE_RESET; \
  388. (__HANDLE__)->MspInitCallback = NULL; \
  389. (__HANDLE__)->MspDeInitCallback = NULL; \
  390. } while(0U)
  391. #else
  392. #define __HAL_UART_RESET_HANDLE_STATE(__HANDLE__) do{ \
  393. (__HANDLE__)->gState = HAL_UART_STATE_RESET; \
  394. (__HANDLE__)->RxState = HAL_UART_STATE_RESET; \
  395. } while(0U)
  396. #endif /*USE_HAL_UART_REGISTER_CALLBACKS */
  397. /** @brief Flushes the UART DR register
  398. * @param __HANDLE__ specifies the UART Handle.
  399. * UART Handle selects the USARTx or UARTy peripheral
  400. * (USART,UART availability and x,y values depending on device).
  401. */
  402. #define __HAL_UART_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
  403. /** @brief Checks whether the specified UART flag is set or not.
  404. * @param __HANDLE__ specifies the UART Handle.
  405. * UART Handle selects the USARTx or UARTy peripheral
  406. * (USART,UART availability and x,y values depending on device).
  407. * @param __FLAG__ specifies the flag to check.
  408. * This parameter can be one of the following values:
  409. * @arg UART_FLAG_CTS: CTS Change flag
  410. * @arg UART_FLAG_TXE: Transmit data register empty flag
  411. * @arg UART_FLAG_TC: Transmission Complete flag
  412. * @arg UART_FLAG_RXNE: Receive data register not empty flag
  413. * @arg UART_FLAG_IDLE: Idle Line detection flag
  414. * @arg UART_FLAG_ORE: Overrun Error flag
  415. * @arg UART_FLAG_NE: Noise Error flag
  416. * @arg UART_FLAG_FE: Framing Error flag
  417. * @arg UART_FLAG_PE: Parity Error flag
  418. * @retval The new state of __FLAG__ (TRUE or FALSE).
  419. */
  420. #define __HAL_UART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
  421. /** @brief Clears the specified UART pending flag.
  422. * @param __HANDLE__ specifies the UART Handle.
  423. * UART Handle selects the USARTx or UARTy peripheral
  424. * (USART,UART availability and x,y values depending on device).
  425. * @param __FLAG__ specifies the flag to check.
  426. * This parameter can be any combination of the following values:
  427. * @arg UART_FLAG_CTS: CTS Change flag.
  428. * @arg UART_FLAG_TC: Transmission Complete flag.
  429. * @arg UART_FLAG_RXNE: Receive data register not empty flag.
  430. *
  431. * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (Overrun
  432. * error) and IDLE (Idle line detected) flags are cleared by software
  433. * sequence: a read operation to USART_SR register followed by a read
  434. * operation to USART_DR register.
  435. * @note RXNE flag can be also cleared by a read to the USART_DR register.
  436. * @note TC flag can be also cleared by software sequence: a read operation to
  437. * USART_SR register followed by a write operation to USART_DR register.
  438. * @note TXE flag is cleared only by a write to the USART_DR register.
  439. *
  440. * @retval None
  441. */
  442. #define __HAL_UART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
  443. /** @brief Clears the UART PE pending flag.
  444. * @param __HANDLE__ specifies the UART Handle.
  445. * UART Handle selects the USARTx or UARTy peripheral
  446. * (USART,UART availability and x,y values depending on device).
  447. * @retval None
  448. */
  449. #define __HAL_UART_CLEAR_PEFLAG(__HANDLE__) \
  450. do{ \
  451. __IO uint32_t tmpreg = 0x00U; \
  452. tmpreg = (__HANDLE__)->Instance->SR; \
  453. tmpreg = (__HANDLE__)->Instance->DR; \
  454. UNUSED(tmpreg); \
  455. } while(0U)
  456. /** @brief Clears the UART FE pending flag.
  457. * @param __HANDLE__ specifies the UART Handle.
  458. * UART Handle selects the USARTx or UARTy peripheral
  459. * (USART,UART availability and x,y values depending on device).
  460. * @retval None
  461. */
  462. #define __HAL_UART_CLEAR_FEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  463. /** @brief Clears the UART NE pending flag.
  464. * @param __HANDLE__ specifies the UART Handle.
  465. * UART Handle selects the USARTx or UARTy peripheral
  466. * (USART,UART availability and x,y values depending on device).
  467. * @retval None
  468. */
  469. #define __HAL_UART_CLEAR_NEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  470. /** @brief Clears the UART ORE pending flag.
  471. * @param __HANDLE__ specifies the UART Handle.
  472. * UART Handle selects the USARTx or UARTy peripheral
  473. * (USART,UART availability and x,y values depending on device).
  474. * @retval None
  475. */
  476. #define __HAL_UART_CLEAR_OREFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  477. /** @brief Clears the UART IDLE pending flag.
  478. * @param __HANDLE__ specifies the UART Handle.
  479. * UART Handle selects the USARTx or UARTy peripheral
  480. * (USART,UART availability and x,y values depending on device).
  481. * @retval None
  482. */
  483. #define __HAL_UART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  484. /** @brief Enable the specified UART interrupt.
  485. * @param __HANDLE__ specifies the UART Handle.
  486. * UART Handle selects the USARTx or UARTy peripheral
  487. * (USART,UART availability and x,y values depending on device).
  488. * @param __INTERRUPT__ specifies the UART interrupt source to enable.
  489. * This parameter can be one of the following values:
  490. * @arg UART_IT_CTS: CTS change interrupt
  491. * @arg UART_IT_LBD: LIN Break detection interrupt
  492. * @arg UART_IT_TXE: Transmit Data Register empty interrupt
  493. * @arg UART_IT_TC: Transmission complete interrupt
  494. * @arg UART_IT_RXNE: Receive Data register not empty interrupt
  495. * @arg UART_IT_IDLE: Idle line detection interrupt
  496. * @arg UART_IT_PE: Parity Error interrupt
  497. * @arg UART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  498. * @retval None
  499. */
  500. #define __HAL_UART_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == UART_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & UART_IT_MASK)): \
  501. (((__INTERRUPT__) >> 28U) == UART_CR2_REG_INDEX)? ((__HANDLE__)->Instance->CR2 |= ((__INTERRUPT__) & UART_IT_MASK)): \
  502. ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & UART_IT_MASK)))
  503. /** @brief Disable the specified UART interrupt.
  504. * @param __HANDLE__ specifies the UART Handle.
  505. * UART Handle selects the USARTx or UARTy peripheral
  506. * (USART,UART availability and x,y values depending on device).
  507. * @param __INTERRUPT__ specifies the UART interrupt source to disable.
  508. * This parameter can be one of the following values:
  509. * @arg UART_IT_CTS: CTS change interrupt
  510. * @arg UART_IT_LBD: LIN Break detection interrupt
  511. * @arg UART_IT_TXE: Transmit Data Register empty interrupt
  512. * @arg UART_IT_TC: Transmission complete interrupt
  513. * @arg UART_IT_RXNE: Receive Data register not empty interrupt
  514. * @arg UART_IT_IDLE: Idle line detection interrupt
  515. * @arg UART_IT_PE: Parity Error interrupt
  516. * @arg UART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  517. * @retval None
  518. */
  519. #define __HAL_UART_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == UART_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
  520. (((__INTERRUPT__) >> 28U) == UART_CR2_REG_INDEX)? ((__HANDLE__)->Instance->CR2 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
  521. ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & UART_IT_MASK)))
  522. /** @brief Checks whether the specified UART interrupt has occurred or not.
  523. * @param __HANDLE__ specifies the UART Handle.
  524. * UART Handle selects the USARTx or UARTy peripheral
  525. * (USART,UART availability and x,y values depending on device).
  526. * @param __IT__ specifies the UART interrupt source to check.
  527. * This parameter can be one of the following values:
  528. * @arg UART_IT_CTS: CTS change interrupt
  529. * @arg UART_IT_LBD: LIN Break detection interrupt
  530. * @arg UART_IT_TXE: Transmit Data Register empty interrupt
  531. * @arg UART_IT_TC: Transmission complete interrupt
  532. * @arg UART_IT_RXNE: Receive Data register not empty interrupt
  533. * @arg UART_IT_IDLE: Idle line detection interrupt
  534. * @arg UART_IT_ERR: Error interrupt
  535. * @retval The new state of __IT__ (TRUE or FALSE).
  536. */
  537. #define __HAL_UART_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28U) == UART_CR1_REG_INDEX)? (__HANDLE__)->Instance->CR1:(((((uint32_t)(__IT__)) >> 28U) == UART_CR2_REG_INDEX)? \
  538. (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & (((uint32_t)(__IT__)) & UART_IT_MASK))
  539. /** @brief Enable CTS flow control
  540. * @note This macro allows to enable CTS hardware flow control for a given UART instance,
  541. * without need to call HAL_UART_Init() function.
  542. * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
  543. * @note As macro is expected to be used for modifying CTS Hw flow control feature activation, without need
  544. * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
  545. * - UART instance should have already been initialised (through call of HAL_UART_Init() )
  546. * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
  547. * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
  548. * @param __HANDLE__ specifies the UART Handle.
  549. * The Handle Instance can be any USARTx (supporting the HW Flow control feature).
  550. * It is used to select the USART peripheral (USART availability and x value depending on device).
  551. * @retval None
  552. */
  553. #define __HAL_UART_HWCONTROL_CTS_ENABLE(__HANDLE__) \
  554. do{ \
  555. SET_BIT((__HANDLE__)->Instance->CR3, USART_CR3_CTSE); \
  556. (__HANDLE__)->Init.HwFlowCtl |= USART_CR3_CTSE; \
  557. } while(0U)
  558. /** @brief Disable CTS flow control
  559. * @note This macro allows to disable CTS hardware flow control for a given UART instance,
  560. * without need to call HAL_UART_Init() function.
  561. * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
  562. * @note As macro is expected to be used for modifying CTS Hw flow control feature activation, without need
  563. * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
  564. * - UART instance should have already been initialised (through call of HAL_UART_Init() )
  565. * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
  566. * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
  567. * @param __HANDLE__ specifies the UART Handle.
  568. * The Handle Instance can be any USARTx (supporting the HW Flow control feature).
  569. * It is used to select the USART peripheral (USART availability and x value depending on device).
  570. * @retval None
  571. */
  572. #define __HAL_UART_HWCONTROL_CTS_DISABLE(__HANDLE__) \
  573. do{ \
  574. CLEAR_BIT((__HANDLE__)->Instance->CR3, USART_CR3_CTSE); \
  575. (__HANDLE__)->Init.HwFlowCtl &= ~(USART_CR3_CTSE); \
  576. } while(0U)
  577. /** @brief Enable RTS flow control
  578. * This macro allows to enable RTS hardware flow control for a given UART instance,
  579. * without need to call HAL_UART_Init() function.
  580. * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
  581. * @note As macro is expected to be used for modifying RTS Hw flow control feature activation, without need
  582. * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
  583. * - UART instance should have already been initialised (through call of HAL_UART_Init() )
  584. * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
  585. * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
  586. * @param __HANDLE__ specifies the UART Handle.
  587. * The Handle Instance can be any USARTx (supporting the HW Flow control feature).
  588. * It is used to select the USART peripheral (USART availability and x value depending on device).
  589. * @retval None
  590. */
  591. #define __HAL_UART_HWCONTROL_RTS_ENABLE(__HANDLE__) \
  592. do{ \
  593. SET_BIT((__HANDLE__)->Instance->CR3, USART_CR3_RTSE); \
  594. (__HANDLE__)->Init.HwFlowCtl |= USART_CR3_RTSE; \
  595. } while(0U)
  596. /** @brief Disable RTS flow control
  597. * This macro allows to disable RTS hardware flow control for a given UART instance,
  598. * without need to call HAL_UART_Init() function.
  599. * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
  600. * @note As macro is expected to be used for modifying RTS Hw flow control feature activation, without need
  601. * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
  602. * - UART instance should have already been initialised (through call of HAL_UART_Init() )
  603. * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
  604. * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
  605. * @param __HANDLE__ specifies the UART Handle.
  606. * The Handle Instance can be any USARTx (supporting the HW Flow control feature).
  607. * It is used to select the USART peripheral (USART availability and x value depending on device).
  608. * @retval None
  609. */
  610. #define __HAL_UART_HWCONTROL_RTS_DISABLE(__HANDLE__) \
  611. do{ \
  612. CLEAR_BIT((__HANDLE__)->Instance->CR3, USART_CR3_RTSE);\
  613. (__HANDLE__)->Init.HwFlowCtl &= ~(USART_CR3_RTSE); \
  614. } while(0U)
  615. #if defined(USART_CR3_ONEBIT)
  616. /** @brief Macro to enable the UART's one bit sample method
  617. * @param __HANDLE__ specifies the UART Handle.
  618. * @retval None
  619. */
  620. #define __HAL_UART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
  621. /** @brief Macro to disable the UART's one bit sample method
  622. * @param __HANDLE__ specifies the UART Handle.
  623. * @retval None
  624. */
  625. #define __HAL_UART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT))
  626. #endif /* UART_ONE_BIT_SAMPLE_Feature */
  627. /** @brief Enable UART
  628. * @param __HANDLE__ specifies the UART Handle.
  629. * @retval None
  630. */
  631. #define __HAL_UART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
  632. /** @brief Disable UART
  633. * @param __HANDLE__ specifies the UART Handle.
  634. * @retval None
  635. */
  636. #define __HAL_UART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
  637. /** @brief Send Auto-Baud Rate Request.
  638. * @param __HANDLE__ specifies the UART Handle.
  639. * @retval None
  640. */
  641. #define __HAL_UART_SEND_AUTOBAUD_REQ(__HANDLE__) ((__HANDLE__)->Instance->SR |= (uint16_t)(USART_SR_ABRRQ))
  642. /**
  643. * @}
  644. */
  645. /* Exported functions --------------------------------------------------------*/
  646. /** @addtogroup UART_Exported_Functions
  647. * @{
  648. */
  649. /** @addtogroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  650. * @{
  651. */
  652. /* Initialization/de-initialization functions **********************************/
  653. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart);
  654. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart);
  655. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod);
  656. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart);
  657. void HAL_UART_MspInit(UART_HandleTypeDef *huart);
  658. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart);
  659. /* Callbacks Register/UnRegister functions ***********************************/
  660. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  661. HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID, pUART_CallbackTypeDef pCallback);
  662. HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID);
  663. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  664. /**
  665. * @}
  666. */
  667. /** @addtogroup UART_Exported_Functions_Group2 IO operation functions
  668. * @{
  669. */
  670. /* IO operation functions *******************************************************/
  671. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  672. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  673. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  674. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  675. #if (defined(DMA1) || defined(DMA))
  676. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  677. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  678. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart);
  679. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart);
  680. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart);
  681. #endif /* DMA1 or DMA */
  682. /* Transfer Abort functions */
  683. HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart);
  684. HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart);
  685. HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart);
  686. HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart);
  687. HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart);
  688. HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart);
  689. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart);
  690. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
  691. void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart);
  692. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
  693. void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart);
  694. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart);
  695. void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart);
  696. void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart);
  697. void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart);
  698. void HAL_UART_IdleFrameDetectCpltCallback(UART_HandleTypeDef *huart);
  699. /**
  700. * @}
  701. */
  702. /** @addtogroup UART_Exported_Functions_Group3
  703. * @{
  704. */
  705. /* Peripheral Control functions ************************************************/
  706. HAL_StatusTypeDef HAL_UART_SendBreak(UART_HandleTypeDef *huart);
  707. HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart);
  708. HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart);
  709. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart);
  710. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart);
  711. /**
  712. * @}
  713. */
  714. /** @addtogroup UART_Exported_Functions_Group4
  715. * @{
  716. */
  717. /* Peripheral State functions **************************************************/
  718. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart);
  719. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart);
  720. /**
  721. * @}
  722. */
  723. /**
  724. * @}
  725. */
  726. /* Private types -------------------------------------------------------------*/
  727. /* Private variables ---------------------------------------------------------*/
  728. /* Private constants ---------------------------------------------------------*/
  729. /** @defgroup UART_Private_Constants UART Private Constants
  730. * @{
  731. */
  732. /** @brief UART interruptions flag mask
  733. *
  734. */
  735. #define UART_IT_MASK 0x0000FFFFU
  736. #define UART_CR1_REG_INDEX 1U
  737. #define UART_CR2_REG_INDEX 2U
  738. #define UART_CR3_REG_INDEX 3U
  739. /**
  740. * @}
  741. */
  742. /* Private macros ------------------------------------------------------------*/
  743. /** @defgroup UART_Private_Macros UART Private Macros
  744. * @{
  745. */
  746. #define IS_UART_WORD_LENGTH(LENGTH) (((LENGTH) == UART_WORDLENGTH_8B) || \
  747. ((LENGTH) == UART_WORDLENGTH_9B))
  748. #define IS_UART_LIN_WORD_LENGTH(LENGTH) (((LENGTH) == UART_WORDLENGTH_8B))
  749. #define IS_UART_STOPBITS(STOPBITS) (((STOPBITS) == UART_STOPBITS_1) || \
  750. ((STOPBITS) == UART_STOPBITS_2))
  751. #define IS_UART_PARITY(PARITY) (((PARITY) == UART_PARITY_NONE) || \
  752. ((PARITY) == UART_PARITY_EVEN) || \
  753. ((PARITY) == UART_PARITY_ODD))
  754. #define IS_UART_HARDWARE_FLOW_CONTROL(CONTROL)\
  755. (((CONTROL) == UART_HWCONTROL_NONE) || \
  756. ((CONTROL) == UART_HWCONTROL_RTS) || \
  757. ((CONTROL) == UART_HWCONTROL_CTS) || \
  758. ((CONTROL) == UART_HWCONTROL_RTS_CTS))
  759. #define IS_UART_MODE(MODE) ((((MODE) & (~((uint32_t)UART_MODE_TX_RX))) == 0x00U) && ((MODE) != 0x00U))
  760. #define IS_UART_STATE(STATE) (((STATE) == UART_STATE_DISABLE) || \
  761. ((STATE) == UART_STATE_ENABLE))
  762. #if defined(USART_CR3_OVER8)
  763. #define IS_UART_OVERSAMPLING(SAMPLING) (((SAMPLING) == UART_OVERSAMPLING_16) || \
  764. ((SAMPLING) == UART_OVERSAMPLING_8))
  765. #endif /* USART_CR3_OVER8 */
  766. #define IS_UART_LIN_OVERSAMPLING(SAMPLING) (((SAMPLING) == UART_OVERSAMPLING_16))
  767. #define IS_UART_LIN_BREAK_DETECT_LENGTH(LENGTH) (((LENGTH) == UART_LINBREAKDETECTLENGTH_10B) || \
  768. ((LENGTH) == UART_LINBREAKDETECTLENGTH_11B))
  769. #define IS_UART_WAKEUPMETHOD(WAKEUP) (((WAKEUP) == UART_WAKEUPMETHOD_IDLELINE) || \
  770. ((WAKEUP) == UART_WAKEUPMETHOD_ADDRESSMARK))
  771. #define IS_UART_BAUDRATE(BAUDRATE) ((BAUDRATE) <= 4500000U)
  772. #define IS_UART_ADDRESS(ADDRESS) ((ADDRESS) <= 0x0FU)
  773. #define IS_UART_ADVFEATURE_INIT(__PARAM__) (((__PARAM__) == UART_ADVFEATURE_NO_INIT) || \
  774. ((__PARAM__) == UART_ADVFEATURE_AUTOBAUDRATE_INIT))
  775. #define IS_UART_ADVFEATURE_AUTOBAUDRATE(__PARAM__) (((__PARAM__) == UART_ADVFEATURE_AUTOBAUDRATE_DISABLE) || \
  776. ((__PARAM__) == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE))
  777. #define IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(__PARAM__) (((__PARAM__) == UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT) || \
  778. ((__PARAM__) == UART_ADVFEATURE_AUTOBAUDRATE_ONFALLINGEDGE))
  779. #define UART_DIV_SAMPLING16(_PCLK_, _BAUD_) (((_PCLK_)*25U)/(4U*(_BAUD_)))
  780. #define UART_DIVMANT_SAMPLING16(_PCLK_, _BAUD_) (UART_DIV_SAMPLING16((_PCLK_), (_BAUD_))/100U)
  781. #define UART_DIVFRAQ_SAMPLING16(_PCLK_, _BAUD_) (((UART_DIV_SAMPLING16((_PCLK_), (_BAUD_)) - (UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) * 100U)) * 16U + 50U) / 100U)
  782. /* UART BRR = mantissa + overflow + fraction
  783. = (UART DIVMANT << 4) + (UART DIVFRAQ & 0xF0) + (UART DIVFRAQ & 0x0FU) */
  784. #define UART_BRR_SAMPLING16(_PCLK_, _BAUD_) (((UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) << 4U) + \
  785. (UART_DIVFRAQ_SAMPLING16((_PCLK_), (_BAUD_)) & 0xF0U)) + \
  786. (UART_DIVFRAQ_SAMPLING16((_PCLK_), (_BAUD_)) & 0x0FU))
  787. #define UART_DIV_SAMPLING8(_PCLK_, _BAUD_) (((_PCLK_)*25U)/(2U*(_BAUD_)))
  788. #define UART_DIVMANT_SAMPLING8(_PCLK_, _BAUD_) (UART_DIV_SAMPLING8((_PCLK_), (_BAUD_))/100U)
  789. #define UART_DIVFRAQ_SAMPLING8(_PCLK_, _BAUD_) (((UART_DIV_SAMPLING8((_PCLK_), (_BAUD_)) - (UART_DIVMANT_SAMPLING8((_PCLK_), (_BAUD_)) * 100U)) * 8U + 50U) / 100U)
  790. /* UART BRR = mantissa + overflow + fraction
  791. = (UART DIVMANT << 4) + ((UART DIVFRAQ & 0xF8) << 1) + (UART DIVFRAQ & 0x07U) */
  792. #define UART_BRR_SAMPLING8(_PCLK_, _BAUD_) (((UART_DIVMANT_SAMPLING8((_PCLK_), (_BAUD_)) << 4U) + \
  793. ((UART_DIVFRAQ_SAMPLING8((_PCLK_), (_BAUD_)) & 0xF8U) << 1U)) + \
  794. (UART_DIVFRAQ_SAMPLING8((_PCLK_), (_BAUD_)) & 0x07U))
  795. /**
  796. * @}
  797. */
  798. /**
  799. * @}
  800. */
  801. /**
  802. * @}
  803. */
  804. #ifdef __cplusplus
  805. }
  806. #endif
  807. #endif /* __PY32F0xx_HAL_UART_H */
  808. /************************ (C) COPYRIGHT Puya *****END OF FILE****/