stm32f4xx_ll_usart.c 18 KB

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