2 ******************************************************************************
3 * @file stm32g4xx_ll_lpuart.c
4 * @author MCD Application Team
5 * @brief LPUART LL module driver.
6 ******************************************************************************
9 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
10 * All rights reserved.</center></h2>
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
17 ******************************************************************************
19 #if defined(USE_FULL_LL_DRIVER)
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32g4xx_ll_lpuart.h"
23 #include "stm32g4xx_ll_rcc.h"
24 #include "stm32g4xx_ll_bus.h"
25 #ifdef USE_FULL_ASSERT
26 #include "stm32_assert.h"
28 #define assert_param(expr) ((void)0U)
29 #endif /* USE_FULL_ASSERT */
31 /** @addtogroup STM32G4xx_LL_Driver
37 /** @addtogroup LPUART_LL
41 /* Private types -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 /* Private constants ---------------------------------------------------------*/
44 /** @addtogroup LPUART_LL_Private_Constants
53 /* Private macros ------------------------------------------------------------*/
54 /** @addtogroup LPUART_LL_Private_Macros
58 /* Check of parameters for configuration of LPUART registers */
60 #define IS_LL_LPUART_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPUART_PRESCALER_DIV1) \
61 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV2) \
62 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV4) \
63 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV6) \
64 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV8) \
65 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV10) \
66 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV12) \
67 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV16) \
68 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV32) \
69 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV64) \
70 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV128) \
71 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV256))
73 /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register */
75 /* - fck must be in the range [3 x baudrate, 4096 x baudrate] */
76 /* - LPUART_BRR register value should be >= 0x300 */
77 /* - LPUART_BRR register value should be <= 0xFFFFF (20 bits) */
78 /* Baudrate specified by the user should belong to [8, 50000000].*/
79 #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 50000000U) && ((__BAUDRATE__) >= 8U))
81 /* __VALUE__ BRR content must be greater than or equal to 0x300. */
82 #define IS_LL_LPUART_BRR_MIN(__VALUE__) ((__VALUE__) >= 0x300U)
84 /* __VALUE__ BRR content must be lower than or equal to 0xFFFFF. */
85 #define IS_LL_LPUART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x000FFFFFU)
87 #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \
88 || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \
89 || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \
90 || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX))
92 #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \
93 || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \
94 || ((__VALUE__) == LL_LPUART_PARITY_ODD))
96 #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \
97 || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \
98 || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B))
100 #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \
101 || ((__VALUE__) == LL_LPUART_STOPBITS_2))
103 #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \
104 || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \
105 || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \
106 || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS))
112 /* Private function prototypes -----------------------------------------------*/
114 /* Exported functions --------------------------------------------------------*/
115 /** @addtogroup LPUART_LL_Exported_Functions
119 /** @addtogroup LPUART_LL_EF_Init
124 * @brief De-initialize LPUART registers (Registers restored to their default values).
125 * @param LPUARTx LPUART Instance
126 * @retval An ErrorStatus enumeration value:
127 * - SUCCESS: LPUART registers are de-initialized
128 * - ERROR: not applicable
130 ErrorStatus
LL_LPUART_DeInit(USART_TypeDef
*LPUARTx
)
132 ErrorStatus status
= SUCCESS
;
134 /* Check the parameters */
135 assert_param(IS_LPUART_INSTANCE(LPUARTx
));
137 if (LPUARTx
== LPUART1
)
139 /* Force reset of LPUART peripheral */
140 LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_LPUART1
);
142 /* Release reset of LPUART peripheral */
143 LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_LPUART1
);
154 * @brief Initialize LPUART registers according to the specified
155 * parameters in LPUART_InitStruct.
156 * @note As some bits in LPUART configuration registers can only be written when the LPUART is disabled (USART_CR1_UE bit =0),
157 * LPUART Peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
158 * @note Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0).
159 * @param LPUARTx LPUART Instance
160 * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
161 * that contains the configuration information for the specified LPUART peripheral.
162 * @retval An ErrorStatus enumeration value:
163 * - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content
164 * - ERROR: Problem occurred during LPUART Registers initialization
166 ErrorStatus
LL_LPUART_Init(USART_TypeDef
*LPUARTx
, LL_LPUART_InitTypeDef
*LPUART_InitStruct
)
168 ErrorStatus status
= ERROR
;
171 /* Check the parameters */
172 assert_param(IS_LPUART_INSTANCE(LPUARTx
));
173 assert_param(IS_LL_LPUART_PRESCALER(LPUART_InitStruct
->PrescalerValue
));
174 assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct
->BaudRate
));
175 assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct
->DataWidth
));
176 assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct
->StopBits
));
177 assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct
->Parity
));
178 assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct
->TransferDirection
));
179 assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct
->HardwareFlowControl
));
181 /* LPUART needs to be in disabled state, in order to be able to configure some bits in
182 CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */
183 if (LL_LPUART_IsEnabled(LPUARTx
) == 0U)
185 /*---------------------------- LPUART CR1 Configuration -----------------------
186 * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters:
187 * - DataWidth: USART_CR1_M bits according to LPUART_InitStruct->DataWidth value
188 * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value
189 * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value
191 MODIFY_REG(LPUARTx
->CR1
,
192 (USART_CR1_M
| USART_CR1_PCE
| USART_CR1_PS
| USART_CR1_TE
| USART_CR1_RE
),
193 (LPUART_InitStruct
->DataWidth
| LPUART_InitStruct
->Parity
| LPUART_InitStruct
->TransferDirection
));
195 /*---------------------------- LPUART CR2 Configuration -----------------------
196 * Configure LPUARTx CR2 (Stop bits) with parameters:
197 * - Stop Bits: USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value.
199 LL_LPUART_SetStopBitsLength(LPUARTx
, LPUART_InitStruct
->StopBits
);
201 /*---------------------------- LPUART CR3 Configuration -----------------------
202 * Configure LPUARTx CR3 (Hardware Flow Control) with parameters:
203 * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to LPUART_InitStruct->HardwareFlowControl value.
205 LL_LPUART_SetHWFlowCtrl(LPUARTx
, LPUART_InitStruct
->HardwareFlowControl
);
207 /*---------------------------- LPUART BRR Configuration -----------------------
208 * Retrieve Clock frequency used for LPUART Peripheral
210 periphclk
= LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE
);
212 /* Configure the LPUART Baud Rate :
213 - prescaler value is required
214 - valid baud rate value (different from 0) is required
215 - Peripheral clock as returned by RCC service, should be valid (different from 0).
217 if ((periphclk
!= LL_RCC_PERIPH_FREQUENCY_NO
)
218 && (LPUART_InitStruct
->BaudRate
!= 0U))
221 LL_LPUART_SetBaudRate(LPUARTx
,
223 LPUART_InitStruct
->PrescalerValue
,
224 LPUART_InitStruct
->BaudRate
);
226 /* Check BRR is greater than or equal to 0x300 */
227 assert_param(IS_LL_LPUART_BRR_MIN(LPUARTx
->BRR
));
229 /* Check BRR is lower than or equal to 0xFFFFF */
230 assert_param(IS_LL_LPUART_BRR_MAX(LPUARTx
->BRR
));
233 /*---------------------------- LPUART PRESC Configuration -----------------------
234 * Configure LPUARTx PRESC (Prescaler) with parameters:
235 * - PrescalerValue: LPUART_PRESC_PRESCALER bits according to LPUART_InitStruct->PrescalerValue value.
237 LL_LPUART_SetPrescaler(LPUARTx
, LPUART_InitStruct
->PrescalerValue
);
244 * @brief Set each @ref LL_LPUART_InitTypeDef field to default value.
245 * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
246 * whose fields will be set to default values.
250 void LL_LPUART_StructInit(LL_LPUART_InitTypeDef
*LPUART_InitStruct
)
252 /* Set LPUART_InitStruct fields to default values */
253 LPUART_InitStruct
->PrescalerValue
= LL_LPUART_PRESCALER_DIV1
;
254 LPUART_InitStruct
->BaudRate
= 9600U;
255 LPUART_InitStruct
->DataWidth
= LL_LPUART_DATAWIDTH_8B
;
256 LPUART_InitStruct
->StopBits
= LL_LPUART_STOPBITS_1
;
257 LPUART_InitStruct
->Parity
= LL_LPUART_PARITY_NONE
;
258 LPUART_InitStruct
->TransferDirection
= LL_LPUART_DIRECTION_TX_RX
;
259 LPUART_InitStruct
->HardwareFlowControl
= LL_LPUART_HWCONTROL_NONE
;
274 #endif /* defined (LPUART1) */
280 #endif /* USE_FULL_LL_DRIVER */
282 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/