STM32L443xx HAL User Manual
stm32l4xx_ll_gpio.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_ll_gpio.c
00004   * @author  MCD Application Team
00005   * @brief   GPIO LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * Copyright (c) 2017 STMicroelectronics.
00010   * All rights reserved.
00011   *
00012   * This software is licensed under terms that can be found in the LICENSE file
00013   * in the root directory of this software component.
00014   * If no LICENSE file comes with this software, it is provided AS-IS.
00015   *
00016   ******************************************************************************
00017   */
00018 #if defined(USE_FULL_LL_DRIVER)
00019 
00020 /* Includes ------------------------------------------------------------------*/
00021 #include "stm32l4xx_ll_gpio.h"
00022 #include "stm32l4xx_ll_bus.h"
00023 #ifdef  USE_FULL_ASSERT
00024 #include "stm32_assert.h"
00025 #else
00026 #define assert_param(expr) ((void)0U)
00027 #endif
00028 
00029 /** @addtogroup STM32L4xx_LL_Driver
00030   * @{
00031   */
00032 
00033 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI)
00034 
00035 /** @addtogroup GPIO_LL
00036   * @{
00037   */
00038 /** MISRA C:2012 deviation rule has been granted for following rules:
00039   * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
00040   * range of the shift operator in following API :
00041   * LL_GPIO_Init
00042   */
00043 
00044 /* Private types -------------------------------------------------------------*/
00045 /* Private variables ---------------------------------------------------------*/
00046 /* Private constants ---------------------------------------------------------*/
00047 /* Private macros ------------------------------------------------------------*/
00048 /** @addtogroup GPIO_LL_Private_Macros
00049   * @{
00050   */
00051 #define IS_LL_GPIO_PIN(__VALUE__)          (((0x00u) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
00052 
00053 #define IS_LL_GPIO_MODE(__VALUE__)         (((__VALUE__) == LL_GPIO_MODE_INPUT)     ||\
00054                                             ((__VALUE__) == LL_GPIO_MODE_OUTPUT)    ||\
00055                                             ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
00056                                             ((__VALUE__) == LL_GPIO_MODE_ANALOG))
00057 
00058 #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__)  (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL)  ||\
00059                                             ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
00060 
00061 #define IS_LL_GPIO_SPEED(__VALUE__)        (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW)       ||\
00062                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM)    ||\
00063                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH)      ||\
00064                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
00065 
00066 #define IS_LL_GPIO_PULL(__VALUE__)         (((__VALUE__) == LL_GPIO_PULL_NO)   ||\
00067                                             ((__VALUE__) == LL_GPIO_PULL_UP)   ||\
00068                                             ((__VALUE__) == LL_GPIO_PULL_DOWN))
00069 
00070 #define IS_LL_GPIO_ALTERNATE(__VALUE__)    (((__VALUE__) == LL_GPIO_AF_0  )   ||\
00071                                             ((__VALUE__) == LL_GPIO_AF_1  )   ||\
00072                                             ((__VALUE__) == LL_GPIO_AF_2  )   ||\
00073                                             ((__VALUE__) == LL_GPIO_AF_3  )   ||\
00074                                             ((__VALUE__) == LL_GPIO_AF_4  )   ||\
00075                                             ((__VALUE__) == LL_GPIO_AF_5  )   ||\
00076                                             ((__VALUE__) == LL_GPIO_AF_6  )   ||\
00077                                             ((__VALUE__) == LL_GPIO_AF_7  )   ||\
00078                                             ((__VALUE__) == LL_GPIO_AF_8  )   ||\
00079                                             ((__VALUE__) == LL_GPIO_AF_9  )   ||\
00080                                             ((__VALUE__) == LL_GPIO_AF_10 )   ||\
00081                                             ((__VALUE__) == LL_GPIO_AF_11 )   ||\
00082                                             ((__VALUE__) == LL_GPIO_AF_12 )   ||\
00083                                             ((__VALUE__) == LL_GPIO_AF_13 )   ||\
00084                                             ((__VALUE__) == LL_GPIO_AF_14 )   ||\
00085                                             ((__VALUE__) == LL_GPIO_AF_15 ))
00086 /**
00087   * @}
00088   */
00089 
00090 /* Private function prototypes -----------------------------------------------*/
00091 
00092 /* Exported functions --------------------------------------------------------*/
00093 /** @addtogroup GPIO_LL_Exported_Functions
00094   * @{
00095   */
00096 
00097 /** @addtogroup GPIO_LL_EF_Init
00098   * @{
00099   */
00100 
00101 /**
00102   * @brief  De-initialize GPIO registers (Registers restored to their default values).
00103   * @param  GPIOx GPIO Port
00104   * @retval An ErrorStatus enumeration value:
00105   *          - SUCCESS: GPIO registers are de-initialized
00106   *          - ERROR:   Wrong GPIO Port
00107   */
00108 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
00109 {
00110   ErrorStatus status = SUCCESS;
00111 
00112   /* Check the parameters */
00113   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
00114 
00115   /* Force and Release reset on clock of GPIOx Port */
00116   if (GPIOx == GPIOA)
00117   {
00118     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOA);
00119     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOA);
00120   }
00121   else if (GPIOx == GPIOB)
00122   {
00123     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOB);
00124     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOB);
00125   }
00126   else if (GPIOx == GPIOC)
00127   {
00128     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOC);
00129     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOC);
00130   }
00131 #if defined(GPIOD)
00132   else if (GPIOx == GPIOD)
00133   {
00134     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOD);
00135     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOD);
00136   }
00137 #endif /* GPIOD */
00138 #if defined(GPIOE)
00139   else if (GPIOx == GPIOE)
00140   {
00141     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOE);
00142     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOE);
00143   }
00144 #endif /* GPIOE */
00145 #if defined(GPIOF)
00146   else if (GPIOx == GPIOF)
00147   {
00148     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOF);
00149     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOF);
00150   }
00151 #endif /* GPIOF */
00152 #if defined(GPIOG)
00153   else if (GPIOx == GPIOG)
00154   {
00155     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOG);
00156     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOG);
00157   }
00158 #endif /* GPIOG */
00159 #if defined(GPIOH)
00160   else if (GPIOx == GPIOH)
00161   {
00162     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOH);
00163     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOH);
00164   }
00165 #endif /* GPIOH */
00166 #if defined(GPIOI)
00167   else if (GPIOx == GPIOI)
00168   {
00169     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOI);
00170     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOI);
00171   }
00172 #endif /* GPIOI */
00173   else
00174   {
00175     status = ERROR;
00176   }
00177 
00178   return (status);
00179 }
00180 
00181 /**
00182   * @brief  Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
00183   * @param  GPIOx GPIO Port
00184   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
00185   *         that contains the configuration information for the specified GPIO peripheral.
00186   * @retval An ErrorStatus enumeration value:
00187   *          - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
00188   *          - ERROR:   Not applicable
00189   */
00190 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
00191 {
00192   uint32_t pinpos;
00193   uint32_t currentpin;
00194 
00195   /* Check the parameters */
00196   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
00197   assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
00198   assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
00199   assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
00200 
00201   /* ------------------------- Configure the port pins ---------------- */
00202   /* Initialize  pinpos on first pin set */
00203   pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
00204 
00205   /* Configure the port pins */
00206   while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
00207   {
00208     /* Get current io position */
00209     currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
00210 
00211     if (currentpin != 0x00u)
00212     {
00213       if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
00214       {
00215         /* Check Speed mode parameters */
00216         assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
00217 
00218         /* Speed mode configuration */
00219         LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
00220 
00221         /* Check Output mode parameters */
00222         assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
00223 
00224         /* Output mode configuration*/
00225         LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
00226       }
00227 
00228       /* Pull-up Pull down resistor configuration*/
00229       LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
00230 
00231       if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
00232       {
00233         /* Check Alternate parameter */
00234         assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
00235 
00236         /* Speed mode configuration */
00237         if (currentpin < LL_GPIO_PIN_8)
00238         {
00239           LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
00240         }
00241         else
00242         {
00243           LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
00244         }
00245       }
00246 
00247       /* Pin Mode configuration */
00248       LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
00249     }
00250     pinpos++;
00251   }
00252 
00253   return (SUCCESS);
00254 }
00255 
00256 /**
00257   * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
00258   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
00259   *                          whose fields will be set to default values.
00260   * @retval None
00261   */
00262 
00263 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
00264 {
00265   /* Reset GPIO init structure parameters values */
00266   GPIO_InitStruct->Pin        = LL_GPIO_PIN_ALL;
00267   GPIO_InitStruct->Mode       = LL_GPIO_MODE_ANALOG;
00268   GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_LOW;
00269   GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
00270   GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
00271   GPIO_InitStruct->Alternate  = LL_GPIO_AF_0;
00272 }
00273 
00274 /**
00275   * @}
00276   */
00277 
00278 /**
00279   * @}
00280   */
00281 
00282 /**
00283   * @}
00284   */
00285 
00286 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) */
00287 
00288 /**
00289   * @}
00290   */
00291 
00292 #endif /* USE_FULL_LL_DRIVER */
00293