Skip to main content
Visitor II
September 26, 2023
Question

Flash Emulation read write crashes program STM32L412

  • September 26, 2023
  • 3 replies
  • 1415 views

Hello, 

I try to write some user parameters to the non volatile memory of the program like using an EEProm , trying to read or wrote to say memory address 0x08019000 keeps crashing my program and debugger when reading / writing from it.

I see from the memory scan with the debugger that address  0x08019000 did infact get written by luck in one of my many attemps today but still can't read / write to it realy.

Here are the base functions I use:

```

uint32_t flashWriteData(uint32_t Address, uint64_t data){

  HAL_FLASH_Unlock();
  FLASH_EraseInitTypeDef EraseInitStruct;
  EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  // EraseInitStruct.Banks = FLASH_BANK_1; // ?
  EraseInitStruct.Page = Address;
  // EraseInitStruct.NbPages = 1;

  uint32_t PageError;
  if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)    //Erase the Page Before a Write Operation
      return HAL_ERROR;

  // HAL_Delay(50);
  HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,Address,(uint64_t)data); // FLASH_TYPEPROGRAM_DOUBLEWORD FLASH_TYPEPROGRAM_WORD
  HAL_Delay(50);
  HAL_FLASH_Lock();


}


uint32_t flashReadData(uint32_t Address){

  __IO uint32_t read_data = *(__IO uint32_t *)Address;
  return (uint32_t)read_data;
}
```
 
Useage:
 
```

  uint32_t *MyData;

  MyData = flashReadData(0x08019000);

  printf("My Data = %d\n", *MyData);

  if(MyData == 55){
    printf("Variable has already been written to\n");
  }
  else{
    printf("Was %llu Now Writing to variable\n", MyData);
    uint64_t MyWriteData = 55;
    flashWriteData(0x08019000, MyWriteData);

  }
 
  uint32_t *MyReadData = flashReadData(0x08019000);

  printf("My Read Data = %d\n", *MyReadData);
 
```
 
 
Any idea what am I doing wrong

 

    This topic has been closed for replies.

    3 replies

    Explorer
    November 5, 2023

    @MikeLemon  Did you find away for this, as I'm facing the same issue ?

    Visitor II
    November 5, 2023

    You can try this way.

    uint32_t MyData;
    MyData = flashReadData(0x08019000);
    
    printf("My Data = %lu\n", MyData);
    
    if (MyData == 55) {
     printf("Variable has already been written to\n");
    } else {
     printf("Was %lu. Now Writing to variable\n", MyData);
     uint64_t MyWriteData = 55;
     flashWriteData(0x08019000, MyWriteData);
    }
    
    MyData = flashReadData(0x08019000);
    
    printf("My Read Data = %lu\n", MyData);
    Explorer
    November 5, 2023

    @liaifat85 and what about flashWriteData and flashReadData, are they the same as up or what ?