cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to add custom prefix/suffix to functions name generated by CubeMX?

dbicego
Associate II

For example if the original name is:

void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init);

the expected result should be:

void MyPrefix_HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init);

4 REPLIES 4

Hi

By CubeMX it is not possible.

CubeMX uses HAL drivers not manipulates them.

It generates (optionally) functions like void MX_XXXX_Init(void). XXXX is the name of peripheral.

Hi Vangelis, thank you for your answer.

Adding prefix at that level would be fine for me.

Do you know if it possible to add custom prefix to function generated by CubeMX?

For example if the original name is:

void MX_GPIO_Init(void);

the expected result should be:

void MyPrefix_MX_GPIO_Init(void);

I try to explain my purpose, maybe there are other ways to do it.

I want to manage different configurations of the micro from the same software.

Suppose you have two HW configurations and you know at runtime which HW you are running (for example by reading some fixed inputs at startup).

So I thought to have an array of function pointers to the proper GPIO_init function.

For example :

void (*generic_gpio_init[n_of_configurations])(void);
 
generic_gpio_init[0] = C0_MX_GPIO_Init;
generic_gpio_init[1] = C1_MX_GPIO_Init;
...
generic_gpio_init[n] = Cn_MX_GPIO_Init;
 

I don't know if there are other methods to manage different configurations from CubeMX without change the generated code.

Hi again.

I understand very well what is your problem.

Unfortunately for your case, it is not possible to rename by CubeMx.

To configure Pins at run-time demands to design a new module than to link all possible configurations together.

Take a look at this piece of code to get an idea what i mean.

It is for F4 family , no error check, only initialization.

After call this function you can use any GPIO HAL function about any pin you configured.

Use configurations with only parameters (not with code) to initialise GPIOs.

//##################################################################
//port > 0 - max_port
//pin > 0-16
//behavior 0= input    other= output
//rise_IRQ,fall_IRQ  > generate an event on it (if !=0)  otherwise not
//initial_state > if GPIO is output , determines the initial state
void IODeviceConfigurePin(unsigned char port, unsigned char pin,unsigned char behavior,
	unsigned char rise_IRQ,unsigned char fall_IRQ,unsigned char initial_state)
{
	GPIO_TypeDef* IO_PORT;
	GPIO_InitTypeDef GPIO_InitStruct;	
	IO_PORT= ((GPIO_TypeDef *) (AHB1PERIPH_BASE + 0x0400*(port)));	
	if			(IO_PORT==GPIOA)__GPIOA_CLK_ENABLE();
	else if	(IO_PORT==GPIOB)__GPIOB_CLK_ENABLE();
	else if	(IO_PORT==GPIOC)__GPIOC_CLK_ENABLE();
	else if	(IO_PORT==GPIOD)__GPIOD_CLK_ENABLE();
	else if	(IO_PORT==GPIOE)__GPIOE_CLK_ENABLE();
	else if	(IO_PORT==GPIOF)__GPIOF_CLK_ENABLE();
	else if	(IO_PORT==GPIOG)__GPIOG_CLK_ENABLE();
	else if	(IO_PORT==GPIOH)__GPIOH_CLK_ENABLE();
	else if	(IO_PORT==GPIOI)__GPIOI_CLK_ENABLE();
#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx)
	else if	(IO_PORT==GPIOJ)__GPIOJ_CLK_ENABLE();
	else if	(IO_PORT==GPIOK)__GPIOK_CLK_ENABLE();
#endif //#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx)
	if (behavior)//output mode
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP ;
	else //input mode
	{
		GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
		if(rise_IRQ) GPIO_InitStruct.Mode |= GPIO_MODE_IT_RISING;
		if(fall_IRQ) GPIO_InitStruct.Mode |= GPIO_MODE_IT_FALLING;
	}
		/*Configure GPIO pin */
	GPIO_InitStruct.Pin		= 0x0001<<pin;
	//GPIO_InitStruct.Mode |= GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull	= GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	GPIO_InitStruct.Alternate = 0;
	HAL_GPIO_Init(IO_PORT, &GPIO_InitStruct);
//****************************************************
	if (behavior)//if is an output
	{
		if(initial_state)
			IO_PORT->BSRR = 1<<pin;// set the pin
		else 
			IO_PORT->BSRR = 1<<(pin+16);// reset the pin
	}
}	
//##################################################################

Hi Vangelis, thank you for sharing your code.

I analyzed better the output code from CubeMX and I found that there are only few functions/variables that are really different from one configuration to another.

My plan is to use a post-generation script to apply some regular expressions to add my custom prefix to that code.

Obviously I can't test all possible configurations now and the regex will need to be updated from time to time, but if this method works it will be ok.

In case it will not work, I'll go with your suggestion. 😉

Thanks.

db