This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Turn On a LED on STM32F4-discovery

Hi everyone,

I want to turn on a LED on my STM32F4-discovery board

Here is the code :

int main()
{

// Enable the GPIO Clock

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

// GPIO Configuration

GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_14; // Led 6 Blue selected

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; // Mod out !

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // 50 MHZ clock frequency

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // Push pull mod

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // Pull up

GPIO_Init(GPIOD, &GPIO_InitStruct);

// Turn on the LED

GPIO_SetBits(GPIOD, GPIO_Pin_14);

return 0;
}

I'm using uvision 5.13.0.0

It compiles and flash succesfully but nothing happen after. I don't know what is wrong with this. I also tried with this :

STM_EVAL_LEDInit(LED6);
STM_EVAL_LEDOn(LED6);

but there is no worked.

Can you help me ?
Many thanks :)

Parents
  • Use something like this in your loop

            while(1)
            {
                    GPIO_SetBits(GPIOD, GPIO_Pin_15); // Toggle the LED, so you can see it change as you step each function
                    GPIO_ResetBits(GPIOD, GPIO_Pin_15); // this isn't magic just requires some thought
            }
    

    Ok, we're getting really off in the weeds. When you use the debugger, does it get you to the first instruction of main(), OR NOT??

    If you hit STOP, where does it stop?

    If you uncheck "run to main", can you step over SystemInit, or does it crash?

    The code in __main sets up the C runtime environment and eventually calls your main() function.

    If SystemInit() is failing, you need to look at the source code in system_stm32f4xx.c and determine if it's doing something stupid, like setting the clocks wrong, or the PLL too high. The Discovery board uses an 8 MHz source clock, make sure your code is consistent with that, and the HSE_VALUE for the project is 8000000.

Reply
  • Use something like this in your loop

            while(1)
            {
                    GPIO_SetBits(GPIOD, GPIO_Pin_15); // Toggle the LED, so you can see it change as you step each function
                    GPIO_ResetBits(GPIOD, GPIO_Pin_15); // this isn't magic just requires some thought
            }
    

    Ok, we're getting really off in the weeds. When you use the debugger, does it get you to the first instruction of main(), OR NOT??

    If you hit STOP, where does it stop?

    If you uncheck "run to main", can you step over SystemInit, or does it crash?

    The code in __main sets up the C runtime environment and eventually calls your main() function.

    If SystemInit() is failing, you need to look at the source code in system_stm32f4xx.c and determine if it's doing something stupid, like setting the clocks wrong, or the PLL too high. The Discovery board uses an 8 MHz source clock, make sure your code is consistent with that, and the HSE_VALUE for the project is 8000000.

Children
  • Hi,

    Fisrt i appologize, I'm a newbie and this is the fisrt time I use Keil and STM32.
    I checked with all your instructions and when i check "Run to main" option it crashes and i'm on this loop :

    void HardFault_Handler(void)
    {
      /* Go to infinite loop when Hard Fault exception occurs */
      while (1)
      {
      }
    }
    

    So i can't go in main.
    Then I unchecked "Run to main" option. SystemInit is ok and doesn't crash.
    In my project i have these options in preprocessor symbols definition:

     USE_STDPERIPH_DRIVER,STM32F4XX,KEIL_IDE,PLL_M=8,PLL_N=336,PLL_P=2,PLL_Q=7,HSE_VALUE=8000000
    

    Then it starts __main instruction and it seems to crash here. What can i do after that to check the error ?

    Many thanks,

  • When you call __main, lots of things will happen before you reach your normal main() function.

    You might get a stack overflow.

    Or maybe your memory layout doesn't match your chip, so the startup code tries to initialize non-existing memory.

    Maybe your project specifies external memory, and you haven't initialized the memory controller that interfaces with the external memory.

  • You might get a stack overflow.

    Or it could be that the stack pointer is initialized assuming that the stack resides in memory that is only available once __main is executed. With the execution of __main being a call, that execution occurs, only to fail when it attempts to return to the caller.