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.
 
 
 

390 lines
16 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_ll_usart.c
  4. * @author MCD Application Team
  5. * @brief USART LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2019 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. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32wbxx_ll_usart.h"
  22. #include "stm32wbxx_ll_rcc.h"
  23. #include "stm32wbxx_ll_bus.h"
  24. #ifdef USE_FULL_ASSERT
  25. #include "stm32_assert.h"
  26. #else
  27. #define assert_param(expr) ((void)0U)
  28. #endif
  29. /** @addtogroup STM32WBxx_LL_Driver
  30. * @{
  31. */
  32. #if defined (USART1)
  33. /** @addtogroup USART_LL
  34. * @{
  35. */
  36. /* Private types -------------------------------------------------------------*/
  37. /* Private variables ---------------------------------------------------------*/
  38. /* Private constants ---------------------------------------------------------*/
  39. /** @addtogroup USART_LL_Private_Constants
  40. * @{
  41. */
  42. /**
  43. * @}
  44. */
  45. /* Private macros ------------------------------------------------------------*/
  46. /** @addtogroup USART_LL_Private_Macros
  47. * @{
  48. */
  49. #define IS_LL_USART_PRESCALER(__VALUE__) (((__VALUE__) == LL_USART_PRESCALER_DIV1) \
  50. || ((__VALUE__) == LL_USART_PRESCALER_DIV2) \
  51. || ((__VALUE__) == LL_USART_PRESCALER_DIV4) \
  52. || ((__VALUE__) == LL_USART_PRESCALER_DIV6) \
  53. || ((__VALUE__) == LL_USART_PRESCALER_DIV8) \
  54. || ((__VALUE__) == LL_USART_PRESCALER_DIV10) \
  55. || ((__VALUE__) == LL_USART_PRESCALER_DIV12) \
  56. || ((__VALUE__) == LL_USART_PRESCALER_DIV16) \
  57. || ((__VALUE__) == LL_USART_PRESCALER_DIV32) \
  58. || ((__VALUE__) == LL_USART_PRESCALER_DIV64) \
  59. || ((__VALUE__) == LL_USART_PRESCALER_DIV128) \
  60. || ((__VALUE__) == LL_USART_PRESCALER_DIV256))
  61. /* __BAUDRATE__ The maximum Baud Rate is derived from the maximum clock available
  62. * divided by the smallest oversampling used on the USART (i.e. 8) */
  63. #define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 8000000U)
  64. /* __VALUE__ In case of oversampling by 16 and 8, BRR content must be greater than or equal to 16d. */
  65. #define IS_LL_USART_BRR_MIN(__VALUE__) ((__VALUE__) >= 16U)
  66. /* __VALUE__ BRR content must be lower than or equal to 0xFFFF. */
  67. #define IS_LL_USART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
  68. #define IS_LL_USART_DIRECTION(__VALUE__) (((__VALUE__) == LL_USART_DIRECTION_NONE) \
  69. || ((__VALUE__) == LL_USART_DIRECTION_RX) \
  70. || ((__VALUE__) == LL_USART_DIRECTION_TX) \
  71. || ((__VALUE__) == LL_USART_DIRECTION_TX_RX))
  72. #define IS_LL_USART_PARITY(__VALUE__) (((__VALUE__) == LL_USART_PARITY_NONE) \
  73. || ((__VALUE__) == LL_USART_PARITY_EVEN) \
  74. || ((__VALUE__) == LL_USART_PARITY_ODD))
  75. #define IS_LL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_USART_DATAWIDTH_7B) \
  76. || ((__VALUE__) == LL_USART_DATAWIDTH_8B) \
  77. || ((__VALUE__) == LL_USART_DATAWIDTH_9B))
  78. #define IS_LL_USART_OVERSAMPLING(__VALUE__) (((__VALUE__) == LL_USART_OVERSAMPLING_16) \
  79. || ((__VALUE__) == LL_USART_OVERSAMPLING_8))
  80. #define IS_LL_USART_LASTBITCLKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_LASTCLKPULSE_NO_OUTPUT) \
  81. || ((__VALUE__) == LL_USART_LASTCLKPULSE_OUTPUT))
  82. #define IS_LL_USART_CLOCKPHASE(__VALUE__) (((__VALUE__) == LL_USART_PHASE_1EDGE) \
  83. || ((__VALUE__) == LL_USART_PHASE_2EDGE))
  84. #define IS_LL_USART_CLOCKPOLARITY(__VALUE__) (((__VALUE__) == LL_USART_POLARITY_LOW) \
  85. || ((__VALUE__) == LL_USART_POLARITY_HIGH))
  86. #define IS_LL_USART_CLOCKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_CLOCK_DISABLE) \
  87. || ((__VALUE__) == LL_USART_CLOCK_ENABLE))
  88. #define IS_LL_USART_STOPBITS(__VALUE__) (((__VALUE__) == LL_USART_STOPBITS_0_5) \
  89. || ((__VALUE__) == LL_USART_STOPBITS_1) \
  90. || ((__VALUE__) == LL_USART_STOPBITS_1_5) \
  91. || ((__VALUE__) == LL_USART_STOPBITS_2))
  92. #define IS_LL_USART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_USART_HWCONTROL_NONE) \
  93. || ((__VALUE__) == LL_USART_HWCONTROL_RTS) \
  94. || ((__VALUE__) == LL_USART_HWCONTROL_CTS) \
  95. || ((__VALUE__) == LL_USART_HWCONTROL_RTS_CTS))
  96. /**
  97. * @}
  98. */
  99. /* Private function prototypes -----------------------------------------------*/
  100. /* Exported functions --------------------------------------------------------*/
  101. /** @addtogroup USART_LL_Exported_Functions
  102. * @{
  103. */
  104. /** @addtogroup USART_LL_EF_Init
  105. * @{
  106. */
  107. /**
  108. * @brief De-initialize USART registers (Registers restored to their default values).
  109. * @param USARTx USART Instance
  110. * @retval An ErrorStatus enumeration value:
  111. * - SUCCESS: USART registers are de-initialized
  112. * - ERROR: USART registers are not de-initialized
  113. */
  114. ErrorStatus LL_USART_DeInit(USART_TypeDef *USARTx)
  115. {
  116. ErrorStatus status = SUCCESS;
  117. /* Check the parameters */
  118. assert_param(IS_UART_INSTANCE(USARTx));
  119. if (USARTx == USART1)
  120. {
  121. /* Force reset of USART clock */
  122. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART1);
  123. /* Release reset of USART clock */
  124. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART1);
  125. }
  126. else
  127. {
  128. status = ERROR;
  129. }
  130. return (status);
  131. }
  132. /**
  133. * @brief Initialize USART registers according to the specified
  134. * parameters in USART_InitStruct.
  135. * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
  136. * USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  137. * @note Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
  138. * @param USARTx USART Instance
  139. * @param USART_InitStruct pointer to a LL_USART_InitTypeDef structure
  140. * that contains the configuration information for the specified USART peripheral.
  141. * @retval An ErrorStatus enumeration value:
  142. * - SUCCESS: USART registers are initialized according to USART_InitStruct content
  143. * - ERROR: Problem occurred during USART Registers initialization
  144. */
  145. ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, LL_USART_InitTypeDef *USART_InitStruct)
  146. {
  147. ErrorStatus status = ERROR;
  148. uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
  149. /* Check the parameters */
  150. assert_param(IS_UART_INSTANCE(USARTx));
  151. assert_param(IS_LL_USART_PRESCALER(USART_InitStruct->PrescalerValue));
  152. assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct->BaudRate));
  153. assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct->DataWidth));
  154. assert_param(IS_LL_USART_STOPBITS(USART_InitStruct->StopBits));
  155. assert_param(IS_LL_USART_PARITY(USART_InitStruct->Parity));
  156. assert_param(IS_LL_USART_DIRECTION(USART_InitStruct->TransferDirection));
  157. assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct->HardwareFlowControl));
  158. assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct->OverSampling));
  159. /* USART needs to be in disabled state, in order to be able to configure some bits in
  160. CRx registers */
  161. if (LL_USART_IsEnabled(USARTx) == 0U)
  162. {
  163. /*---------------------------- USART CR1 Configuration ---------------------
  164. * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
  165. * - DataWidth: USART_CR1_M bits according to USART_InitStruct->DataWidth value
  166. * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
  167. * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
  168. * - Oversampling: USART_CR1_OVER8 bit according to USART_InitStruct->OverSampling value.
  169. */
  170. MODIFY_REG(USARTx->CR1,
  171. (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
  172. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
  173. (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
  174. USART_InitStruct->TransferDirection | USART_InitStruct->OverSampling));
  175. /*---------------------------- USART CR2 Configuration ---------------------
  176. * Configure USARTx CR2 (Stop bits) with parameters:
  177. * - Stop Bits: USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
  178. * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
  179. */
  180. LL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
  181. /*---------------------------- USART CR3 Configuration ---------------------
  182. * Configure USARTx CR3 (Hardware Flow Control) with parameters:
  183. * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to USART_InitStruct->HardwareFlowControl value.
  184. */
  185. LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
  186. /*---------------------------- USART BRR Configuration ---------------------
  187. * Retrieve Clock frequency used for USART Peripheral
  188. */
  189. if (USARTx == USART1)
  190. {
  191. periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART1_CLKSOURCE);
  192. }
  193. else
  194. {
  195. /* Nothing to do, as error code is already assigned to ERROR value */
  196. }
  197. /* Configure the USART Baud Rate :
  198. - prescaler value is required
  199. - valid baud rate value (different from 0) is required
  200. - Peripheral clock as returned by RCC service, should be valid (different from 0).
  201. */
  202. if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
  203. && (USART_InitStruct->BaudRate != 0U))
  204. {
  205. status = SUCCESS;
  206. LL_USART_SetBaudRate(USARTx,
  207. periphclk,
  208. USART_InitStruct->PrescalerValue,
  209. USART_InitStruct->OverSampling,
  210. USART_InitStruct->BaudRate);
  211. /* Check BRR is greater than or equal to 16d */
  212. assert_param(IS_LL_USART_BRR_MIN(USARTx->BRR));
  213. /* Check BRR is lower than or equal to 0xFFFF */
  214. assert_param(IS_LL_USART_BRR_MAX(USARTx->BRR));
  215. }
  216. /*---------------------------- USART PRESC Configuration -----------------------
  217. * Configure USARTx PRESC (Prescaler) with parameters:
  218. * - PrescalerValue: USART_PRESC_PRESCALER bits according to USART_InitStruct->PrescalerValue value.
  219. */
  220. LL_USART_SetPrescaler(USARTx, USART_InitStruct->PrescalerValue);
  221. }
  222. /* Endif (=> USART not in Disabled state => return ERROR) */
  223. return (status);
  224. }
  225. /**
  226. * @brief Set each @ref LL_USART_InitTypeDef field to default value.
  227. * @param USART_InitStruct pointer to a @ref LL_USART_InitTypeDef structure
  228. * whose fields will be set to default values.
  229. * @retval None
  230. */
  231. void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
  232. {
  233. /* Set USART_InitStruct fields to default values */
  234. USART_InitStruct->PrescalerValue = LL_USART_PRESCALER_DIV1;
  235. USART_InitStruct->BaudRate = 9600U;
  236. USART_InitStruct->DataWidth = LL_USART_DATAWIDTH_8B;
  237. USART_InitStruct->StopBits = LL_USART_STOPBITS_1;
  238. USART_InitStruct->Parity = LL_USART_PARITY_NONE ;
  239. USART_InitStruct->TransferDirection = LL_USART_DIRECTION_TX_RX;
  240. USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  241. USART_InitStruct->OverSampling = LL_USART_OVERSAMPLING_16;
  242. }
  243. /**
  244. * @brief Initialize USART Clock related settings according to the
  245. * specified parameters in the USART_ClockInitStruct.
  246. * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
  247. * USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  248. * @param USARTx USART Instance
  249. * @param USART_ClockInitStruct pointer to a @ref LL_USART_ClockInitTypeDef structure
  250. * that contains the Clock configuration information for the specified USART peripheral.
  251. * @retval An ErrorStatus enumeration value:
  252. * - SUCCESS: USART registers related to Clock settings are initialized according to USART_ClockInitStruct content
  253. * - ERROR: Problem occurred during USART Registers initialization
  254. */
  255. ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  256. {
  257. ErrorStatus status = SUCCESS;
  258. /* Check USART Instance and Clock signal output parameters */
  259. assert_param(IS_UART_INSTANCE(USARTx));
  260. assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
  261. /* USART needs to be in disabled state, in order to be able to configure some bits in
  262. CRx registers */
  263. if (LL_USART_IsEnabled(USARTx) == 0U)
  264. {
  265. /*---------------------------- USART CR2 Configuration -----------------------*/
  266. /* If Clock signal has to be output */
  267. if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
  268. {
  269. /* Deactivate Clock signal delivery :
  270. * - Disable Clock Output: USART_CR2_CLKEN cleared
  271. */
  272. LL_USART_DisableSCLKOutput(USARTx);
  273. }
  274. else
  275. {
  276. /* Ensure USART instance is USART capable */
  277. assert_param(IS_USART_INSTANCE(USARTx));
  278. /* Check clock related parameters */
  279. assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
  280. assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
  281. assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
  282. /*---------------------------- USART CR2 Configuration -----------------------
  283. * Configure USARTx CR2 (Clock signal related bits) with parameters:
  284. * - Enable Clock Output: USART_CR2_CLKEN set
  285. * - Clock Polarity: USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
  286. * - Clock Phase: USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
  287. * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
  288. */
  289. MODIFY_REG(USARTx->CR2,
  290. USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
  291. USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
  292. USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
  293. }
  294. }
  295. /* Else (USART not in Disabled state => return ERROR */
  296. else
  297. {
  298. status = ERROR;
  299. }
  300. return (status);
  301. }
  302. /**
  303. * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
  304. * @param USART_ClockInitStruct pointer to a @ref LL_USART_ClockInitTypeDef structure
  305. * whose fields will be set to default values.
  306. * @retval None
  307. */
  308. void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  309. {
  310. /* Set LL_USART_ClockInitStruct fields with default values */
  311. USART_ClockInitStruct->ClockOutput = LL_USART_CLOCK_DISABLE;
  312. USART_ClockInitStruct->ClockPolarity = LL_USART_POLARITY_LOW; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  313. USART_ClockInitStruct->ClockPhase = LL_USART_PHASE_1EDGE; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  314. USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  315. }
  316. /**
  317. * @}
  318. */
  319. /**
  320. * @}
  321. */
  322. /**
  323. * @}
  324. */
  325. #endif /* USART1 */
  326. /**
  327. * @}
  328. */
  329. #endif /* USE_FULL_LL_DRIVER */
  330. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/