cancel
Showing results for 
Search instead for 
Did you mean: 

Git Commit Hash flashed along with my code?

Javier1
Principal

Howdy everyone.

I need my MCU to be selfaware of what FW version is currently flashed,

so far im just manually typing the git commit hash inside a const string every time i change the FW version.

0693W000008GHL5QAO.pngIt looks to me like a feature that should be supported somewhere but i couldnt find a thing.

Is there a way to automate this process?

cheers

we dont need to firmware by ourselves, lets talk
1 ACCEPTED SOLUTION

Accepted Solutions
Javier1
Principal

I found how:

From this post https://stackoverflow.com/questions/44038428/include-git-commit-hash-and-or-branch-name-in-c-c-source

I added the following command inside Pre-build steps:

git log --pretty=format:'#define GIT_INFO_PRESENT%n static const char* GIT_INFO = "Version Information=[%H,%d]\r\n";' -n 1 > ../Core/Inc/gitcommit.h

0693W00000BaGGpQAN.png 

Creates the following content inside gitcommit.h

#define GIT_INFO_PRESENT
 static const char* GIT_INFO = "Version Information=[abc493ac20blablablae94a692e68, (HEAD -> master, origin/master, origin/HEAD)]\r\n";

we dont need to firmware by ourselves, lets talk

View solution in original post

11 REPLIES 11
KnarfB
Principal III

Here is a nice idea requiring git only:

https://stackoverflow.com/questions/16524225/how-can-i-populate-the-git-commit-id-into-a-file-when-i-commit/38087913#38087913

Instead of the makefile modification, it can be done in STMCubeIDE as a pre-build command.

hth

KnarfB

Javier1
Principal

ill try that out and let you know.

thanks

we dont need to firmware by ourselves, lets talk
Uwe Bonnes
Principal III

This is what we have inth BMP debugger Makefile:

```

include/version.h: FORCE

       $(Q)echo " GIT include/version.h"

       $(Q)echo "#define FIRMWARE_VERSION \"$(shell git describe --always --dirty --tags)\"" > $@

```

sorry but im a bit new with the makefile structure of stm.

What (where) do you mean with BMP debugger makefile?

we dont need to firmware by ourselves, lets talk

BMP is some project for a debugger. But that does not matter for your problem, it should only show the principle

So youre saying BMP is just the name of your project.

I didnt understand what did you mean with "it should only show the principle"

Ok im diving inside the makefiles :0693W000008GS74QAG.png 

I am new with makefiles and i dont really know where to stick your code snippet:

include/version.h: FORCE
       $(Q)echo " GIT include/version.h"
       $(Q)echo "#define FIRMWARE_VERSION \"$(shell git describe --always --dirty --tags)\"" > $@

0693W000008GS79QAG.png

we dont need to firmware by ourselves, lets talk

When run in the example above, include/version.h contains something like '#define FIRMWARE_VERSION "v1.7.1-129-g43b79f10-dirty' Then "#include "version.h" in your code and you can access the string as FIRMWARE_VERSION.

and i can paste

include/version.h: FORCE
           $(Q)echo " GIT include/version.h"
           $(Q)echo "#define FIRMWARE_VERSION \"$(shell git describe --always --dirty --tags)\"" > $@

anywhere in my makefile?

Also i noticed the makefiles get autogenerated everytime you rebuild your project in stm32cubeIDE so they overwrite anything i add to them,how are you dealing with this?

we dont need to firmware by ourselves, lets talk

I do no use CubeIDE.