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