cancel
Showing results for 
Search instead for 
Did you mean: 

How to define the GPIO mode ?

614678489
Associate II
Posted on August 13, 2012 at 09:20

typedef enum

{ GPIO_Mode_AIN = 0x0,

  GPIO_Mode_IN_FLOATING = 0x04,

  GPIO_Mode_IPD = 0x28,

  GPIO_Mode_IPU = 0x48,

  GPIO_Mode_Out_OD = 0x14,

  GPIO_Mode_Out_PP = 0x10,

  GPIO_Mode_AF_OD = 0x1C,

  GPIO_Mode_AF_PP = 0x18

}GPIOMode_TypeDef;

How come 0x0, 0x28, 0x48? Which register are they based on?

10 REPLIES 10
schalk
Associate II
Posted on August 13, 2012 at 10:22

Hi

For example

void Outputs_Init(Output_Name_TypeDef Output) 
{ 
/* Enable the GPIO Clock */ 
RCC_AHB1PeriphClockCmd(Outputs_CLK[Output], ENABLE); 
/* Configure the GPIO pin */ 
GPIO_InitStructure.GPIO_Pin = Outputs_PIN[Output]; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_Init(Outputs_PORT[Output], &GPIO_InitStructure); 
}

If you go and look in stm32f4xx_gpio.h you will see that the types is already defined and you don't have to use the HEX values... for example:

/** 
* @brief GPIO Configuration Mode enumeration 
*/ 
typedef enum 
{ 
GPIO_Mode_IN = 0x00, /*!< GPIO Input Mode */ 
GPIO_Mode_OUT = 0x01, /*!< GPIO Output Mode */ 
GPIO_Mode_AF = 0x02, /*!< GPIO Alternate function Mode */ 
GPIO_Mode_AN = 0x03 /*!< GPIO Analog Mode */ 
}GPIOMode_TypeDef; 
#define IS_GPIO_MODE(MODE) (((MODE) == GPIO_Mode_IN) || ((MODE) == GPIO_Mode_OUT) || \ 
((MODE) == GPIO_Mode_AF)|| ((MODE) == GPIO_Mode_AN)) 
/** 
* @brief GPIO Output type enumeration 
*/ 
typedef enum 
{ 
GPIO_OType_PP = 0x00, 
GPIO_OType_OD = 0x01 
}GPIOOType_TypeDef; 
#define IS_GPIO_OTYPE(OTYPE) (((OTYPE) == GPIO_OType_PP) || ((OTYPE) == GPIO_OType_OD)) 
/** 
* @brief GPIO Output Maximum frequency enumeration 
*/ 
typedef enum 
{ 
GPIO_Speed_2MHz = 0x00, /*!< Low speed */ 
GPIO_Speed_25MHz = 0x01, /*!< Medium speed */ 
GPIO_Speed_50MHz = 0x02, /*!< Fast speed */ 
GPIO_Speed_100MHz = 0x03 /*!< High speed on 30 pF (80 MHz Output max speed on 15 pF) */ 
}GPIOSpeed_TypeDef;

Then if you refer to the RM0090 manual (or the core you are using, the RM0090 is for STM32F4).. You will see things like the GPIO port mode register in which things like ''Analog mode'' etc is defined... and obviously these values relate to the control registers for GPIO...
614678489
Associate II
Posted on August 14, 2012 at 07:45

Thanks, I know I don't need to use those HEX values in the programming, I just wonder how they come....

thibaut2
Associate II
Posted on August 14, 2012 at 09:38

0x28 = b''0010 1000''

so i haven't programmed on f4 yet and i haven't seen its datasheet but, GPIO_Mode_IPD shall be input, pull down.

Now you have to look at the GPIO registers in the user manual (or reference manual), that are named something like Control register1 (CR1) (or low CRL) and control register 2 (CR2) (or High CRH), there is also data direction register (DDR). You'll see which bits are set.

The type_def you show comes out a firmware library, i guess, so it comes also with a function which use this type. it shall be well commented.

ricardo
Associate II
Posted on June 13, 2013 at 19:55

Good afternoon, I'm updating a program that uses stm32f1xx.h for stm32f4xx.h.

Defined settings:

/ / = GPIO_InitStruct.GPIO_Mode GPIO_Mode_AF_PP;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

/ / = GPIO_InitStruct.GPIO_Mode GPIO_Mode_Out_PP;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

Setting not set:

/ / = GPIO_InitStruct.GPIO_Mode GPIO_Mode_IPU;

        GPIO_InitStruct.GPIO_Mode GPIO_Mode_ =??;

       GPIO_InitStruct.GPIO_OType GPIO_OType_ =??;

Can you help me ?  I do not know how to solve. Thank you for your attention.

Posted on June 13, 2013 at 20:22

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

// Don't need speed or af/pp settings

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ricardo
Associate II
Posted on June 13, 2013 at 20:40

Thank clive1

Can you help me in this case?

RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);

Posted on June 13, 2013 at 20:43

RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ricardo
Associate II
Posted on June 13, 2013 at 21:12

Checking the values​​:

stm32f10x_rcc.h

# define RCC_APB2Periph_GPIOA ((uint32_t) 0x00000004)

# define RCC_APB2Periph_GPIOB ((uint32_t) 0x00000008)

# define RCC_APB2Periph_GPIOC ((uint32_t) 0x00000010)

stm32f4xx_rcc.h

# define RCC_AHB1Periph_GPIOA ((uint32_t) 0x00000001)

# define RCC_AHB1Periph_GPIOB ((uint32_t) 0x00000002)

# define RCC_AHB1Periph_GPIOC ((uint32_t) 0x00000004)

are different. I did not understand why.

Sorry, I'm starting now on ARM. Always worked with 8-bit microcontroller.

Posted on June 13, 2013 at 21:58

It's a different chip with different registers and configurations. You should not expect there to be any usable correlation.

The GPIO units are now on the Hardware Bus (AHB), not the Peripheral Bus (APB)
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..