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