STM32G474xx HAL 用户手册
stm32g4xx_hal_gpio.c
转到此文件的文档。
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_hal_gpio.c
00004   * @author  MCD Application Team
00005   * @brief   GPIO HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the General Purpose Input/Output (GPIO) peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + IO operation functions
00010   *
00011   ******************************************************************************
00012   * @attention
00013   *
00014   * Copyright (c) 2019 STMicroelectronics.
00015   * All rights reserved.
00016   *
00017   * This software is licensed under terms that can be found in the LICENSE file
00018   * in the root directory of this software component.
00019   * If no LICENSE file comes with this software, it is provided AS-IS.
00020   *
00021   ******************************************************************************
00022   @verbatim
00023   ==============================================================================
00024                     ##### GPIO Peripheral features #####
00025   ==============================================================================
00026   [..]
00027     (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
00028         configured by software in several modes:
00029         (++) Input mode
00030         (++) Analog mode
00031         (++) Output mode
00032         (++) Alternate function mode
00033         (++) External interrupt/event lines
00034 
00035     (+) During and just after reset, the alternate functions and external interrupt
00036         lines are not active and the I/O ports are configured in input floating mode.
00037 
00038     (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
00039         activated or not.
00041 
00042     (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
00043         type and the IO speed can be selected depending on the VDD value.
00045 
00046     (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
00047         multiplexer that allows only one peripheral alternate function (AF) connected
00048        to an IO pin at a time. In this way, there can be no conflict between peripherals
00049        sharing the same IO pin.
00051 
00052     (+) All ports have external interrupt/event capability. To use external interrupt
00053         lines, the port must be configured in input mode. All available GPIO pins are
00055         connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
00057 
00058     (+) The external interrupt/event controller consists of up to 44 edge detectors
00059         (16 lines are connected to GPIO) for generating event/interrupt requests (each
00060         input line can be independently configured to select the type (interrupt or event)
00061         and the corresponding trigger event (rising or falling or both). Each line can
00062         also be masked independently.
00064 
00066                      ##### How to use this driver #####
00068   ==============================================================================
00069   [..]
00070     (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
00071 
00072     (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
00074         (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
00076         (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
00077              structure.
00078         (++) In case of Output or alternate function mode selection: the speed is
00079              configured through "Speed" member from GPIO_InitTypeDef structure.
00080         (++) In alternate mode is selection, the alternate function connected to the IO
00081              is configured through "Alternate" member from GPIO_InitTypeDef structure.
00082         (++) Analog mode is required when a pin is to be used as ADC channel
00083              or DAC output.
00084         (++) In case of external interrupt/event selection the "Mode" member from
00086              GPIO_InitTypeDef structure select the type (interrupt or event) and
00087              the corresponding trigger event (rising or falling or both).
00088 
00089     (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
00090         mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
00091         HAL_NVIC_EnableIRQ().
00093 
00094     (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
00096 
00097     (#) To set/reset the level of a pin configured in output mode use
00098         HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
00099 
00100    (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
00101 
00102     (#) During and just after reset, the alternate functions are not
00103         active and the GPIO pins are configured in input floating mode (except JTAG
00104         pins).
00105 
00106     (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
00107         (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
00108         priority over the GPIO function.
00109 
00110     (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
00111         general purpose PF0 and PF1, respectively, when the HSE oscillator is off.
00112         The HSE has priority over the GPIO function.
00113 
00114   @endverbatim
00115   ******************************************************************************
00116   */
00117 
00118 /* Includes ------------------------------------------------------------------*/
00119 #include "stm32g4xx_hal.h"
00120 
00121 /** @addtogroup STM32G4xx_HAL_Driver
00122   * @{
00124   */
00126 
00127 /** @addtogroup GPIO
00128   * @{
00129   */
00130 /** MISRA C:2012 deviation rule has been granted for following rules:
00131   * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
00133   * range of the shift operator in following API :
00134   * HAL_GPIO_Init
00135   * HAL_GPIO_DeInit
00136   */
00137 
00138 #ifdef HAL_GPIO_MODULE_ENABLED
00139 
00140 /* Private typedef -----------------------------------------------------------*/
00141 /* Private defines -----------------------------------------------------------*/
00142 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
00143   * @{
00145   */
00146 00147 #define GPIO_NUMBER           (16U)
00148 /**
00149   * @}
00150   */
00151 
00152 /* Private macros ------------------------------------------------------------*/
00153 /* Private variables ---------------------------------------------------------*/
00154 /* Private function prototypes -----------------------------------------------*/
00155 /* Exported functions --------------------------------------------------------*/
00156 
00157 /** @addtogroup GPIO_Exported_Functions
00158   * @{
00160   */
00161 
00163 /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
00164   *  @brief    Initialization and Configuration functions
00166   *
00168 @verbatim
00169  ===============================================================================
00170               ##### Initialization and de-initialization functions #####
00171  ===============================================================================
00173 
00174 @endverbatim
00175   * @{
00176   */
00177 
00179 /**
00180   * @brief  Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
00181   * @param  GPIOx where x can be (A..G) to select the GPIO peripheral for STM32G4xx family
00182   * @param  GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
00183   *         the configuration information for the specified GPIO peripheral.
00185   * @retval None
00187   */
00188 void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)
00189 {
00190   uint32_t position = 0x00U;
00191   uint32_t iocurrent;
00192   uint32_t temp;
00193 
00194   /* Check the parameters */
00195   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
00196   assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
00197   assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
00198 
00199   /* Configure the port pins */
00200   while (((GPIO_Init->Pin) >> position) != 0U)
00201   {
00202     /* Get current io position */
00203     iocurrent = (GPIO_Init->Pin) & (1UL << position);
00204 
00205     if (iocurrent != 0x00u)
00206     {
00207       /*--------------------- GPIO Mode Configuration ------------------------*/
00208       /* In case of Output or Alternate function mode selection */
00209       if(((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) ||
00210          ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
00211       {
00212         /* Check the Speed parameter */
00213         assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
00214         /* Configure the IO Speed */
00215         temp = GPIOx->OSPEEDR;
00216         temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
00217         temp |= (GPIO_Init->Speed << (position * 2U));
00218         GPIOx->OSPEEDR = temp;
00219 
00220         /* Configure the IO Output Type */
00221         temp = GPIOx->OTYPER;
00222         temp &= ~(GPIO_OTYPER_OT0 << position) ;
00223         temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
00224         GPIOx->OTYPER = temp;
00225       }
00226 
00227 
00227       if (((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG) ||
00228           (((GPIO_Init->Mode & GPIO_MODE) == MODE_ANALOG) && (GPIO_Init->Pull != GPIO_PULLUP)))
00229       {
00230         /* Check the Pull parameter */</