stm32 To update the program , The general practice is to use j-link Or serial port download . Is it possible to realize unlimited communication , How about online update ?
In order to realize this function of online update , I wrote a simple analogy BootLoader Procedures for . The idea is to add a piece of code before the application executes , You can choose a communication method to receive the new application and compile the generated bin file , Then solidify to flash in , And jump to the application to execute the corresponding code . An interface is given in the application code , When executing the application, you can also jump to the beginning of the program and then perform the update operation . So you can update online .
First one STM32Flash Address bus diagram of
You can see , from 0x8000000 reach 0x807FFFF All of them flash Storage range of . And we put online updates on the flash start , So that it can be called when it is powered on .
therefore , This update we're going to write , Only one way of communication is needed , Receive new applications , Then cure it to flash Another section of the . So this program often takes up a small amount of memory space .
ad locum , We assigned it a certain size flash and RAM:
flash and RAM It can be selected according to the size and resources of your actual update program .
In the update program , You've chosen a way of communication , Received application's .bin file (.bin Files can be created by MDK Generated when the application is compiled , How to do it is not necessary to elaborate ). Then call flash Write operation , Cache received in RAM Inside .bin file copy To your flash in . It's very simple , Just call the library function .
The next step is to call your application . Application's main Function address ( That is, the reset operation corresponding to the interrupt vector table ) It's stored in MDK Generated .bin The fourth to eighth byte in the file ( Because the address is 32bit Of ) in ( The first four bytes hold the top pointer of the stack ). So we define a function pointer , Then assign a value to the pointer , You can call . Look at the code :
typedef void (*myFunction)(void);
The one defined here myFunction Function pointer with no parameter and no return type . This statement may be written as typedef myFunction void (*)(void);
Will be better understood , But we still have to comply C The grammar of language .
Then we define a function pointer variable :
myFunction junpMyAPP;
When we write our applications , We can also MDK Of the program RAM and flash Starting address and space used for , Just don't conflict with our previous updates . Let's assume that the starting address we selected for the application is myAPP_ADDR, At this point, the function pointer can be assigned :
junpMyAPP=(myFunction)*(volatile unsigned int*)(myAPP_ADDR+4);
Let's analyze this code . I've already mentioned that , Application's main The function address is stored in the .bin In the fourth to eighth byte of the file . therefore (myAPP_ADDR+4) It's pointing main Pointer to the address of the function , So we use it (volatile
unsigned int*) Indicates that it is a 32bit Pointer to , At the same time volatile Guaranteed not to be optimized by compiler .(volatile unsigned
int*) Former * Represents the value of this address , that is main The value of the pointer to the function , Last but not least (myFunction) hold main The value of the function pointer is cast to the previously defined function pointer type , It's done. Right junpMyAPP Assignment operation of function pointer .
next , Just call :junpMyAPP(); You can successfully jump to our application . In the app , You can also use this method to jump to our update program at an appropriate time , Realize online update .
Because this paper only describes the principle of simple online update , So a lot of details are ignored , In addition, my level is limited , deficiencies , Please correct it , thank you .
Technology
Daily Recommendation