cancel
Showing results for 
Search instead for 
Did you mean: 

How to change Init functions to global?

Francis B.
Associate II

I am currently attempting to build code in simulink for a GIGA R1 project I have. However when setting the ioc file up in CubeMX I am unable to change the init functions to be global. Any help would be appreciated. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Ghofrane GSOURI
ST Employee

Hello @Francis B.​ 

First let me thank you for posting.

To change the initialization functions generated by CubeMX and make them global functions, you can follow these general steps:

1-Open your project in CubeMX and navigate to the "Pinout & Configuration" tab.

2-Review and modify the peripheral configurations as per your requirements.

3-In the "Project" tab, make sure that the "Generate peripheral initialization as a pair of '.c/.h' files per peripheral" option is selected. This will generate separate source (.c) and header (.h) files for each peripheral.

4-Generate the code based on your configurations.

5-Open the generated source file ,find the initialization function for the peripheral you want to make global. The function name should follow a pattern like HAL_<Peripheral>_MspInit. For example, for the USART1 peripheral, the function will be HAL_UART_MspInit then copy the function declaration from the generated source file.

6-Create a new header file (e.g., peripherals.h) or open an existing header file where you want to declare the global initialization functions and paste the copied function declaration into the header file.

7-Add the function declaration to the header file's list of function prototypes. Make sure it is declared with the appropriate visibility (e.g., extern) to be accessible globally.

8-Save the header file.

9-Open the main source file (e.g., main.c) or any other file where you want to call the global initialization functions.

10-Include the header file where you declared the global initialization functions using the #include preprocessor directive.

11-Call the global initialization function(s) as needed in your code.

12-Build and compile your project.

By following these steps, you can modify the initialization functions generated by CubeMX and make them accessible globally in your code. This allows you to have more control over the initialization process and make any necessary modifications without modifying the generated code directly.

Thx

Ghofrane

View solution in original post

3 REPLIES 3
Ghofrane GSOURI
ST Employee

Hello @Francis B.​ 

First let me thank you for posting.

To change the initialization functions generated by CubeMX and make them global functions, you can follow these general steps:

1-Open your project in CubeMX and navigate to the "Pinout & Configuration" tab.

2-Review and modify the peripheral configurations as per your requirements.

3-In the "Project" tab, make sure that the "Generate peripheral initialization as a pair of '.c/.h' files per peripheral" option is selected. This will generate separate source (.c) and header (.h) files for each peripheral.

4-Generate the code based on your configurations.

5-Open the generated source file ,find the initialization function for the peripheral you want to make global. The function name should follow a pattern like HAL_<Peripheral>_MspInit. For example, for the USART1 peripheral, the function will be HAL_UART_MspInit then copy the function declaration from the generated source file.

6-Create a new header file (e.g., peripherals.h) or open an existing header file where you want to declare the global initialization functions and paste the copied function declaration into the header file.

7-Add the function declaration to the header file's list of function prototypes. Make sure it is declared with the appropriate visibility (e.g., extern) to be accessible globally.

8-Save the header file.

9-Open the main source file (e.g., main.c) or any other file where you want to call the global initialization functions.

10-Include the header file where you declared the global initialization functions using the #include preprocessor directive.

11-Call the global initialization function(s) as needed in your code.

12-Build and compile your project.

By following these steps, you can modify the initialization functions generated by CubeMX and make them accessible globally in your code. This allows you to have more control over the initialization process and make any necessary modifications without modifying the generated code directly.

Thx

Ghofrane

Thanks! This is very helpful. However, just to be clear there isn’t a setting to do this when generating the code?

Ghofrane GSOURI
ST Employee

Hello again @Francis B.​ 

No, there isn't a specific setting in CubeMX to automatically generate the initialization functions as global functions. The initialization functions generated by CubeMX are typically intended to be called internally within the generated code, and they are not directly accessible globally by default.

When your question is answered, please close this topic by choosing Select as Best. This will help other users find that answer faster.

Thx

Ghofrane