TUKS MCU Introductory course / TUKS-COURSE-2-LED
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_ll_gpio.c Source File

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