Writing to internal flash STM32L422KBT6
Hi I hove something trobels with writing data after erease. I have only option bank and page erease and nothing other. I doing a bank1 erease and after this tryuing to write something whith is complitly not writing to memeory. Only on another compilation if memory is eresed I have data writen. How to clean and then write data? Im writing a uint16_t like a doble word, also I have only duble word. This making me cry, plese help just make a data rewrite, like it should be. I not wish have additional function for erease. Many thanks.
#define FLASH_START_ADDRESS 0x08010000
// Function to erase the flash memory
void erase_flash()
{
// Unlock the flash memory for writing
HAL_FLASH_Unlock();
// Initialize the erase configuration structure
FLASH_EraseInitTypeDef erase_init;
erase_init.TypeErase = FLASH_TYPEERASE_MASSERASE; // Mass erase the flash memory
erase_init.Banks = FLASH_BANK_1; // Select the bank of the flash memory to erase
// Perform the erase operation and store any errors
uint32_t flash_error;
HAL_FLASHEx_Erase(&erase_init, &flash_error);
// Lock the flash memory after erasing is complete
HAL_FLASH_Lock();
}
// Function to write data to the flash memory
void write_flash(uint16_t *data, uint32_t size)
{
// Unlock the flash memory for writing
HAL_FLASH_Unlock();
// Iterate over the array of data and write each element to the flash memory
uint32_t i, flash_error;
for (i = 0; i < size; i++)
{
// Convert the 16-bit data to a 64-bit value and write it to the flash memory
uint64_t value = *(data + i);
flash_error = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, FLASH_START_ADDRESS + i * 8, value);
// Handle any errors that occur during the write operation
if (flash_error != HAL_OK)
{
// Insert code to handle the error here
}
}
// Lock the flash memory after writing is complete
HAL_FLASH_Lock();
}