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?
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.