cancel
Showing results for 
Search instead for 
Did you mean: 

Jump to application. STM32F051 vs STM32L151 (M0 to M3)

JFELI.13
Associate III
 
15 REPLIES 15
JFELI.13
Associate III

Hi all,

Here a method , writed with mikroC, to jump to application.

It works for STM32F051, it doesn't for STM32L151

Could you help me ?

Thanks

void Set_VectorsCodes(unsigned long SP,unsigned long PC) 
 
{
 
  unsigned int x2;
 
  unsigned long *ptr=VECT_PG_ADDR+8,x4,y4;
 
 
 
  x4=*ptr;
 
  ptr++;
 
  y4=*ptr;
 
  if(x4!=SP || y4!=PC )
 
  {
 
    FLASH_Unlock();
 
    FLASH_ErasePage(VECT_PG_ADDR);
 
 
 
    /* following codes works for STM32F051
 
    FLASH_Write_Word(VECT_PG_ADDR, 0x48014685);  
 
    FLASH_Write_Word(VECT_PG_ADDR+4, 0x48014700); 
 
    FLASH_Write_Word(VECT_PG_ADDR+8, SP);
 
    FLASH_Write_Word(VECT_PG_ADDR+12,PC);
 
    */
 
 
 
    // those codes doesn't work for STM32L151
 
    FLASH_Write_Word(VECT_PG_ADDR,  0xA4046012);
 
    FLASH_Write_Word(VECT_PG_ADDR+4, 0xA4046811);
 
    FLASH_Write_Word(VECT_PG_ADDR+8, 0x46984612);
 
    FLASH_Write_Word(VECT_PG_ADDR+12, 0x68084700);
 
    FLASH_Write_Word(VECT_PG_ADDR+16, SP);
 
    FLASH_Write_Word(VECT_PG_ADDR+20, PC);
 
 
 
    FLASH_Lock();
 
    #ifdef OLED
 
    Display_LineCode(5,"SP: ",VALUETYPE_HEXA32,SP,0);
 
    Display_LineCode(6,"PC: ",VALUETYPE_HEXA32,PC,0);
 
    #endif
 
  }
 
}

My application is at 0x400 (before calling "Set_VectorsCodes" it had minimum led flash function), my bootloader at 0x5B000 ( asm codes at VECT_PG_ADDR=0x5FF00 )

If I look at registers, I can see PC is in bootloader area (soft freeze as soon I try to launch it)

Thanks for help

Foued_KH
ST Employee

Hello @JFELI.13​,

I suggest you refer to this article : How to switch from one STM32 to another using STM32CubeMX

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

JFELI.13
Associate III

I tried it -and it's usefull for some needs- but there is nothing to do with my problem.

If the purpose is to change MikroC to STMCube , the BIG problem should be to convert a large code !

So, I recommend you to start a new STM32 project base on STM32CubeIDE and then copy, paste it on your new project.

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

The CM0 doesn't have a SCB->VTOR, for the STM32F0 what you need to do is carve out a small area, say 192-bytes at the base of RAM (0x20000000)

Copy/Create the vector table there, and then REMAP the ZERO address where the VECTORS are to map/shadow the 0x20000000 region.

I'll look for some example / IAP code, but basically SYSCFG clock gets enabled and MEMRMP or something similar. Look at the REFERENCE MANUAL

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
    FLASH_Write_Word(VECT_PG_ADDR,  0xA4046012);
    FLASH_Write_Word(VECT_PG_ADDR+4, 0xA4046811);
    FLASH_Write_Word(VECT_PG_ADDR+8, 0x46984612);
    FLASH_Write_Word(VECT_PG_ADDR+12, 0x68084700);

What do these code do?

JW

STM32Cube_FW_F0_V1.11.4\Projects\STM32091C_EVAL\Applications\IAP\IAP_Binary_Template\Src\main.c

...
#if   (defined ( __CC_ARM ))
__IO uint32_t VectorTable[48] __attribute__((at(0x20000000)));
#elif (defined (__ICCARM__))
#pragma location = 0x20000000
__no_init __IO uint32_t VectorTable[48];
#elif defined   (  __GNUC__  )
__IO uint32_t VectorTable[48] __attribute__((section(".RAMVectorTable")));
#endif
...
  /* Relocate by software the vector table to the internal SRAM at 0x20000000 ***/
 
  /* Copy the vector table from the Flash (mapped at the base of the application
     load address 0x08004000) to the base address of the SRAM at 0x20000000. */
  for(i = 0; i < 48; i++)
  {
    VectorTable[i] = *(__IO uint32_t*)(APPLICATION_ADDRESS + (i<<2));
  }
 
  /* Enable the SYSCFG peripheral clock*/
  __HAL_RCC_SYSCFG_CLK_ENABLE();
  /* Remap SRAM at 0x00000000 */
  __HAL_SYSCFG_REMAPMEMORY_SRAM();

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

I think you're wright

I tried to use VTOR with some values and obsevre it changed the PC register value so it should be interesting to look at it.

I'm french speaking/writing so here I'm trying to clarify what you wrote:

First I need to create an array (here "VectorTable") starting at 0x20000000 and copy values from flash (my application is at 0x400 so I need to copy to 0x0800400 ?).

No, I don't think you mean this because my bin file contain vectors SP PC from application at (applicationAdress-8) . I merge bootloader file with application file so bootloader vectors are at 0x0 and 0x4 and application vectors are at ApplicationAdress+8

Here is my adresses values

#define BOOTLOADER_ADRESS 0x5B000  
#define VECT_PG_ADDR      0x5FF00  
#define PROGRAM1_ADDR     0x400    
#define PROGRAM2_ADDR     0x2E000 
 
#pragma orgall BOOTLOADER_ADRESS

I call the "Set_VectorsCodes" like this

unsigned long SP2,PC2,*ptr;  
 
 
 
StartingAdress=0x400-8; // (for example)
 
ptr=StartingAdress;
 
SP2=*ptr;
 
if(SP2!=0xFFFFFFFF) 
 
{
 
    ptr++;
 
    y4=*ptr;
 
    Set_VectorsCodes(SP2,PC2);
 
}
 

JFELI.13
Associate III

Thos code are assembler codes ... and are completely wrong :smirking_face: (gived by chatgpt)

The should be probably

        FLASH_Write_Word(VECT_PG_ADDR, 0x46854801);
        FLASH_Write_Word(VECT_PG_ADDR+4, 0x47004801);
        FLASH_Write_Word(VECT_PG_ADDR+8, SP2);
        FLASH_Write_Word(VECT_PG_ADDR+12,PC2);