Skip to main content
JR2963
Senior II
July 23, 2024
Question

Flash Write Errors on STM32WB with BLE Stack

  • July 23, 2024
  • 2 replies
  • 1161 views

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;
}

 

2 replies

JR2963
JR2963Author
Senior II
July 24, 2024

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.

EPASZ.1
ST Employee
July 24, 2024

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.

JR2963
JR2963Author
Senior II
August 12, 2024

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.