Skip to content

STM32WBA: zephyr/drivers/flash/flash_stm32wba_fm.c: flash_stm32_erase: unexpected erase size #84842

@FaStaig

Description

@FaStaig

Describe the bug

During firmware update with MCUboot/ MCUmgr I’m facing the issue that the first flash page of my storage partition, which is located direct after the image-1 partition, is erased.
After some research I found out, that this happens during the erase of the image trailer area of image-1 slot, which is located at the end of the image-1 partition. At this point there should be one flash page (8192 byte) erased, but actually there were two pages erased (the last page of image-1 partition and the first page of the storage partition).
It seams that the flash_stm32_erase() function of zephyr/drivers/flash/flash_stm32wba_fm.c handles the case when len is equal FLASH_PAGE_SIZE not correct.
In this case sect_num becomes 2 instead of 1, which leads in two erased pages instead of one.

Image

To Reproduce

Create a basic Zephyr application with main.c and prj.conf as shown below:

main.c

#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include "zephyr/device.h"
#include <zephyr/drivers/flash.h>

LOG_MODULE_REGISTER(main);

#define FLASH_SECTOR_SIZE 0x2000
#define OFFSET_SECTOR_1   0xFA000
#define OFFSET_SECTOR_2   0xFC000
#define SECTOR_1          (OFFSET_SECTOR_1 / FLASH_SECTOR_SIZE)
#define SECTOR_2          (OFFSET_SECTOR_2 / FLASH_SECTOR_SIZE)

int main(void)
{
	const struct device *flash = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));

	if (!device_is_ready(flash)) {
		printk("%s: device not ready.\n", flash->name);
		return 0;
	}

	uint32_t writeBuffer[] = {0xAAAAAAAA, 0xBBBBBBBB, 0xCCCCCCCC, 0xDDDDDDDD};
	flash_write(flash, OFFSET_SECTOR_1, &writeBuffer, sizeof(writeBuffer));
	flash_write(flash, OFFSET_SECTOR_2, &writeBuffer, sizeof(writeBuffer));
	LOG_INF("Write buffer to sector %d: 0x%08x 0x%08x 0x%08x 0x%08x", SECTOR_1, writeBuffer[0],
		writeBuffer[1], writeBuffer[2], writeBuffer[3]);
	LOG_INF("Write buffer to sector %d: 0x%08x 0x%08x 0x%08x 0x%08x", SECTOR_2, writeBuffer[0],
		writeBuffer[1], writeBuffer[2], writeBuffer[3]);

	flash_erase(flash, OFFSET_SECTOR_1, FLASH_SECTOR_SIZE);

	LOG_INF("Erased sector %d", SECTOR_1);

	uint32_t readBufferSector1[4];
	uint32_t readBufferSector2[4];
	flash_read(flash, OFFSET_SECTOR_1, readBufferSector1, sizeof(readBufferSector1));
	flash_read(flash, OFFSET_SECTOR_2, readBufferSector2, sizeof(readBufferSector2));

	LOG_INF("Read buffer from sector %d: 0x%08x 0x%08x 0x%08x 0x%08x", SECTOR_1,
		readBufferSector1[0], readBufferSector1[1], readBufferSector1[2],
		readBufferSector1[3]);
	LOG_INF("Read buffer from sector %d: 0x%08x 0x%08x 0x%08x 0x%08x", SECTOR_2,
		readBufferSector2[0], readBufferSector2[1], readBufferSector2[2],
		readBufferSector2[3]);

	flash_erase(flash, OFFSET_SECTOR_1, FLASH_SECTOR_SIZE);
	flash_erase(flash, OFFSET_SECTOR_2, FLASH_SECTOR_SIZE);

	k_sleep(K_FOREVER);
	return 0;
}

prj.conf

CONFIG_LOG=y

CONFIG_FLASH=y

# It's important that Bluetooth is enabled, as only then flash_stm32wba_fm.c is used
CONFIG_BT=y

run west build -p -b nucleo_wba55cg and west flash

Expected behavior
One flash page should be erased.

Experienced behavior
Two flash pages are erased.

Logs and console output

Image

If you disable bluetooth in prj.conf

CONFIG_BT=n

flash_stm32wba_fm.c isn't used anymore as flash driver and you can se the following output, where only one flash page is erased as expected.

Image

Environment (please complete the following information):

  • OS: Linux (Ubuntu)
  • Toolchain: Zephyr SDK 0.16.8
  • Zephyr OS: v3.7.0
  • Board: nucleo_wba55cg

Metadata

Metadata

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions