注:英文引文,机翻未校。
图片清晰度限于引文原状。
Introduction to Programming of Microcontroller: ISP, IAP and ICP
微控制器编程介绍:ISP、IAP 和 ICP
Date: 30-11-2022
1. What is Microcontroller Programming
什么是微控制器编程
Microcontroller programming is to load the original program into the computer after compiling and processing, and let the computer execute the written program.
微控制器编程是将编译和处理后的原始程序加载到计算机中,并让计算机执行所编写的程序。
For example, when programming a single-chip microcomputer, it loads the .hex file and stores it in the single-chip computer.
例如,在编程单片机时,它会加载.hex 文件并将其存储在单片机中。
The written program can be realized when the computer is turned on.
编写的程序在计算机启动时可以实现。
To put it simply, it is the process of making the microcomputer boot and execute the programming.
简而言之,这就是让微计算机启动并执行编程的过程。
Chip programming is an important part of the production process of electronic products.
芯片编程是电子产品生产过程中的一个重要环节。
The efficiency is an important aspect that customers pay attention to.
效率是客户关注的一个重要方面。
Generally, the current programming methods are divided into two types: offline programming and online programming.
一般来说,目前的编程方法分为两种:离线编程和在线编程。
Offline programming:
离线编程:
Through the link of the adapter and the chips of different packages, the chip and the adapter can be used together to realize the programming of the program.
通过适配器与不同封装的芯片的连接,芯片和适配器可以一起用于实现程序的编程。
The essence of the adapter is similar to a precision fixture, and chips in different packages need to be matched with different adapters.
适配器的本质类似于一种精密夹具,不同封装的芯片需要与不同的适配器匹配。
If there is an error during the production test, the chip needs to be disassembled from the adapter and re-programmed according to the prescribed process, which consumes a lot of manpower and material resources, and the cost is high, and it is also relatively prone to problems.
如果在生产测试过程中出现错误,芯片需要从适配器上拆卸下来,并按照规定流程重新编程,这会消耗大量的人力和物力资源,成本较高,也相对容易出现问题。
Therefore, offline programming is generally not recommended.
因此,不建议使用离线编程。
Online programming:
在线编程:
Online programming uses the standard communication bus of the chip, such as USB, SWD, JTAG, UART, etc.
在线编程使用芯片的标准通信总线,例如 USB、SWD、JTAG、UART 等。
The interface is generally fixed, and there are few pins that need to be connected during programming.
接口通常是固定的,在编程过程中需要连接的引脚很少。
Since the communication rate of the interface is not high, the programming can be completed by using ordinary wires without high consumption.
由于接口的通信速率不高,可以通过使用普通导线完成编程,而无需高消耗。
Online programming is programmed by connecting wires.
在线编程通过连接导线进行编程。
If an error is found during production testing, you can perform programming again without disassembling the chip.
如果在生产测试过程中发现错误,无需拆卸芯片即可重新进行编程。
It not only saves production cost, but also increases the efficiency of programming.
这不仅节省了生产成本,还提高了编程效率。
So now the way of online burning is generally used.
因此,现在通常使用在线烧录的方式。
2. Three Common Methods of Microcontroller Programming: ISP, IAP and ICP
三种常见的微控制器编程方法:ISP、IAP 和 ICP
Let's talk about the three common methods of microcontroller programming: ISP, IAP and ICP, and their differences.
让我们来谈谈微控制器编程的三种常见方法:ISP、IAP 和 ICP,以及它们之间的区别。
What are the principles of these three programs?
这三种程序的原理是什么?
Before analyzing the principle, you need to know the access address of the microcontroller Flash to see where the program is burned.
在分析原理之前,你需要了解微控制器 Flash 的访问地址,以查看程序烧录的位置。
1). The location of MCU Flash in the address mapping table
1). MCU Flash 在地址映射表中的位置
The figure below is an address mapping table of STM32F4xx, from 0x0000 0000 to 0xFFFF FFFF, totaling 4Gbyte.
下图是 STM32F4xx 的地址映射表,从 0x0000 0000 到 0xFFFF FFFF,总共 4G 字节。
The access addresses of the on-chip Flash, on-chip RAM, on-chip peripherals and external expansion interfaces of the microcontroller are all mapped within the range of 4Gbyte.
微控制器的片上 Flash、片上 RAM、片上外设和外部扩展接口的访问地址都映射在 4G 字节的范围内。

In this picture, we need to pay attention to the lower part:
在这张图中,我们需要关注下部:
Main memory main storage area: Usually, the code we write is placed at the starting position of the main storage area (0x0800 0000) to start running.
主存储区主存储区域: 通常,我们编写的代码放置在主存储区的起始位置(0x0800 0000)以开始运行。
When run the program, just from here 0x0800 0000.
运行程序时,就从这里 0x0800 0000 开始。
System memory system storage area: System memory (starting position 0x1FFF 0000).
系统存储区系统存储区域: 系统存储器(起始位置 0x1FFF 0000)。
This is a piece of ROM that cannot be modified after leaving the factory.
这是一片在出厂后无法修改的 ROM。
After understanding the specific address of the program, we also need to look at the startup method of the single-chip microcomputer:
了解程序的具体地址后,我们还需要查看单片微机的启动方式:
2). Introduction to ISP, ICP, and IAP
2). ISP、ICP 和 IAP 介绍
ISP: In System Programming.
ISP:系统内编程。
ISP means that programming can be performed on the board level, that is, the entire program is written without dismantling the chip, usually through the ISP interface line.
ISP 意味着可以在板级进行编程,即无需拆卸芯片即可写入整个程序,通常通过 ISP 接口线完成。
Chips that support ISP generally solidify a boot program (upgraded with ISP) inside the chip.
支持 ISP 的芯片通常在芯片内部固化了一个引导程序(可通过 ISP 升级)。
BOOT0 = 1, BOOT1 = 0
Start address: 0x1FFF0000
For example:
例如:
use Flash loader to program STM32, use STC-ISP to program STC chip.
使用 Flash loader 对 STM32 进行编程,使用 STC-ISP 对 STC 芯片进行编程。

ICP: In Circuit Programming
ICP:电路内编程
ICSP: In-Circuit Serial Programming, in-circuit serial programming. Such as: EEPROM programming, etc.
ICSP:电路内串行编程,例如 EEPROM 编程等。
BOOT0 = 0, BOOT1 = x
BOOT0 = 0,BOOT1 = x
Start address: 0x08000000
起始地址:0x08000000
There are different opinions on the ICP programming method on the Internet.
网上对 ICP 编程方法有不同的看法。
From the literal meaning (in the circuit), all the chips that are being programmed need to be powered on, and they are all in the circuit.
从字面意思(在电路中)来看,所有正在编程的芯片都需要通电,并且它们都在电路中。
Strictly speaking, programming using J-Link, ST-Link, e-Link32 and other tools also belongs to in-circuit programming (ICP).
严格来说,使用 J-Link、ST-Link、e-Link32 等工具进行的编程也属于电路内编程(ICP)。

IAP: In application programming.
IAP:应用内编程。
Perform programming (upgrade program, update firmware) while the program is running.
在程序运行时执行编程(升级程序、更新固件)。
The principle of IAP is quite different from the above two.
IAP 的原理与上述两种方式有很大不同。
In this way, the main storage area is divided into two areas (allocated by the developer according to actual needs).
在这种方式中,主存储区被划分为两个区域(由开发者根据实际需要分配)。
This part at the beginning of 0x0800 0000 stores a developer's own designed Bootloader program, the other part stores the APP program that actually needs to run.
从 0x0800 0000 开始的部分存储开发者自己设计的 Bootloader 程序,另一部分存储实际需要运行的 APP 程序。
IAP is the programming of part of the User Flash by the user's own program during operation.
IAP 是在运行过程中由用户自己的程序对部分用户 Flash 进行编程。
The purpose is to update and upgrade the firmware program in the product through the reserved communication port conveniently after the product is released.
其目的是通过预留的通信端口,在产品发布后方便地更新和升级产品中的固件程序。
IAP communication port There are many kinds of IAP communication ports: UART serial port, ETH Ethernet, I2C, SPI, etc.
IAP 通信端口有很多种:UART 串口、ETH 以太网、I2C、SPI 等。
It stands to reason that as long as the communication port that can transmit data can realize the function of IAP.
只要能够传输数据的通信端口就可以实现 IAP 功能。

The Difference Between ISPs and IAPs
ISP 与 IAP 的区别
1) For Applications
1) 应用方面
-
The update of ISP program needs to be solved on site, but it does not need to dismantle the machine;
-
ISP 程序的更新需要在现场解决,但无需拆卸机器;
-
IAP If there is a network management system, use the network management to download everything, and people don't have to run around.
-
IAP 如果有网络管理系统,可以使用网络管理下载所有内容,人们无需四处奔波。
2) How ISPs and IAPs Work
2) ISP 和 IAP 的工作方式
The implementation of ISP is relatively simple, and the common practice is that the internal memory can be rewritten by the software of the host computer through the serial port.
ISP 的实现相对简单,常见的做法是通过串口由宿主计算机的软件重写内部存储器。
For the microcontroller, the data can be received through SPI or other serial interfaces and written into the memory.
对于微控制器,数据可以通过 SPI 或其他串行接口接收并写入存储器。
So even if we solder the chip on the circuit board, as long as the serial port to interface with the host computer is reserved, the internal memory can be rewritten.
因此,即使我们将芯片焊接到电路板上,只要保留了与宿主计算机接口的串口,就可以重写内部存储器。
The implementation of IAP is complicated.
IAP 的实现较为复杂。
When implementing the function, it needs to have two areas for storage inside the MCU.
在实现该功能时,需要在 MCU 内部有两个存储区域。
Generally, there are 2 areas, one is called the BOOT area, and the other is storage area.
通常有两个区域,一个称为 BOOT 区域,另一个是存储区域。
The MCU is powered on and runs in the BOOT area.
MCU 上电后在 BOOT 区域运行。
If the conditions for external program rewriting are met, the program in the storage area will be rewritten.
如果满足外部程序重写的条件,则存储区域中的程序将被重写。
If the conditions for rewriting the program externally are not met, the program pointer jumps to the storage area and starts to execute the program placed in the storage area, thus realizing the IAP function.
如果不满足外部重写程序的条件,则程序指针跳转到存储区域并开始执行存储区域中放置的程序,从而实现 IAP 功能。
3) Advantages of ISPs and IAPs
3) ISP 和 IAP 的优势
ISP: The experiment and development of the microcontroller can be carried out without a programmer.
ISP:可以在没有编程器的情况下进行微控制器的实验和开发。
The MCU chip can be directly welded to the circuit board, and the finished product is ready after debugging, which eliminates the frequent insertion and removal of the chip to the chip and the circuit board during debugging.
可以将 MCU 芯片直接焊接到电路板上,调试后即可完成成品,避免了在调试过程中频繁地将芯片插入和拔出芯片和电路板。
Inconvenience caused.
由此带来的不便。
IAP: The IAP technology maps the Flash memory into two memory parts structurally.
IAP:IAP 技术在结构上将 Flash 内存映射为两个内存部分。
When running the user program on one memory bank, the other memory bank can be reprogrammed, and then the program is transferred from one memory bank to the other.
当在一个存储库上运行用户程序时,可以对另一个存储库进行重新编程,然后将程序从一个存储库转移到另一个存储库。
The realization of ISP generally requires few external circuits to assist the realization, while the realization of IAP is more flexible.
ISP 的实现通常需要很少的外部电路来协助实现,而 IAP 的实现则更加灵活。
Usually, the serial port of the single-chip microcomputer can be used to connect to the RS232 port of the computer, and the internal memory can be programmed through a specially designed firmware program.
通常,可以使用单片微机的串行端口连接到计算机的 RS232 端口,并通过专门设计的固件程序对内部存储器进行编程。
It is very convenient to realize remote upgrade and maintenance through Internet or other communication methods.
通过 Internet 或其他通信方式实现远程升级和维护非常方便。
单片机三种烧录方式 ICP、IAP 和 ISP 详解
深藏功名丿小志 _zhihu
单片机的烧录方式主要可以分为三种,分别为 ICP(In Circuit Programming,在电路编程)、IAP(In Application Programming,在应用编程)以及 ISP(In System Programming,在系统编程)。学习单片机的同学都应该熟悉 IAP、ICP 和 ISP 这几个术语。以下是对这三种烧录方式的详细介绍。
1. ICP(In Circuit Programming)在电路编程
使用过新唐单片机的开发者一定知道,新唐单片机配套了一套编程工具,即 NuMicro ICP Programming Tool。类似地,ST 芯片也配备了 J-Flash 工具。
通过使用对应厂家的软件以及仿真器,可以烧录程序。目前主流的接口包括 JTAG(Joint Test Action Group)和 SWD(Serial Wire Debug)。ICP 编程主要通过 SWD 接口实现。
下图为 JTAG 接口和 SWD 接口的区别:

执行 ICP 功能仅需要三个引脚:RESET、ICPDA 和 ICPCK。RESET 用于进入或退出 ICP 模式,ICPDA 为数据输入输出引脚,ICPCK 为编程时钟输入引脚。用户需要在系统板上预留 VDD、GND 以及这三个引脚。

新唐官方给出了详细描述:ICP 指的是"在电路编程",PC 上运行的软件"NuMicro ICP 编程工具"通过 SWD 接口更新芯片内部的 APROM、LDROM、数据闪存(DataFlash)和目标用户配置字(Config)。

2. ISP(In System Programming)在系统编程
ISP 指的是"在系统上编程",目标芯片通过 USB/UART/SPI/I²C/RS-485/CAN 等周边接口的 LDROM 引导代码更新芯片内部的 APROM、数据闪存(DataFlash)和用户配置字(Config)。

3. IAP(In Application Programming)在应用编程
IAP 是通过软件实现在线电擦除和编程的方法。IAP 技术从结构上将 Flash 存储器映射为两个存储体,当运行一个存储体上的用户程序时,可以对另一个存储体重新编程,之后将程序从一个存储体转向另一个。
烧录工具及案例
以下是三种烧录方式在烧录过程中使用的烧录工具及具体案例:
-
ICP 使用 SWD 接口进行烧录程序。常用的烧录工具包括 J-Link、ST-Link、Nu-Link,与之配套的烧录软件分别为 J-Flash、NuMicro ICP Programming Tool、ST-Link Utility。
-
ISP 使用引导程序通过 USB/UART 等接口进行烧录。首先需要有 Bootload 程序。最常见的烧录方式是学习 8051 单片机时使用的 STC-ISP 烧录工具。
-
IAP 是通过软件实现在线电擦除和编程的方法,不使用任何外部工具,仅通过软件方法更新 Flash 中的数据。例如,通过 4G 模块远程更新程序。将 Flash 分成两块区域,第一块为 Bootload 程序,第二块区域存放应用程序(APP)。4G 模块与目标板通信,通信中包含是否更新的标志位。如果主板接收到需要更新的标志位,就往 Flash 中写入一个标志位(如"P"),之后程序跳转到第一段程序(Bootload 程序)执行。首先判断 Flash 中是否有更新程序的标志位"P",如果有,则通过规定的协议更新应用程序中的程序,更新完毕后清除 Flash 中的更新标志位,跳转到应用程序执行。如果没有更新程序标志位"P",则直接跳转到应用程序执行。
总结
- ICP:使用 SWD 接口进行烧录,例如 J-Link 烧录器配合 J-Flash 软件使用。
- ISP:使用引导程序(Bootload)加上外围 UART/USB 等接口进行烧录。
- IAP:通过软件自身实现在线电擦除和编程的方法,不使用任何外部工具。程序通常分为两部分:引导程序和应用程序。
最后提出一个问题:
在使用IAP(In - Application Programming,应用内编程)技术时,一个值得探讨的问题是:能否将引导程序(Bootloader)与应用程序(APP)合并至同一代码区,并且在此情况下依然实现软件更新功能?
答案是可行的,但这需要满足特定条件并遵循一定的设计要求。以下将从可行性分析、实现方法、注意事项及补充说明等方面进行详细阐述。
IAP 编程中引导程序与应用程序在同一代码区的更新可行性分析及实现注意事项
一、可行性分析
- 资源限制:单片机的 Flash 容量有限,将引导程序和应用程序合并,可减少因划分多个存储区域造成的资源浪费,有效节省存储空间。
- 程序结构:传统 IAP 实现通常将引导程序和应用程序分离,由引导程序负责应用程序的更新。但通过精心设计,让引导程序在完成更新后直接跳转至应用程序,且二者共享同一代码区,就能实现合并。
二、实现方法
- 程序设计
- 引导程序功能:引导程序需具备以下核心功能。一是检测是否有程序更新需求;二是通过通信接口接收新的应用程序数据,并将其写入 Flash 存储器;三是采用 CRC(循环冗余校验)校验或哈希算法等,验证新应用程序的完整性;四是在验证成功后,安全地跳转至应用程序的入口地址。
- 应用程序功能:应用程序正常运行时执行核心业务逻辑。软件更新时,引导程序会临时覆盖应用程序代码区域,应用程序本身无需感知更新过程。
- 内存布局
- 单一代码区设置:把引导程序和应用程序置于连续的 Flash 存储空间内。一般引导程序位于 Flash 存储器起始位置,应用程序紧随其后。编译链接时,要准确指定各个段的起始地址和长度。
- 更新机制:更新时,引导程序接收新的应用程序数据后,将其写入分配给应用程序的 Flash 区域。更新完成后,引导程序执行跳转指令,指向应用程序的入口地址。
- 更新流程
- 启动检测:设备启动时,引导程序率先执行,通过读取特定 Flash 地址的值、检测特定引脚电平或检查版本信息等方式,判断是否存在更新请求标志或条件。
- 新程序接收:若检测到更新需求,引导程序通过 UART、USB、以太网等预定通信接口接收新的应用程序数据。为保障数据传输可靠性,应实现握手协议和错误检测机制。
- Flash 擦除:写入新数据前,必须先擦除 Flash 存储器中原应用程序所在区域。操作时要注意 Flash 存储器的擦写特性,如擦除最小单位是扇区或块,写入最小单位是页或字。
- 数据写入:将接收到的新应用程序数据写入 Flash 存储器的应用程序区域,确保写入数据的正确性和完整性。
- 新程序校验:写入完成后,采用 CRC 校验或哈希校验等方式对新写入的数据进行校验。若校验失败,表明更新过程出错,需采取重新下载或回滚到旧版本等错误处理措施。
- 跳转执行:若校验成功,引导程序设置程序计数器(PC)指向应用程序的入口地址,并执行跳转指令,启动新的应用程序。
三、注意事项
- 代码保护:更新过程中,必须确保引导程序不被覆盖,否则设备将无法正常启动。保护方法有:将引导程序置于 Flash 存储器固定区域(如起始位置);启用 Flash 存储器的写保护功能;在引导程序中添加自校验代码,检测是否被篡改。
- Flash 操作:进行 Flash 存储器的擦除和写入操作时,要严格遵循其特性和时序要求,防止因误操作导致存储器损坏或数据丢失。
- 通信协议设计:需设计健壮的通信协议传输新的应用程序数据,确保数据完整性和正确性。可采用校验和、CRC 校验或哈希算法检测传输错误;运用重传机制处理数据包丢失;实现流量控制机制防止数据溢出。
- 异常处理:更新过程中可能出现断电、通信中断、校验失败等异常情况。需设计完善的异常处理机制,如在关键操作前保存寄存器值、堆栈指针等上下文信息;利用看门狗定时器监控程序运行状态;提供回滚机制以便在更新失败时恢复到旧版本,保障设备在异常情况下能够安全恢复。
- 版本管理:建议在引导程序和应用程序中均添加版本号管理功能,包含主版本号、次版本号、修订号等信息,便于识别当前运行的软件版本,并在更新时进行版本兼容性校验。
四、补充说明
- 引导程序设计原则:引导程序应保持代码简洁,避免依赖外部库或复杂逻辑,以确保其可靠性和稳定性,实现各种情况下安全启动设备和执行更新操作的目标。
- 应用程序兼容性:更新应用程序时,必须确保新版本与硬件及操作系统、设备驱动程序、中间件等其他软件模块完全兼容,通过兼容性测试避免因更新导致系统无法正常运行或出现未知错误。
- 测试与验证:在将 IAP 系统部署到实际产品前,需对整个更新流程进行充分测试与验证,包括正常更新场景测试、异常情况处理测试(如模拟断电、通信中断、校验失败等)、压力测试(模拟长时间运行和频繁更新)以及安全测试(检测是否存在恶意代码注入、数据篡改等安全漏洞),确保 IAP 系统的鲁棒性。
通过上述设计与实现方法,能够将引导程序和应用程序合并至一个代码区,实现便捷的软件更新功能。同时,完善的异常处理和版本管理机制可显著提升系统的稳定性和可维护性。
发布于 2019-06-15 10:43
via:
-
Introduction to Programming of Microcontroller: ISP, IAP and ICP Embedded Technology Information EmbedIc
https://www.embedic.com/technology/details/introduction-to-programming-of-microcontroller--isp--iap-and-icp -
单片机三种烧录方式ICP、IAP和ISP详解 - 知乎
https://zhuanlan.zhihu.com/p/69237591