![](http://stamssolution.com/wp-content/uploads/2023/03/poYBAGLOYUGAVLeFAACGWYo6uzs871.png)
STM32 IAP Advanced redirect using SRAM
Preface
Here, in the development of USB DFU, examples typically involve using external pins for switching. However, in practice, commands are issued via USB or other interfaces to switch between the APP or Bootloader mode. Using Flash as a flag is limited to 10,000 usage cycles. In this case, SRAM can be employed as an alternative solution.
Implementation
Essentially, a specific block needs to be allocated in SRAM. It is crucial to note that power should not be cut off because a power loss would reset the SRAM, causing the flag to be cleared. Below, you can refer to the declaration method for the SRAM location.
#define END_SRAM_ADDR 0x20017FEC
uint32_t boot_value=0;
You can refer to the Flash ld file for the SRAM location.
![](https://en.stamssolution.com/wp-content/uploads/2023/10/image-1.png)
Because we have opted for STM32F401RET6, the SRAM’s starting address is 0x20000000. Additionally, the chosen memory region must not be initialized; otherwise, the data inside SRAM will be cleared every time the MCU resets. Hence, we have selected END_SRAM_ADDR starting from 0x20017FEC as the last address, and the SIZE is not the original 96KB but 96KB minus the required usage area. This block will remain unaffected by MCU resets.
boot_value = *( ( unsigned long* ) END_SRAM_ADDR );
//printf("boot_value0= 0x%x\r\n",*( ( unsigned long* ) END_SRAM_ADDR ));
if(boot_value!=0 &&boot_value!=0x2345)
boot_value=0;
if(boot_value==0)//HAL_GPIO_ReadPin(GPIOB,ChangeMode_Pin)==GPIO_PIN_SET)
{
*( ( unsigned long* ) END_SRAM_ADDR ) =0;
// printf("boot_value01= 0x%x\r\n",*( ( unsigned long* ) END_SRAM_ADDR ));
printf("Jump to APP\n\r");
JumpAddress = *(__IO uint32_t*) (USBD_DFU_APP_DEFAULT_ADD + 4);
JumpToApplication = (pFunction) JumpAddress;
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t*) USBD_DFU_APP_DEFAULT_ADD);
JumpToApplication();
}
printf("Keep DFU\n\r");
Here, you can see that using pointer notation allows accessing specific addresses to serve as flags.
![](https://en.stamssolution.com/wp-content/uploads/2023/10/image-2.png)