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