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