stm32g4xx_ll_usart.c 18 KB

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