monoplex
monoplex

Reputation: 11

STM32F103C8 project - I can't get HAL to work in my own functions

This is a strange one for me and I certainly did not expect to get so completely stumped in my first day or so of learning the STM32CubeIDE. The first order of the day was making the onboard led blink with

HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
HAL_Delay(500);

Going through the New Project/pin config and code generator this worked fine inside the while loop provided in the main.c file but when I called my own function inside the loop with the HAL commands it stops working. The code loops and calls my function mainLoop() but any HAL commands inside my function don't do anything.

Why is this - am I missing some kind of handle that must be passed or an #include to extend the scope of HAL commands within a source file and not just main() itself?

      while (1)
      {
          HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
          void mainLoop(void);
    //    HAL_Delay(500);         // works fine when uncommented
      }
    }   // end of main()

    void mainLoop(void)
    {
          HAL_Delay(200);         // this does nothing

}                             // no HAL commands actioned

Yes, you are quite right and thanks for that. My stupid question was the result of me actually trying to put mainLoop() in another .cpp file, with appropriate .h header file containing the prototype and call it from the main.c while loop. Since I got an undefined reference to mainLoop linker error, I moved mainloop() back into main.c and then tried to run the prototype. Doh!

So, despite my flawed attempt to simplify the problem, I still cannot call my mainloop() function with its HAL commands residing in another .cpp file. Additionally, although I have yet to reach this point, are the HAL commands available in every project source file or will I need to #include a particular header?

Upvotes: 1

Views: 843

Answers (2)

0___________
0___________

Reputation: 67476

void mainLoop(void);  //function declaration (prototype) needed as mainLoop is defined after the call

int main(void)
{
    while (1)
    {
        HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
        mainLoop(); //call to the function
    }
}

void mainLoop(void)  // function definition
{
    HAL_Delay(200);         // this does nothing

}   

As your mainLoop call is before the function definition, you need to provide the function prototype to tell compiler that parameters takes the function and what it returns.

Then you need to call the function by mainLoop() - not void mainLoop()

Upvotes: 2

dev_eng
dev_eng

Reputation: 126

You should call function in the following way.

  while (1)
      {
          HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
          mainLoop();
    //    HAL_Delay(500);         // works fine when uncommented
      }
    }   // end of main()

    void mainLoop(void)
    {
          HAL_Delay(200);         // this does nothing

}    

This is not the way to call the function.

Upvotes: 2

Related Questions