STM32CubeMX Round Trip? - Page 1
Forum section: electronics / general

author: NivagSwerdna
author_rank: Super Contributor
timestamp: April 26, 2018, 07:46:36 am
content: Got my first taste of the STM32 ecosystem at IoT workshop thing last night... (I'm more used to PIC and AVR)...Anyway...The configurator seems to be STM32CubeMX along with the family specific STM32Cube_FW_L4_V1.11.0 which I imported and then seems to have mysteriously extracted toC:\Users\Nivag\STM32Cube\Repository\STM32Cube_FW_L4_V1.11.0 never to be seen again.Using STM32CubeMX I can click on a few pins, configure some clocks etc and generate a bare bones project... got that to workBut... Can you round trip in STM32CubeMX? i.e. go back add some more bits and pieces and then get back to your project without wiping out your user code?Is there a trick to surviving the round trip?/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();/* USER CODE BEGIN 2 *//* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */I think the USER CODE comments are the magic... how does the parser work... is the name of the USER CODE section significant?/* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE */It seems a bit weird... since the code generator seems to have actually created some code in the USER code section. The "while (1) {" text wasn't created by me.

author: newbrain
author_rank: Super Contributor
timestamp: April 26, 2018, 08:02:16 am
content: No special trick is needed as long as you keep the added code inside the USER CODE comment brackets.This is a main loop, e.g.:Code:/* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */Just make sure that the "Keep User Code when regenerating" tick box be marked in the Code Generation tab of the project properties (Alt-P).Everything added between an USER CODE BEGIN xxx and an USER CODE END xxx comment is preserved when regenerating the code from CubeMX.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: April 26, 2018, 08:30:12 am
content: Ah OK... so the "while (1) {" was just boiler plate when it originally created the sections... so it won't regenerate that...I got.../* USER CODE BEGIN WHILE */int sausage=1;int fruitbat=1;while (sausage==fruitbat){HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);HAL_GPIO_TogglePin(LED3_WIFI__LED4_BLE_GPIO_Port, LED3_WIFI__LED4_BLE_Pin);HAL_Delay(500);/* USER CODE END WHILE */to make the roundtrip.after checking the checkbox on the options. Thanks!

author: obiwanjacobi
author_rank: Super Contributor
timestamp: April 26, 2018, 10:08:05 am
content: Put your code in a separate file (inline function) and call that from within the comment indicators.When you accidentally forget to check the correct options or start anew, you still have your (original) code.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: April 26, 2018, 12:11:50 pm
content: Quote from: obiwanjacobi on April 26, 2018, 10:08:05 amAbsolutely.I was just intrigued by how the round trip works. It must use the block name as a key and then extract the user bits and subsequently merge them back with the newly generated code. Some blocks must have default content.

author: newbrain
author_rank: Super Contributor
timestamp: April 26, 2018, 01:57:56 pm
content: Good advice from obiwanjacobi.Though I must admit I do exactly the opposite: I use CubeMX to generate an initial template (mainly with LL, lately), then remove all the /* USER ...*/ stuff and forget about it...If I need to add a peripheral initialization, I do it by hand.OTOH, the option is set by default and of course we all use git or hg or any RCS, don't we?

author: lucazader
author_rank: Regular Contributor
timestamp: May 03, 2018, 09:58:51 pm
content: I use the cube like obiwanjacobi has suggested.Generate a project using the cube as a makefile project (allows for more portability of code down the road). I then adjust the makefile according to my needs (including making the source files use relative paths rather than the absolute paths the cube generator uses).I then leave all of the generated files alone, apart from adding a "user_main" function inside the /* user code */ section inside the while(1) loop.I then add the correct #include for my user_main in the appropriate include section at the top of the cube generated main file.Then whenever i want to re-configure a peripherals setting or similar, i open up the cube file, adjust the setting and re-generate.The only hiccup with the re-generation is that it will re-add all the absolute paths into your makefile, But i just use git to discard the changes to the makefile to make sure the generation didn't mess that up.