cancel
Showing results for 
Search instead for 
Did you mean: 

Avoid code being overwritten by auto-generation

pass3master
Associate III

This is a question about automatically generated code.
When we change the ioc file in STM32CubeIDE, there is some code that We don't want to be overwritten by the automatic generation. How can we avoid this?
Specifically, We have changed the value of HAL_StatusTypeDef in stm32f4xx_hal_def.h as follows. We don't want this to be overwritten.

 

typedef enum
{
HAL_OK = 0x01U,

HAL_ERROR = 0x02U,

HAL_BUSY = 0x04U,

HAL_TIMEOUT = 0x08U

} HAL_StatusTypeDef;
1 ACCEPTED SOLUTION

Accepted Solutions

@pass3master wrote:

We would like to slightly shift the return value from HAL_SPI_TransmitReceive.


Please explain!

As @SofLit suggested, you could just create a "wrapper" function to transform the return value; eg,

inline your_return_type hacked_SPI_TransmitReceive( stuff )
{
   return HAL_SPI_TransmitReceive( stuff ) + your_offset;
}

View solution in original post

7 REPLIES 7
Andrew Neil
Evangelist III

@pass3master wrote:

When we change the ioc file in STM32CubeIDE, there is some code that We don't want to be overwritten by the automatic generation. How can we avoid this?


That's what the USER CODE sections are for - see this thread, for example:

https://community.st.com/t5/stm32cubeide-mcus/code-generation-would-clear-my-code-in-main-c/m-p/720044

 

See also:

https://www.st.com/resource/en/user_manual/um1718-stm32cubemx-for-stm32-configuration-and-initialization-c-code-generation-stmicroelectronics.pdf#page=468

 

and make sure that you have 'Keep user code when re-generating' selected:

AndrewNeil_0-1727085015652.png

 

Sorry, I'm not very smart.
Am I correct in thinking that code that is not in the USER CODE section can be updated, and that users cannot add USER CODE sections on their own?
In that case, what would be an effective way to slightly shift the return value from HAL_SPI_TransmitReceive?

SofLit
ST Employee

Hello @pass3master,


@pass3master wrote:
typedef enum
{
HAL_OK = 0x01U,

HAL_ERROR = 0x02U,

HAL_BUSY = 0x04U,

HAL_TIMEOUT = 0x08U

} HAL_StatusTypeDef;

This is a part of the HAL, you cannot change its definitions. May be you need to do it in your application, get the value from HAL_SPI_TransmitReceive and transform it to the value you want.

But you can add your own code between /* USER CODE BEGIN  XXX */ and /* USER CODE END XXX */

 

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.

Thank you for your answer. I see, I understand that it cannot be changed.
We would like to slightly shift the return value from HAL_SPI_TransmitReceive. Is there any good way to do this?


@pass3master wrote:

We would like to slightly shift the return value from HAL_SPI_TransmitReceive.


Please explain!

As @SofLit suggested, you could just create a "wrapper" function to transform the return value; eg,

inline your_return_type hacked_SPI_TransmitReceive( stuff )
{
   return HAL_SPI_TransmitReceive( stuff ) + your_offset;
}

@pass3master 


@pass3master wrote:

We would like to slightly shift the return value from HAL_SPI_TransmitReceive. Is there any good way to do this?


Already said:   "May be you need to do it in your application, get the value from HAL_SPI_TransmitReceive and transform it to the value you want."

@Andrew Neil gave you an example of implementation of it.

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.
TDK
Guru

Changing core HAL definitions sounds like a nightmare for future maintainers.

If you want to fork HAL and change it, go for it, but don't expect all the tools to work correctly. HAL_StatusTypeDef works fine as is, no need to "improve" it. Having it be defined as a bit mask doesn't make a whole lot of logical sense here.

If you feel a post has answered your question, please click "Accept as Solution".