2024-07-23 06:51 AM
Hi,
I use an STM32WB with the BLE stack. In my code, I need to write data to the flash memory (writing new firmware to a prepared memory area). Unfortunately, when I attempt to write to the flash, an error occurs. Is there anything special that I need to change? This worked fine on other MCUs like the G0 and F4.
bool MM_WriteData(uint32_t addr,uint8_t *data, uint32_t size )
{
uint64_t data64;
HAL_StatusTypeDef status=HAL_OK;
HAL_FLASH_Unlock(); //Unlocks the flash memory
while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET) {}
memcpy(&data64,&data[0], sizeof(uint64_t));
status|=HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, data64);
}
HAL_FLASH_Lock(); //Locks again the flash memory
if(status == HAL_OK)
{
return true;
}
return false;
}
2024-07-24 02:28 AM
I just found out that it works :-), but I'm not sure if there should still be some protection using a semaphore - in case the other core with M0 and BLE stack is accessing the flash at the same time.
2024-07-24 05:47 AM
The proper procedure to protect flash access is described here How to build wireless applications with STM32WB MCUs - Application note in section 4.7. It is then implemented in the BLE_RfWithFlash example in WB package. You can copy the implementation from there into your project.
2024-08-12 04:11 AM
Hello,
Thanks for your guidance - I tried using the `flash_driver.c` as indicated in the example, and it works! However, this is only the case if you have STM32WPAN and FreeRTOS enabled in CubeMX. In my application (Bootloader), I don't have either FreeRTOS or STM32WPAN, and I found that implementing `flash_driver.c` becomes very complicated because it's dependent on too many files, which I don't want to manually add to the project - who knows if it would work without RTOS. It's a pity that the library doesn't work independently.