STM32CUBE Change pins without losing all you code - Page 1
Forum section: electronics / general

author: ebclr
author_rank: Super Contributor
timestamp: September 18, 2022, 09:46:24 am
content: I have a project made with stm32cubeMX and is running OK.Then I changed the .ioc to add 2 more LEDs on Gpio X, When I clicked generate all my code vanished, and it is now a project without any of my previous code.Is there a way, to change the IOC but maintain your previous code?

author: Doctorandus_P
author_rank: Super Contributor
timestamp: September 18, 2022, 09:56:31 am
content: Put your project in GIT.Another (additional) method is to use two projects. Use one project from the Cube software just for examples for the initialization, and use another project for writing your own code. And then, whatever ST Cube decides to change to the configuration project can be merged into your project. Either manually or with GIT.

author: dtodorov
author_rank: Contributor
timestamp: September 18, 2022, 10:03:48 am
content: If you change any code in the auto-generated files by Cube, make sure to only edit inside User begin/end sections as marked by the comments.I've been doing many IO assignment changes thru STM32 IDE and while respecting this User Begin/End code placement I have not had any code loss after ioc modifications.

author: Kleinstein
author_rank: Super Contributor
timestamp: September 18, 2022, 04:18:50 pm
content: Before use the auto generate code function make a backup. Normally the user code sections should be kept as is, but there is a chance that thing go wrong. This could happen with things like wrong comment ends or accidentially deleted / coppied parts so that the file structure is not recognized.It anyway helps to have only a small part of the actual code in the main file and much in separate files that are included. The devision in many small user sections with much auto-generated code in between makes it a bit hard to read.

author: ebclr
author_rank: Super Contributor
timestamp: September 18, 2022, 04:26:14 pm
content: I found the problem, I was using the wrong user begin end session, Now the code isn't lost

author: lucazader
author_rank: Regular Contributor
timestamp: September 18, 2022, 08:44:43 pm
content: In addition to using git and placing the code in the user sections,I would also siggest adding new c files to perform your user code.And then just place a single include and single function call that jumps into your user code in the main.c file.This is what we use at work for our stm based products and it works quite well,It allows us to update the cube config for new features or peripheral configurations. In addition to allowing us to easily update to the latest HAL versions, with no disruption to our application.

author: DavidAlfa
author_rank: Super Contributor
timestamp: September 20, 2022, 07:39:16 am
content: And as they told you, git is your friend! You don't even need doing it online, you can make a local repo.Setting up git is not hard at all:- Go to Help, Eclipse Marketplace, install EGit, restart IDE.- Go Window/Show view/Other/Git, select Git Staging and Git Reflog.- Right click on your project, Team, Share Project, create.- Done.Better to not do it under the same project folder, but to ex. C:\Users\username\git\yourProjectName so in case some weird bug or user mistake wipes the whole project folder (I've done that few times) the git repro will survive and you'll be able to recover.If you also deleted the repo, that's the end of the game, start over!In Git Staging window, add the files you want to add to a certain commit, if you double click the file you can add only specific lines, so you can write the code in a go and then slowly add the changes in different commits.Then write your commit message, ex. "Add SPI driver", "Add parsing function", "Create UART functions", "Test new SPI driver settings", etc, and click Commit.Normally your first commit is adding the whole project, "Initial commit". Then keep pushing commits as you progress.Git is a little complex and takes some practice, but the basics are easy, commit for every new important modification and do some effort in describing the commit messages, because you won't remember all your changes in 2 months from now lol.If anytime you want to revert your project to a certain revision, go to Reflog, right click on the commit, click Reset...Hard. All your project will be cleared and set to that specific version.As long as it's in the git repo, you can go back and forth as you wish, ex. if you made some change in a driver causing a bug you could easily test different code revisions, quickly tracking where it started happening.No more "What the HELL did I change, why doesn't anymore?" thing. Spending two hours trying to remember or analyzing the code line by line. If you change a single letter, the change will appear in the git staging window.No more dirty project backups flooding everything (v001.zip, v002.zip, v002_test.zip, v003_new_test.zip...), no more dirty text files trying to document the thing (changes.txt, whatever).You will see how it greatly improves your live and ask yourself why you didn't use it before!Write your code within the user code tags or put your code in new source files to avoid CubeIDE wiping your work.Everything outside the tags will be wiped and regenerated (User's custom source files are unaffected)Ex.Code:/* USER CODE BEGIN */Your code here/* USER CODE END */

author: TomS_
author_rank: Frequent Contributor
timestamp: September 22, 2022, 06:26:55 am
content: +1 for git (or similar - anything is better than nothing really).I also use GitHub to store my repos (some public, some private) because it acts like an off-site backup, and I can browse my code while I'm out and about or on a lunch break.Sometimes I have even used my mobile phone to make code changes e.g. on the train to/from work. It's quite handy for those situations where you're stuck on something and have that "aha!" moment and can fix it right there and then. When you get home you can just pull down the changes and continue on.