py32f002b_ll_usart.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_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 "py32f002b_ll_usart.h"
  33. #include "py32f002b_ll_rcc.h"
  34. #include "py32f002b_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 PY32F002B_LL_Driver
  41. * @{
  42. */
  43. #if defined (USART1)
  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. else
  123. {
  124. status = ERROR;
  125. }
  126. return (status);
  127. }
  128. /**
  129. * @brief Initialize USART registers according to the specified
  130. * parameters in USART_InitStruct.
  131. * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
  132. * USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  133. * @note Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
  134. * @param USARTx USART Instance
  135. * @param USART_InitStruct pointer to a LL_USART_InitTypeDef structure
  136. * that contains the configuration information for the specified USART peripheral.
  137. * @retval An ErrorStatus enumeration value:
  138. * - SUCCESS: USART registers are initialized according to USART_InitStruct content
  139. * - ERROR: Problem occurred during USART Registers initialization
  140. */
  141. ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, LL_USART_InitTypeDef *USART_InitStruct)
  142. {
  143. ErrorStatus status = ERROR;
  144. uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
  145. LL_RCC_ClocksTypeDef rcc_clocks;
  146. /* Check the parameters */
  147. assert_param(IS_UART_INSTANCE(USARTx));
  148. assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct->BaudRate));
  149. assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct->DataWidth));
  150. assert_param(IS_LL_USART_STOPBITS(USART_InitStruct->StopBits));
  151. assert_param(IS_LL_USART_PARITY(USART_InitStruct->Parity));
  152. assert_param(IS_LL_USART_DIRECTION(USART_InitStruct->TransferDirection));
  153. assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct->HardwareFlowControl));
  154. #if defined(USART_CR3_OVER8)
  155. assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct->OverSampling));
  156. #endif /* USART_OverSampling_Feature */
  157. /* USART needs to be in disabled state, in order to be able to configure some bits in
  158. CRx registers */
  159. if (LL_USART_IsEnabled(USARTx) == 0U)
  160. {
  161. /*---------------------------- USART CR1 Configuration -----------------------
  162. * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
  163. * - DataWidth: USART_CR1_M bits according to USART_InitStruct->DataWidth value
  164. * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
  165. * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
  166. * - Oversampling: USART_CR3_OVER8 bit according to USART_InitStruct->OverSampling value.
  167. */
  168. #if defined(USART_CR3_OVER8)
  169. MODIFY_REG(USARTx->CR1,
  170. (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
  171. USART_CR1_TE | USART_CR1_RE),
  172. (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
  173. USART_InitStruct->TransferDirection));
  174. MODIFY_REG(USARTx->CR3, USART_CR3_OVER8, USART_InitStruct->OverSampling);
  175. #else
  176. MODIFY_REG(USARTx->CR1,
  177. (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
  178. USART_CR1_TE | USART_CR1_RE),
  179. (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
  180. USART_InitStruct->TransferDirection));
  181. #endif /* USART_OverSampling_Feature */
  182. /*---------------------------- USART CR2 Configuration -----------------------
  183. * Configure USARTx CR2 (Stop bits) with parameters:
  184. * - Stop Bits: USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
  185. * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
  186. */
  187. LL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
  188. /*---------------------------- USART CR3 Configuration -----------------------
  189. * Configure USARTx CR3 (Hardware Flow Control) with parameters:
  190. * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to USART_InitStruct->HardwareFlowControl value.
  191. */
  192. LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
  193. /*---------------------------- USART BRR Configuration -----------------------
  194. * Retrieve Clock frequency used for USART Peripheral
  195. */
  196. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  197. periphclk = rcc_clocks.PCLK1_Frequency;
  198. /* Configure the USART Baud Rate :
  199. - valid baud rate value (different from 0) is required
  200. - Peripheral clock as returned by RCC service, should be valid (different from 0).
  201. */
  202. if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
  203. && (USART_InitStruct->BaudRate != 0U))
  204. {
  205. status = SUCCESS;
  206. #if defined(USART_CR3_OVER8)
  207. LL_USART_SetBaudRate(USARTx,
  208. periphclk,
  209. USART_InitStruct->OverSampling,
  210. USART_InitStruct->BaudRate);
  211. #else
  212. LL_USART_SetBaudRate(USARTx,
  213. periphclk,
  214. USART_InitStruct->BaudRate);
  215. #endif /* USART_OverSampling_Feature */
  216. /* Check BRR is greater than or equal to 16d */
  217. assert_param(IS_LL_USART_BRR_MIN(USARTx->BRR));
  218. /* Check BRR is greater than or equal to 16d */
  219. assert_param(IS_LL_USART_BRR_MAX(USARTx->BRR));
  220. }
  221. }
  222. /* Endif (=> USART not in Disabled state => return ERROR) */
  223. return (status);
  224. }
  225. /**
  226. * @brief Set each @ref LL_USART_InitTypeDef field to default value.
  227. * @param USART_InitStruct Pointer to a @ref LL_USART_InitTypeDef structure
  228. * whose fields will be set to default values.
  229. * @retval None
  230. */
  231. void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
  232. {
  233. /* Set USART_InitStruct fields to default values */
  234. USART_InitStruct->BaudRate = 9600U;
  235. USART_InitStruct->DataWidth = LL_USART_DATAWIDTH_8B;
  236. USART_InitStruct->StopBits = LL_USART_STOPBITS_1;
  237. USART_InitStruct->Parity = LL_USART_PARITY_NONE ;
  238. USART_InitStruct->TransferDirection = LL_USART_DIRECTION_TX_RX;
  239. USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  240. #if defined(USART_CR3_OVER8)
  241. USART_InitStruct->OverSampling = LL_USART_OVERSAMPLING_16;
  242. #endif /* USART_OverSampling_Feature */
  243. }
  244. /**
  245. * @brief Initialize USART Clock related settings according to the
  246. * specified parameters in the USART_ClockInitStruct.
  247. * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
  248. * USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  249. * @param USARTx USART Instance
  250. * @param USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
  251. * that contains the Clock configuration information for the specified USART peripheral.
  252. * @retval An ErrorStatus enumeration value:
  253. * - SUCCESS: USART registers related to Clock settings are initialized according to USART_ClockInitStruct content
  254. * - ERROR: Problem occurred during USART Registers initialization
  255. */
  256. ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  257. {
  258. ErrorStatus status = SUCCESS;
  259. /* Check USART Instance and Clock signal output parameters */
  260. assert_param(IS_UART_INSTANCE(USARTx));
  261. assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
  262. /* USART needs to be in disabled state, in order to be able to configure some bits in
  263. CRx registers */
  264. if (LL_USART_IsEnabled(USARTx) == 0U)
  265. {
  266. /*---------------------------- USART CR2 Configuration -----------------------*/
  267. /* If Clock signal has to be output */
  268. if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
  269. {
  270. /* Deactivate Clock signal delivery :
  271. * - Disable Clock Output: USART_CR2_CLKEN cleared
  272. */
  273. LL_USART_DisableSCLKOutput(USARTx);
  274. }
  275. else
  276. {
  277. /* Ensure USART instance is USART capable */
  278. assert_param(IS_USART_INSTANCE(USARTx));
  279. /* Check clock related parameters */
  280. assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
  281. assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
  282. assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
  283. /*---------------------------- USART CR2 Configuration -----------------------
  284. * Configure USARTx CR2 (Clock signal related bits) with parameters:
  285. * - Enable Clock Output: USART_CR2_CLKEN set
  286. * - Clock Polarity: USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
  287. * - Clock Phase: USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
  288. * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
  289. */
  290. MODIFY_REG(USARTx->CR2,
  291. USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
  292. USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
  293. USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
  294. }
  295. }
  296. /* Else (USART not in Disabled state => return ERROR */
  297. else
  298. {
  299. status = ERROR;
  300. }
  301. return (status);
  302. }
  303. /**
  304. * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
  305. * @param USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
  306. * whose fields will be set to default values.
  307. * @retval None
  308. */
  309. void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  310. {
  311. /* Set LL_USART_ClockInitStruct fields with default values */
  312. USART_ClockInitStruct->ClockOutput = LL_USART_CLOCK_DISABLE;
  313. USART_ClockInitStruct->ClockPolarity = LL_USART_POLARITY_LOW; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  314. USART_ClockInitStruct->ClockPhase = LL_USART_PHASE_1EDGE; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  315. USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  316. }
  317. /**
  318. * @}
  319. */
  320. /**
  321. * @}
  322. */
  323. /**
  324. * @}
  325. */
  326. #endif /* USART1 */
  327. /**
  328. * @}
  329. */
  330. #endif /* USE_FULL_LL_DRIVER */
  331. /************************ (C) COPYRIGHT Puya *****END OF FILE****/