3 Ways to Program Microcontrollers (ISP, ICP, IAP)

A microcontroller is a type of programmable controller that, after setting up the hardware circuit, can use programs to implement many very complex logical functions. Compared to pure hardware circuits, it simplifies the design of hardware peripherals, facilitates logical design, and enriches logical outputs. Different manufacturers of microcontrollers require different programming IDEs to achieve programming. Before learning about microcontrollers, it is essential to understand how the microcontroller program is written into the microcontroller’s internal memory and what tools are needed during the writing process. Non-generic programming tools can be quite expensive; for instance, the ICD series programmer used for Microchip microcontrollers costs between 600 to 1000 RMB. Today, we will explore the four common programming methods for microcontrollers and the tools used, which can be categorized into three types:

ISP (In-System Programming)

ICP (In-Circuit Programmer)

IAP (In-Application Programming)

This means that the MCU can obtain new code in the system and reprogram itself, i.e., using a program to change a program. What are the principles behind these three programming methods? Before analyzing the principles, we need to understand the access addresses of the microcontroller’s Flash memory to see where the program is written.

The position of the microcontroller’s Flash in the address mapping table

The following image is an address mapping table for STM32F4xx, ranging from 0x0000 0000 to 0xFFFF FFFF, totaling 4Gbyte. The access addresses for the microcontroller’s on-chip Flash, on-chip RAM, on-chip peripherals, and external expansion interfaces are all mapped within this 4Gbyte range.

3 Ways to Program Microcontrollers (ISP, ICP, IAP)

1. ISP (In-System Programming) allows programming on the system, meaning that blank devices on the circuit board can be programmed with the final user code without needing to remove the device from the circuit board. Already programmed devices can also be erased or reprogrammed using ISP.

2. IAP (In-Application Programming) means that the MCU can obtain new code in the system and reprogram itself, allowing a program to change another program.

3 Ways to Program Microcontrollers (ISP, ICP, IAP)

Implementation of IAP programming

To implement the IAP function, two programs need to be written during product design. The first part of the program does not perform the normal functions; it only receives firmware or new application programs through a communication method (such as USB or UART) and updates the second part of the code, which we call the Bootloader. The second part of the code is the user’s application code that implements the actual functionality of the product. The first part of the code must be pre-written (using a programmer or debugger) into the microcontroller, while the second part can be programmed using a programmer or debugger or can be written using the IAP functionality of the first part. When the chip is powered on, the first part of the code runs first. If no update is needed, it jumps to execute the second part of the code; if an update is detected, it executes the update program, and after completion, it jumps to execute the second part of the code. Taking STM32 as an example, see the image below:

3 Ways to Program Microcontrollers (ISP, ICP, IAP)

In the example above, the starting address of the Bootloader is stored at 0x08000000 (which is also the starting address of the STM32 chip’s Flash). The program executes from here, then jumps to the reset interrupt vector address, and subsequently jumps to the main function entry of the Bootloader. When it detects that an upgrade is needed, it executes the update of the second part application program code to the Flash area at address 0x08000000 + N. After the update is complete, it jumps to the reset interrupt vector address of the second part application (0x08000004 + N + M), and then jumps to the main function of the second part application. During the execution of the main function, if the CPU encounters an interrupt request, the PC pointer forcibly jumps to the Bootloader’s interrupt vector table (0x08000004 in the figure above) instead of the second part’s interrupt vector table. The program then jumps to the corresponding new interrupt service routine according to the interrupt vector table offset we set, and after execution, it returns to the main function of the second part application.

It is important to note that the address to which the first part of the program (Bootloader) jumps and the starting address of the second part application must remain consistent, and the corresponding interrupt vector table must also be modified; otherwise, the program will not run correctly.

3 Ways to Program Microcontrollers (ISP, ICP, IAP)

Leave a Comment