Skip to main content
Visitor II
September 9, 2025
Solved

How to select Key I/O for In-application program(IAP) through UART for STM32F407VGT6TR?

  • September 9, 2025
  • 4 replies
  • 825 views

Hi,

I would like to in-live upgrade firmware of STM32F407 through USART1, that means  in-application program(IAP) . If I remember right, there need a trigger signal from key meanwhile NRST is low.

My question is that the Key signal could be connected to any GPIO I/O of STM32F407 except those dedicated pin, right?  Is there special point to care about selection of  Key I/O pin?

 

Thanks in advance!

Best regards!

Jason

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    IAP is more related to the application note: AN4657 "STM32 in-application programming (IAP) using the USART" and the X Cube package: X-CUBE-IAP-USART.

    So in the firmware check what was implemented for that "Key" and find how it could be changed.

    mALLEm_0-1757414983906.png

    Edit (After downloading the package and checked the code):

    This is the code of the button (Tamper pin is used here) initialization in \IAP_Main\Src\main.c:

     /* Initialize Key Button mounted on STM32L476G-EVAL board */
     BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_GPIO);
    
     /* Test if Key push-button on STM32L476G-EVAL Board is pressed */
     if (BSP_PB_GetState(BUTTON_TAMPER) == GPIO_PIN_SET)
     { 
     	/* Initialise Flash */
     	FLASH_If_Init();
     	/* Execute the IAP driver in order to reprogram the Flash */
     IAP_Init();
     /* Display main menu */
     Main_Menu ();
     }
     /* Keep the user application running */
     else
     {
     /* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */
     if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
     {
     /* Jump to user application */
     JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
     JumpToApplication = (pFunction) JumpAddress;
     /* Initialize user application's Stack Pointer */
     __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
     JumpToApplication();
     }
     }

    This confirms my saying: You don't have to keep the key pressed during the programming. Just ensure the key is pressed for a moment after Reset then release it.

    You can modify the code to use any other available pin.

    4 replies

    Super User
    September 9, 2025

    @JieShen_Jason wrote:

    If I remember right, there need a trigger signal from key meanwhile NRST is low.


    That depends on what IAP method you're using.

    If you're using the built-in ROM bootloader, that requires a reset & specific BOOT settings - see the datasheet for details.

    See also Application note AN2606Introduction to system memory boot mode on STM32 MCUs

     

    If you're using some other method (some other bootloader), its requirements will differ - see the documentation for whatever you're using.

    mƎALLEmAnswer
    Technical Moderator
    September 9, 2025

    Hello,

    IAP is more related to the application note: AN4657 "STM32 in-application programming (IAP) using the USART" and the X Cube package: X-CUBE-IAP-USART.

    So in the firmware check what was implemented for that "Key" and find how it could be changed.

    mALLEm_0-1757414983906.png

    Edit (After downloading the package and checked the code):

    This is the code of the button (Tamper pin is used here) initialization in \IAP_Main\Src\main.c:

     /* Initialize Key Button mounted on STM32L476G-EVAL board */
     BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_GPIO);
    
     /* Test if Key push-button on STM32L476G-EVAL Board is pressed */
     if (BSP_PB_GetState(BUTTON_TAMPER) == GPIO_PIN_SET)
     { 
     	/* Initialise Flash */
     	FLASH_If_Init();
     	/* Execute the IAP driver in order to reprogram the Flash */
     IAP_Init();
     /* Display main menu */
     Main_Menu ();
     }
     /* Keep the user application running */
     else
     {
     /* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */
     if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
     {
     /* Jump to user application */
     JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
     JumpToApplication = (pFunction) JumpAddress;
     /* Initialize user application's Stack Pointer */
     __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
     JumpToApplication();
     }
     }

    This confirms my saying: You don't have to keep the key pressed during the programming. Just ensure the key is pressed for a moment after Reset then release it.

    You can modify the code to use any other available pin.

    Visitor II
    September 9, 2025

    Hi,

    Thank for your response in the time!

    I checked AN4657, and want to know if push-button must be pressed all the time until user application have been download to flash memory, or just pressed then release which generate time-limited negative pulse?

    JieShen_Jason_0-1757424704103.png

    Thanks in advance!

    Best regards!

    Jason

    Super User
    September 9, 2025

    It seems highly unlikely that the user would have to keep the button pressed for the entire time - that would be a nightmare UX!

    But the source code is available - take a look!

    Or build it and try it out.

    You can modify it as required ...

     

    PS:

    Look at the flowchart @mƎALLEm shared:

    AndrewNeil_0-1757427182129.png

    The switch is only used to decide whether to 'Jump to user program' or to 'Initialise IAP'.

    Once that decision has been made, the key can be released.

    Again, the code presented is just an example - you can do whatever suits in your own implementation...

    AndrewNeil_1-1757427383840.png

     

    Visitor II
    September 10, 2025

    Hi,

    Thanks again!

    So, the pin for key could be  any GPIO I/O of STM32F407 only if the input of this pin is judged in below switch ,right?

    JieShen_Jason_1-1757467949179.png

     

    Suddenly I notice that reset must occur at the same time with the Key signal, so the reset brought by NRST pin also is not kept pressed all the time, right?

    JieShen_Jason_0-1757467411037.png

     

    Many thanks to you!

    Best regards!

    Jason

     

    Graduate II
    September 10, 2025

    You're causing a Reset, so a brief press of the reset button, then in the Reset_Handler path you're checking a GPIO state to decide what code to execute. Any available GPIO that's free will suit. Avoid debug pins.

    Visitor II
    September 10, 2025

    Hi

    very clear, get it!

    Many thanks to all replier

     

    Best regards!

    Jason