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.
 
 
 

281 lines
14 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_usart_ex.h
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  7. * @brief Header file of USART HAL Extended module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32L4xx_HAL_USART_EX_H
  39. #define __STM32L4xx_HAL_USART_EX_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32l4xx_hal_def.h"
  45. /** @addtogroup STM32L4xx_HAL_Driver
  46. * @{
  47. */
  48. /** @addtogroup USARTEx
  49. * @{
  50. */
  51. /* Exported types ------------------------------------------------------------*/
  52. /* Exported constants --------------------------------------------------------*/
  53. /** @defgroup USARTEx_Exported_Constants USARTEx Exported Constants
  54. * @{
  55. */
  56. /** @defgroup USARTEx_Word_Length USARTEx Word Length
  57. * @{
  58. */
  59. #define USART_WORDLENGTH_7B ((uint32_t)USART_CR1_M1) /*!< 7-bit long USART frame */
  60. #define USART_WORDLENGTH_8B ((uint32_t)0x00000000) /*!< 8-bit long USART frame */
  61. #define USART_WORDLENGTH_9B ((uint32_t)USART_CR1_M0) /*!< 9-bit long USART frame */
  62. /**
  63. * @}
  64. */
  65. /**
  66. * @}
  67. */
  68. /* Exported functions --------------------------------------------------------*/
  69. /* Private macros ------------------------------------------------------------*/
  70. /** @defgroup USARTEx_Private_Macros USARTEx Private Macros
  71. * @{
  72. */
  73. /** @brief Report the USART clock source.
  74. * @param __HANDLE__: specifies the USART Handle.
  75. * @param __CLOCKSOURCE__: output variable.
  76. * @retval the USART clocking source, written in __CLOCKSOURCE__.
  77. */
  78. #if defined (STM32L432xx) || defined (STM32L442xx)
  79. #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \
  80. do { \
  81. if((__HANDLE__)->Instance == USART1) \
  82. { \
  83. switch(__HAL_RCC_GET_USART1_SOURCE()) \
  84. { \
  85. case RCC_USART1CLKSOURCE_PCLK2: \
  86. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2; \
  87. break; \
  88. case RCC_USART1CLKSOURCE_HSI: \
  89. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  90. break; \
  91. case RCC_USART1CLKSOURCE_SYSCLK: \
  92. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  93. break; \
  94. case RCC_USART1CLKSOURCE_LSE: \
  95. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  96. break; \
  97. default: \
  98. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  99. break; \
  100. } \
  101. } \
  102. else if((__HANDLE__)->Instance == USART2) \
  103. { \
  104. switch(__HAL_RCC_GET_USART2_SOURCE()) \
  105. { \
  106. case RCC_USART2CLKSOURCE_PCLK1: \
  107. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  108. break; \
  109. case RCC_USART2CLKSOURCE_HSI: \
  110. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  111. break; \
  112. case RCC_USART2CLKSOURCE_SYSCLK: \
  113. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  114. break; \
  115. case RCC_USART2CLKSOURCE_LSE: \
  116. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  117. break; \
  118. default: \
  119. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  120. break; \
  121. } \
  122. } \
  123. } while(0)
  124. #else
  125. #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \
  126. do { \
  127. if((__HANDLE__)->Instance == USART1) \
  128. { \
  129. switch(__HAL_RCC_GET_USART1_SOURCE()) \
  130. { \
  131. case RCC_USART1CLKSOURCE_PCLK2: \
  132. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2; \
  133. break; \
  134. case RCC_USART1CLKSOURCE_HSI: \
  135. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  136. break; \
  137. case RCC_USART1CLKSOURCE_SYSCLK: \
  138. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  139. break; \
  140. case RCC_USART1CLKSOURCE_LSE: \
  141. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  142. break; \
  143. default: \
  144. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  145. break; \
  146. } \
  147. } \
  148. else if((__HANDLE__)->Instance == USART2) \
  149. { \
  150. switch(__HAL_RCC_GET_USART2_SOURCE()) \
  151. { \
  152. case RCC_USART2CLKSOURCE_PCLK1: \
  153. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  154. break; \
  155. case RCC_USART2CLKSOURCE_HSI: \
  156. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  157. break; \
  158. case RCC_USART2CLKSOURCE_SYSCLK: \
  159. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  160. break; \
  161. case RCC_USART2CLKSOURCE_LSE: \
  162. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  163. break; \
  164. default: \
  165. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  166. break; \
  167. } \
  168. } \
  169. else if((__HANDLE__)->Instance == USART3) \
  170. { \
  171. switch(__HAL_RCC_GET_USART3_SOURCE()) \
  172. { \
  173. case RCC_USART3CLKSOURCE_PCLK1: \
  174. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
  175. break; \
  176. case RCC_USART3CLKSOURCE_HSI: \
  177. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
  178. break; \
  179. case RCC_USART3CLKSOURCE_SYSCLK: \
  180. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
  181. break; \
  182. case RCC_USART3CLKSOURCE_LSE: \
  183. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
  184. break; \
  185. default: \
  186. (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
  187. break; \
  188. } \
  189. } \
  190. } while(0)
  191. #endif /* STM32L432xx || STM32L442xx */
  192. /** @brief Compute the USART mask to apply to retrieve the received data
  193. * according to the word length and to the parity bits activation.
  194. * @note If PCE = 1, the parity bit is not included in the data extracted
  195. * by the reception API().
  196. * This masking operation is not carried out in the case of
  197. * DMA transfers.
  198. * @param __HANDLE__: specifies the USART Handle.
  199. * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field.
  200. */
  201. #define USART_MASK_COMPUTATION(__HANDLE__) \
  202. do { \
  203. if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B) \
  204. { \
  205. if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \
  206. { \
  207. (__HANDLE__)->Mask = 0x01FF ; \
  208. } \
  209. else \
  210. { \
  211. (__HANDLE__)->Mask = 0x00FF ; \
  212. } \
  213. } \
  214. else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B) \
  215. { \
  216. if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \
  217. { \
  218. (__HANDLE__)->Mask = 0x00FF ; \
  219. } \
  220. else \
  221. { \
  222. (__HANDLE__)->Mask = 0x007F ; \
  223. } \
  224. } \
  225. else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B) \
  226. { \
  227. if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \
  228. { \
  229. (__HANDLE__)->Mask = 0x007F ; \
  230. } \
  231. else \
  232. { \
  233. (__HANDLE__)->Mask = 0x003F ; \
  234. } \
  235. } \
  236. } while(0)
  237. /**
  238. * @brief Ensure that USART frame length is valid.
  239. * @param __LENGTH__: USART frame length.
  240. * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
  241. */
  242. #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \
  243. ((__LENGTH__) == USART_WORDLENGTH_8B) || \
  244. ((__LENGTH__) == USART_WORDLENGTH_9B))
  245. /**
  246. * @}
  247. */
  248. /* Exported functions --------------------------------------------------------*/
  249. /**
  250. * @}
  251. */
  252. /**
  253. * @}
  254. */
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. #endif /* __STM32L4xx_HAL_USART_EX_H */
  259. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/