Skip to main content
Associate II
March 22, 2024
Solved

Custom initialization of peripherals

  • March 22, 2024
  • 2 replies
  • 1384 views

Hello,
I am working with timers in a stm32 project. In the generic INIT function that is generated using the .ioc file important functions like HAL_PWM_Init(),  HAL_TIMEx_MasterConfigSynchronization() etc are understandably called outside the user code sections. Now, I want to customize the init function so that I can use variables for prescaler, period etc.  For that I need to add user code and call functions like PWM_init and ConfigSync again after they are already called(?) Wouldn't that be inefficient? Alternatively, if I remove the code outside the user code section or completely remove the function itself it will reappear once I generate code after making some changes to the .ioc file. I can make custom init functions but the init functions will still remain in the code. I know I can stop using the generate code functionality but is there some way around it where I can have my cake and eat it too? Modify code outside the user code sections and it not be overwritten when I need other peripherals' code added/modified using the .ioc file?

 

This topic has been closed for replies.
Best answer by Bob S

When you click on the line to edit, say, the PWM "Pulse" line, look at the right-hand edge of the line and you will see a "gear" icon.  Click on that and select "no check".  Then you can enter your function or variable name in that field.  Alas I am not at a computer with CubeMX installed so I can't post a picture.  Then make sure you add the function prototype or variable "extern" declaration (or include the proper file) up the user sections at the top of the tim.c file.

I do this with I2C slave addresses that are determined at run time, among other things.

2 replies

Bob S
Bob SBest answer
Super User
March 22, 2024

When you click on the line to edit, say, the PWM "Pulse" line, look at the right-hand edge of the line and you will see a "gear" icon.  Click on that and select "no check".  Then you can enter your function or variable name in that field.  Alas I am not at a computer with CubeMX installed so I can't post a picture.  Then make sure you add the function prototype or variable "extern" declaration (or include the proper file) up the user sections at the top of the tim.c file.

I do this with I2C slave addresses that are determined at run time, among other things.

Andrew Neil
Super User
March 25, 2024

@Avi_8 wrote:

I can make custom init functions but the init functions will still remain in the code. I know I can stop using the generate code functionality but is there some way around it where I can have my cake and eat it too? Modify code outside the user code sections and it not be overwritten when I need other peripherals' code added/modified using the .ioc file?


You could simply not put any timer setup in the .ioc ?

ie, Put the other peripheral configs in the .ioc and, effectively, do the Timer(s) "manually" ?

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.