how to over write data in same offset into internal flash on zephyr

Hi,

I am using the nRF Connect SDK 2.6.1 with the nRF5340 board.

I referred to the soc_flash_nrf example to read and write data into flash.

I need to overwrite data at the same offset, but when I try this in the soc_flash_nrf example, I don't get the updated value after reading from the same offset,I still see the previously written value when i read it from same offset.

How can I properly overwrite data at the same offset?

using flash_erase(flash_dev, offset,sizeof(uint32_t)) i try to erase the flash but i got the Flash erase failed.

#define TEST_PARTITION	storage_partition

#define TEST_PARTITION_OFFSET	FIXED_PARTITION_OFFSET(TEST_PARTITION)
#define TEST_PARTITION_DEVICE	FIXED_PARTITION_DEVICE(TEST_PARTITION)

#define FLASH_PAGE_SIZE   4096
#define TEST_DATA_WORD_0  0x11
#define TEST_DATA_WORD_1  0xaabb
#define TEST_DATA_WORD_2  0xab
#define TEST_DATA_WORD_3  0x12

#define FLASH_TEST_OFFSET2 0x41234
#define FLASH_TEST_PAGE_IDX 37

#define DATA_TO_WRITE     0xDEADBEEF 

int main(void)
{
	const struct device *flash_dev = TEST_PARTITION_DEVICE;
	uint32_t buf_array_1[4] = { TEST_DATA_WORD_0, TEST_DATA_WORD_1,
				    TEST_DATA_WORD_2, TEST_DATA_WORD_3 };
	uint32_t buf_array_2[4] = { TEST_DATA_WORD_3, TEST_DATA_WORD_1,
				    TEST_DATA_WORD_2, TEST_DATA_WORD_0 };
	uint32_t buf_array_3[8] = { TEST_DATA_WORD_0, TEST_DATA_WORD_1,
				    TEST_DATA_WORD_2, TEST_DATA_WORD_3,
				    TEST_DATA_WORD_0, TEST_DATA_WORD_1,
				    TEST_DATA_WORD_2, TEST_DATA_WORD_3 };
	uint32_t buf_word= 0;
	uint32_t i, offset;

	printf("\nNordic nRF5 Flash Testing\n");
	printf("=========================\n");

	if (!device_is_ready(flash_dev)) {
		printf("Flash device not ready\n");
		return 0;
	}

	printf("\nTest 1: Flash erase page at 0x%x\n", TEST_PARTITION_OFFSET);
	if (flash_erase(flash_dev, TEST_PARTITION_OFFSET, FLASH_PAGE_SIZE) != 0) {
		printf("   Flash erase failed!\n");
	} else {
		printf("   Flash erase succeeded!\n");
	}

	printf("\nTest 2: Flash write (word array 1)\n");
	//for (i = 0U; i < 4; i++) 
	{
		offset = TEST_PARTITION_OFFSET;
		printf("   Attempted to write %x at 0x%x\n", buf_array_1[0],
				offset);
		if (flash_write(flash_dev, offset,&buf_array_1[0],
					sizeof(uint32_t)) != 0) {
			printf("   Flash write failed!\n");
			return 0;
		}
		printf("   Attempted to read 0x%x\n", offset);
		if (flash_read(flash_dev, offset, &buf_word,
					sizeof(uint32_t)) != 0) {
			printf("   Flash read failed!\n");
			return 0;
		}
		//for (i = 0U; i < ARRAY_SIZE(buf_array_1); i++) 
	    {
		 printf("   Data read: %x\n",buf_word);
		}
		buf_word = 0;

		if (flash_erase(flash_dev, offset,sizeof(uint32_t)) != 0) {
		printf("   Flash erase failed!\n");
	} else {
		printf("   Flash erase succeeded!\n");
	}
		
		offset = TEST_PARTITION_OFFSET;
		printf("   Attempted to write %x at 0x%x\n", buf_array_1[1],
				offset);
		if (flash_write(flash_dev, offset,&buf_array_1[1],
					sizeof(uint32_t)) != 0) {
			printf("   Flash write failed!\n");
			return 0;
		}
		printf("   Attempted to read 0x%x\n", offset);
		if (flash_read(flash_dev, offset, &buf_word,
					sizeof(uint32_t)) != 0) {
			printf("   Flash read failed!\n");
			return 0;
		}
		//for (i = 0U; i < ARRAY_SIZE(buf_array_1); i++) 
	    {
		 printf("   Data read: %x\n",buf_word);
		}
		buf_word = 0;
		offset = TEST_PARTITION_OFFSET+(1<<2);
		printf("   Attempted to write %x at 0x%x\n", buf_array_1[3],
				offset);
		if (flash_write(flash_dev, offset,&buf_array_1[3],
					sizeof(uint32_t)) != 0) {
			printf("   Flash write failed!\n");
			return 0;
		}


		if (flash_erase(flash_dev, offset, 4) != 0) {
		printf("   Flash erase failed!\n");
	} else {
		printf("   Flash erase succeeded!\n");
	}

		offset = TEST_PARTITION_OFFSET;
		printf("   Attempted to write %x at 0x%x\n", buf_array_1[2],
				offset);
		if (flash_write(flash_dev, offset,&buf_array_1[1],
					sizeof(uint32_t)) != 0) {
			printf("   Flash write failed!\n");
			return 0;
		}
		printf("   Attempted to read 0x%x\n", offset);
		if (flash_read(flash_dev, offset, &buf_word,
					sizeof(uint32_t)) != 0) {
			printf("   Flash read failed!\n");
			return 0;
		}
		//for (i = 0U; i < ARRAY_SIZE(buf_array_1); i++) 
	    {
		 printf("   Data read: %x\n",buf_word);
		}
		offset = TEST_PARTITION_OFFSET+(1<<2);
		printf("   Attempted to read 0x%x\n", offset);
		if (flash_read(flash_dev, offset, &buf_word,
					sizeof(uint32_t)) != 0) {
			printf("   Flash read failed!\n");
			return 0;
		}
		{
		 printf("   Data read: %x\n",buf_word);
		}

        buf_word = 0;


		// if (buf_array_1[i] == buf_word) {
		// 	printf("   Data read matches data written. Good!\n");
		// } else {
		// 	printf("   Data read does not match data written!\n");
		// }
	}

 

Parents Reply Children
  • Hi,

    I am able to erase the flash using flash_erase(flash_dev, TEST_PARTITION_OFFSET, FLASH_PAGE_SIZE). However, when I use flash_erase(flash_dev, offset, sizeof(uint32_t)), the flash does not erase as expected. What could be the reason behind this?

    After erasing the flash, I write data (e.g., 1234) and can successfully read it back. However, when I write new data (e.g., 1122) to the same offset, I still read back the original value (1234). Is there a method to properly overwrite data at the same offset?

    Additionally, could you provide an example of using NVS (Non-Volatile Storage) for storing data?

Related