TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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