You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

325 lines
15 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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) 2016 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST 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. */
  19. /* Define to prevent recursive inclusion -------------------------------------*/
  20. #ifndef STM32L0xx_HAL_USART_EX_H
  21. #define STM32L0xx_HAL_USART_EX_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32l0xx_hal_def.h"
  27. /** @addtogroup STM32L0xx_HAL_Driver
  28. * @{
  29. */
  30. /** @addtogroup USARTEx
  31. * @{
  32. */
  33. /* Exported types ------------------------------------------------------------*/
  34. /* Exported constants --------------------------------------------------------*/
  35. /** @defgroup USARTEx_Exported_Constants USARTEx Exported Constants
  36. * @{
  37. */
  38. /** @defgroup USARTEx_Word_Length USARTEx Word Length
  39. * @{
  40. */
  41. #define USART_WORDLENGTH_7B ((uint32_t)USART_CR1_M1) /*!< 7-bit long USART frame */
  42. #define USART_WORDLENGTH_8B 0x00000000U /*!< 8-bit long USART frame */
  43. #define USART_WORDLENGTH_9B ((uint32_t)USART_CR1_M0) /*!< 9-bit long USART frame */
  44. /**
  45. * @}
  46. */
  47. /**
  48. * @}
  49. */
  50. /* Private macros ------------------------------------------------------------*/
  51. /** @defgroup USARTEx_Private_Macros USARTEx Private Macros
  52. * @{
  53. */
  54. /** @brief Report the USART clock source.
  55. * @param __HANDLE__ specifies the USART Handle.
  56. * @param __CLOCKSOURCE__ output variable.
  57. * @retval the USART clocking source, written in __CLOCKSOURCE__.
  58. */
  59. #if defined (STM32L051xx) || defined (STM32L052xx) || defined (STM32L053xx) || defined (STM32L061xx) || defined (STM32L062xx) || defined (STM32L063xx)
  60. #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \
  61. do { \
  62. if((__HANDLE__)->Instance == USART1) \
  63. { \
  64. switch(__HAL_RCC_GET_USART1_SOURCE()) \
  65. { \
  66. case RCC_USART1CLKSOURCE_PCLK2: \
  67. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2; \
  68. break; \
  69. case RCC_USART1CLKSOURCE_HSI: \
  70. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  71. break; \
  72. case RCC_USART1CLKSOURCE_SYSCLK: \
  73. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  74. break; \
  75. case RCC_USART1CLKSOURCE_LSE: \
  76. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  77. break; \
  78. default: \
  79. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  80. break; \
  81. } \
  82. } \
  83. else if((__HANDLE__)->Instance == USART2) \
  84. { \
  85. switch(__HAL_RCC_GET_USART2_SOURCE()) \
  86. { \
  87. case RCC_USART2CLKSOURCE_PCLK1: \
  88. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  89. break; \
  90. case RCC_USART2CLKSOURCE_HSI: \
  91. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  92. break; \
  93. case RCC_USART2CLKSOURCE_SYSCLK: \
  94. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  95. break; \
  96. case RCC_USART2CLKSOURCE_LSE: \
  97. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  98. break; \
  99. default: \
  100. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  101. break; \
  102. } \
  103. } \
  104. else \
  105. { \
  106. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  107. } \
  108. } while(0U)
  109. #elif defined(STM32L071xx) || defined (STM32L081xx) || defined(STM32L072xx) || defined (STM32L082xx) || defined(STM32L073xx) || defined (STM32L083xx)
  110. #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \
  111. do { \
  112. if((__HANDLE__)->Instance == USART1) \
  113. { \
  114. switch(__HAL_RCC_GET_USART1_SOURCE()) \
  115. { \
  116. case RCC_USART1CLKSOURCE_PCLK2: \
  117. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2; \
  118. break; \
  119. case RCC_USART1CLKSOURCE_HSI: \
  120. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  121. break; \
  122. case RCC_USART1CLKSOURCE_SYSCLK: \
  123. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  124. break; \
  125. case RCC_USART1CLKSOURCE_LSE: \
  126. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  127. break; \
  128. default: \
  129. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  130. break; \
  131. } \
  132. } \
  133. else if((__HANDLE__)->Instance == USART2) \
  134. { \
  135. switch(__HAL_RCC_GET_USART2_SOURCE()) \
  136. { \
  137. case RCC_USART2CLKSOURCE_PCLK1: \
  138. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  139. break; \
  140. case RCC_USART2CLKSOURCE_HSI: \
  141. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  142. break; \
  143. case RCC_USART2CLKSOURCE_SYSCLK: \
  144. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  145. break; \
  146. case RCC_USART2CLKSOURCE_LSE: \
  147. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  148. break; \
  149. default: \
  150. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  151. break; \
  152. } \
  153. } \
  154. else if((__HANDLE__)->Instance == USART4) \
  155. { \
  156. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  157. } \
  158. else if((__HANDLE__)->Instance == USART5) \
  159. { \
  160. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  161. } \
  162. else \
  163. { \
  164. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  165. } \
  166. } while(0U)
  167. #else
  168. #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \
  169. do { \
  170. if((__HANDLE__)->Instance == USART2) \
  171. { \
  172. switch(__HAL_RCC_GET_USART2_SOURCE()) \
  173. { \
  174. case RCC_USART2CLKSOURCE_PCLK1: \
  175. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  176. break; \
  177. case RCC_USART2CLKSOURCE_HSI: \
  178. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  179. break; \
  180. case RCC_USART2CLKSOURCE_SYSCLK: \
  181. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  182. break; \
  183. case RCC_USART2CLKSOURCE_LSE: \
  184. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  185. break; \
  186. default: \
  187. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  188. break; \
  189. } \
  190. } \
  191. else \
  192. { \
  193. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  194. } \
  195. } while(0U)
  196. #endif
  197. /** @brief Compute the USART mask to apply to retrieve the received data
  198. * according to the word length and to the parity bits activation.
  199. * @note If PCE = 1, the parity bit is not included in the data extracted
  200. * by the reception API().
  201. * This masking operation is not carried out in the case of
  202. * DMA transfers.
  203. * @param __HANDLE__ specifies the USART Handle.
  204. * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field.
  205. */
  206. #define USART_MASK_COMPUTATION(__HANDLE__) \
  207. do { \
  208. if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B) \
  209. { \
  210. if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \
  211. { \
  212. (__HANDLE__)->Mask = 0x01FFU; \
  213. } \
  214. else \
  215. { \
  216. (__HANDLE__)->Mask = 0x00FFU; \
  217. } \
  218. } \
  219. else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B) \
  220. { \
  221. if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \
  222. { \
  223. (__HANDLE__)->Mask = 0x00FFU; \
  224. } \
  225. else \
  226. { \
  227. (__HANDLE__)->Mask = 0x007FU; \
  228. } \
  229. } \
  230. else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B) \
  231. { \
  232. if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \
  233. { \
  234. (__HANDLE__)->Mask = 0x007FU; \
  235. } \
  236. else \
  237. { \
  238. (__HANDLE__)->Mask = 0x003FU; \
  239. } \
  240. } \
  241. else \
  242. { \
  243. (__HANDLE__)->Mask = 0x0000U; \
  244. } \
  245. } while(0U)
  246. /**
  247. * @brief Ensure that USART frame length is valid.
  248. * @param __LENGTH__ USART frame length.
  249. * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
  250. */
  251. #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \
  252. ((__LENGTH__) == USART_WORDLENGTH_8B) || \
  253. ((__LENGTH__) == USART_WORDLENGTH_9B))
  254. /**
  255. * @}
  256. */
  257. /* Exported functions --------------------------------------------------------*/
  258. /** @addtogroup USARTEx_Exported_Functions
  259. * @{
  260. */
  261. /** @addtogroup USARTEx_Exported_Functions_Group1
  262. * @{
  263. */
  264. /* IO operation functions *****************************************************/
  265. /**
  266. * @}
  267. */
  268. /** @addtogroup USARTEx_Exported_Functions_Group2
  269. * @{
  270. */
  271. /* Peripheral Control functions ***********************************************/
  272. /**
  273. * @}
  274. */
  275. /**
  276. * @}
  277. */
  278. /**
  279. * @}
  280. */
  281. /**
  282. * @}
  283. */
  284. #ifdef __cplusplus
  285. }
  286. #endif
  287. #endif /* STM32L0xx_HAL_USART_EX_H */
  288. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/