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