cancel
Showing results for 
Search instead for 
Did you mean: 

How to update the program by USB DFU bootloader in system flash?

Junde
Senior II

Hi there,

I am verifying the USB DFU bootloader in STM32F429BI.

I can normally enter the bootloader and connect my MCU using stm32CubeProg.

I can succeed in the program flash when I check the "Full Chip erase" option, otherwise, it will fail sometimes(see the LOG in the attachment)!

Junde_0-1717559293607.png

But if "Full Chip erase", it needs a bit long time. Can I erase the sector when needed only?

One more, each time I flash the program, I must reset the MCU by hand, can I set the MCU to auto-reset after download?

Thanks for your help.

 

My code as below:

#define BOOTLOADER_ADDRESS					(0x1FFF0000)
#define BOOTLOADER_FLAG_VALUE 			0xDEADBEEF
#define BOOTLOADER_FLAG_OFFSET 			0x50

typedef void (*pFunction)(void);
uint32_t endstack = 0x20030000;
uint32_t *bootloader_flag;
uint32_t JumpAddress;
pFunction JumpToBootLoader;

int main(void)
{
  bootloader_flag = (uint32_t*)(endstack - BOOTLOADER_FLAG_OFFSET);
  if(*bootloader_flag == BOOTLOADER_FLAG_VALUE) {
    *bootloader_flag = 0;
    __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();
    JumpAddress = *(__IO uint32_t*)(BOOTLOADER_ADDRESS + 4);
    JumpToBootLoader = (pFunction)JumpAddress;
    __set_MSP(*(__IO uint32_t*)BOOTLOADER_ADDRESS);
    JumpToBootLoader();
  } else {
    *bootloader_flag = 0x1234ABCD;
  }

  HAL_Init();
  SystemClock_Config();

  LOG_INIT();
  LOG_DBG("\n\nSTM32V6 USB DFU BootLoader test flag[0x%08x]...\n", *bootloader_flag);

  MX_GPIO_Init();
  MX_USART1_UART_Init();
  HAL_UART_Transmit(&huart1, (uint8_t*)"send 0x5A to update...\r\n", strlen("send 0x5A to update...\r\n"), 100);

  uint8_t rxBuf[10];
  while (1)
  {
    HAL_Delay(1000);
    LOG_DBG("DELAY 1s\n");     // hex1
    //LOG_DBG("delay 1s\n");   // hex2

    HAL_UART_Receive(&huart1, rxBuf, 1, 20);
    if(rxBuf[0] == 0x5A) {
      *bootloader_flag = BOOTLOADER_FLAG_VALUE;
      LOG_DBG("Jump to BootLoader, flag[0x%08x]...\n", *bootloader_flag);
      HAL_Delay(100);
      __disable_irq(); 
      HAL_NVIC_SystemReset();
    }
  }
}
2 REPLIES 2
Junde
Senior II

I have generated "hex1" and "hex2" for testing. (The difference at line 39-40)

When I check the "Full Chip erase", both "hex1" to "hex2" and "hex2" to "hex1" are OK;

But when I don't check the "Full Chip erase", only "hex2" to "hex1" is successful, and "hex1" to "hex2" failed. (This means "delay to DELAY" failed, "DELAY to delay" successed)

Junde
Senior II

I am trying to connect the bootloader by UART in stm32cubeProg, but it failed... The steps are as follows:

    1. pull boot0 high;

    2. click the connect button in stm32cubeProg;

    3. reset the MCU, then some error message in the attachment;

          Junde_0-1717568203978.png

 

I can enter the UART bootloader by serial assistant as following steps:

    1. open the serial assistant and keep sending 0x7F;

    2. pull boot0 high;

    3. reset the MCU;

    4. the serial assistant can receive 0x79 0x1F 0x1F....