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.
 
 
 

429 lines
16 KiB

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