Hello @Enour.1 ,
In CubeMx for keeping the USER CODE section, you should check "code generator" setting and ensure that you ticked this option "Keep user code when re-generating", as following: Project Manager -> [Code Generator] TAB, then tick "Keep User Code when re-generating"
To protect all the modifications you did, you have to insert your code in the USER CODE sections parts defined in the main file like :
/* USER CODE BEGIN xx */
...
/* USER CODE END xx */
When you modify your configuration with CubeMX, this tool generates a new set of files needed to support your configuration. So all files part of the previous configuration are overwritten. But any code located in those sections is preserved against overwriting. Otherwise the user code is lost.
Hope this helps you.
When your question is answered, please select the Best answer so that this thread will be marked as solved.
Imen
In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks