cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with HAL_Init()

seprac
Associate II
Posted on July 10, 2015 at 17:18

Hi, I am kind of new to the STM32 but have found the libraries (HAL) to be greatly useful.

I am using the STM32F4 Discovery Board,

Even when I was trying to simply blink the onboard LEDs using the GPIO Peripheral, it is simply going to an infinite loop (Found in CooCOX debugger), except when an interrupt is triggered). 

After an entire day's struggle, trying to find out what is wrong....the programme worked as intended on deleting HAL_Init() from the main.

Am I missing something?...if you want I can post my code here

10 REPLIES 10
seprac
Associate II
Posted on July 11, 2015 at 16:17

Can someone please reply..

Posted on July 11, 2015 at 16:40

So figure out where this infinite loop is in the code, and why it's stuck there. Is it in the Hard Fault handler?

Review what's in HAL_Init(), if you're just blinking an LED, there can't conceivably much code to look over. Step through everything with the debugger if that helps.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
seprac
Associate II
Posted on July 11, 2015 at 18:39

Hi. Thanks for your reply. Yes, it goes to the fault handler with description as follows 

This is the code that gets called when the processor receives an 

 

 *         unexpected interrupt.  This simply enters an infinite loop, preserving

 

 *         the system state for examination by a debugger.

 

I have tried step by step debgging.....The only time the board responds is when I trigger the interrupt,( triggerred when I press the user button), but once the handler is executed it again returns to the fault handler.

My main question that I was under the impression that HAL_Init() has to be necessarily called when I am using the HAL libraries....But my code seems to be running fine even without it. Are there any instances when it should specifically not be called?

My Code: What happens when i call HAL_Init is that all four LEDs light up and stay constant (Instead of blinking). Interrupt works as intended. After the interrupt handler is executed no LEDs light up any more...in debugger I can see that it is at fault handler. Even during this period interrupt works fine. The whole program, including the blinking works fine if i don't call HAL_Init.....Can you please let me know if I am doing a mistake..Thanks for your time....

seprac
Associate II
Posted on July 11, 2015 at 18:57

 #include <stm32f4xx_hal.h>

#include <stm32f4xx_hal_gpio.h>

#include <stm32f4xx_hal_rcc.h>

int main(void)

{

HAL_Init();

__GPIOD_CLK_ENABLE();

GPIO_InitTypeDef GPIO_Blink;

GPIO_Blink.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;

GPIO_Blink.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_Blink.Pull = GPIO_NOPULL;

GPIO_Blink.Speed = GPIO_SPEED_FAST;

HAL_GPIO_Init(GPIOD, &GPIO_Blink);

__GPIOA_CLK_ENABLE();

GPIO_InitTypeDef GPIO_Button;

GPIO_Button.Pin = GPIO_PIN_0;

GPIO_Button.Mode = GPIO_MODE_IT_RISING;

GPIO_Button.Pull = GPIO_PULLDOWN;

GPIO_Button.Speed = GPIO_SPEED_FAST;

HAL_GPIO_Init(GPIOA, &GPIO_Button);

HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(EXTI0_IRQn);

    while(1)

    {

    int i = 0;

     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12 |GPIO_PIN_13 |GPIO_PIN_14 |GPIO_PIN_15, GPIO_PIN_SET);

    for (i = 0; i < 500000; i++) {

    } //For Delay

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12 |GPIO_PIN_13 |GPIO_PIN_14 |GPIO_PIN_15, GPIO_PIN_RESET);

    for (i = 0; i < 500000; i++) {

    } //For delay

    }

    }

void EXTI0_IRQHandler(void)

{

  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);

}

void HAL_GPIO_EXTI_Callback (uint16_t GPIO_Pin)

{

int i=0;

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);

    for (i = 0; i < 500000; i++) {

    } //For Delay

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);

    for (i = 0; i < 500000; i++) {

    } //For Delay

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);

    for (i = 0; i < 500000; i++) {

    } //For Delay

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);

    for (i = 0; i < 500000; i++) {

    } //For Delay

 }

Posted on July 12, 2015 at 19:49

Are you doing this in a .CPP file?

Pretty much the primary causes for a Hard Fault are touching memory inappropriately, or firing an interrupt you aren't providing service for.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
seprac
Associate II
Posted on July 13, 2015 at 11:27

No. I am doing in a .c file. I am using the CooCox IDE...

Have used only the code mentioned above. Somehow, it refuses to work when I include the HAL_Init()

My main worry is that it should not cause any problems further in my project..that is why I wanted to clarify....

Amel NASRI
ST Employee
Posted on July 14, 2015 at 13:14

Hi seprac,

As described in the HAL package, the function HAL_Init is used to initialize the HAL Library and MUST be the first instruction to be executed in the main program.

Could you please debug again you code and step in the function ''HAL_Init'' to know where exactly it gets blocked?

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

seprac
Associate II
Posted on July 24, 2015 at 17:41

My apologies for answering this late.

The problem is not inside the function. Let me give you an example

1. When I include the function. My first line in while(1) is to light up two LEDs and then after a small delay turn off the LEDs. Now when I debug by putting breakpoints at the turn on and turn off commands, it is working perfectly fine. But even that is only until I give it an interrupt by means of my user button. Once the command in interrupt handler is executed (blinking of other two Leds), it directly jumps to the error handler.

If I don't give any breakpoints and simply run the program. The initial two LEDs from the main simply stay On instead of blinking. When I press my user button, the other two LEDs blink. After this, none of the LEDs are blinking anymore....it is when I pause I see that they are at the error handler

2. I don't include the HAL_Init() function. Everything is working super.

I was under the presumption that without including the function...my program won't work at all....Now I have even used peripherals like Timers and ADC...without calling HAL_Init()....Thanks a lot for your help...

Posted on July 24, 2015 at 18:32

Let me give you an example

Ok, but let's be clear, you keep describing the same basic set of facts, and a symptom. This is different from debugging and understanding the cause of something.

My guess would be an interrupt handler (SysTick or something else) is missing from stm32f4xx_it.c, or perhaps that CooCox isn't calling SystemInit() prior to entry.

I don't think you've provide enough source and project for anyone to independently understand what you're doing here and what is wrong. Given that we're not overwhelmed with people that have a problems with HAL_Init(), I'm going to assume that the problem is at your end, with either the project or the tools you're using.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..