当前位置:网站首页>[embedded] about IAP + XMODEM receiving bin file from outside to upgrade the chip
[embedded] about IAP + XMODEM receiving bin file from outside to upgrade the chip
2022-04-21 20:51:00 【Oh, Yu è】
This article is based on GD32F303CGT6 chip
Use software :
- KEIL5
- Tera Term
What is? IAP
For the program burning of the chip , There are three ways :
- ICP
- ISP
- IAP
ICP(in-circuit-programmer In circuit programming )
ICP Is when the factory produces products , Burn the chip first and then paste it
ISP(in-system-programmer In system programming )
ISP That is, the chip is pasted first and then the code is burned , That is, the most commonly used use J-Link、DAP How to burn code
IAP(in-application-programmer In application programming )
IAP It is most often used for external upgrading of factory chips , Through existing bootloader Boot the system to burn the program to the corresponding storage block
What is? Xmodem
about Xmodem, Baidu Encyclopedia says so :
XMODEM Protocol is an asynchronous file transport protocol widely used in personal computer communication using dial-up modem . This agreement is based on 128 Transfer data in the form of byte blocks , And each block uses a checksum process for error detection .
It's actually a transmission protocol , Subcontract the files you want to transfer , Through serial port 、SPI、I2C、USB、BLE( Low power Bluetooth protocol )、WIFI And other various ways to transfer files .
After years of update iterations ,Xmodem Several series of protocols have been derived :
- Xmodem-1k
- Xmodem-CRC
- Ymodem
Xmodem-1k
Xmodem-1k It's just Xmodem Upgraded version , use CRC check , The data of each package can reach 1k
Xmodem-CRC
Xmodem-CRC And ordinary Xmodem similar , use CRC check , Each bag has only 128 byte
Ymodem
Ymodem There are some differences from the above agreement , Although it's all Xmodem Evolved , And Xmodem Each package is a file with different data ,Ymodem The first packet sends the file name and file size , The data frame length is only 128 byte , And its next package is 1k byte .( Other small differences can be found in Baidu )
Xmodem The frame format of
Each protocol has its own way to customize the frame format ,Xmodem As an earlier data transmission protocol (1978 year ), It must have its own frame format
Controller
| Definition | Value | effect |
|---|---|---|
| SOH | 0x01 | 128 Byte frame header flag |
| STX | 0x02 | 1k Byte frame header flag |
| EOT | 0x04 | Send end flag |
| ACK | 0x06 | Answer flag |
| NCK | 0x15 | Non response flag |
| CAN | 0x18 | Cancel sending flag |
| CRC16 | 0x43 | CRC16 Check frame |
Frame packet format ( Just say Xmodem)
| BYTE1 | BYTE2 | BYTE3 | BYTE4-BYTE131 | BYTE132 |
|---|---|---|---|---|
| SOH | Packet sequence | ~ Packet sequence | data | checksum |
Transmission flow
The above is Xmodem Transmission flow , At the very beginning NCK To inform the sender that the receiver is ready , Then the sender starts sending data in sequence , among DATA[0-127] Express Each frame is sent fixedly 128 Bytes of data , When the remaining data is not enough 128 Byte time , It's usually used ctrlZ replace . In the process of sending , If a frame data transmission error , It can lead to checksum error , This is what the receiver will send NAK To the sender , Send receive NAK Then resend the frame data . When all data is sent , The sender sends a EOT, The receiver replies ACK Represents the whole sending process technology .
GD32F30X Of IAP Usage method
about MCU, There are usually erasable areas inside , This area can be used to store some small pieces of data , Or it can be used to store code, etc , and IAP It's using this area , Or the function of the chip to modify the internal code of the chip .
about GD32F30X, It has one called FMC(flash memory controller Flash controller ) Internal peripherals , The peripheral can control the internal of the chip flash To erase and write , After referring to the user manual and data manual of the chip, you can partition the code and data of the chip , Preliminary judgment of chip erasure time .

This picture comes from GD32F30X User's Manual , Shows GD32F30X Inside each series flash The partition , about IAP, Our main operation is the... In the figure Main storage flash block
It can be seen from the picture that , The main storage flash memory block is divided into two parts , Part of it is before 256 page , User manual and official demo I'm going to call it bank0, The other part is 256-896 page , User manual and official demo I'm going to call it bank1
these two items. bank The difference is that :
- about bank0,CPU Command operation zero wait , This range has the advantage of low operation delay for other memory addresses
- bank0 The capacity of each page is 2k,bank1 The capacity of each page is 4k
- For primary storage, the flash memory block capacity shall not be greater than 512k Chip , Only bank0
The information block in the figure is Boot loader Area and our own bootloader Different , This is the area inside the chip that guides the chip burning mode and execution mode , The chip has been fixed when it leaves the factory , The user cannot erase , It can be understood as the of computer BIOS
chip IAP The way
As mentioned above , Chip IAP The mode can be through serial port 、SPI、I2C、BLE、WIFI And so on . I chose the simplest serial port +Xmodem The way to get started .
IAP technological process
IAP The process can be broken down into many , In either case, it is inseparable from a chip that writes the data after receiving the data , And when you're done, you'll PC The pointer jumps to where it should go, the basic framework .
For the safety of the chip in the burning process , Avoid becoming a dead chip after chip upgrade failure due to some force majeure factors , This is after the product is released , For example, when the mobile phone is upgraded in the hands of users , Something that must never happen . therefore , In order to ensure the absolute safety of the upgrade process , There are usually various ways :
- Write a... In a flash memory area of the chip FLAG, When the chip starts to upgrade and after the upgrade is successful FLAG Operate accordingly .
- The burning position of the chip and bootloader Code is strictly distinguished , Make sure their well water doesn't invade the river .
Here I use both , That is, when the chip needs to be upgraded , First select a fixed flash memory area , Write a FLAG go in , Then jump to bootloader District , Receive bin file , During this process, even if the reception is stopped due to power failure , When the chip is powered on , Judged FLAG When the zone is upgrading , Will still stay bootloader Restart the upgrade of the zone . When the chip upgrade is successful , Will jump to APP District , And put FLAG Erase . At this time, even if it is powered on again , The chip can also successfully jump to APP District .
IAP What should we pay attention to when doing this
- about Xmodem, Before sending data to the sender , Be sure to receive from the sender NAK, Otherwise, it will not start sending data .
- selected APP The address written , The address should be with bootloader Well water does not offend river water , So we're finishing bootloader after , Need to compute bootloader How much memory does the chip occupy , after APP Generate bin Before document , To perform the corresponding address offset operation , as well as FLASH-VTOR The offset ( This operation is to tell the chip where to find the interrupt vector table , For example, you put APP Write in the flash memory area away from the main storage 0x2000 The place of , Your interrupt vector should be offset 2000).
- The second point leads to the most important point is , When the chip jumps, it should jump to the address written +4, Why add 4 Well ? This is because the address written is not Reset interrupt service function address , It is Interrupt vector table address , When we reset the chip ,PC The pointer should jump to the address of the reset interrupt service function to reset the chip , This is how it works .
IAP In the process of learning bug
- When writing the code of chip erasure process , Because the serial port sends data in the form of one byte , and FMC When writing data, it is written in the form of four bytes , At this time, we need to pay attention to the relationship between the amount of data received and the number of receiving addresses , And when I'm writing code , Because considering APP A large amount of code , Think of if APP More than one page or even more than bank1 What to do when ? When judging the data, the judgment operation of the amount of received data is added , This leads to subsequent judgment on whether it exceeds one page , Confuse it with the write address . When the data is written to a quarter, jump to the next page and start erasing .
This situation also taught me , When you don't know whether the data is written correctly , You can use J-FLASH View the data written .
Jump code
if(*(uint32_t *)UPGRADE_ADDR == UPGRADE_FLAG)
{
/* jump to APP */
// Recovery pointer
if(((*(__IO uint32_t *)0x08002000) & 0x2FFE0000) == 0x20000000) // Determine whether the jump address is an available address
{
LED_deinit(LED1);
usart_disable(USART0);
timer_disable(BASE_TIMER); // Turn off some peripherals used during upgrade
jUMP_ADDR = *(__IO uint32_t *)(0x08002000 + 4); // +4 Reset function for reset interrupt
jump2App = (pfunction) jUMP_ADDR;
__set_MSP(*(__IO uint32_t *)0x08002000); // Set the stack top pointer
// eliminate flag
fmc_sector_erase(0x082FF000);
// jump to app
nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x2000); // Interrupt vector table offset
jump2App();
}
/***************/
}
版权声明
本文为[Oh, Yu è]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212043017067.html
边栏推荐
- [high concurrency] analysis of thread pool and ThreadPoolExecutor class
- 《动手学机器人学》7.3.1齐次坐标变换&&齐次变换矩阵
- String.length()和String.getBytes().length的区别
- He was in '98. I can't play with him
- 再谈企业信息化的本质
- 824.山羊拉丁文
- Yaml
- Mysql坑爹指南_sql_mode=“ONLY_FULL_GROUP_BY“所导致错误以及不能使用group by
- 5、Qt使用MySQL
- Trackup | six unique benefits of using project management
猜你喜欢

3、MySQL Workbench 对表进行增删改查

Live555学习

PR视频添加字幕

SQL 错误: ORA-01428: 参数 ‘0‘ 超出范围 01428. 00000 - “argument ‘%s‘ is out of range“

How to install the slip ring correctly and effectively

单、双链表的循环链表(十五)

10分钟快速入门RDS

外包干了五年,差不多是个废人了

MySQL numeric function
![[high concurrency] analysis of thread pool and ThreadPoolExecutor class](/img/9c/ccd119193ffb9b82bf08cedb38fd0b.jpg)
[high concurrency] analysis of thread pool and ThreadPoolExecutor class
随机推荐
[system analyst's road] real topic of system analyst's paper writing in the next 2020
gstreamer学习
SQL error: ora-01428: parameter '0' is out of range 00000 - “argument ‘%s‘ is out of range“
【网络安全】红队渗透项目之Stapler1(下)
< 2021SC@SDUSC > Introduction to the jpress group on software engineering application and practice of Shandong University
谷歌手机Nexus和Pixel系列研发代号
Unity3d C#利用材质(Material)贴图的偏移实现2D游戏单背景图无限移动背景效果功能(含源码)
APM 行业认知系列 - 十五
尿素期货几个点涨停?尿素期货怎么做才安全?
Deploying redis in Linux Environment
824. Goat Latin
Map&Set
Get application instance through reflection
《动手学机器人学》7.3.2动手学坐标变换 齐次矩阵生成(四元数+欧拉角)齐次坐标运算
Google mobile phone nexus and pixel series R & D code
Liu Zhigang, former COO of Jinshan cloud, joined Baihai technology as president
双链表插入、删除操作单步解析(十四)
教你轻松解决CSRF跨站请求伪造攻击
Weekly report from April 11, 2022 to April 17, 2022
AttributeError: ‘list‘ object has no attribute ‘endswich‘