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