cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX: don't generate PeriphCommonClock_Config(); leave clock config in MSP of HAL

sdtbb
Associate II

For background see:  https://community.st.com/t5/stm32cubemx-mcus/stm32cubemx-6-2-1-for-nucleo-h745zi-q-doesn-t-generate/m-p/226159/highlight/true#M10675

In many embedded systems most peripherals are off most of the time, for power saving, and they are enabled only when needed.  When CubeMX moves peripheral clock configuration from the MSP file of the HAL peripheral to PeriphCommonClock_Config() it makes this behavior harder to achieve.  Is there an option to tell CubeMX to leave clock configuration in the HAL MSPs rather than creating the shared function ?

1 ACCEPTED SOLUTION

Accepted Solutions
Ghofrane GSOURI
ST Employee

Hello @sdtbb 

CubeMX does not provide an option to keep peripheral clock configuration within the HAL MSP files when peripherals share a common clock source.

You can use user code sections in the MSP files to enable or disable individual peripheral clocks at runtime, while leaving the shared clock source configured at startup. This method is CubeMX-safe and allows for some level of dynamic control, though the shared clock source will remain active.

THX

Ghofrane

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.

View solution in original post

3 REPLIES 3
Ghofrane GSOURI
ST Employee

Hello @sdtbb 

CubeMX does not provide an option to keep peripheral clock configuration within the HAL MSP files when peripherals share a common clock source.

You can use user code sections in the MSP files to enable or disable individual peripheral clocks at runtime, while leaving the shared clock source configured at startup. This method is CubeMX-safe and allows for some level of dynamic control, though the shared clock source will remain active.

THX

Ghofrane

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.

STM32CubeMX only allows limited customization.

You can:

  • Disable calls of some generated functions such as SystemClock_Config(). but not PeriphCommonClock_Config()
  • modify generated functions by adding things in user code sections in those functions. PeriphCommonClock_Config() doesn't have usercode section
  • disable enabled clocks in user code after PeriphCommonClock_Config() (such as MSP file of a specific peripheral as mentioned by (Ghofrane GSOURI)
  • use macro tricks in user code sections to override the definition of PeriphCommonClock_Config()

Here is an example of how to use a macro to edit the definition of PeriphCommonClock_Config().
Original code:

 

#include <stdio.h>


/* USER CODE BEGIN PV */

/* USER CODE END PV */

void PeriphCommonClock_Config(void);

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main()
{
    
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */
  
   //can't change this code:    
   PeriphCommonClock_Config();
   
   /* USER CODE BEGIN SysInit */
   
   /* USER CODE END SysInit */
   
   return 0;
}

//can't change this code:
void PeriphCommonClock_Config()
{
    printf ("PeriphCommonClock_Config original\n");
}


/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

 

 Modified code:

#include <stdio.h>


/* USER CODE BEGIN PV */

/* USER CODE END PV */

void PeriphCommonClock_Config(void);

/* USER CODE BEGIN 0 */
void PeriphCommonClock_Config_modified(void);
#define PeriphCommonClock_Config() PeriphCommonClock_Config_modified()
/* USER CODE END 0 */


int main()
{
    
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */
  
   //can't change this code:    
   PeriphCommonClock_Config();
   
   /* USER CODE BEGIN SysInit */
   #undef PeriphCommonClock_Config
   #define PeriphCommonClock_Config() PeriphCommonClock_Config_original()
   /* USER CODE END SysInit */
   
   return 0;
}

//can't change this code:
void PeriphCommonClock_Config()
{
    printf ("PeriphCommonClock_Config original\n");
}


/* USER CODE BEGIN 4 */
void PeriphCommonClock_Config_modified()
{
    printf ("PeriphCommonClock_Config modified\n");
}
/* USER CODE END 4 */

Of course you need to manually merge any changes to PeriphCommonClock_Config() in your own implementation. But at least regenerating code with STM32CubeMX won't delete your changes.

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.
sdtbb
Associate II

Thanks for taking the time to reply.