I have a function that was implemented for STM32F4 discovery board and needs to be migrated to STM32L4R9I.
static void hal_flash_bank_stm32_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size)
{
hal_flash_bank_stm32_t * self = (hal_flash_bank_stm32_t *) context;
if (bank > 1) return;
if (offset > self->sector_size) return;
if ((offset + size) > self->sector_size) return;
unsigned int i;
HAL_FLASH_Unlock();
for (i = 0; i < size; i++)
{
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, self->banks[bank] + offset + i, data[i]);
}
HAL_FLASH_Lock();
}
The STM32L4R9I does not provide FLASH_TYPEPROGRAM_BYTE but rather FLASH_TYPEPROGRAM_DOUBLEWORD. I am struggling with how to write all bytes properly. I tried the following:
uint64_t dword = 0;
for (i = 0; i < size; i++)
{
dword = (dword << 8) | data[i];
if (i > 0 && i % 8 == 0)
{
uint32_t addr = self->banks[bank] + offset + i;
HAL_FLASH_PROGRAM(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, dword);
dword = 0;
}
}
I am currently unable to test my logic on a board. Is my logic correct or am I missing something?