当前位置:网站首页>Write bootloader from 0
Write bootloader from 0
2022-07-27 20:45:00 【Work hard and you can go to heaven!】
So-called bootloader Boot loader , Be similar to windows Inside BIOS, It is used for loading some environments and necessary initialization before the program runs .
And the single chip microcomputer bootloader Mainly for OTA Upgraded , The function is to initialize the necessary running environment , Determine whether to upgrade , If you want to upgrade , Then copy the downloaded upgrade program to the operation of the MCU flash in , Then jump to APP Program running address , If you don't upgrade, jump directly to APP Program running address .
bootloader Implementation function diagram 
Is to achieve a jump .
Realization bootloaer And running the program requires two documents :
1.bootloader Program engineering 
1. Set up bootloader Program start position of , Such as 1 Shown , Generally not changed .
2. Set up bootloader The size of the program , As shown in Figure 2 , Generally, it is more practical to use bootloaer The program needs a little more space .
2.APP Program settings 
Same as bootloader The same meaning , however APP The running address needs to be set by you cheaply bootloader The address size of , stay 2 You also need to reduce your settings bootlader Program size of 
You can also change the starting position of the program here , But it is not recommended to change here , Just use the default here .
notes :SP The stack pointer does not need to be changed , Because bootloader and APP Only one program is running at the same time , So only one program is using the stack .
APP Program
1. No exception vector table is required 
You don't need the exception vector table to jump to APP The program start address can be executed .
2. Need exception vector table 
When the program jumps , If you directly use the address as a pointer , Pay attention to ,Thumb Instruction sets and ARM The difference between instruction sets .
typedef void(*IapAppFun)(void); // Define a function pointer , Used to jump to APP Program
#define FLASH_APP_ADDR 0x08010000 // Definition APP Program start address
void main
{
IapAppFun JumpApp;
// bootloader Some column initialization to be done
if (((*(__IO uint32_t *)FLASH_APP_ADDR) & 0xFF000000) == 0x08000000)
{
__disable_irq();// Turn off interrupt
JumpApp = (IapAppFun)*(__IO uint32_t *)(FLASH_APP_ADDR +4);
SCB->VTOR = FLASH_BASE|0x10000; // Inside the chip FLASH The starting address plus the offset address will be equal to APP The starting address of the program
JumpApp();
}
printf("APP is invalid!\r\n");// If the execution reaches here, it means that the jump fails
}
if (((*(__IO uint32_t )FLASH_APP_ADDR) & 0xFF000000) == 0x08000000)
The purpose of this sentence is to judge FLASH_APP_ADDR The value stored in this address is used to jump to flash OK operation SRAM Or other places , If M3 As an example , So if it is SRAM The operation should be changed to
if ((((__IO uint32_t *)FLASH_APP_ADDR) & 0x2FF80000) == 0x20000000) From SRAM Began to run
This depends on where the upgrade program you write wants to start .
JumpApp = (IapAppFun)*(__IO uint32_t *)(FLASH_APP_ADDR +4);
The purpose of this sentence is because ,FLASH_APP_ADDR The address of the exception vector table is stored on , Add 4 The following address means that the program start address is stored inside , So we need to use dereference plus its value to get it and give it to the function pointer , Used to jump execution APP Program .
SCB->VTOR = FLASH_BASE|0x10000;
The purpose of this sentence is to change APP Program exception vector table , because APP The program is relative to 0x08000000 Has shifted 0x10000, So the first address after the offset stores the exception vector table .
The above is purely personal opinion , I hope you can correct the mistakes .
边栏推荐
- Interviewer: what is the abstract factory model?
- 做测试, 就得去大厂,内部披露BAT大厂招聘“潜规则”
- Common methods of object learning [clone and equals]
- Leetcode:1498. Number of subsequences that meet the conditions [sort + bisection + power hash table]
- JVS基础介绍
- [dataset display annotation] VOC file structure + dataset annotation visualization + code implementation
- C language -- array
- 用户和权限限制用户使用资源
- In 2019, the global semiconductor market revenue was $418.3 billion, a year-on-year decrease of 11.9%
- What app should individuals use for stock speculation to be safer and faster
猜你喜欢

海康设备接入EasyCVR,出现告警信息缺失且不同步该如何解决?

Flask-MDict搭建在线Mdict词典服务

Graphic leetcode - Sword finger offer II 115. reconstruction sequence (difficulty: medium)

Oracle +JDBC

Injection attack

Oracle Xe installation and user operation

用户组织架构的管理

Understand │ what is cross domain? How to solve cross domain problems?

JVM overview and memory management (to be continued)

Redis thing learning
随机推荐
一个程序员的水平能差到什么程度?
Office automation solution - docuware cloud is a complete solution to migrate applications and processes to the cloud
Clickhouse implements materializedpostgresql
Preprocessing and macro definition
Flask-MDict搭建在线Mdict词典服务
In 2019, the global semiconductor market revenue was $418.3 billion, a year-on-year decrease of 11.9%
洋葱集团携手OceanBase实现分布式升级,全球数据首次实现跨云融合
What is a multi-layer perceptron (what is a multi-layer perceptron)
软件测试面试题:已知一个数字为1,如何输出“0001
未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
Redis queue, RDB learning
JVM overview and memory management (to be continued)
Session attack
Passive income: return to the original and safe two ways to earn
Common methods of object learning [clone and equals]
[design tutorial] yolov7 target detection network interpretation
How to solve the problem of missing alarm information and synchronization when Haikang equipment is connected to easycvr?
Scrollintoview realizes simple anchor location (example: select city list)
C language -- array
Redis hash structure command