cancel
Showing results for 
Search instead for 
Did you mean: 

Custom bootloader STM32F405VG

abarrotes
Associate II
Posted on November 26, 2016 at 00:33

Hello,

I'm working in a custom bootloader. I have follow many discussions here and then i end up with this code.

commenting the function PerformFirmwareUpdate() the code works ok!  

pFunction appEntry;

uint32_t appStack;

HAL_Init();

/* Get the application stack pointer (First entry in the application vector table) */

appStack = (uint32_t) *((__IO uint32_t*)EXEC_ZONE_ADDR);

/* Get the application entry point (Second entry in the application vector table) */

appEntry = (pFunction) *(__IO uint32_t*) (EXEC_ZONE_ADDR);// + 4);

if (CheckFirmwareUpdate)

//PerformFirmwareUpdate();

HAL_DeInit();

/* Reconfigure vector table offset register to match the application location */

SCB->VTOR = EXEC_ZONE_ADDR;

/* Set the application stack pointer */

__disable_irq();

__set_MSP(appStack);

__DSB();

/* Start the application */

appEntry();

when i uncomment the function which copies the program in the execution sector (5), then i get the problem:

the 

appEntry gets the value of sector 6 instead of sector 5 which is the real value assigned.

void PerformFirmwareUpdate(void)

{

if (FlashErase(FLASH_SECTOR_5))//(FLASH_SECTOR_5);

{

uint32_t *data_ptr = (uint32_t *)PROG_ZONE_ADDR;

HAL_FLASH_Unlock();

for(uint32_t i = 0; i < (SECTOR_SIZE); i += sizeof(uint32_t), data_ptr++ )

{

if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, EXEC_ZONE_ADDR + i, *data_ptr) != HAL_OK)

{ //WORD = 32bits

break;

}

FLASH_WaitForLastOperation (4000); //in ms, max time to erase whole sector; see datasheet

}

HAL_FLASH_Lock();

}

return;

}

Thanks to all, I hope someone could help me with this!

#stm2 #bootloader #stm32f4 #!stm32-!bootloader #c
14 REPLIES 14
Posted on November 26, 2016 at 01:17

It is a vector table with a list of addresses, those can fall anywhere in the firmware image, and not at EXEC_ZONE_ADDR plus 0 or 4

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
abarrotes
Associate II
Posted on November 26, 2016 at 17:37

Hello Clive and thanks for your reply,

Could you please give me more clues? 

Do you think the firmware application is passing this value to the pointer. the msp is taking the right value that I have in memory but I cant explain how the ''jump to my app'' is getting a wrong address.

Could be the compiler (keil)?, I have to load a version to sector 6 from keil in order to the application have an image to copy on sector 5.

Do you think the application is falling in an exception and then setting a wrong value?   

Posted on November 27, 2016 at 01:31

Could you please give me more clues? 

 

Your descriptions need more *detail* about what the processor is seeing, because I have no visibility. A couple of disconnected code fragments, without defines doesn't help me understand the problem. The description of the problem doesn't make sense to me, provide a more thorough presentation of the problem so I can make my own determination.

Do you unlock the flash prior to the erase? Does it erase? Do you get any errors? Can you output some diagnostic information to understand what is going on, and what the processor is seeing? ie Values in memory, values you are loading/jumping too?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
abarrotes
Associate II
Posted on November 27, 2016 at 04:02

I appreciate your help clive

I have attached an image of what i have at the end of the debugging.

the complete code is:

#include ''stm32f4xx_hal.h''

#include ''crc.h''

#include ''gpio.h''

#include ''LCD.h''

//#include ''main.h''

#include <stdbool.h>

#include <stdio.h>

#include <string.h>

#define EXEC_ZONE_ADDR 0x08020000 // Sector 5

#define PROG_ZONE_ADDR 0x08040000 // Sector 6

#define BCKUP_ZONE_ADDR 0x08060000 // Sector 7

#define SECTOR_SIZE 0x20000 // 32K

typedef void (*pFunction)(void);

bool FlashErase(uint32_t SectorNumber);

void PerformFirmwareUpdate(void);

bool CheckFirmwareUpdate(void);

int main(void)

{

pFunction appEntry;

uint32_t appStack;

HAL_Init();

if (CheckFirmwareUpdate)

PerformFirmwareUpdate();

HAL_DeInit();

/* Get the application stack pointer (First entry in the application vector table) */

appStack = (uint32_t) *((__IO uint32_t*)EXEC_ZONE_ADDR);

/* Get the application entry point (Second entry in the application vector table) */

appEntry = (pFunction) *(__IO uint32_t*) (EXEC_ZONE_ADDR + 4);

/* Reconfigure vector table offset register to match the application location */

SCB->VTOR = EXEC_ZONE_ADDR;

/* Set the application stack pointer */

__set_MSP(appStack);

/* Start the application */

appEntry();

while(1);

}

// -----------------------------------------------------------------------------

bool CheckFirmwareUpdate(void)

{

if( *(uint32_t *)(EXEC_ZONE_ADDR)

!= *(uint32_t *)(PROG_ZONE_ADDR) )

{

return true;

}else return false;

}

// -----------------------------------------------------------------------------

void PerformFirmwareUpdate(void)

{

if (FlashErase(FLASH_SECTOR_5))//(FLASH_SECTOR_5);

{

///LCD_gotoxy(1,1);

///LCD_print(''Exe Sector Erased!'');

uint32_t *data_ptr = (uint32_t *)PROG_ZONE_ADDR;

HAL_FLASH_Unlock();

for(uint32_t i = 0; i < (SECTOR_SIZE); i += sizeof(uint32_t), data_ptr++ )

{

if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, EXEC_ZONE_ADDR + i, *data_ptr) != HAL_OK)

{ //WORD = 32bits

///LCD_gotoxy(1,2);

///LCD_print(''Fault on Copy'');

break;

}

FLASH_WaitForLastOperation (4000); //in ms, max time to erase whole sector; see datasheet

}

HAL_FLASH_Lock();

///LCD_gotoxy(1,3);

///LCD_print(''Exe Sector Copied'');

}

return;

}

/////////////////////////////////////////////////////////

bool FlashErase(uint32_t SectorNumber)

{

FLASH_EraseInitTypeDef FlashEraseInitStruct;

uint32_t SectorError = 0;

HAL_FLASH_Unlock();

/* Fill EraseInit structure*/

FlashEraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;

FlashEraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;

FlashEraseInitStruct.Sector = SectorNumber;

FlashEraseInitStruct.NbSectors = 1;

if(HAL_FLASHEx_Erase(&FlashEraseInitStruct, &SectorError) == HAL_OK){

HAL_FLASH_Lock();

return true;

} else{

HAL_FLASH_Lock();

return false;

}

}

I unlock the flash before erase, it erase properly and copies ok.

the attached image shows the memory values and msp, etc..

thanks

________________

Attachments :

screen2.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I1HL&d=%2Fa%2F0X0000000bl3%2FdlOSHUGE9uPuhqVRjEkObc0dCiU_4SMkjfIfvAKQMm8&asPdf=false

Main.txt : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I1Im&d=%2Fa%2F0X0000000bl2%2FgzdYQ6pxNrkfnSNzhEGGnpSbnXcE68jVThniqiErDqc&asPdf=false
Posted on November 27, 2016 at 06:39

Ok, so the second vector entry points to 0x0804019D

Does the image exceed 128KB in size? Or was the code built for 0x08040000 rather than 0x08020000?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
abarrotes
Associate II
Posted on November 27, 2016 at 16:53

Hello Clive,

the image is about 27000 less than 128k.

I have to built the code for sector 0x08040000 because it copies it from 6 to 5. however the vector table offset is set to 0x08020000 just in case. 

Do you think building the code for sector 6 is the problem? 

thanks

Posted on November 27, 2016 at 17:06

Do you think building the code for sector 6 is the problem?

The Vector Table contains a list of absolute addresses, so yes.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
abarrotes
Associate II
Posted on November 27, 2016 at 19:06

hello clive,

how should i handle this?

Should I start doing the flash programming after the table vector definition address.

I'm looking at the PM0214 but its not too clear to me. 

thanks

Posted on November 27, 2016 at 21:14

how should i handle this? 

Do you plan on running the code at 0x08040000 or are you just parking it there?

If all the rest of your code is position independent then you could relocate/fix-up the vector table entries as you copy them into the 0x08020000 region.

I would suggest you review the Cortex-M4 Technical Reference Manual to better understand the processor, and its expectations.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..