2017-12-06 04:59 AM
I add some code to the files generated by CubeMX. When I regenerate the project, my added code will be deleted.
In the main.c is /* USER CODE BEGIN */ and /* USER CODE END*/ but in other files, there is no area where one can add user code.any idea how can I solve this issue?2017-12-06 05:53 AM
I suggest putting the source under source/version control and then look at the diffs after regenerating with CubeMX. It may also be helpful to add your own markers to the code you modified. This helps to visually show where your modifications are and may be particularly helpful after a CubeMX and/or Firmware Library update that result in a messy diff.
2017-12-06 08:45 AM
At the very least sandbox the code and merge in from the CubeMX generated tree, there have been several cases where user code and files have been destroyed. Not having some backup or protection strategy courts failure and loss.
2017-12-06 09:25 AM
I prefer never to mix generated code with hand-written code in the same files. I don't know how easy this is with CubeMX/HAL. I generated a CubeMX project just now for STM32F4: there were /* USER CODE xxx */ sections all over the place. Are you sure you have only one?
2017-12-07 07:39 AM
Hello
a.haloui
,Which CubeMx version are you using ?
Check the 'Code Generator' Tab in Project Settings and ensure that you pick 'Keep user code when re-generating', in that way you will note lose it in the next generation.
Best Regards
Imen
2017-12-08 06:13 AM
Hello Imen D,
I am using STM32CubeMX V1.0 Version 4.23.0. The 'Keep user code when re-generating' is set.
There is /* USER CODE xxx */ sections only in the 'main.c' , 'main.h' and 'stm32f7xx_it.c'. In this files the user code will not be deleted.
In other files 'Drivers folder' for example 'stm32f7xx_hal_uart.c/.h' the added code will be deleted because they havn't /* USER CODE xxx */ sections.
The result is, I have to create for each module my one module. This cause too many new files and repetitions This is not good for the overview in a big project.
Thank you for your support.
Best regards
2017-12-08 06:16 AM
There are many /* USER CODE xxx */ sections but only in 'main.c' , 'main.h' and 'stm32f7xx_it.c'. In this files the user code will not be deleted.
2017-12-08 06:46 AM
Ah, only files with USER CODE sections are 'intended' to be modified. This will be files in the Inc/ and Src/ directories. Files in other directories (e.g. Drivers) are not intended to be modified. As Alan Chambers points out, it's not a good idea to just add code to theses files. However, to my original point, sometimes it's necessary to modify these files; to fix a bug for example. This is where I use a combination of source/revision control and my own custom code markers. Clive One suggestion of sandboxing also works well.
If you generated code in individual c/h files, you should have Src/usart.c and Inc/usart.h. Custom UART code can be added to these files in the desired USER CODE sections. Alternatively, you can add your own files to the project with your custom code.