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 /* 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 #if defined(USART_PRESC_PRESCALER)
63 #define IS_LL_LPUART_PRESCALER(__VALUE__)  (((__VALUE__) == LL_LPUART_PRESCALER_DIV1) \
64                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV2) \
65                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV4) \
66                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV6) \
67                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV8) \
68                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV10) \
69                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV12) \
70                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV16) \
71                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV32) \
72                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV64) \
73                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV128) \
74                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV256))
75 
76 #endif /* USART_PRESC_PRESCALER */
77 /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register   */
78 /*              value :                                                       */
79 /*                - fck must be in the range [3 x baudrate, 4096 x baudrate]  */
80 /*                - LPUART_BRR register value should be >= 0x300              */
81 /*                - LPUART_BRR register value should be <= 0xFFFFF (20 bits)  */
82 /*              Baudrate specified by the user should belong to [8, 40000000].*/
83 #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 40000000U) && ((__BAUDRATE__) >= 8U))
84 
85 /* __VALUE__ BRR content must be greater than or equal to 0x300. */
86 #define IS_LL_LPUART_BRR_MIN(__VALUE__)   ((__VALUE__) >= 0x300U)
87 
88 /* __VALUE__ BRR content must be lower than or equal to 0xFFFFF. */
89 #define IS_LL_LPUART_BRR_MAX(__VALUE__)   ((__VALUE__) <= 0x000FFFFFU)
90 
91 #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \
92                                            || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \
93                                            || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \
94                                            || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX))
95 
96 #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \
97                                         || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \
98                                         || ((__VALUE__) == LL_LPUART_PARITY_ODD))
99 
100 #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \
101                                            || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \
102                                            || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B))
103 
104 #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \
105                                           || ((__VALUE__) == LL_LPUART_STOPBITS_2))
106 
107 #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \
108                                            || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \
109                                            || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \
110                                            || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS))
111 
112 /**
113   * @}
114   */
115 
116 /* Private function prototypes -----------------------------------------------*/
117 
118 /* Exported functions --------------------------------------------------------*/
119 /** @addtogroup LPUART_LL_Exported_Functions
120   * @{
121   */
122 
123 /** @addtogroup LPUART_LL_EF_Init
124   * @{
125   */
126 
127 /**
128   * @brief  De-initialize LPUART registers (Registers restored to their default values).
129   * @param  LPUARTx LPUART Instance
130   * @retval An ErrorStatus enumeration value:
131   *          - SUCCESS: LPUART registers are de-initialized
132   *          - ERROR: not applicable
133   */
LL_LPUART_DeInit(const USART_TypeDef * LPUARTx)134 ErrorStatus LL_LPUART_DeInit(const USART_TypeDef *LPUARTx)
135 {
136   ErrorStatus status = SUCCESS;
137 
138   /* Check the parameters */
139   assert_param(IS_LPUART_INSTANCE(LPUARTx));
140 
141   if (LPUARTx == LPUART1)
142   {
143     /* Force reset of LPUART peripheral */
144     LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_LPUART1);
145 
146     /* Release reset of LPUART peripheral */
147     LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_LPUART1);
148   }
149   else
150   {
151     status = ERROR;
152   }
153 
154   return (status);
155 }
156 
157 /**
158   * @brief  Initialize LPUART registers according to the specified
159   *         parameters in LPUART_InitStruct.
160   * @note   As some bits in LPUART configuration registers can only be written when
161   *         the LPUART is disabled (USART_CR1_UE bit =0),
162   *         LPUART Peripheral should be in disabled state prior calling this function.
163   *         Otherwise, ERROR result will be returned.
164   * @note   Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0).
165   * @param  LPUARTx LPUART Instance
166   * @param  LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
167   *         that contains the configuration information for the specified LPUART peripheral.
168   * @retval An ErrorStatus enumeration value:
169   *          - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content
170   *          - ERROR: Problem occurred during LPUART Registers initialization
171   */
LL_LPUART_Init(USART_TypeDef * LPUARTx,const LL_LPUART_InitTypeDef * LPUART_InitStruct)172 ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, const LL_LPUART_InitTypeDef *LPUART_InitStruct)
173 {
174   ErrorStatus status = ERROR;
175   uint32_t periphclk;
176 
177   /* Check the parameters */
178   assert_param(IS_LPUART_INSTANCE(LPUARTx));
179 #if defined(USART_PRESC_PRESCALER)
180   assert_param(IS_LL_LPUART_PRESCALER(LPUART_InitStruct->PrescalerValue));
181 #endif /* USART_PRESC_PRESCALER */
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     periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
220 
221     /* Configure the LPUART Baud Rate :
222     #if defined(USART_PRESC_PRESCALER)
223        - prescaler value is required
224     #endif
225        - valid baud rate value (different from 0) is required
226        - Peripheral clock as returned by RCC service, should be valid (different from 0).
227     */
228     if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
229         && (LPUART_InitStruct->BaudRate != 0U))
230     {
231       status = SUCCESS;
232       LL_LPUART_SetBaudRate(LPUARTx,
233                             periphclk,
234 #if defined(USART_PRESC_PRESCALER)
235                             LPUART_InitStruct->PrescalerValue,
236 #endif /* USART_PRESC_PRESCALER */
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 #if defined(USART_PRESC_PRESCALER)
247     /*---------------------------- LPUART PRESC Configuration -----------------------
248      * Configure LPUARTx PRESC (Prescaler) with parameters:
249      * - PrescalerValue: LPUART_PRESC_PRESCALER bits according to LPUART_InitStruct->PrescalerValue value.
250      */
251     LL_LPUART_SetPrescaler(LPUARTx, LPUART_InitStruct->PrescalerValue);
252 #endif /* USART_PRESC_PRESCALER */
253   }
254 
255   return (status);
256 }
257 
258 /**
259   * @brief Set each @ref LL_LPUART_InitTypeDef field to default value.
260   * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
261   *                          whose fields will be set to default values.
262   * @retval None
263   */
264 
LL_LPUART_StructInit(LL_LPUART_InitTypeDef * LPUART_InitStruct)265 void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct)
266 {
267   /* Set LPUART_InitStruct fields to default values */
268 #if defined(USART_PRESC_PRESCALER)
269   LPUART_InitStruct->PrescalerValue      = LL_LPUART_PRESCALER_DIV1;
270 #endif /* USART_PRESC_PRESCALER */
271   LPUART_InitStruct->BaudRate            = LPUART_DEFAULT_BAUDRATE;
272   LPUART_InitStruct->DataWidth           = LL_LPUART_DATAWIDTH_8B;
273   LPUART_InitStruct->StopBits            = LL_LPUART_STOPBITS_1;
274   LPUART_InitStruct->Parity              = LL_LPUART_PARITY_NONE ;
275   LPUART_InitStruct->TransferDirection   = LL_LPUART_DIRECTION_TX_RX;
276   LPUART_InitStruct->HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
277 }
278 
279 /**
280   * @}
281   */
282 
283 /**
284   * @}
285   */
286 
287 /**
288   * @}
289   */
290 
291 #endif /* LPUART1 */
292 
293 /**
294   * @}
295   */
296 
297 #endif /* USE_FULL_LL_DRIVER */
298