| 1 | /**
|
|---|
| 2 | ******************************************************************************
|
|---|
| 3 | * @file stm32g0xx_ll_lpuart.c
|
|---|
| 4 | * @author MCD Application Team
|
|---|
| 5 | * @brief LPUART LL module driver.
|
|---|
| 6 | ******************************************************************************
|
|---|
| 7 | * @attention
|
|---|
| 8 | *
|
|---|
| 9 | * <h2><center>© Copyright (c) 2018 STMicroelectronics.
|
|---|
| 10 | * All rights reserved.</center></h2>
|
|---|
| 11 | *
|
|---|
| 12 | * This software component is licensed by ST under BSD 3-Clause license,
|
|---|
| 13 | * the "License"; You may not use this file except in compliance with the
|
|---|
| 14 | * License. You may obtain a copy of the License at:
|
|---|
| 15 | * opensource.org/licenses/BSD-3-Clause
|
|---|
| 16 | *
|
|---|
| 17 | ******************************************************************************
|
|---|
| 18 | */
|
|---|
| 19 | #if defined(USE_FULL_LL_DRIVER)
|
|---|
| 20 |
|
|---|
| 21 | /* Includes ------------------------------------------------------------------*/
|
|---|
| 22 | #include "stm32g0xx_ll_lpuart.h"
|
|---|
| 23 | #include "stm32g0xx_ll_rcc.h"
|
|---|
| 24 | #include "stm32g0xx_ll_bus.h"
|
|---|
| 25 | #ifdef USE_FULL_ASSERT
|
|---|
| 26 | #include "stm32_assert.h"
|
|---|
| 27 | #else
|
|---|
| 28 | #define assert_param(expr) ((void)0U)
|
|---|
| 29 | #endif
|
|---|
| 30 |
|
|---|
| 31 | /** @addtogroup STM32G0xx_LL_Driver
|
|---|
| 32 | * @{
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #if defined (LPUART1)
|
|---|
| 36 |
|
|---|
| 37 | /** @addtogroup LPUART_LL
|
|---|
| 38 | * @{
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | /* Private types -------------------------------------------------------------*/
|
|---|
| 42 | /* Private variables ---------------------------------------------------------*/
|
|---|
| 43 | /* Private constants ---------------------------------------------------------*/
|
|---|
| 44 | /** @addtogroup LPUART_LL_Private_Constants
|
|---|
| 45 | * @{
|
|---|
| 46 | */
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * @}
|
|---|
| 50 | */
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | /* Private macros ------------------------------------------------------------*/
|
|---|
| 54 | /** @addtogroup LPUART_LL_Private_Macros
|
|---|
| 55 | * @{
|
|---|
| 56 | */
|
|---|
| 57 |
|
|---|
| 58 | /* Check of parameters for configuration of LPUART registers */
|
|---|
| 59 |
|
|---|
| 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 | /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register */
|
|---|
| 74 | /* value : */
|
|---|
| 75 | /* - fck must be in the range [3 x baudrate, 4096 x baudrate] */
|
|---|
| 76 | /* - LPUART_BRR register value should be >= 0x300 */
|
|---|
| 77 | /* - LPUART_BRR register value should be <= 0xFFFFF (20 bits) */
|
|---|
| 78 | /* Baudrate specified by the user should belong to [8, 21300000].*/
|
|---|
| 79 | #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 21300000U) && ((__BAUDRATE__) >= 8U))
|
|---|
| 80 |
|
|---|
| 81 | /* __VALUE__ BRR content must be greater than or equal to 0x300. */
|
|---|
| 82 | #define IS_LL_LPUART_BRR_MIN(__VALUE__) ((__VALUE__) >= 0x300U)
|
|---|
| 83 |
|
|---|
| 84 | /* __VALUE__ BRR content must be lower than or equal to 0xFFFFF. */
|
|---|
| 85 | #define IS_LL_LPUART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x000FFFFFU)
|
|---|
| 86 |
|
|---|
| 87 | #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \
|
|---|
| 88 | || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \
|
|---|
| 89 | || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \
|
|---|
| 90 | || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX))
|
|---|
| 91 |
|
|---|
| 92 | #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \
|
|---|
| 93 | || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \
|
|---|
| 94 | || ((__VALUE__) == LL_LPUART_PARITY_ODD))
|
|---|
| 95 |
|
|---|
| 96 | #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \
|
|---|
| 97 | || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \
|
|---|
| 98 | || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B))
|
|---|
| 99 |
|
|---|
| 100 | #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \
|
|---|
| 101 | || ((__VALUE__) == LL_LPUART_STOPBITS_2))
|
|---|
| 102 |
|
|---|
| 103 | #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \
|
|---|
| 104 | || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \
|
|---|
| 105 | || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \
|
|---|
| 106 | || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS))
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * @}
|
|---|
| 110 | */
|
|---|
| 111 |
|
|---|
| 112 | /* Private function prototypes -----------------------------------------------*/
|
|---|
| 113 |
|
|---|
| 114 | /* Exported functions --------------------------------------------------------*/
|
|---|
| 115 | /** @addtogroup LPUART_LL_Exported_Functions
|
|---|
| 116 | * @{
|
|---|
| 117 | */
|
|---|
| 118 |
|
|---|
| 119 | /** @addtogroup LPUART_LL_EF_Init
|
|---|
| 120 | * @{
|
|---|
| 121 | */
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * @brief De-initialize LPUART registers (Registers restored to their default values).
|
|---|
| 125 | * @param LPUARTx LPUART Instance
|
|---|
| 126 | * @retval An ErrorStatus enumeration value:
|
|---|
| 127 | * - SUCCESS: LPUART registers are de-initialized
|
|---|
| 128 | * - ERROR: not applicable
|
|---|
| 129 | */
|
|---|
| 130 | ErrorStatus LL_LPUART_DeInit(USART_TypeDef *LPUARTx)
|
|---|
| 131 | {
|
|---|
| 132 | ErrorStatus status = SUCCESS;
|
|---|
| 133 |
|
|---|
| 134 | /* Check the parameters */
|
|---|
| 135 | assert_param(IS_LPUART_INSTANCE(LPUARTx));
|
|---|
| 136 |
|
|---|
| 137 | if (LPUARTx == LPUART1)
|
|---|
| 138 | {
|
|---|
| 139 | /* Force reset of LPUART peripheral */
|
|---|
| 140 | LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPUART1);
|
|---|
| 141 |
|
|---|
| 142 | /* Release reset of LPUART peripheral */
|
|---|
| 143 | LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPUART1);
|
|---|
| 144 | }
|
|---|
| 145 | else
|
|---|
| 146 | {
|
|---|
| 147 | status = ERROR;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | return (status);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /**
|
|---|
| 154 | * @brief Initialize LPUART registers according to the specified
|
|---|
| 155 | * parameters in LPUART_InitStruct.
|
|---|
| 156 | * @note As some bits in LPUART configuration registers can only be written when the LPUART is disabled (USART_CR1_UE bit =0),
|
|---|
| 157 | * LPUART Peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
|
|---|
| 158 | * @note Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0).
|
|---|
| 159 | * @param LPUARTx LPUART Instance
|
|---|
| 160 | * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
|
|---|
| 161 | * that contains the configuration information for the specified LPUART peripheral.
|
|---|
| 162 | * @retval An ErrorStatus enumeration value:
|
|---|
| 163 | * - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content
|
|---|
| 164 | * - ERROR: Problem occurred during LPUART Registers initialization
|
|---|
| 165 | */
|
|---|
| 166 | ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, LL_LPUART_InitTypeDef *LPUART_InitStruct)
|
|---|
| 167 | {
|
|---|
| 168 | ErrorStatus status = ERROR;
|
|---|
| 169 | uint32_t periphclk;
|
|---|
| 170 |
|
|---|
| 171 | /* Check the parameters */
|
|---|
| 172 | assert_param(IS_LPUART_INSTANCE(LPUARTx));
|
|---|
| 173 | assert_param(IS_LL_LPUART_PRESCALER(LPUART_InitStruct->PrescalerValue));
|
|---|
| 174 | assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct->BaudRate));
|
|---|
| 175 | assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct->DataWidth));
|
|---|
| 176 | assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct->StopBits));
|
|---|
| 177 | assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct->Parity));
|
|---|
| 178 | assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct->TransferDirection));
|
|---|
| 179 | assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct->HardwareFlowControl));
|
|---|
| 180 |
|
|---|
| 181 | /* LPUART needs to be in disabled state, in order to be able to configure some bits in
|
|---|
| 182 | CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */
|
|---|
| 183 | if (LL_LPUART_IsEnabled(LPUARTx) == 0U)
|
|---|
| 184 | {
|
|---|
| 185 | /*---------------------------- LPUART CR1 Configuration -----------------------
|
|---|
| 186 | * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters:
|
|---|
| 187 | * - DataWidth: USART_CR1_M bits according to LPUART_InitStruct->DataWidth value
|
|---|
| 188 | * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value
|
|---|
| 189 | * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value
|
|---|
| 190 | */
|
|---|
| 191 | MODIFY_REG(LPUARTx->CR1,
|
|---|
| 192 | (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
|
|---|
| 193 | (LPUART_InitStruct->DataWidth | LPUART_InitStruct->Parity | LPUART_InitStruct->TransferDirection));
|
|---|
| 194 |
|
|---|
| 195 | /*---------------------------- LPUART CR2 Configuration -----------------------
|
|---|
| 196 | * Configure LPUARTx CR2 (Stop bits) with parameters:
|
|---|
| 197 | * - Stop Bits: USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value.
|
|---|
| 198 | */
|
|---|
| 199 | LL_LPUART_SetStopBitsLength(LPUARTx, LPUART_InitStruct->StopBits);
|
|---|
| 200 |
|
|---|
| 201 | /*---------------------------- LPUART CR3 Configuration -----------------------
|
|---|
| 202 | * Configure LPUARTx CR3 (Hardware Flow Control) with parameters:
|
|---|
| 203 | * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to LPUART_InitStruct->HardwareFlowControl value.
|
|---|
| 204 | */
|
|---|
| 205 | LL_LPUART_SetHWFlowCtrl(LPUARTx, LPUART_InitStruct->HardwareFlowControl);
|
|---|
| 206 |
|
|---|
| 207 | /*---------------------------- LPUART BRR Configuration -----------------------
|
|---|
| 208 | * Retrieve Clock frequency used for LPUART Peripheral
|
|---|
| 209 | */
|
|---|
| 210 | periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
|
|---|
| 211 |
|
|---|
| 212 | /* Configure the LPUART Baud Rate :
|
|---|
| 213 | - prescaler value is required
|
|---|
| 214 | - valid baud rate value (different from 0) is required
|
|---|
| 215 | - Peripheral clock as returned by RCC service, should be valid (different from 0).
|
|---|
| 216 | */
|
|---|
| 217 | if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
|
|---|
| 218 | && (LPUART_InitStruct->BaudRate != 0U))
|
|---|
| 219 | {
|
|---|
| 220 | status = SUCCESS;
|
|---|
| 221 | LL_LPUART_SetBaudRate(LPUARTx,
|
|---|
| 222 | periphclk,
|
|---|
| 223 | LPUART_InitStruct->PrescalerValue,
|
|---|
| 224 | LPUART_InitStruct->BaudRate);
|
|---|
| 225 |
|
|---|
| 226 | /* Check BRR is greater than or equal to 0x300 */
|
|---|
| 227 | assert_param(IS_LL_LPUART_BRR_MIN(LPUARTx->BRR));
|
|---|
| 228 |
|
|---|
| 229 | /* Check BRR is lower than or equal to 0xFFFFF */
|
|---|
| 230 | assert_param(IS_LL_LPUART_BRR_MAX(LPUARTx->BRR));
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /*---------------------------- LPUART PRESC Configuration -----------------------
|
|---|
| 234 | * Configure LPUARTx PRESC (Prescaler) with parameters:
|
|---|
| 235 | * - PrescalerValue: LPUART_PRESC_PRESCALER bits according to LPUART_InitStruct->PrescalerValue value.
|
|---|
| 236 | */
|
|---|
| 237 | LL_LPUART_SetPrescaler(LPUARTx, LPUART_InitStruct->PrescalerValue);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | return (status);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /**
|
|---|
| 244 | * @brief Set each @ref LL_LPUART_InitTypeDef field to default value.
|
|---|
| 245 | * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
|
|---|
| 246 | * whose fields will be set to default values.
|
|---|
| 247 | * @retval None
|
|---|
| 248 | */
|
|---|
| 249 |
|
|---|
| 250 | void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct)
|
|---|
| 251 | {
|
|---|
| 252 | /* Set LPUART_InitStruct fields to default values */
|
|---|
| 253 | LPUART_InitStruct->PrescalerValue = LL_LPUART_PRESCALER_DIV1;
|
|---|
| 254 | LPUART_InitStruct->BaudRate = 9600U;
|
|---|
| 255 | LPUART_InitStruct->DataWidth = LL_LPUART_DATAWIDTH_8B;
|
|---|
| 256 | LPUART_InitStruct->StopBits = LL_LPUART_STOPBITS_1;
|
|---|
| 257 | LPUART_InitStruct->Parity = LL_LPUART_PARITY_NONE ;
|
|---|
| 258 | LPUART_InitStruct->TransferDirection = LL_LPUART_DIRECTION_TX_RX;
|
|---|
| 259 | LPUART_InitStruct->HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /**
|
|---|
| 263 | * @}
|
|---|
| 264 | */
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| 267 | * @}
|
|---|
| 268 | */
|
|---|
| 269 |
|
|---|
| 270 | /**
|
|---|
| 271 | * @}
|
|---|
| 272 | */
|
|---|
| 273 |
|
|---|
| 274 | #endif /* defined (LPUART1) */
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * @}
|
|---|
| 278 | */
|
|---|
| 279 |
|
|---|
| 280 | #endif /* USE_FULL_LL_DRIVER */
|
|---|
| 281 |
|
|---|
| 282 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
|---|
| 283 |
|
|---|