STM32F103xB HAL User Manual
stm32f1xx_ll_gpio.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_ll_gpio.c
00004   * @author  MCD Application Team
00005   * @brief   GPIO LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00010   * All rights reserved.</center></h2>
00011   *
00012   * This software component is licensed by ST under BSD 3-Clause license,
00013   * the "License"; You may not use this file except in compliance with the
00014   * License. You may obtain a copy of the License at:
00015   *                        opensource.org/licenses/BSD-3-Clause
00016   *
00017   ******************************************************************************
00018   */
00019 
00020 #if defined(USE_FULL_LL_DRIVER)
00021 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f1xx_ll_gpio.h"
00024 #include "stm32f1xx_ll_bus.h"
00025 #ifdef  USE_FULL_ASSERT
00026 #include "stm32_assert.h"
00027 #else
00028 #define assert_param(expr) ((void)0U)
00029 #endif
00030 
00031 /** @addtogroup STM32F1xx_LL_Driver
00032   * @{
00033   */
00034 
00035 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG)
00036 
00037 /** @addtogroup GPIO_LL
00038   * @{
00039   */
00040 
00041 /* Private types -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 /* Private constants ---------------------------------------------------------*/
00044 /* Private macros ------------------------------------------------------------*/
00045 /** @addtogroup GPIO_LL_Private_Macros
00046   * @{
00047   */
00048 
00049 #define IS_LL_GPIO_PIN(__VALUE__)          ((((__VALUE__) & LL_GPIO_PIN_ALL)!= 0u) &&\
00050                                             (((__VALUE__) & (~LL_GPIO_PIN_ALL))== 0u))
00051 
00052 #define IS_LL_GPIO_MODE(__VALUE__)         (((__VALUE__) == LL_GPIO_MODE_ANALOG)       ||\
00053                                             ((__VALUE__) == LL_GPIO_MODE_FLOATING)     ||\
00054                                             ((__VALUE__) == LL_GPIO_MODE_INPUT)        ||\
00055                                             ((__VALUE__) == LL_GPIO_MODE_OUTPUT)       ||\
00056                                             ((__VALUE__) == LL_GPIO_MODE_ALTERNATE))
00057 
00058 #define IS_LL_GPIO_SPEED(__VALUE__)        (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW)       ||\
00059                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM)    ||\
00060                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH))
00061 
00062 #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__)  (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL)  ||\
00063                                             ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
00064 
00065 #define IS_LL_GPIO_PULL(__VALUE__)         (((__VALUE__) == LL_GPIO_PULL_DOWN)   ||\
00066                                             ((__VALUE__) == LL_GPIO_PULL_UP))
00067 
00068 /**
00069   * @}
00070   */
00071 
00072 /* Private function prototypes -----------------------------------------------*/
00073 
00074 /* Exported functions --------------------------------------------------------*/
00075 /** @addtogroup GPIO_LL_Exported_Functions
00076   * @{
00077   */
00078 
00079 /** @addtogroup GPIO_LL_EF_Init
00080   * @{
00081   */
00082 
00083 /**
00084   * @brief  De-initialize GPIO registers (Registers restored to their default values).
00085   * @param  GPIOx GPIO Port
00086   * @retval An ErrorStatus enumeration value:
00087   *          - SUCCESS: GPIO registers are de-initialized
00088   *          - ERROR:   Wrong GPIO Port
00089   */
00090 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
00091 {
00092   ErrorStatus status = SUCCESS;
00093 
00094   /* Check the parameters */
00095   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
00096 
00097   /* Force and Release reset on clock of GPIOx Port */
00098   if (GPIOx == GPIOA)
00099   {
00100     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOA);
00101     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOA);
00102   }
00103   else if (GPIOx == GPIOB)
00104   {
00105     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOB);
00106     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOB);
00107   }
00108   else if (GPIOx == GPIOC)
00109   {
00110     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOC);
00111     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOC);
00112   }
00113   else if (GPIOx == GPIOD)
00114   {
00115     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOD);
00116     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOD);
00117   }
00118 #if defined(GPIOE)
00119   else if (GPIOx == GPIOE)
00120   {
00121     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOE);
00122     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOE);
00123   }
00124 #endif
00125 #if defined(GPIOF)
00126   else if (GPIOx == GPIOF)
00127   {
00128     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOF);
00129     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOF);
00130   }
00131 #endif
00132 #if defined(GPIOG)
00133   else if (GPIOx == GPIOG)
00134   {
00135     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOG);
00136     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOG);
00137   }
00138 #endif
00139   else
00140   {
00141     status = ERROR;
00142   }
00143 
00144   return (status);
00145 }
00146 
00147 /**
00148   * @brief  Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
00149   * @param  GPIOx GPIO Port
00150   * @param  GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
00151   *         that contains the configuration information for the specified GPIO peripheral.
00152   * @retval An ErrorStatus enumeration value:
00153   *          - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
00154   *          - ERROR:   Not applicable
00155   */
00156 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
00157 {
00158   uint32_t pinmask;
00159   uint32_t pinpos;
00160   uint32_t currentpin;
00161 
00162   /* Check the parameters */
00163   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
00164   assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
00165 
00166   /* ------------------------- Configure the port pins ---------------- */
00167   /* Initialize  pinpos on first pin set */
00168 
00169   pinmask = ((GPIO_InitStruct->Pin) << GPIO_PIN_MASK_POS) >> GPIO_PIN_NB;
00170   pinpos = POSITION_VAL(pinmask);
00171 
00172   /* Configure the port pins */
00173   while ((pinmask  >> pinpos) != 0u)
00174   {
00175     /* skip if bit is not set */
00176     if ((pinmask & (1u << pinpos)) != 0u)
00177     {
00178       /* Get current io position */
00179       if (pinpos < GPIO_PIN_MASK_POS)
00180       {
00181         currentpin = (0x00000101uL << pinpos);
00182       }
00183       else
00184       {
00185         currentpin = ((0x00010001u << (pinpos - GPIO_PIN_MASK_POS)) | 0x04000000u);
00186       }
00187 
00188       if (GPIO_InitStruct->Mode == LL_GPIO_MODE_INPUT)
00189       {
00190         /* Check The Pull parameter */
00191         assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
00192 
00193         /* Pull-up Pull-down resistor configuration*/
00194         LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
00195       }
00196       
00197       /* Check Pin Mode parameters */
00198       assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
00199       
00200       /* Pin Mode configuration */
00201       LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
00202 
00203       if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
00204       {
00205         /* Check speed and Output mode parameters */
00206         assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
00207         assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
00208 
00209         /* Speed mode configuration */
00210         LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
00211 
00212         /* Output mode configuration*/
00213         LL_GPIO_SetPinOutputType(GPIOx, currentpin, GPIO_InitStruct->OutputType);
00214       }
00215     }
00216     pinpos++;
00217   }
00218   return (SUCCESS);
00219 }
00220 
00221 /**
00222   * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
00223   * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
00224   *                          whose fields will be set to default values.
00225   * @retval None
00226   */
00227 
00228 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
00229 {
00230   /* Reset GPIO init structure parameters values */
00231   GPIO_InitStruct->Pin        = LL_GPIO_PIN_ALL;
00232   GPIO_InitStruct->Mode       = LL_GPIO_MODE_FLOATING;
00233   GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_LOW;
00234   GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
00235   GPIO_InitStruct->Pull       = LL_GPIO_PULL_DOWN;
00236 }
00237 
00238 /**
00239   * @}
00240   */
00241 
00242 /**
00243   * @}
00244   */
00245 
00246 /**
00247   * @}
00248   */
00249 
00250 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) */
00251 
00252 /**
00253   * @}
00254   */
00255 
00256 #endif /* USE_FULL_LL_DRIVER */
00257 
00258 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/