Use hex file to manually load code on stm32

691 views Asked by At

I am looking to manually load a project from a .hex file (output of cube ide build) directly into the flash of my uC (I am using stm32L431). I used a bit of code from an example in st's youtube channel to upload the data manually from another project using:

  .myBufBlockFLASH 0x8008000 :
  {
   KEEP(*(.myBufSectionFLASH))
  } >FLASH

to set up the flash section, then used:

const unsigned char __attribute__((section(".myBufSectionFLASH"))) buf_flash[45504] = 
{
 <all hex data (only data without other code)>
};

to insert the data into the flash section.

I then manually verified that the data is saved in the flash section (set by "myBufSectionFLASH") exactly as it is in the original project (the one that generated the hex file) only with a different starting address.

Lastly, I attempted to jump to this address with:

typedef void (*pFunction)(void);
#define FLASH_APP_ADDR 0x08008000

uint32_t JumpAddress;
pFunction Jump_To_Application;

HAL_Delay(100);
//jump to the application
JumpAddress = *(volatile uint32_t *) (FLASH_APP_ADDR + 4 );
Jump_To_Application = (pFunction) JumpAddress;
__disable_irq();
//initialize application's stack pointer
__set_MSP(*(volatile uint32_t *)FLASH_APP_ADDR);
Jump_To_Application();

the Jump_To_Application(); command immediately crashes to hardfault with No surce available for "buf_flash() at 0x800a510"

the buf_flash section is shown below in the cube ide build analyzer: bootloader project memory deatils image

I tried different addresses, and I tried a different "partition" of the memory. These all failed in the same way.

I don't know how these things work and am hoping to learn the basics of how code is read and run from the flash. can anyone help me? even directing me to a similar bit of code or a lecture on the subject would work.

0

There are 0 answers