What is IAP in microcontroller?

 In microcontroller context, IAP almost always means In-Application Programming.



1. Basic idea

In-Application Programming (IAP) =
The MCU can re-program its own Flash while your application code is running, without needing an external programmer (ST-Link, J-Link, etc.) or going into a separate factory programming mode.

So instead of:

PC → programmer → MCU (ISP / SWD / JTAG)

you can do:

PC / other device → your firmware → MCU’s Flash (via IAP)

Your code acts as its own flash programmer.


2. IAP vs ISP vs bootloader

  • ISP (In-System Programming)

    • Usually means programming via a debug/programmer tool (JTAG/SWD, UART, etc.)

    • Often uses a bootloader mode that runs instead of your application.

  • Bootloader

    • Small piece of code that runs first after reset.

    • It may:

      • Check for a new firmware image,

      • Receive it over UART/USB/CAN/etc.,

      • Program it into Flash (using IAP services),

      • Then jump to the main application.

  • IAP

    • The mechanism by which code already running on the MCU calls special routines (ROM functions or library calls) to erase/program Flash.

    • Often used inside a bootloader or the main application.

In practice:

  • The ROM (or vendor library) provides low-level IAP functions (erase sector, program word/page).

  • Your bootloader or app calls these IAP routines to update firmware or store configuration.


3. Typical uses of IAP

  1. Firmware update in the field

    • Device receives new firmware via:

      • USB, UART, CAN, Ethernet, BLE, etc.

    • Stores received image in a buffer area / separate flash bank.

    • Uses IAP to:

      • Erase old firmware area.

      • Program new image.

    • Optionally verifies checksum/CRC.

    • Jumps to the new firmware.

  2. Dual-bank / fail-safe updates

    • Bank A = running firmware

    • Bank B = new firmware image

    • IAP swaps which bank is “active” or copies new image over.

    • If update fails, device can still boot from the old image.

  3. Storing parameters / logs in Flash

    • Using part of Flash as simple NVM.

    • Your code uses IAP to erase/program parameter blocks, calibration data, usage counters, etc.


4. How it looks in code (conceptually)

Exact API depends on vendor, but the pattern is typically:

// 1) Prepare IAP interface (unlock flash, etc.) flash_unlock(); // 2) Erase one or more pages/sectors flash_erase_sector(SECTOR_5); // 3) Program data (word/page at a time) for (uint32_t i = 0; i < size; i += 4) { flash_program_word(DEST_ADDR + i, *(uint32_t *)(src + i)); } // 4) Lock flash again flash_lock();

Important details:

  • You CANNOT execute from Flash while erasing/programming that same region.
    Common solutions:

    • Run IAP routines from ROM (provided by vendor).

    • Or copy a small flash-programming routine to RAM and execute from there.

  • Interrupts may need to be disabled or moved to RAM-based vectors to avoid executing from Flash mid-operation.


5. Key safety considerations

When using IAP for firmware updates:

  • Never erase the only copy of your working code before verifying the new image.

  • Use CRC/Checksum on the received firmware to ensure integrity.

  • Ideally implement a:

    • Bootloader partition (never overwritten in normal updates).

    • Application partition (the part you update via IAP).

  • Have a rollback or recovery strategy:

    • E.g., if update fails, bootloader detects invalid app image and stays in update mode.


6. One-sentence summary

IAP (In-Application Programming) is the ability of a microcontroller to rewrite its own Flash memory under control of the running firmware, typically used for self-updates and storing non-volatile data, without needing an external programmer.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How to remove write protection of STM32 chip?

The automatic white balance algorithm of Raspberry Pi