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.

stm32l1xx_ll_usart.c 16KB

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