Analysis of stm32 library function GPIO_Init()

Publisher:TechVoyagerLatest update time:2016-12-23 Source: eefocusKeywords:stm32  GPIO_Init() Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The GPIO_Init function is the initialization function of the IO pin. It performs the initialization configuration of each pin. It mainly accepts two parameters, one is the configuration pin group (GPIO_TypeDef* GPIOx), and the other is the configuration parameter (GPIO_InitTypeDef* GPIO_InitStruct), as follows


  1. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

  2. /*The first parameter is the group of pins. Each group has 16 pins, and each group has a different register configuration address. The second parameter is a data structure, which is where the basic configuration information is placed, and then the structure is passed into the function for configuration*/

  3. //The data structure can be expressed as follows

  4. typedef struct

  5. {

  6.   uint16_t GPIO_Pin; //pin number

  7.   GPIOSpeed_TypeDef GPIO_Speed; //Configure speed

  8.   GPIOMode_TypeDef GPIO_Mode; //Working mode

  9. }GPIO_InitTypeDef;


  10. //The configuration mode and working mode are enumeration variables of GPIOSpeed_TypeDef and GPIOMode_TypeDef

In order to analyze this function conveniently, we need to list the definitions of several constants.


  1. //First is the pin definition

  2. #define GPIO_Pin_0 ((uint16_t)0x0001) /* Pin 0 selected */

  3. #define GPIO_Pin_1 ((uint16_t)0x0002) /* Pin 1 selected */

  4. #define GPIO_Pin_2 ((uint16_t)0x0004) /* Pin 2 selected */

  5. #define GPIO_Pin_3 ((uint16_t)0x0008) /* Pin 3 selected */

  6. #define GPIO_Pin_4 ((uint16_t)0x0010) /* Pin 4 selected */

  7. #define GPIO_Pin_5 ((uint16_t)0x0020) /* Pin 5 selected */

  8. #define GPIO_Pin_6 ((uint16_t)0x0040) /* Pin 6 selected */

  9. #define GPIO_Pin_7 ((uint16_t)0x0080) /* Pin 7 selected */

  10. #define GPIO_Pin_8 ((uint16_t)0x0100) /* Pin 8 selected */

  11. #define GPIO_Pin_9 ((uint16_t)0x0200) /* Pin 9 selected */

  12. #define GPIO_Pin_10 ((uint16_t)0x0400) /* Pin 10 selected */

  13. #define GPIO_Pin_11 ((uint16_t)0x0800) /* Pin 11 selected */

  14. #define GPIO_Pin_12 ((uint16_t)0x1000) /* Pin 12 selected */

  15. #define GPIO_Pin_13 ((uint16_t)0x2000) /* Pin 13 selected */

  16. #define GPIO_Pin_14 ((uint16_t)0x4000) /* Pin 14 selected */

  17. #define GPIO_Pin_15 ((uint16_t)0x8000) /* Pin 15 selected */

  18. #define GPIO_Pin_All ((uint16_t)0xFFFF) /* All pins selected */


  19. // Followed by the mode definition

  20. typedef enum

  21. { GPIO_Mode_AIN = 0x0, //Analog input

  22. GPIO_Mode_IN_FLOATING = 0x04, //Floating input mode, default

  23. GPIO_Mode_IPD = 0x28, //Pull-up/pull-down input mode

  24. GPIO_Mode_IPU = 0x48, //Reserved

  25. GPIO_Mode_Out_OD = 0x14, //General purpose open drain output

  26. GPIO_Mode_Out_PP = 0x10, //General push-pull output

  27. GPIO_Mode_AF_OD = 0x1C, //Multiplexed (open drain) output

  28. GPIO_Mode_AF_PP = 0x18 //Multiplexed (push-pull) output

  29. }GPIOMode_TypeDef;


  30. //Finally, the speed definition

  31. typedef enum

  32. {

  33. GPIO_Speed_10MHz = 1,

  34. GPIO_Speed_2MHz,

  35. GPIO_Speed_50MHz

  36. }GPIOSpeed_TypeDef;

It is easy to see from the pin definition that if pin 0 is 1, the 0th position of the 16 bits is 1, if pin 2 is 1, and so on. Therefore, if you want to define multiple pins, you only need to use the OR logic operation (|).
Secondly, the mode definition also has its rules. Referring to the "stmf10xxx Reference Manual", it can be concluded that every 4 bits in the high and low configuration registers store a mode + speed, where the mode occupies the high 2 bits and the speed occupies the low 2 bits. 16 pins have 4*16=64 bits to store, so this definition makes sense. The reason is that the mode occupies the high bit. For example, 10 represents the pull-up/pull-down output mode, which occupies the high 2 bits in the 4 bits, which should be 1000. The low 2 bits are first represented by 00, so the pull-up/pull-down output mode can be represented as 1000=0x8, or it can be represented by the fourth bit of 0x28. The same is true for the others, that is, the fifth bit 1 represents the output mode and 0 represents the input mode.

The speed is directly represented by 1, 2, and 3.
The specific function analysis is as follows


  1. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

  2. {

  3.  /* Initialize each variable */

  4.   uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;

  5.   uint32_t tmpreg = 0x00, pinmask = 0x00;

  6.   //currentmode is used to store temporary LCIR

  7.   //currentpin is used to store the configured pin position

  8.   //pinpos is used to store the pin number of the current operation

  9.   //pos stores the pin position of the current operation

  10.   //tmreg current CIR

  11.   //pinmask



  12.   //Judge the parameters

  13.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

  14.   assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));

  15.   assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin)); 

  16.   

  17.   // Take out the mode information in the configuration information and take its lower 4 bits

  18.   currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);

  19.   if ((((uint32_t)GPIO_OInitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00) //Output mode

  20.   { 


  21.     //Judge the parameters

  22.     assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));

  23.     

  24.     //Put the speed information into the lower two bits of currentmode

  25.     currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;

  26.   }

  27.   if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00) //The pin is defined

  28.   {

  29.     //Save the current CRL

  30.     tmpreg = GPIOx->CRL;


  31.     //Loop the lower eight pins

  32.     for (pinpos = 0x00; pinpos < 0x08; pinpos++)

  33.     {

  34.       //The current pin is at position 1

  35.       pos = ((uint32_t)0x01) << pinpos;


  36.       //Read the current pin in the pin information

  37.       currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;

  38.       if (currentpin == pos) //If the current pin exists in the configuration information

  39.       {

  40.         pos = pinpos << 2; //pos = pin number x2

  41.         pinmask = ((uint32_t)0x0F) << pos; //1111<

  42.         tmpreg &= ~pinmask; //The CRL bit that should be operated currently is cleared to 0

  43.         tmpreg |= (currentmode << pos); //Set the CRL bit of the current operation

  44.         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD) // Set the port to high level

  45.         {

  46.           GPIOx->BRR = (((uint32_t)0x01) << pinpos);

  47.         }

  48.         else

  49.         {

  50.           if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU) // Port cleared

  51.           {

  52.             GPIOx->BSRR = (((uint32_t)0x01) << pinpos);

  53.           }

  54.         }

  55.       }

  56.     }

  57.     GPIOx->CRL = tmpreg;

  58.   }

The last step is to transfer the configured CRL to the CRL register.


Keywords:stm32  GPIO_Init() Reference address:Analysis of stm32 library function GPIO_Init()

Previous article:STM32 USART configuration
Next article:STM32 input and output summary

Recommended ReadingLatest update time:2024-11-16 20:24

stm32 microcontroller detects 12V circuit
1. First, use the sampling circuit to divide the voltage to get a 3.3V voltage 2. Then use a voltage follower to convert the 3.3V high impedance signal voltage into a 3.3V low impedance drive voltage, which can be directly connected to the ADC pin of stm32 3. Note: The supply voltage of the op amp chip LM358 must be a
[Microcontroller]
stm32 microcontroller detects 12V circuit
STM32--CAN simple receiving and sending
The principle of CAN has been explained above. Here I will use a simple example to illustrate the use of CAN. The STM32 chip I use is STM32F103ZE, and the basic configurations are: 1. Configure CAN clock:  /* CAN Periph clock enable */  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE); 2. CAN pin configuration, the
[Microcontroller]
STM32 learning notes (2): Use of external interrupts
The status of interrupts in developing embedded systems is absolutely unquestionable. In the era of C51 microcontrollers, there were only 5 interrupts, including 2 external interrupts, 2 timer/counter interrupts and one serial port interrupt. However, in STM32, the number of interrupts has greatly increased, and the in
[Microcontroller]
Explanation of STM32 USB Mass Storage Project
Alas, when it comes to USB mass storage devices, it is not as simple as USB mouse. Let's start with the mass storage project in the official example. The official project is more complicated, and the program is compatible with different series of evaluation versions officially launched, so macro switch statements such
[Microcontroller]
Explanation of STM32 USB Mass Storage Project
STM32 only enables SWD mode debugging program
Due to insufficient IO ports, it is necessary to reuse JTAG as an IO port and retain the SWD function to debug the program. The settings are as follows:     GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
[Microcontroller]
STM32 learning record 8: DMA
Official example: STM32F10x_StdPeriph_Lib_V3.1.2\Project\STM32F10x_StdPeriph_Examples\DMA\FLASH_RAM /* DMA1 channel6 configuration */ DMA_DeInit(DMA1_Channel6);   //peripheral base address The peripheral address is a definition of its own //例如#define SPI1_DR_Addr ( (u32)0x4001300C ) DMA_InitStructure.DMA_Periphe
[Microcontroller]
STM32 learning record 8: DMA
Serial communication source code based on STM32 Shenzhou series development board
#include "stm32f10x.h" #include "stm32f10x_usart.h"   void RCC_Config(void); void GPIO_Config(void); void USART_Config(void); void Put_String(u8 *p);   int main() {   RCC_Config();//Configure clock   GPIO_Config();//Configure input and output   USART_Config();//Configure sending and receiving /
[Microcontroller]
STM32 Hardware I2C EEPROM Command Analysis
void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite) {     u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;     //Write the address to see which number of each page     Addr = WriteAddr % I2C_PageSize;     //The number of entries to be written at the beginning of the page     count =
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号