0

I cannot protect the data in USER_FLASH when I disconnect the ST-Link, connect it and then program the microcontroller via OpenOCD. I test it with the option (NOLOAD) in the linker script but the data always deleted.

STM32F103C8TX_FLASH.ld:

...
/* Memories definition */
MEMORY
{
  RAM        (xrw)    : ORIGIN = 0x20000000,   LENGTH = 20K
  FLASH      (rx)     : ORIGIN = 0x08000000,   LENGTH = 63K
  USER_FLASH (xrw)    : ORIGIN = 0x0800FC00,   LENGTH = 1K
}

/* Sections */
SECTIONS
{
  /* User data to be stored in the flash memory goes into USER_FLASH */
  .user_data_flash (NOLOAD):
  {
    . = ALIGN(4);
    *(.user_data_flash)     /* .user_data_flash sections */
    *(.user_data_flash*)    /* .user_data_flash sections */
    . = ALIGN(4);
  } >USER_FLASH 
...

The function works well while not disconnect the programmer:

void testFlash(void){
    uint32_t temp = 0;

    //writeFlash(test);
    //Flash_Read_Data(0x0800FC00, temp);
    temp = readFlashTest((uint32_t *)0x0800FC00);
    temp = temp + 4;
    writeFlash((uint32_t)temp);

}

uint32_t readFlashTest(uint32_t *mem){
    uint32_t temp = 0;

    HAL_FLASH_Unlock();
    temp = *mem;
    HAL_FLASH_Lock();

    return temp;
} void writeFlash(uint32_t toWrite){

    eraseFlash(); // Necesario si o si sino no escribe

    HAL_FLASH_Unlock();
    HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x0800FC00, toWrite);
    HAL_FLASH_Lock();
}
  • Instead of 0x8000FC00, it should be 0x0800FC00. – Codo Mar 10 '21 at 19:38
  • I have modified the memory address but I still have the same problem... Maybe I need to modify some parameter in OpenOCD. The console returns: `Info : device id = 0x20036410 Info : flash size = 128kbytes` but the microcontroller has 64K of Flash. – Mauro Alejandro Pereira Mar 14 '21 at 17:04
  • Please better describe what you expect to happen and what happens instead. I'm a bit confused by the term "protect". Additionally add the code for `readFlashTest` and `writeFlash`. – Codo Mar 14 '21 at 20:56
  • The USER_FLASH returns to the value 0xFFFFFFFF after I disconnect the programmer and reconnect it. – Mauro Alejandro Pereira Mar 16 '21 at 16:23

1 Answers1

0

The solution is to set BOOT0 and BOOT1 to 1. This way, the boot mode is done from the Embedded SRAM, not from the Main Flash memory.

  • I somehow doubt this is the solution. I don't see the link to the flash programming issue, and I don't see how you get a program into SRAM at power up. It's more likely that your problem is in *eraseFlash()*. Do you wait after erasing the page until the operation has completed? – Codo Mar 16 '21 at 16:52