I am using STM32 ARM cortex M3 MCU. I have flashed the MCU with ST Link V2 hardware. I was eager to know how does MCU switch between programming mode and execution mode. Does the MCU use polling method or interrupt method to acknowledge program request from ST Link hardware?

  • 1
    \$\begingroup\$ Possibly neither if it's like other MCUs. Have you looked at the data sheet to see what special conditions are set up on the programming pins to enable flashing to occur? \$\endgroup\$
    – Andy aka
    Commented Oct 1, 2023 at 10:55

1 Answer 1


It really depends on MCU, but for the STM32, it really does not switch between running and programming modes at all.

In both cases it will be running code it has been told to run and the CPU has no notion what code it is running and if this is now normal code or a programming situation.

The PC flashing program uploads a flasher program code stub through the ST-Link into MCU SRAM and tells the MCU to go execute it (or sets the CPU into stop mode so it does not execute anything unless told to run again).

The PC, through ST-Link, then uploads data to be programmed into SRAM and tells the MCU to execute the flasher stub to handle the programming of data from SRAM to Flash.

That is much faster and flexible than directly accessing the Flash programming through the JTAG/SWD debug port.

So the MCU does not communicate with ST-Link really, it's the other way really, the MCU just does what it is told to do and it just is controlled by the ST-Link. At most the loader stub will poll a SRAM memory address if there is a command it should perform, the ST-Link just writes to SRAM the data and commands.

This also allows a custom flasher stub to have the MCU to do anything you want, like instead of programming the internal flash, it could be extended to support programming of external SPI or I2C Flash, and every time there is need the flasher stub can be expanded to support larger and different types of SPI or I2C memories from different manufacturers as they might be incompatible or require special handling to be able to program them, such as controlling a Write Protect of the SPI or I2C Flash with a MCU GPIO pin.

  • \$\begingroup\$ What I understand from your answer is that MCU acts just as a memory peripheral to the PC and ST link hardware trying to program it. \$\endgroup\$ Commented Oct 1, 2023 at 12:46
  • \$\begingroup\$ The ARM website has extensive documentation on their cpus. Search for ‘debug SWD’. It will tell you precisely how it works. \$\endgroup\$
    – Kartman
    Commented Oct 1, 2023 at 13:12
  • \$\begingroup\$ @DeepakKumar No it does not really act like a memory peripheral and I did not say it does. If you have a question what is unclear I could try to answer it, but your comment is just a statement that does not ask for further clarification. \$\endgroup\$
    – Justme
    Commented Oct 1, 2023 at 13:13
  • \$\begingroup\$ @justme Is it safe to say MCU is just a passive element (Slave) w.r.t. programming PC acting as a master? \$\endgroup\$ Commented Oct 2, 2023 at 2:16
  • \$\begingroup\$ There is no master/slave term. There is Programmer and Target. Programmer can take (and release) control over Target, but Target still live independently. Programming works in a way that Programmer take control over Target, copy data to its RAM (not FLASH) and releases control over Target. Taget flashes data from RAM to FLASH itself and then indicate to the Programmer, that opoeration is complete. \$\endgroup\$
    – Misaz
    Commented Oct 3, 2023 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.