STM32L486xx 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   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00010   *
00011   * Redistribution and use in source and binary forms, with or without modification,
00012   * are permitted provided that the following conditions are met:
00013   *   1. Redistributions of source code must retain the above copyright notice,
00014   *      this list of conditions and the following disclaimer.
00015   *   2. Redistributions in binary form must reproduce the above copyright notice,
00016   *      this list of conditions and the following disclaimer in the documentation
00017   *      and/or other materials provided with the distribution.
00018   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00019   *      may be used to endorse or promote products derived from this software
00020   *      without specific prior written permission.
00021   *
00022   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00026   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00030   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032   *
00033   ******************************************************************************
00034   */
00035 #if defined(USE_FULL_LL_DRIVER)
00036 
00037 /* Includes ------------------------------------------------------------------*/
00038 #include "stm32l4xx_ll_gpio.h"
00039 #include "stm32l4xx_ll_bus.h"
00040 #ifdef  USE_FULL_ASSERT
00041 #include "stm32_assert.h"
00042 #else
00043 #define assert_param(expr) ((void)0U)
00044 #endif
00045 
00046 /** @addtogroup STM32L4xx_LL_Driver
00047   * @{
00048   */
00049 
00050 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI)
00051 
00052 /** @addtogroup GPIO_LL
00053   * @{
00054   */
00055 
00056 /* Private types -------------------------------------------------------------*/
00057 /* Private variables ---------------------------------------------------------*/
00058 /* Private constants ---------------------------------------------------------*/
00059 /* Private macros ------------------------------------------------------------*/
00060 /** @addtogroup GPIO_LL_Private_Macros
00061   * @{
00062   */
00063 #define IS_LL_GPIO_PIN(__VALUE__)          (((0x00000000U) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
00064 
00065 #define IS_LL_GPIO_MODE(__VALUE__)         (((__VALUE__) == LL_GPIO_MODE_INPUT)     ||\
00066                                             ((__VALUE__) == LL_GPIO_MODE_OUTPUT)    ||\
00067                                             ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
00068                                             ((__VALUE__) == LL_GPIO_MODE_ANALOG))
00069 
00070 #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__)  (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL)  ||\
00071                                             ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
00072 
00073 #define IS_LL_GPIO_SPEED(__VALUE__)        (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW)       ||\
00074                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM)    ||\
00075                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH)      ||\
00076                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
00077 
00078 #define IS_LL_GPIO_PULL(__VALUE__)         (((__VALUE__) == LL_GPIO_PULL_NO)   ||\
00079                                             ((__VALUE__) == LL_GPIO_PULL_UP)   ||\
00080                                             ((__VALUE__) == LL_GPIO_PULL_DOWN))
00081 
00082 #define IS_LL_GPIO_ALTERNATE(__VALUE__)    (((__VALUE__) == LL_GPIO_AF_0  )   ||\
00083                                             ((__VALUE__) == LL_GPIO_AF_1  )   ||\
00084                                             ((__VALUE__) == LL_GPIO_AF_2  )   ||\
00085                                             ((__VALUE__) == LL_GPIO_AF_3  )   ||\
00086                                             ((__VALUE__) == LL_GPIO_AF_4  )   ||\
00087                                             ((__VALUE__) == LL_GPIO_AF_5  )   ||\
00088                                             ((__VALUE__) == LL_GPIO_AF_6  )   ||\
00089                                             ((__VALUE__) == LL_GPIO_AF_7  )   ||\
00090                                             ((__VALUE__) == LL_GPIO_AF_8  )   ||\
00091                                             ((__VALUE__) == LL_GPIO_AF_9  )   ||\
00092                                             ((__VALUE__) == LL_GPIO_AF_10 )   ||\
00093                                             ((__VALUE__) == LL_GPIO_AF_11 )   ||\
00094                                             ((__VALUE__) == LL_GPIO_AF_12 )   ||\
00095                                             ((__VALUE__) == LL_GPIO_AF_13 )   ||\
00096                                             ((__VALUE__) == LL_GPIO_AF_14 )   ||\
00097                                             ((__VALUE__) == LL_GPIO_AF_15 ))
00098 /**
00099   * @}
00100   */
00101 
00102 /* Private function prototypes -----------------------------------------------*/
00103 
00104 /* Exported functions --------------------------------------------------------*/
00105 /** @addtogroup GPIO_LL_Exported_Functions
00106   * @{
00107   */
00108 
00109 /** @addtogroup GPIO_LL_EF_Init
00110   * @{
00111   */
00112 
00113 /**
00114   * @brief  De-initialize GPIO registers (Registers restored to their default values).
00115   * @param  GPIOx GPIO Port
00116   * @retval An ErrorStatus enumeration value:
00117   *          - SUCCESS: GPIO registers are de-initialized
00118   *          - ERROR:   Wrong GPIO Port
00119   */
00120 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
00121 {
00122   ErrorStatus status = SUCCESS;
00123 
00124   /* Check the parameters */
00125   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
00126 
00127   /* Force and Release reset on clock of GPIOx Port */
00128   if (GPIOx == GPIOA)
00129   {
00130     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOA);
00131     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOA);
00132   }
00133   else if (GPIOx == GPIOB)
00134   {
00135     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOB);
00136     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOB);
00137   }
00138   else if (GPIOx == GPIOC)
00139   {
00140     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOC);
00141     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOC);
00142   }
00143 #if defined(GPIOD)
00144   else if (GPIOx == GPIOD)
00145   {
00146     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOD);
00147     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOD);
00148   }
00149 #endif /* GPIOD */
00150 #if defined(GPIOE)
00151   else if (GPIOx == GPIOE)
00152   {
00153     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOE);
00154     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOE);
00155   }
00156 #endif /* GPIOE */
00157 #if defined(GPIOF)
00158   else if (GPIOx == GPIOF)
00159   {
00160     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOF);
00161     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOF);
00162   }
00163 #endif /* GPIOF */
00164 #if defined(GPIOG)
00165   else if (GPIOx == GPIOG)
00166   {
00167     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOG);
00168     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOG);
00169   }
00170 #endif /* GPIOG */
00171 #if defined(GPIOH)
00172   else if (GPIOx == GPIOH)
00173   {
00174     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOH);
00175     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOH);
00176   }
00177 #endif /* GPIOH */
00178 #if defined(GPIOI)
00179   else if (GPIOx == GPIOI)
00180   {
00181     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOI);
00182     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOI);
00183   }
00184 #endif /* GPIOI */
00185   else
00186   {
00187     status = ERROR;
00188   }
00189 
00190   return (status);
00191 }
00192 
00193 /**
00194   * @brief  Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
00195   * @param  GPIOx GPIO Port
00196   * @param  GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
00197   *         that contains the configuration information for the specified GPIO peripheral.
00198   * @retval An ErrorStatus enumeration value:
00199   *          - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
00200   *          - ERROR:   Not applicable
00201   */
00202 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
00203 {
00204   uint32_t pinpos     = 0x00000000U;
00205   uint32_t currentpin = 0x00000000U;
00206 
00207   /* Check the parameters */
00208   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
00209   assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
00210   assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
00211   assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
00212 
00213   /* ------------------------- Configure the port pins ---------------- */
00214   /* Initialize  pinpos on first pin set */
00215   pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
00216 
00217   /* Configure the port pins */
00218   while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00000000U)
00219   {
00220     /* Get current io position */
00221     currentpin = (GPIO_InitStruct->Pin) & (0x00000001U << pinpos);
00222 
00223     if (currentpin)
00224     {
00225       /* Pin Mode configuration */
00226       LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
00227 
00228       if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
00229       {
00230         /* Check Speed mode parameters */
00231         assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
00232 
00233         /* Speed mode configuration */
00234         LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
00235       }
00236 
00237       /* Pull-up Pull down resistor configuration*/
00238       LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
00239 
00240       if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
00241       {
00242         /* Check Alternate parameter */
00243         assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
00244 
00245         /* Speed mode configuration */
00246         if (POSITION_VAL(currentpin) < 0x00000008U)
00247         {
00248           LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
00249         }
00250         else
00251         {
00252           LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
00253         }
00254       }
00255     }
00256     pinpos++;
00257   }
00258 
00259   if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
00260   {
00261     /* Check Output mode parameters */
00262     assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
00263 
00264     /* Output mode configuration*/
00265     LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
00266 
00267   }
00268   return (SUCCESS);
00269 }
00270 
00271 /**
00272   * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
00273   * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
00274   *                          whose fields will be set to default values.
00275   * @retval None
00276   */
00277 
00278 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
00279 {
00280   /* Reset GPIO init structure parameters values */
00281   GPIO_InitStruct->Pin        = LL_GPIO_PIN_ALL;
00282   GPIO_InitStruct->Mode       = LL_GPIO_MODE_ANALOG;
00283   GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_LOW;
00284   GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
00285   GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
00286   GPIO_InitStruct->Alternate  = LL_GPIO_AF_0;
00287 }
00288 
00289 /**
00290   * @}
00291   */
00292 
00293 /**
00294   * @}
00295   */
00296 
00297 /**
00298   * @}
00299   */
00300 
00301 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) */
00302 
00303 /**
00304   * @}
00305   */
00306 
00307 #endif /* USE_FULL_LL_DRIVER */
00308 
00309 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/