stm32l4xx_hal_usart_ex.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_usart_ex.h
  4. * @author MCD Application Team
  5. * @brief Header file of USART HAL Extended module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Define to prevent recursive inclusion -------------------------------------*/
  36. #ifndef __STM32L4xx_HAL_USART_EX_H
  37. #define __STM32L4xx_HAL_USART_EX_H
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32l4xx_hal_def.h"
  43. /** @addtogroup STM32L4xx_HAL_Driver
  44. * @{
  45. */
  46. /** @addtogroup USARTEx
  47. * @{
  48. */
  49. /* Exported types ------------------------------------------------------------*/
  50. /* Exported constants --------------------------------------------------------*/
  51. /** @defgroup USARTEx_Exported_Constants USARTEx Exported Constants
  52. * @{
  53. */
  54. /** @defgroup USARTEx_Word_Length USARTEx Word Length
  55. * @{
  56. */
  57. #define USART_WORDLENGTH_7B ((uint32_t)USART_CR1_M1) /*!< 7-bit long USART frame */
  58. #define USART_WORDLENGTH_8B 0x00000000U /*!< 8-bit long USART frame */
  59. #define USART_WORDLENGTH_9B ((uint32_t)USART_CR1_M0) /*!< 9-bit long USART frame */
  60. /**
  61. * @}
  62. */
  63. #if defined(USART_CR2_SLVEN)
  64. /** @defgroup USARTEx_Slave_Select_management USARTEx Slave Select Management
  65. * @{
  66. */
  67. #define USART_NSS_HARD 0x00000000U /*!< SPI slave selection depends on NSS input pin */
  68. #define USART_NSS_SOFT USART_CR2_DIS_NSS /*!< SPI slave is always selected and NSS input pin is ignored */
  69. /**
  70. * @}
  71. */
  72. #endif
  73. #if defined(USART_CR1_FIFOEN)
  74. /** @defgroup USARTEx_TXFIFO_threshold_level USARTEx TXFIFO threshold level
  75. * @brief USART TXFIFO level
  76. * @{
  77. */
  78. #define USART_TXFIFO_THRESHOLD_1_8 0x00000000U /*!< TXFIFO reaches 1/8 of its depth */
  79. #define USART_TXFIFO_THRESHOLD_1_4 USART_CR3_TXFTCFG_0 /*!< TXFIFO reaches 1/4 of its depth */
  80. #define USART_TXFIFO_THRESHOLD_1_2 USART_CR3_TXFTCFG_1 /*!< TXFIFO reaches 1/2 of its depth */
  81. #define USART_TXFIFO_THRESHOLD_3_4 (USART_CR3_TXFTCFG_0|USART_CR3_TXFTCFG_1) /*!< TXFIFO reaches 3/4 of its depth */
  82. #define USART_TXFIFO_THRESHOLD_7_8 USART_CR3_TXFTCFG_2 /*!< TXFIFO reaches 7/8 of its depth */
  83. #define USART_TXFIFO_THRESHOLD_8_8 (USART_CR3_TXFTCFG_2|USART_CR3_TXFTCFG_0) /*!< TXFIFO becomes empty */
  84. /**
  85. * @}
  86. */
  87. /** @defgroup USARTEx_RXFIFO_threshold_level USARTEx RXFIFO threshold level
  88. * @brief USART RXFIFO level
  89. * @{
  90. */
  91. #define USART_RXFIFO_THRESHOLD_1_8 0x00000000U /*!< RXFIFO FIFO reaches 1/8 of its depth */
  92. #define USART_RXFIFO_THRESHOLD_1_4 USART_CR3_RXFTCFG_0 /*!< RXFIFO FIFO reaches 1/4 of its depth */
  93. #define USART_RXFIFO_THRESHOLD_1_2 USART_CR3_RXFTCFG_1 /*!< RXFIFO FIFO reaches 1/2 of its depth */
  94. #define USART_RXFIFO_THRESHOLD_3_4 (USART_CR3_RXFTCFG_0|USART_CR3_RXFTCFG_1) /*!< RXFIFO FIFO reaches 3/4 of its depth */
  95. #define USART_RXFIFO_THRESHOLD_7_8 USART_CR3_RXFTCFG_2 /*!< RXFIFO FIFO reaches 7/8 of its depth */
  96. #define USART_RXFIFO_THRESHOLD_8_8 (USART_CR3_RXFTCFG_2|USART_CR3_RXFTCFG_0) /*!< RXFIFO FIFO becomes full */
  97. /**
  98. * @}
  99. */
  100. #endif
  101. /**
  102. * @}
  103. */
  104. /* Private constants ---------------------------------------------------------*/
  105. /** @defgroup USARTEx_Private_Constants USARTEx Private Constants
  106. * @{
  107. */
  108. #if defined(USART_CR2_SLVEN)
  109. /** @defgroup USARTEx_Slave_Mode USARTEx Synchronous Slave mode
  110. * @{
  111. */
  112. #define USART_SLAVEMODE_DISABLE 0x00000000U /*!< USART SPI Slave Mode Enable */
  113. #define USART_SLAVEMODE_ENABLE USART_CR2_SLVEN /*!< USART SPI Slave Mode Disable */
  114. /**
  115. * @}
  116. */
  117. #endif
  118. #if defined(USART_CR1_FIFOEN)
  119. /** @defgroup USARTEx_FIFO_mode USARTEx FIFO mode
  120. * @{
  121. */
  122. #define USART_FIFOMODE_DISABLE 0x00000000U /*!< FIFO mode disable */
  123. #define USART_FIFOMODE_ENABLE USART_CR1_FIFOEN /*!< FIFO mode enable */
  124. /**
  125. * @}
  126. */
  127. #endif
  128. /**
  129. * @}
  130. */
  131. /* Private macros ------------------------------------------------------------*/
  132. /** @defgroup USARTEx_Private_Macros USARTEx Private Macros
  133. * @{
  134. */
  135. /** @brief Report the USART clock source.
  136. * @param __HANDLE__: specifies the USART Handle.
  137. * @param __CLOCKSOURCE__: output variable.
  138. * @retval the USART clocking source, written in __CLOCKSOURCE__.
  139. */
  140. #if defined (STM32L432xx) || defined (STM32L442xx)
  141. #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \
  142. do { \
  143. if((__HANDLE__)->Instance == USART1) \
  144. { \
  145. switch(__HAL_RCC_GET_USART1_SOURCE()) \
  146. { \
  147. case RCC_USART1CLKSOURCE_PCLK2: \
  148. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2; \
  149. break; \
  150. case RCC_USART1CLKSOURCE_HSI: \
  151. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  152. break; \
  153. case RCC_USART1CLKSOURCE_SYSCLK: \
  154. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  155. break; \
  156. case RCC_USART1CLKSOURCE_LSE: \
  157. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  158. break; \
  159. default: \
  160. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  161. break; \
  162. } \
  163. } \
  164. else if((__HANDLE__)->Instance == USART2) \
  165. { \
  166. switch(__HAL_RCC_GET_USART2_SOURCE()) \
  167. { \
  168. case RCC_USART2CLKSOURCE_PCLK1: \
  169. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  170. break; \
  171. case RCC_USART2CLKSOURCE_HSI: \
  172. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  173. break; \
  174. case RCC_USART2CLKSOURCE_SYSCLK: \
  175. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  176. break; \
  177. case RCC_USART2CLKSOURCE_LSE: \
  178. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  179. break; \
  180. default: \
  181. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  182. break; \
  183. } \
  184. } \
  185. } while(0)
  186. #else
  187. #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \
  188. do { \
  189. if((__HANDLE__)->Instance == USART1) \
  190. { \
  191. switch(__HAL_RCC_GET_USART1_SOURCE()) \
  192. { \
  193. case RCC_USART1CLKSOURCE_PCLK2: \
  194. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2; \
  195. break; \
  196. case RCC_USART1CLKSOURCE_HSI: \
  197. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  198. break; \
  199. case RCC_USART1CLKSOURCE_SYSCLK: \
  200. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  201. break; \
  202. case RCC_USART1CLKSOURCE_LSE: \
  203. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  204. break; \
  205. default: \
  206. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  207. break; \
  208. } \
  209. } \
  210. else if((__HANDLE__)->Instance == USART2) \
  211. { \
  212. switch(__HAL_RCC_GET_USART2_SOURCE()) \
  213. { \
  214. case RCC_USART2CLKSOURCE_PCLK1: \
  215. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  216. break; \
  217. case RCC_USART2CLKSOURCE_HSI: \
  218. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  219. break; \
  220. case RCC_USART2CLKSOURCE_SYSCLK: \
  221. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  222. break; \
  223. case RCC_USART2CLKSOURCE_LSE: \
  224. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  225. break; \
  226. default: \
  227. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  228. break; \
  229. } \
  230. } \
  231. else if((__HANDLE__)->Instance == USART3) \
  232. { \
  233. switch(__HAL_RCC_GET_USART3_SOURCE()) \
  234. { \
  235. case RCC_USART3CLKSOURCE_PCLK1: \
  236. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  237. break; \
  238. case RCC_USART3CLKSOURCE_HSI: \
  239. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  240. break; \
  241. case RCC_USART3CLKSOURCE_SYSCLK: \
  242. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  243. break; \
  244. case RCC_USART3CLKSOURCE_LSE: \
  245. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  246. break; \
  247. default: \
  248. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  249. break; \
  250. } \
  251. } \
  252. } while(0)
  253. #endif /* STM32L432xx || STM32L442xx */
  254. /** @brief Compute the USART mask to apply to retrieve the received data
  255. * according to the word length and to the parity bits activation.
  256. * @note If PCE = 1, the parity bit is not included in the data extracted
  257. * by the reception API().
  258. * This masking operation is not carried out in the case of
  259. * DMA transfers.
  260. * @param __HANDLE__ specifies the USART Handle.
  261. * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field.
  262. */
  263. #define USART_MASK_COMPUTATION(__HANDLE__) \
  264. do { \
  265. if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B) \
  266. { \
  267. if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \
  268. { \
  269. (__HANDLE__)->Mask = 0x01FF ; \
  270. } \
  271. else \
  272. { \
  273. (__HANDLE__)->Mask = 0x00FF ; \
  274. } \
  275. } \
  276. else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B) \
  277. { \
  278. if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \
  279. { \
  280. (__HANDLE__)->Mask = 0x00FF ; \
  281. } \
  282. else \
  283. { \
  284. (__HANDLE__)->Mask = 0x007F ; \
  285. } \
  286. } \
  287. else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B) \
  288. { \
  289. if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \
  290. { \
  291. (__HANDLE__)->Mask = 0x007F ; \
  292. } \
  293. else \
  294. { \
  295. (__HANDLE__)->Mask = 0x003F ; \
  296. } \
  297. } \
  298. } while(0)
  299. /**
  300. * @brief Ensure that USART frame length is valid.
  301. * @param __LENGTH__ USART frame length.
  302. * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
  303. */
  304. #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \
  305. ((__LENGTH__) == USART_WORDLENGTH_8B) || \
  306. ((__LENGTH__) == USART_WORDLENGTH_9B))
  307. #if defined(USART_CR2_SLVEN)
  308. /**
  309. * @brief Ensure that USART Negative Slave Select (NSS) pin management is valid.
  310. * @param __NSS__ USART Negative Slave Select pin management.
  311. * @retval SET (__NSS__ is valid) or RESET (__NSS__ is invalid)
  312. */
  313. #define IS_USART_NSS(__NSS__) (((__NSS__) == USART_NSS_HARD) || \
  314. ((__NSS__) == USART_NSS_SOFT))
  315. #endif
  316. #if defined(USART_CR1_FIFOEN)
  317. /**
  318. * @brief Ensure that USART TXFIFO threshold level is valid.
  319. * @param __THRESHOLD__ USART TXFIFO threshold level.
  320. * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
  321. */
  322. #define IS_USART_TXFIFO_THRESHOLD(__THRESHOLD__) (((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_8) || \
  323. ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_4) || \
  324. ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_2) || \
  325. ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_3_4) || \
  326. ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_7_8) || \
  327. ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_8_8))
  328. /**
  329. * @brief Ensure that USART RXFIFO threshold level is valid.
  330. * @param __THRESHOLD__ USART RXFIFO threshold level.
  331. * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
  332. */
  333. #define IS_USART_RXFIFO_THRESHOLD(__THRESHOLD__) (((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_8) || \
  334. ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_4) || \
  335. ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_2) || \
  336. ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_3_4) || \
  337. ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_7_8) || \
  338. ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_8_8))
  339. #endif
  340. /**
  341. * @}
  342. */
  343. /* Exported functions --------------------------------------------------------*/
  344. /** @addtogroup USARTEx_Exported_Functions
  345. * @{
  346. */
  347. /** @addtogroup USARTEx_Exported_Functions_Group2
  348. * @{
  349. */
  350. /* IO operation functions *****************************************************/
  351. #if defined(USART_CR1_FIFOEN)
  352. void HAL_USARTEx_RxFifoFullCallback(USART_HandleTypeDef *husart);
  353. void HAL_USARTEx_TxFifoEmptyCallback(USART_HandleTypeDef *husart);
  354. #endif
  355. /**
  356. * @}
  357. */
  358. /** @addtogroup USARTEx_Exported_Functions_Group3
  359. * @{
  360. */
  361. /* Peripheral Control functions ***********************************************/
  362. #if defined(USART_CR2_SLVEN)
  363. HAL_StatusTypeDef HAL_USARTEx_EnableSlaveMode(USART_HandleTypeDef *husart);
  364. HAL_StatusTypeDef HAL_USARTEx_DisableSlaveMode(USART_HandleTypeDef *husart);
  365. HAL_StatusTypeDef HAL_USARTEx_ConfigNSS(USART_HandleTypeDef *husart, uint32_t NSSConfig);
  366. #endif
  367. #if defined(USART_CR1_FIFOEN)
  368. HAL_StatusTypeDef HAL_USARTEx_EnableFifoMode(USART_HandleTypeDef *husart);
  369. HAL_StatusTypeDef HAL_USARTEx_DisableFifoMode(USART_HandleTypeDef *husart);
  370. HAL_StatusTypeDef HAL_USARTEx_SetTxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold);
  371. HAL_StatusTypeDef HAL_USARTEx_SetRxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold);
  372. #endif
  373. /**
  374. * @}
  375. */
  376. /**
  377. * @}
  378. */
  379. /**
  380. * @}
  381. */
  382. /**
  383. * @}
  384. */
  385. #ifdef __cplusplus
  386. }
  387. #endif
  388. #endif /* __STM32L4xx_HAL_USART_EX_H */
  389. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/