cancel
Showing results for 
Search instead for 
Did you mean: 

GPIO from STM32F1 to STM32F4 (digital in)

skillet
Associate II
Posted on January 14, 2013 at 09:53

Hi, I am currently porting robotics software from STM32F1 (Cortex M3) to STM32F4 (Cortex M4). I want to write a simple program to switch on an LED via digital out. The program is supposed to send a digital input interrupt from a pushbutton and output to LED when it is pressed. Somehow I cannot get my digital input configuration to work. I get strange result, which is, I can only initialize GPIO pin for input from GPIO-E Pin 0. After this, initialization does not work. Even if I only used Pin 0 in my code, it does not work. So I commented off initialization for the rest of the pins and use polling technique for Pin 0 (if-else button is pushed, turn off LED). It still does not work. Any idea where to look?

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

GPIO_Init(GPIOE, &GPIO_InitStructure); // Initialize pin 0

//CODE DOES NOT WORK BELOW THIS POINT

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_Init(GPIOE, &GPIO_InitStructure); // Initialize pin 1

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_Init(GPIOE, &GPIO_InitStructure); // Initialize pin 2

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_Init(GPIOE, &GPIO_InitStructure);  // Initialize pin 3

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_Init(GPIOE, &GPIO_InitStructure);  // Initialize pin 4

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

GPIO_Init(GPIOE, &GPIO_InitStructure);  // Initialize pin 5

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_Init(GPIOE, &GPIO_InitStructure);  // Initialize pin 6

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

GPIO_Init(GPIOE, &GPIO_InitStructure); // Initialize pin 7

#gpio-digital-in
10 REPLIES 10
Posted on January 14, 2013 at 10:39

It always helps to be more specific with ''does not work''.

JW

skillet
Associate II
Posted on January 14, 2013 at 11:09

+Works - When I place a function DIGITALOUT_On(LED2); or DIGITALOUT_Off(LED2); below that line, the function executes what it is supposed to to

+Does not work - function does not light up/turn off LED when function is placed below the line

i used the LED out function as a breakpoint to locate precisely where the error is

first I figured out that inside main.c, DIGITALIN_Config(); is causing problems. Then I look inside my digitalin.c file and i located that below GPIOE Pin 0 (see above code), for some reason the rest of the code causes problems. No idea why this is so.

initially the light up routine used interrupt with function callback, but I wanted to narrow down the problem so I used polling.

So, I bypass DIGITALIN_Config() entirely by using GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_0); and if it returns 1, DIGITALOUT_On(LED2) should light up LED2. i.e. if the pin detects a high which means button is pressed, light up LED2. Even when using this method the LED does not turn on/off as it should.

I am running out of explanations for why it is.

Posted on January 14, 2013 at 13:13

What is DIGITALOUT_On() and the other functions you are mentioning?

Try posting a minimal program but complete enough to be compilable, exhibiting the problem.

JW

Posted on January 14, 2013 at 15:31

Double Post - Stupid Forum

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 14, 2013 at 15:32

Pin initialization looks Ok, need to look elsewhere for where your real problem lies.

It might be more simply accomplished with :

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |

GPIO_Pin_1 |

GPIO_Pin_2 |

GPIO_Pin_3 |

GPIO_Pin_4 |

GPIO_Pin_5 |

GPIO_Pin_6 |

GPIO_Pin_7

;

GPIO_Init(GPIOE, &GPIO_InitStructure);0
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
skillet
Associate II
Posted on January 15, 2013 at 04:13

This should be about it... Any help on where it went wrong? Where do I look?

---------------------

void DIGITALOUT_Config(void)

{

 GPIO_InitTypeDef GPIO_InitStructure;

 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

           

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

 GPIO_Init(GPIOE, &GPIO_InitStructure);

 

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

 GPIO_Init(GPIOE, &GPIO_InitStructure);

 

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

 GPIO_Init(GPIOE, &GPIO_InitStructure);

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

 GPIO_Init(GPIOE, &GPIO_InitStructure);

}

void DIGITALOUT_On(char LEDx)

{

 switch(LEDx)

 {

  case LED1:

   GPIO_SetBits(GPIOE, GPIO_Pin_8);

   break;

  case LED2:

   GPIO_SetBits(GPIOE, GPIO_Pin_9);

   break;

  case LED3:

   GPIO_SetBits(GPIOE, GPIO_Pin_10);

   break;

  case LED4:

   GPIO_SetBits(GPIOE, GPIO_Pin_11);

   break;

 }

}

void DIGITALOUT_Off(char LEDx)

{

 switch(LEDx)

 {

  case LED1:

   GPIO_ResetBits(GPIOE, GPIO_Pin_8);

   break;

  case LED2:

   GPIO_ResetBits(GPIOE, GPIO_Pin_9);

   break;

  case LED3:

   GPIO_ResetBits(GPIOE, GPIO_Pin_10);

   break;

  case LED4:

   GPIO_ResetBits(GPIOE, GPIO_Pin_11);

   break;

 }

}

char DIGITALIN_Get(char DINx)

{

 char ret=0;

 switch(DINx)

 {

  case PUSHBUTTON1:

   ret = GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_0);

   break;

  case PUSHBUTTON2:

   ret = GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_1);

   break;

  case PUSHBUTTON3:

   ret = GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2);

   break;

  case PUSHBUTTON4:

   ret = GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3);

   break;

  case DIPSWITCH1:

   ret = GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4);

   break;

  case DIPSWITCH2:

   ret = GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_5);

   break;

  case DIPSWITCH3:

   ret = GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_6);

   break;

  case DIPSWITCH4:

   ret = GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_7);

   break;

 }

 return ret;

}

int main(void)

{

 SYSTEM_Config();

 DIGITALOUT_Config();

 DIGITALOUT_On(LED1);

 DIGITALOUT_On(LED2);

 DIGITALOUT_On(LED3);

 DIGITALOUT_On(LED4);

 GPIO_InitTypeDef GPIO_InitStructure;

 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

 

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

 GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

 GPIO_Init(GPIOE, &GPIO_InitStructure); // Initialize pin 0

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

 GPIO_Init(GPIOE, &GPIO_InitStructure); // Initialize pin 1

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

 GPIO_Init(GPIOE, &GPIO_InitStructure); // Initialize pin 2

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

 GPIO_Init(GPIOE, &GPIO_InitStructure);  // Initialize pin 3

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

 GPIO_Init(GPIOE, &GPIO_InitStructure);  // Initialize pin 4

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

 GPIO_Init(GPIOE, &GPIO_InitStructure);  // Initialize pin 5

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

 GPIO_Init(GPIOE, &GPIO_InitStructure);  // Initialize pin 6

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

 GPIO_Init(GPIOE, &GPIO_InitStructure); // Initialize pin 7

 DIGITALIN_CallBack_PushButton1 = NULL;

 DIGITALIN_CallBack_PushButton2 = NULL;

 DIGITALIN_CallBack_PushButton3 = NULL;

 DIGITALIN_CallBack_PushButton4 = NULL;

 

 

 while (1)

 {

  SYSTEM_Task();

  if (DIGITALIN_Get(PUSHBUTTON1)!=0)

  {

   DIGITALOUT_Off(LED2);

  }

  else

  {

   DIGITALOUT_On(LED2);

  }

 }

}

What is DIGITALOUT_On() and the other functions you are mentioning?

Try posting a minimal program but complete enough to be compilable, exhibiting the problem.

JW

Andrew Neil
Evangelist III
Posted on January 15, 2013 at 07:39

So what about clive1's suggestion above?

skillet
Associate II
Posted on January 15, 2013 at 08:27

tried it.. no effect. am really perplexed.

Posted on January 15, 2013 at 09:25

What is

SYSTEM_Task()

?

What happens if you drop it?

And what about a simple blinkey, i.e. just toggle the LED with loop delay in between - does that work?

JW