Author Topic: STM32CubeMX Round Trip?  (Read 1577 times)

0 Members and 1 Guest are viewing this topic.

Offline NivagSwerdnaTopic starter

  • Super Contributor
  • ***
  • Posts: 2495
  • Country: gb
STM32CubeMX Round Trip?
« on: April 26, 2018, 07:46:36 am »
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 to
C:\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 work  :-+

But... 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.

« Last Edit: April 26, 2018, 07:59:30 am by NivagSwerdna »
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1719
  • Country: se
Re: STM32CubeMX Round Trip?
« Reply #1 on: April 26, 2018, 08:02:16 am »
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: [Select]
  /* 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.

Nandemo wa shiranai wa yo, shitteru koto dake.
 
The following users thanked this post: rqsall

Offline NivagSwerdnaTopic starter

  • Super Contributor
  • ***
  • Posts: 2495
  • Country: gb
Re: STM32CubeMX Round Trip?
« Reply #2 on: April 26, 2018, 08:30:12 am »
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!
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: STM32CubeMX Round Trip?
« Reply #3 on: April 26, 2018, 10:08:05 am »
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.

[2c]
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 
The following users thanked this post: Tandy, rqsall

Offline NivagSwerdnaTopic starter

  • Super Contributor
  • ***
  • Posts: 2495
  • Country: gb
Re: STM32CubeMX Round Trip?
« Reply #4 on: April 26, 2018, 12:11:50 pm »
Put your code in a separate file (inline function) and call that from within the comment indicators.
Absolutely.

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.
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1719
  • Country: se
Re: STM32CubeMX Round Trip?
« Reply #5 on: April 26, 2018, 01:57:56 pm »
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? :popcorn:
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline lucazader

  • Regular Contributor
  • *
  • Posts: 221
  • Country: au
Re: STM32CubeMX Round Trip?
« Reply #6 on: May 03, 2018, 09:58:51 pm »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf