cancel
Showing results for 
Search instead for 
Did you mean: 

Flashing a firmware through USB

j_filipe
Associate III

Greetings, 
So, I am developing a bootloader, that receives a firmware, using a similar protocol to xmodem (128 bytes), through the USB. I followed this tutorial to implement the USB and adapted it to make it work on a B-U585I-IOT02A board. The USB communication works. So I am making something like this (some minor details have been omitted, but the big picture is here): 

#define APP_START  ((uint32_t)0x0800C000
#define OFFSET  128

uint32_t addr = 0;
uint8_t buff[OFFSET] __atribute__((aligned(4)));  // 32-bit aligned

int main(void)
{
   Flash_Erase(bank, page); 

   for(;;)
   {
      if(!lastPacket)
      {
         GetPacket(&buff);
         Flash_Firmware(APP_START+addr, buff);
         addr += OFFSET;
      }
      else
      {
         Flash_Firmware(APP_START+addr, buff)
         break;
   }
   jumpToApp();
}


static HAL_StatusTypeDef Flash_Erase(uint32_t _bank, uint32_t _page)
{
	HAL_StatusTypeDef status = HAL_FLASH_Unlock();

	if(status != HAL_OK)
	{
		return status;
	}

	FLASH_EraseInitTypeDef erase = {0};
	uint32_t PageError = 0;

	status = HAL_ERROR;

	if(_bank == FLASH_BANK_1)
	{
		erase.TypeErase = FLASH_TYPEERASE_PAGES;
		erase.Banks = _bank;
		erase.Page = _page;				
		erase.NbPages = 128 - _page; 	
		status = HAL_FLASHEx_Erase(&erase, &PageError);
		if(status != HAL_OK)
		{
			return status;
		}
		if(PageError != 0xFFFFFFFF)	
		{
			Error_Handler();
		}

	}

	status = HAL_ERROR;
	PageError = 0;

	erase.TypeErase = FLASH_TYPEERASE_PAGES;
	erase.Banks = FLASH_BANK_2;
	erase.Page = 0;
	erase.NbPages = 128;
	status = HAL_FLASHEx_Erase(&erase, &PageError);
	if(status != HAL_OK)
	{
		return status;
	}
	if(PageError != 0xFFFFFFFF)	
	{
		Error_Handler();
	}

	status = HAL_ERROR;
	status = HAL_FLASH_Lock();

	return status;
}


static HAL_StatusTypeDef Flash_Firmware(uint32_t addr, uint8_t* buff)
{
	HAL_StatusTypeDef status = HAL_FLASH_Unlock();

	if(status != HAL_OK)
	{
		return status;
	}

	status = HAL_ERROR;
	status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BURST, addr, (uint32_t)buff);

	if(status != HAL_OK)
	{
		return status;
	}

	status = HAL_ERROR;
	status = HAL_FLASH_Lock();

	return status;
}
   


So, I still have a problem that the program is not jumping to the main program, but that's not the issue, the thing is, when I open the STM32CubeProgrammer and check the addresses I see some repeated information and I wonder if that's "normal".

Screenshot_20251024_122912.png

1 REPLY 1
TDK
Super User

> when I open the STM32CubeProgrammer and check the addresses I see some repeated information and I wonder if that's "normal".

The vector table, which is at the start of the program, typically has duplicate entries for interrupts that are not implemented as they all point to Default_Handler. That is normal.

If you feel a post has answered your question, please click "Accept as Solution".