stm32g0xx_ll_lpuart.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_ll_lpuart.c
  4. * @author MCD Application Team
  5. * @brief LPUART LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2018 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 "stm32g0xx_ll_lpuart.h"
  21. #include "stm32g0xx_ll_rcc.h"
  22. #include "stm32g0xx_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 STM32G0xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (LPUART1) || defined (LPUART2)
  32. /** @addtogroup LPUART_LL
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /** @addtogroup LPUART_LL_Private_Constants
  39. * @{
  40. */
  41. /* Definition of default baudrate value used for LPUART initialisation */
  42. #define LPUART_DEFAULT_BAUDRATE (9600U)
  43. /**
  44. * @}
  45. */
  46. /* Private macros ------------------------------------------------------------*/
  47. /** @addtogroup LPUART_LL_Private_Macros
  48. * @{
  49. */
  50. /* Check of parameters for configuration of LPUART registers */
  51. #define IS_LL_LPUART_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPUART_PRESCALER_DIV1) \
  52. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV2) \
  53. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV4) \
  54. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV6) \
  55. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV8) \
  56. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV10) \
  57. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV12) \
  58. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV16) \
  59. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV32) \
  60. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV64) \
  61. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV128) \
  62. || ((__VALUE__) == LL_LPUART_PRESCALER_DIV256))
  63. /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register */
  64. /* value : */
  65. /* - fck must be in the range [3 x baudrate, 4096 x baudrate] */
  66. /* - LPUART_BRR register value should be >= 0x300 */
  67. /* - LPUART_BRR register value should be <= 0xFFFFF (20 bits) */
  68. /* Baudrate specified by the user should belong to [8, 21300000].*/
  69. #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 21300000U) && ((__BAUDRATE__) >= 8U))
  70. /* __VALUE__ BRR content must be greater than or equal to 0x300. */
  71. #define IS_LL_LPUART_BRR_MIN(__VALUE__) ((__VALUE__) >= 0x300U)
  72. /* __VALUE__ BRR content must be lower than or equal to 0xFFFFF. */
  73. #define IS_LL_LPUART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x000FFFFFU)
  74. #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \
  75. || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \
  76. || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \
  77. || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX))
  78. #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \
  79. || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \
  80. || ((__VALUE__) == LL_LPUART_PARITY_ODD))
  81. #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \
  82. || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \
  83. || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B))
  84. #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \
  85. || ((__VALUE__) == LL_LPUART_STOPBITS_2))
  86. #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \
  87. || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \
  88. || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \
  89. || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS))
  90. /**
  91. * @}
  92. */
  93. /* Private function prototypes -----------------------------------------------*/
  94. /* Exported functions --------------------------------------------------------*/
  95. /** @addtogroup LPUART_LL_Exported_Functions
  96. * @{
  97. */
  98. /** @addtogroup LPUART_LL_EF_Init
  99. * @{
  100. */
  101. /**
  102. * @brief De-initialize LPUART registers (Registers restored to their default values).
  103. * @param LPUARTx LPUART Instance
  104. * @retval An ErrorStatus enumeration value:
  105. * - SUCCESS: LPUART registers are de-initialized
  106. * - ERROR: not applicable
  107. */
  108. ErrorStatus LL_LPUART_DeInit(const USART_TypeDef *LPUARTx)
  109. {
  110. ErrorStatus status = SUCCESS;
  111. /* Check the parameters */
  112. assert_param(IS_LPUART_INSTANCE(LPUARTx));
  113. if (LPUARTx == LPUART1)
  114. {
  115. /* Force reset of LPUART peripheral */
  116. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPUART1);
  117. /* Release reset of LPUART peripheral */
  118. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPUART1);
  119. }
  120. #if defined(LPUART2)
  121. else if (LPUARTx == LPUART2)
  122. {
  123. /* Force reset of LPUART peripheral */
  124. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPUART2);
  125. /* Release reset of LPUART peripheral */
  126. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPUART2);
  127. }
  128. #endif /* LPUART2 */
  129. else
  130. {
  131. status = ERROR;
  132. }
  133. return (status);
  134. }
  135. /**
  136. * @brief Initialize LPUART registers according to the specified
  137. * parameters in LPUART_InitStruct.
  138. * @note As some bits in LPUART configuration registers can only be written when
  139. * the LPUART is disabled (USART_CR1_UE bit =0),
  140. * LPUART Peripheral should be in disabled state prior calling this function.
  141. * Otherwise, ERROR result will be returned.
  142. * @note Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0).
  143. * @param LPUARTx LPUART Instance
  144. * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
  145. * that contains the configuration information for the specified LPUART peripheral.
  146. * @retval An ErrorStatus enumeration value:
  147. * - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content
  148. * - ERROR: Problem occurred during LPUART Registers initialization
  149. */
  150. ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, const LL_LPUART_InitTypeDef *LPUART_InitStruct)
  151. {
  152. ErrorStatus status = ERROR;
  153. #if defined(LPUART2)
  154. uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
  155. #else
  156. uint32_t periphclk;
  157. #endif /* LPUART2 */
  158. /* Check the parameters */
  159. assert_param(IS_LPUART_INSTANCE(LPUARTx));
  160. assert_param(IS_LL_LPUART_PRESCALER(LPUART_InitStruct->PrescalerValue));
  161. assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct->BaudRate));
  162. assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct->DataWidth));
  163. assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct->StopBits));
  164. assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct->Parity));
  165. assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct->TransferDirection));
  166. assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct->HardwareFlowControl));
  167. /* LPUART needs to be in disabled state, in order to be able to configure some bits in
  168. CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */
  169. if (LL_LPUART_IsEnabled(LPUARTx) == 0U)
  170. {
  171. /*---------------------------- LPUART CR1 Configuration -----------------------
  172. * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters:
  173. * - DataWidth: USART_CR1_M bits according to LPUART_InitStruct->DataWidth value
  174. * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value
  175. * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value
  176. */
  177. MODIFY_REG(LPUARTx->CR1,
  178. (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
  179. (LPUART_InitStruct->DataWidth | LPUART_InitStruct->Parity | LPUART_InitStruct->TransferDirection));
  180. /*---------------------------- LPUART CR2 Configuration -----------------------
  181. * Configure LPUARTx CR2 (Stop bits) with parameters:
  182. * - Stop Bits: USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value.
  183. */
  184. LL_LPUART_SetStopBitsLength(LPUARTx, LPUART_InitStruct->StopBits);
  185. /*---------------------------- LPUART CR3 Configuration -----------------------
  186. * Configure LPUARTx CR3 (Hardware Flow Control) with parameters:
  187. * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according
  188. * to LPUART_InitStruct->HardwareFlowControl value.
  189. */
  190. LL_LPUART_SetHWFlowCtrl(LPUARTx, LPUART_InitStruct->HardwareFlowControl);
  191. /*---------------------------- LPUART BRR Configuration -----------------------
  192. * Retrieve Clock frequency used for LPUART Peripheral
  193. */
  194. #if defined(LPUART2)
  195. if (LPUARTx == LPUART1)
  196. {
  197. periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
  198. }
  199. else if (LPUARTx == LPUART2)
  200. {
  201. periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART2_CLKSOURCE);
  202. }
  203. else
  204. {
  205. /* Nothing to do, as error code is already assigned to ERROR value */
  206. }
  207. #else
  208. periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
  209. #endif /* LPUART2 */
  210. /* Configure the LPUART Baud Rate :
  211. - prescaler value is required
  212. - valid baud rate value (different from 0) is required
  213. - Peripheral clock as returned by RCC service, should be valid (different from 0).
  214. */
  215. if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
  216. && (LPUART_InitStruct->BaudRate != 0U))
  217. {
  218. status = SUCCESS;
  219. LL_LPUART_SetBaudRate(LPUARTx,
  220. periphclk,
  221. LPUART_InitStruct->PrescalerValue,
  222. LPUART_InitStruct->BaudRate);
  223. /* Check BRR is greater than or equal to 0x300 */
  224. assert_param(IS_LL_LPUART_BRR_MIN(LPUARTx->BRR));
  225. /* Check BRR is lower than or equal to 0xFFFFF */
  226. assert_param(IS_LL_LPUART_BRR_MAX(LPUARTx->BRR));
  227. }
  228. /*---------------------------- LPUART PRESC Configuration -----------------------
  229. * Configure LPUARTx PRESC (Prescaler) with parameters:
  230. * - PrescalerValue: LPUART_PRESC_PRESCALER bits according to LPUART_InitStruct->PrescalerValue value.
  231. */
  232. LL_LPUART_SetPrescaler(LPUARTx, LPUART_InitStruct->PrescalerValue);
  233. }
  234. return (status);
  235. }
  236. /**
  237. * @brief Set each @ref LL_LPUART_InitTypeDef field to default value.
  238. * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
  239. * whose fields will be set to default values.
  240. * @retval None
  241. */
  242. void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct)
  243. {
  244. /* Set LPUART_InitStruct fields to default values */
  245. LPUART_InitStruct->PrescalerValue = LL_LPUART_PRESCALER_DIV1;
  246. LPUART_InitStruct->BaudRate = LPUART_DEFAULT_BAUDRATE;
  247. LPUART_InitStruct->DataWidth = LL_LPUART_DATAWIDTH_8B;
  248. LPUART_InitStruct->StopBits = LL_LPUART_STOPBITS_1;
  249. LPUART_InitStruct->Parity = LL_LPUART_PARITY_NONE ;
  250. LPUART_InitStruct->TransferDirection = LL_LPUART_DIRECTION_TX_RX;
  251. LPUART_InitStruct->HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
  252. }
  253. /**
  254. * @}
  255. */
  256. /**
  257. * @}
  258. */
  259. /**
  260. * @}
  261. */
  262. #endif /* LPUART1 || LPUART2 */
  263. /**
  264. * @}
  265. */
  266. #endif /* USE_FULL_LL_DRIVER */