mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
186:707f6e361f3e
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 158:b23ee177fd68 1 /**
Kojto 158:b23ee177fd68 2 ******************************************************************************
Kojto 158:b23ee177fd68 3 * @file stm32l0xx_ll_lpuart.c
Kojto 158:b23ee177fd68 4 * @author MCD Application Team
Kojto 158:b23ee177fd68 5 * @brief LPUART LL module driver.
Kojto 158:b23ee177fd68 6 ******************************************************************************
Kojto 158:b23ee177fd68 7 * @attention
Kojto 158:b23ee177fd68 8 *
Kojto 158:b23ee177fd68 9 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
Kojto 158:b23ee177fd68 10 *
Kojto 158:b23ee177fd68 11 * Redistribution and use in source and binary forms, with or without modification,
Kojto 158:b23ee177fd68 12 * are permitted provided that the following conditions are met:
Kojto 158:b23ee177fd68 13 * 1. Redistributions of source code must retain the above copyright notice,
Kojto 158:b23ee177fd68 14 * this list of conditions and the following disclaimer.
Kojto 158:b23ee177fd68 15 * 2. Redistributions in binary form must reproduce the above copyright notice,
Kojto 158:b23ee177fd68 16 * this list of conditions and the following disclaimer in the documentation
Kojto 158:b23ee177fd68 17 * and/or other materials provided with the distribution.
Kojto 158:b23ee177fd68 18 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Kojto 158:b23ee177fd68 19 * may be used to endorse or promote products derived from this software
Kojto 158:b23ee177fd68 20 * without specific prior written permission.
Kojto 158:b23ee177fd68 21 *
Kojto 158:b23ee177fd68 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Kojto 158:b23ee177fd68 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Kojto 158:b23ee177fd68 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Kojto 158:b23ee177fd68 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Kojto 158:b23ee177fd68 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Kojto 158:b23ee177fd68 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Kojto 158:b23ee177fd68 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Kojto 158:b23ee177fd68 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Kojto 158:b23ee177fd68 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Kojto 158:b23ee177fd68 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kojto 158:b23ee177fd68 32 *
Kojto 158:b23ee177fd68 33 ******************************************************************************
Kojto 158:b23ee177fd68 34 */
Kojto 158:b23ee177fd68 35 #if defined(USE_FULL_LL_DRIVER)
Kojto 158:b23ee177fd68 36
Kojto 158:b23ee177fd68 37 /* Includes ------------------------------------------------------------------*/
Kojto 158:b23ee177fd68 38 #include "stm32l0xx_ll_lpuart.h"
Kojto 158:b23ee177fd68 39 #include "stm32l0xx_ll_rcc.h"
Kojto 158:b23ee177fd68 40 #include "stm32l0xx_ll_bus.h"
Kojto 158:b23ee177fd68 41 #ifdef USE_FULL_ASSERT
Kojto 158:b23ee177fd68 42 #include "stm32_assert.h"
Kojto 158:b23ee177fd68 43 #else
Kojto 158:b23ee177fd68 44 #define assert_param(expr) ((void)0U)
Kojto 158:b23ee177fd68 45 #endif
Kojto 158:b23ee177fd68 46
Kojto 158:b23ee177fd68 47 /** @addtogroup STM32L0xx_LL_Driver
Kojto 158:b23ee177fd68 48 * @{
Kojto 158:b23ee177fd68 49 */
Kojto 158:b23ee177fd68 50
Kojto 158:b23ee177fd68 51 #if defined (LPUART1)
Kojto 158:b23ee177fd68 52
Kojto 158:b23ee177fd68 53 /** @addtogroup LPUART_LL
Kojto 158:b23ee177fd68 54 * @{
Kojto 158:b23ee177fd68 55 */
Kojto 158:b23ee177fd68 56
Kojto 158:b23ee177fd68 57 /* Private types -------------------------------------------------------------*/
Kojto 158:b23ee177fd68 58 /* Private variables ---------------------------------------------------------*/
Kojto 158:b23ee177fd68 59 /* Private constants ---------------------------------------------------------*/
Kojto 158:b23ee177fd68 60 /** @addtogroup LPUART_LL_Private_Constants
Kojto 158:b23ee177fd68 61 * @{
Kojto 158:b23ee177fd68 62 */
Kojto 158:b23ee177fd68 63
Kojto 158:b23ee177fd68 64 /**
Kojto 158:b23ee177fd68 65 * @}
Kojto 158:b23ee177fd68 66 */
Kojto 158:b23ee177fd68 67
Kojto 158:b23ee177fd68 68
Kojto 158:b23ee177fd68 69 /* Private macros ------------------------------------------------------------*/
Kojto 158:b23ee177fd68 70 /** @addtogroup LPUART_LL_Private_Macros
Kojto 158:b23ee177fd68 71 * @{
Kojto 158:b23ee177fd68 72 */
Kojto 158:b23ee177fd68 73
Kojto 158:b23ee177fd68 74 /* Check of parameters for configuration of LPUART registers */
Kojto 158:b23ee177fd68 75
Kojto 158:b23ee177fd68 76 /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register */
Kojto 158:b23ee177fd68 77 /* value : */
Kojto 158:b23ee177fd68 78 /* - fck must be in the range [3 x baudrate, 4096 x baudrate] */
Kojto 158:b23ee177fd68 79 /* - LPUART_BRR register value should be >= 0x300 */
Kojto 158:b23ee177fd68 80 /* - LPUART_BRR register value should be <= 0xFFFFF (20 bits) */
Kojto 158:b23ee177fd68 81 /* Baudrate specified by the user should belong to [8, 10600000].*/
Kojto 158:b23ee177fd68 82 #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 10600000U) && ((__BAUDRATE__) >= 8U))
Kojto 158:b23ee177fd68 83
Kojto 158:b23ee177fd68 84 #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \
Kojto 158:b23ee177fd68 85 || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \
Kojto 158:b23ee177fd68 86 || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \
Kojto 158:b23ee177fd68 87 || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX))
Kojto 158:b23ee177fd68 88
Kojto 158:b23ee177fd68 89 #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \
Kojto 158:b23ee177fd68 90 || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \
Kojto 158:b23ee177fd68 91 || ((__VALUE__) == LL_LPUART_PARITY_ODD))
Kojto 158:b23ee177fd68 92
Kojto 158:b23ee177fd68 93 #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \
Kojto 158:b23ee177fd68 94 || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \
Kojto 158:b23ee177fd68 95 || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B))
Kojto 158:b23ee177fd68 96
Kojto 158:b23ee177fd68 97 #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \
Kojto 158:b23ee177fd68 98 || ((__VALUE__) == LL_LPUART_STOPBITS_2))
Kojto 158:b23ee177fd68 99
Kojto 158:b23ee177fd68 100 #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \
Kojto 158:b23ee177fd68 101 || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \
Kojto 158:b23ee177fd68 102 || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \
Kojto 158:b23ee177fd68 103 || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS))
Kojto 158:b23ee177fd68 104
Kojto 158:b23ee177fd68 105 /**
Kojto 158:b23ee177fd68 106 * @}
Kojto 158:b23ee177fd68 107 */
Kojto 158:b23ee177fd68 108
Kojto 158:b23ee177fd68 109 /* Private function prototypes -----------------------------------------------*/
Kojto 158:b23ee177fd68 110
Kojto 158:b23ee177fd68 111 /* Exported functions --------------------------------------------------------*/
Kojto 158:b23ee177fd68 112 /** @addtogroup LPUART_LL_Exported_Functions
Kojto 158:b23ee177fd68 113 * @{
Kojto 158:b23ee177fd68 114 */
Kojto 158:b23ee177fd68 115
Kojto 158:b23ee177fd68 116 /** @addtogroup LPUART_LL_EF_Init
Kojto 158:b23ee177fd68 117 * @{
Kojto 158:b23ee177fd68 118 */
Kojto 158:b23ee177fd68 119
Kojto 158:b23ee177fd68 120 /**
Kojto 158:b23ee177fd68 121 * @brief De-initialize LPUART registers (Registers restored to their default values).
Kojto 158:b23ee177fd68 122 * @param LPUARTx LPUART Instance
Kojto 158:b23ee177fd68 123 * @retval An ErrorStatus enumeration value:
Kojto 158:b23ee177fd68 124 * - SUCCESS: LPUART registers are de-initialized
Kojto 158:b23ee177fd68 125 * - ERROR: not applicable
Kojto 158:b23ee177fd68 126 */
Kojto 158:b23ee177fd68 127 ErrorStatus LL_LPUART_DeInit(USART_TypeDef *LPUARTx)
Kojto 158:b23ee177fd68 128 {
Kojto 158:b23ee177fd68 129 ErrorStatus status = SUCCESS;
Kojto 158:b23ee177fd68 130
Kojto 158:b23ee177fd68 131 /* Check the parameters */
Kojto 158:b23ee177fd68 132 assert_param(IS_LPUART_INSTANCE(LPUARTx));
Kojto 158:b23ee177fd68 133
Kojto 158:b23ee177fd68 134 /* Force reset of LPUART peripheral */
Kojto 158:b23ee177fd68 135 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPUART1);
Kojto 158:b23ee177fd68 136
Kojto 158:b23ee177fd68 137 /* Release reset of LPUART peripheral */
Kojto 158:b23ee177fd68 138 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPUART1);
Kojto 158:b23ee177fd68 139
Kojto 158:b23ee177fd68 140 return (status);
Kojto 158:b23ee177fd68 141 }
Kojto 158:b23ee177fd68 142
Kojto 158:b23ee177fd68 143 /**
Kojto 158:b23ee177fd68 144 * @brief Initialize LPUART registers according to the specified
Kojto 158:b23ee177fd68 145 * parameters in LPUART_InitStruct.
Kojto 158:b23ee177fd68 146 * @note As some bits in LPUART configuration registers can only be written when the LPUART is disabled (USART_CR1_UE bit =0),
Kojto 158:b23ee177fd68 147 * LPUART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
Kojto 158:b23ee177fd68 148 * @note Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0).
Kojto 158:b23ee177fd68 149 * @param LPUARTx LPUART Instance
Kojto 158:b23ee177fd68 150 * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
Kojto 158:b23ee177fd68 151 * that contains the configuration information for the specified LPUART peripheral.
Kojto 158:b23ee177fd68 152 * @retval An ErrorStatus enumeration value:
Kojto 158:b23ee177fd68 153 * - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content
Kojto 158:b23ee177fd68 154 * - ERROR: Problem occurred during LPUART Registers initialization
Kojto 158:b23ee177fd68 155 */
Kojto 158:b23ee177fd68 156 ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, LL_LPUART_InitTypeDef *LPUART_InitStruct)
Kojto 158:b23ee177fd68 157 {
Kojto 158:b23ee177fd68 158 ErrorStatus status = ERROR;
Kojto 158:b23ee177fd68 159 uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
Kojto 158:b23ee177fd68 160
Kojto 158:b23ee177fd68 161 /* Check the parameters */
Kojto 158:b23ee177fd68 162 assert_param(IS_LPUART_INSTANCE(LPUARTx));
Kojto 158:b23ee177fd68 163 assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct->BaudRate));
Kojto 158:b23ee177fd68 164 assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct->DataWidth));
Kojto 158:b23ee177fd68 165 assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct->StopBits));
Kojto 158:b23ee177fd68 166 assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct->Parity));
Kojto 158:b23ee177fd68 167 assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct->TransferDirection));
Kojto 158:b23ee177fd68 168 assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct->HardwareFlowControl));
Kojto 158:b23ee177fd68 169
Kojto 158:b23ee177fd68 170 /* LPUART needs to be in disabled state, in order to be able to configure some bits in
Kojto 158:b23ee177fd68 171 CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */
Kojto 158:b23ee177fd68 172 if (LL_LPUART_IsEnabled(LPUARTx) == 0U)
Kojto 158:b23ee177fd68 173 {
Kojto 158:b23ee177fd68 174 /*---------------------------- LPUART CR1 Configuration -----------------------
Kojto 158:b23ee177fd68 175 * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters:
Kojto 158:b23ee177fd68 176 * - DataWidth: USART_CR1_M bits according to LPUART_InitStruct->DataWidth value
Kojto 158:b23ee177fd68 177 * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value
Kojto 158:b23ee177fd68 178 * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value
Kojto 158:b23ee177fd68 179 */
Kojto 158:b23ee177fd68 180 MODIFY_REG(LPUARTx->CR1,
Kojto 158:b23ee177fd68 181 (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
Kojto 158:b23ee177fd68 182 (LPUART_InitStruct->DataWidth | LPUART_InitStruct->Parity | LPUART_InitStruct->TransferDirection));
Kojto 158:b23ee177fd68 183
Kojto 158:b23ee177fd68 184 /*---------------------------- LPUART CR2 Configuration -----------------------
Kojto 158:b23ee177fd68 185 * Configure LPUARTx CR2 (Stop bits) with parameters:
Kojto 158:b23ee177fd68 186 * - Stop Bits: USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value.
Kojto 158:b23ee177fd68 187 */
Kojto 158:b23ee177fd68 188 LL_LPUART_SetStopBitsLength(LPUARTx, LPUART_InitStruct->StopBits);
Kojto 158:b23ee177fd68 189
Kojto 158:b23ee177fd68 190 /*---------------------------- LPUART CR3 Configuration -----------------------
Kojto 158:b23ee177fd68 191 * Configure LPUARTx CR3 (Hardware Flow Control) with parameters:
Kojto 158:b23ee177fd68 192 * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to LPUART_InitStruct->HardwareFlowControl value.
Kojto 158:b23ee177fd68 193 */
Kojto 158:b23ee177fd68 194 LL_LPUART_SetHWFlowCtrl(LPUARTx, LPUART_InitStruct->HardwareFlowControl);
Kojto 158:b23ee177fd68 195
Kojto 158:b23ee177fd68 196 /*---------------------------- LPUART BRR Configuration -----------------------
Kojto 158:b23ee177fd68 197 * Retrieve Clock frequency used for LPUART Peripheral
Kojto 158:b23ee177fd68 198 */
Kojto 158:b23ee177fd68 199 periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
Kojto 158:b23ee177fd68 200
Kojto 158:b23ee177fd68 201 /* Configure the LPUART Baud Rate :
Kojto 158:b23ee177fd68 202 - valid baud rate value (different from 0) is required
Kojto 158:b23ee177fd68 203 - Peripheral clock as returned by RCC service, should be valid (different from 0).
Kojto 158:b23ee177fd68 204 */
Kojto 158:b23ee177fd68 205 if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
Kojto 158:b23ee177fd68 206 && (LPUART_InitStruct->BaudRate != 0U))
Kojto 158:b23ee177fd68 207 {
Kojto 158:b23ee177fd68 208 status = SUCCESS;
Kojto 158:b23ee177fd68 209 LL_LPUART_SetBaudRate(LPUARTx,
Kojto 158:b23ee177fd68 210 periphclk,
Kojto 158:b23ee177fd68 211 LPUART_InitStruct->BaudRate);
Kojto 158:b23ee177fd68 212 }
Kojto 158:b23ee177fd68 213 }
Kojto 158:b23ee177fd68 214
Kojto 158:b23ee177fd68 215 return (status);
Kojto 158:b23ee177fd68 216 }
Kojto 158:b23ee177fd68 217
Kojto 158:b23ee177fd68 218 /**
Kojto 158:b23ee177fd68 219 * @brief Set each @ref LL_LPUART_InitTypeDef field to default value.
Kojto 158:b23ee177fd68 220 * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
Kojto 158:b23ee177fd68 221 * whose fields will be set to default values.
Kojto 158:b23ee177fd68 222 * @retval None
Kojto 158:b23ee177fd68 223 */
Kojto 158:b23ee177fd68 224
Kojto 158:b23ee177fd68 225 void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct)
Kojto 158:b23ee177fd68 226 {
Kojto 158:b23ee177fd68 227 /* Set LPUART_InitStruct fields to default values */
Kojto 158:b23ee177fd68 228 LPUART_InitStruct->BaudRate = 9600U;
Kojto 158:b23ee177fd68 229 LPUART_InitStruct->DataWidth = LL_LPUART_DATAWIDTH_8B;
Kojto 158:b23ee177fd68 230 LPUART_InitStruct->StopBits = LL_LPUART_STOPBITS_1;
Kojto 158:b23ee177fd68 231 LPUART_InitStruct->Parity = LL_LPUART_PARITY_NONE ;
Kojto 158:b23ee177fd68 232 LPUART_InitStruct->TransferDirection = LL_LPUART_DIRECTION_TX_RX;
Kojto 158:b23ee177fd68 233 LPUART_InitStruct->HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
Kojto 158:b23ee177fd68 234 }
Kojto 158:b23ee177fd68 235
Kojto 158:b23ee177fd68 236 /**
Kojto 158:b23ee177fd68 237 * @}
Kojto 158:b23ee177fd68 238 */
Kojto 158:b23ee177fd68 239
Kojto 158:b23ee177fd68 240 /**
Kojto 158:b23ee177fd68 241 * @}
Kojto 158:b23ee177fd68 242 */
Kojto 158:b23ee177fd68 243
Kojto 158:b23ee177fd68 244 /**
Kojto 158:b23ee177fd68 245 * @}
Kojto 158:b23ee177fd68 246 */
Kojto 158:b23ee177fd68 247
Kojto 158:b23ee177fd68 248 #endif /* defined (LPUART1) */
Kojto 158:b23ee177fd68 249
Kojto 158:b23ee177fd68 250 /**
Kojto 158:b23ee177fd68 251 * @}
Kojto 158:b23ee177fd68 252 */
Kojto 158:b23ee177fd68 253
Kojto 158:b23ee177fd68 254 #endif /* USE_FULL_LL_DRIVER */
Kojto 158:b23ee177fd68 255
Kojto 158:b23ee177fd68 256 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Kojto 158:b23ee177fd68 257