6

I have a project where I need to add a few lines to one of the generated C files. However, the place where I want to add the change does not have a "USER CODE BEGIN / END" section. So whenever, I regenerate code, the changes are overwritten. I tried adding my own user code section as shown below but even that got overwritten. It seems, CubeMX is looks for a predefined set of USER CODE blocks, and overwrites everything else.

/* USER CODE BEGIN 8 */

/* USER CODE END 8 */

I would like to be able to define my own user code blocks so that I can write custom code in places where CubeMX has not already provided a user code block.

Dojo
  • 5,374
  • 4
  • 49
  • 79
  • I have a hard time believing there exists a location in code where a usable user code begin/end section doesn't already exist. Can you post the whole file and point out the exact location where you'd like to add the custom ones? I've seen a lot of STM32CubeMX-generated code and never found a spot I wanted to add code that didn't have the appropriate blocks. Perhaps I'm wrong (probably am :)) but this seems odd to me. – Gabriel Staples May 02 '19 at 16:05
  • I want to use the UART Idle Interrupt. There is already an ISR defined in code generated by CubeMX. This ISR does not have code to handle UART Idle condition. Nor is there any option in CubeMX UI to generate it. – Dojo May 04 '19 at 10:57
  • Yeah I think you're mistaken. I created a new project in STM32CubeMX, then enabled USART1 in Asynchronous mode, went to its NVIC Settings, and checked the box to enable its global interrupt. I then generated the code, and in the "Src/stm32f7xx_it.c" interrupt file there is a handler ISR called `void USART1_IRQHandler(void)`. It has 2 separate user code sections, labeled `/* USER CODE BEGIN USART1_IRQn 0 */` and `/* USER CODE BEGIN USART1_IRQn 1 */`, respectively. Inside these sections you can read the Idle event flag and handle it as you see fit. I can make this an answer if you like. – Gabriel Staples May 10 '19 at 02:16
  • @GabrielStaples what is the version of your STM32CubeMX and the Firmware Pack? Maybe they added it in the latest version, I downgraded to a previous version because the latest version I installed some time ago, generated unstable code. In any case, this question stays because UART IRQ is just an example, there could be many other places where user might want to insert their own code. – Dojo May 12 '19 at 13:36
  • I don't remember the version, but I verified this about a year ago on whatever version of STM32CubeMX and Firmware Pack was the latest version at the time, and just the other day I verified it 1) in STM32CubeMX 5.0.1 with STM32F7 firmware pack 1.14.0, as well as 2) in STM32CubeIDE 1.0.0 (which contains STM32CubeMX 5.2.0 embedded inside it) with STM32F7 firmware pack 1.15.0. The board I used was Nucleo F767. I don't think this is a new feature. In the last 1~1.5 years of use I've never, not once, found or heard of a single case where a needed USER CODE BEGIN/END section didn't exist. – Gabriel Staples May 13 '19 at 19:49

1 Answers1

6

Adding custom user code sections is not supported by CubeMX.

See this support post:

https://community.st.com/s/question/0D50X0000ALxNlmSQF/is-it-possible-to-add-custom-user-code-sections

Sid
  • 563
  • 6
  • 23