Skip to main content
skillet
Associate II
January 14, 2013
Question

GPIO from STM32F1 to STM32F4 (digital in)

  • January 14, 2013
  • 10 replies
  • 2450 views
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
This topic has been closed for replies.

10 replies

waclawek.jan
Super User
January 14, 2013
Posted on January 14, 2013 at 10:39

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

JW

skillet
skilletAuthor
Associate II
January 14, 2013
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.

waclawek.jan
Super User
January 14, 2013
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

Tesla DeLorean
Guru
January 14, 2013
Posted on January 14, 2013 at 15:31

Double Post - Stupid Forum

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
January 14, 2013
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 VenmoUp vote any posts that you find helpful, it shows what's working..
skillet
skilletAuthor
Associate II
January 15, 2013
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
Super User
January 15, 2013
Posted on January 15, 2013 at 07:39

So what about clive1's suggestion above?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
skillet
skilletAuthor
Associate II
January 15, 2013
Posted on January 15, 2013 at 08:27

tried it.. no effect. am really perplexed.

waclawek.jan
Super User
January 15, 2013
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

slrile05
Visitor II
January 22, 2013
Posted on January 22, 2013 at 18:43

Dear f.d.,

I have been experimenting with the GPIO using the stmf4discovery board, and the example firmware software.  They have one called: IOToggle, or something close to that name in their example software.

 I started from their working software from the discovery kit example, and then kept modifying their code to do more and more of what I needed.  When the code stopped working, I at least had gone from 'working' code to 'nonworking' code, and thus I could pinpoint what was not working.

I did bump into a quirk, I was changing a bit that I had 'initialized' as an 'input',  to later on  be an 'analog' function bit, but it wouldn't work.  so I had to only initialize the bit once, for now.

One thing you could try is what another poster had suggested: notice how he initialized a number of bits, using the logical OR command, with symbol |.  This also was used in the STMF4Discovery firmware examples, and worked good.

Good luck!