py32f0xx_ll_usart.c 16 KB

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