This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF9160 NVS errors out when using different Sector Counts

Hello,

I am attempting to set up NVS to capture GPS data received from the modem for transmission to NB-IoT after collection has ended.

I have successfully implimented the NVS example into my code and have been able to store and read back data from flash (although I keep reading back random characters at the end of the strings for some reason that I would like to understand also). I have an issue however when I try to increase the sector count of the flash storage. I can add 5 sectors without any issue but more than 5 (according to the OPS pg21, there are 255 pages of flash), the operations nvs_clear/write/read all return -22 as an error.

The code below is a trimmed down version, using simulated GPS values.

/*
 * Copyright (c) 2018 Laczen
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#include <zephyr.h>
#include <misc/reboot.h>
#include <device.h>
#include <string.h>

#include "Header/UDP.h"
#include "Header/Types_Defines.h"

#define value1  "53.760241,-5.147095,1.023,11:20:22"
#define value2  "53.760241,-5.147095,1.023,11:20:23"
#define value3  "53.760241,-5.147095,1.023,11:20:24"
#define value4  "53.760241,-5.147095,1.023,11:20:25"
#define value5  "53.760241,-5.147095,1.023,11:20:26"
#define value6  "53.760241,-5.147095,1.023,11:20:27"
#define value7  "53.760241,-5.147095,1.023,11:20:28"
#define value8  "53.760241,-5.147095,1.023,11:20:29"
#define value9  "53.760241,-5.147095,1.023,11:20:30"
#define value10  "53.760241,-5.147095,1.023,11:20:31"
#define value11  "53.760241,-5.147095,1.023,11:20:32"
#define value12  "53.760241,-5.147095,1.023,11:20:33"

static const char results[][60] = { value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12};

static struct nvs_fs fs;
struct flash_pages_info info;

int nvs_setup(void)
{
	int err;

	fs.offset = DT_FLASH_AREA_STORAGE_OFFSET;
	err = flash_get_page_info_by_offs(device_get_binding(DT_FLASH_DEV_NAME),
					 fs.offset, &info);
	if (err) {
		printk("Unable to get page info");
	}
	fs.sector_size = info.size;
	fs.sector_count = 250U;

	err = nvs_init(&fs, DT_FLASH_DEV_NAME);
	if (err) {
		printk("Flash Init failed\n");
	}

	err=nvs_clear(&fs);
	if(err){
		printk("nvs_clear failed\n");
	}

	return err;
}

void main(void)
{
	int err;

	err = enable_modem();
	if(err){
		printk("Modem init failed\n");
	}

	err = nvs_setup();
	if(err){
		printk("nvs_setup failed\n");
	}

	for(int i=0; i<ARRAY_SIZE(results); i++)
	{
		printk("Writing %s to NVS\n", results[i]);
		ssize_t bytes_written = nvs_write(&fs, i, results[i], strlen(results[i]));
		printk("Bytes written to nvs: %d at ID %d\n", bytes_written, i);
	}

	ssize_t freespace = nvs_calc_free_space(&fs);
	printk("Remaining free space in nvs sector is %d Bytes\n", freespace);
	k_sleep(K_MSEC(5000));
	char nvs_rx_buff[39];
	// char nvs_UDP_buff[2145] = "\0";

	for(int i=0; i<ARRAY_SIZE(results); i++)
	{
		ssize_t bytes_read = nvs_read(&fs, i, nvs_rx_buff, sizeof(nvs_rx_buff));
		printk("Bytes read from nvs: %d at ID %d\n", bytes_read, i);
		printk("Data read from nvs: %s at ID %d\n", nvs_rx_buff, i);
		// strcat(nvs_UDP_buff,nvs_rx_buff);
		// strcat(nvs_UDP_buff,"\n");
	}
 // int count=0;
	// do
	// {
	// 	if(count==5)
	// 	{
	// 		break;
	// 	}
	// 	printk("Waiting 5 more seconds!\n");
	// 	k_sleep(K_MSEC(5000));
	// 	count++;
	// }
	// while((err=check_network())<0);
 //
	// err = UDP_Send(nvs_UDP_buff);
	// if(err)
	// {
	// 	printk("UDP_send failed\n");
	// }
}

Output at 5U Sectors:

09 NRF_EGU1		Non-Secure	OK
10 NRF_EGU2		Non-Secure	OK
11 NRF_TWIM2		Non-Secure	OK
12 NRF_SPIM3		Non-Secure	OK
13 NRF_TIMER0		Non-Secure	OK
14 NRF_TIMER1		Non-Secure	OK
15 NRF_TIMER2		Non-Secure	OK
16 NRF_SAADC		Non-Secure	OK
17 NRF_GPIOTE1		Non-Secure	OK

SPM: NS image at 0x8000
SPM: NS MSP at 0x200232e0
SPM: NS reset vector at 0xe329
SPM: prepare to jump to Non-Secure image.
***** Booting Zephyr OS v1.14.99-ncs1 *****
Socket ID: 2
Sending AT command: AT+CFUN=4
Modem response: OK
Sending AT command: AT+CFUN=1
Modem response: OK
Modem enabled
Writing 53.760241,-5.147095,1.023,11:20:22 to NVS
Bytes written to nvs: 34 at ID 0
Writing 53.760241,-5.147095,1.023,11:20:23 to NVS
Bytes written to nvs: 34 at ID 1
Writing 53.760241,-5.147095,1.023,11:20:24 to NVS
Bytes written to nvs: 34 at ID 2
Writing 53.760241,-5.147095,1.023,11:20:25 to NVS
Bytes written to nvs: 34 at ID 3
Writing 53.760241,-5.147095,1.023,11:20:26 to NVS
Bytes written to nvs: 34 at ID 4
Writing 53.760241,-5.147095,1.023,11:20:27 to NVS
Bytes written to nvs: 34 at ID 5
Writing 53.760241,-5.147095,1.023,11:20:28 to NVS
Bytes written to nvs: 34 at ID 6
Writing 53.760241,-5.147095,1.023,11:20:29 to NVS
Bytes written to nvs: 34 at ID 7
Writing 53.760241,-5.147095,1.023,11:20:30 to NVS
Bytes written to nvs: 34 at ID 8
Writing 53.760241,-5.147095,1.023,11:20:31 to NVS
Bytes written to nvs: 34 at ID 9
Writing 53.760241,-5.147095,1.023,11:20:32 to NVS
Bytes written to nvs: 34 at ID 10
Writing 53.760241,-5.147095,1.023,11:20:33 to NVS
Bytes written to nvs: 34 at ID 11
Remaining free space in nvs sector is 15848 Bytes
[00:00:01.271,820] <inf> fs_nvs: 5 Sectors of 4096 bytes
[00:00:01.271,820] <inf> fs_nvs: alloc wra: 0, f90
[00:00:01.271,820] <inf> fs_nvs: data wra: 0, 198
[00:00:01.271,850] <dbg> fs_nvs.nvs_flash_erase_sector: Erasing flash at fa000, len 4096
Bytes read from nvs: 34 at ID 0
Data read from nvs: 53.760241,-5.147095,1.023,11:20:22ßËùò at ID 0
Bytes read from nvs: 34 at ID 1
Data read from nvs: 53.760241,-5.147095,1.023,11:20:23ßËùò at ID 1
Bytes read from nvs: 34 at ID 2
Data read from nvs: 53.760241,-5.147095,1.023,11:20:24ßËùò at ID 2
Bytes read from nvs: 34 at ID 3
Data read from nvs: 53.760241,-5.147095,1.023,11:20:25ßËùò at ID 3
Bytes read from nvs: 34 at ID 4
Data read from nvs: 53.760241,-5.147095,1.023,11:20:26ßËùò at ID 4
Bytes read from nvs: 34 at ID 5
Data read from nvs: 53.760241,-5.147095,1.023,11:20:27ßËùò at ID 5
Bytes read from nvs: 34 at ID 6
Data read from nvs: 53.760241,-5.147095,1.023,11:20:28ßËùò at ID 6
Bytes read from nvs: 34 at ID 7
Data read from nvs: 53.760241,-5.147095,1.023,11:20:29ßËùò at ID 7
Bytes read from nvs: 34 at ID 8
Data read from nvs: 53.760241,-5.147095,1.023,11:20:30ßËùò at ID 8
Bytes read from nvs: 34 at ID 9
Data read from nvs: 53.760241,-5.147095,1.023,11:20:31ßËùò at ID 9
Bytes read from nvs: 34 at ID 10
Data read from nvs: 53.760241,-5.147095,1.023,11:20:32ßËùò at ID 10
Bytes read from nvs: 34 at ID 11
Data read from nvs: 53.760241,-5.147095,1.023,11:20:33ßËùò at ID 11

Output at 10U Sectors:

10 NRF_EGU2		Non-Secure	OK
11 NRF_TWIM2		Non-Secure	OK
12 NRF_SPIM3		Non-Secure	OK
13 NRF_TIMER0		Non-Secure	OK
14 NRF_TIMER1		Non-Secure	OK
15 NRF_TIMER2		Non-Secure	OK
16 NRF_SAADC		Non-Secure	OK
17 NRF_GPIOTE1		Non-Secure	OK

SPM: NS image at 0x8000
SPM: NS MSP at 0x200232e0
SPM: NS reset vector at 0xe329
SPM: prepare to jump to Non-Secure image.
***** Booting Zephyr OS v1.14.99-ncs1 *****
Socket ID: 2
Sending AT command: AT+CFUN=4
Modem response: OK
Sending AT command: AT+CFUN=1
Modem response: OK
Modem enabled
nvs_clear failed
nvs_setup failed
Writing 53.760241,-5.147095,1.023,11:20:22 to NVS
Bytes written to nvs: -22 at ID 0
Writing 53.760241,-5.147095,1.023,11:20:23 to NVS
Bytes written to nvs: -22 at ID 1
Writing 53.760241,-5.147095,1.023,11:20:24 to NVS
Bytes written to nvs: -22 at ID 2
Writing 53.760241,-5.147095,1.023,11:20:25 to NVS
Bytes written to nvs: -22 at ID 3
Writing 53.760241,-5.147095,1.023,11:20:26 to NVS
Bytes written to nvs: -22 at ID 4
Writing 53.760241,-5.147095,1.023,11:20:27 to NVS
Bytes written to nvs: -22 at ID 5
Writing 53.760241,-5.147095,1.023,11:20:28 to NVS
Bytes written to nvs: -22 at ID 6
Writing 53.760241,-5.147095,1.023,11:20:29 to NVS
Bytes written to nvs: -22 at ID 7
Writing 53.760241,-5.147095,1.023,11:20:30 to NVS
Bytes written to nvs: -22 at ID 8
Writing 53.760241,-5.147095,1.023,11:20:31 to NVS
Bytes written to nvs: -22 at ID 9
Writing 53.760241,-5.147095,1.023,11:20:32 to NVS
Bytes written to nvs: -22 at ID 10
Writing 53.760241,-5.147095,1.023,11:20:33 to NVS
Bytes written to nvs: -22 at ID 11
Remaining free space in nvs sector is -22 Bytes
[00:00:02.058,654] <inf> fs_nvs: 10 Sectors of 4096 bytes
[00:00:02.058,654] <inf> fs_nvs: alloc wra: 0, ff0
[00:00:02.058,685] <inf> fs_nvs: data wra: 0, f98
[00:00:02.058,746] <dbg> fs_nvs.nvs_flash_erase_sector: Erasing flash at fa000, len 4096
Bytes read from nvs: -22 at ID 0
Data read from nvs: UUUUUUUUUUUU at ID 0
Bytes read from nvs: -22 at ID 1
Data read from nvs: UUUUUUUUUUUU at ID 1
Bytes read from nvs: -22 at ID 2
Data read from nvs: UUUUUUUUUUUU at ID 2
Bytes read from nvs: -22 at ID 3
Data read from nvs: UUUUUUUUUUUU at ID 3
Bytes read from nvs: -22 at ID 4
Data read from nvs: UUUUUUUUUUUU at ID 4
Bytes read from nvs: -22 at ID 5
Data read from nvs: UUUUUUUUUUUU at ID 5
Bytes read from nvs: -22 at ID 6
Data read from nvs: UUUUUUUUUUUU at ID 6
Bytes read from nvs: -22 at ID 7
Data read from nvs: UUUUUUUUUUUU at ID 7
Bytes read from nvs: -22 at ID 8
Data read from nvs: UUUUUUUUUUUU at ID 8
Bytes read from nvs: -22 at ID 9
Data read from nvs: UUUUUUUUUUUU at ID 9
Bytes read from nvs: -22 at ID 10
Data read from nvs: UUUUUUUUUUUU at ID 10
Bytes read from nvs: -22 at ID 11
Data read from nvs: UUUUUUUUUUUU at ID 11

Output at 250U Sectors:

09 NRF_EGU1		Non-Secure	OK
10 NRF_EGU2		Non-Secure	OK
11 NRF_TWIM2		Non-Secure	OK
12 NRF_SPIM3		Non-Secure	OK
13 NRF_TIMER0		Non-Secure	OK
14 NRF_TIMER1		Non-Secure	OK
15 NRF_TIMER2		Non-Secure	OK
16 NRF_SAADC		Non-Secure	OK
17 NRF_GPIOTE1		Non-Secure	OK

SPM: NS image at 0x8000
SPM: NS MSP at 0x200232e0
SPM: NS reset vector at 0xe329
SPM: prepare to jump to Non-Secure image.
***** Booting Zephyr OS v1.14.99-ncs1 *****
Socket ID: 2
Sending AT command: AT+CFUN=4
Modem response: OK
Sending AT command: AT+CFUN=1
Modem response: OK
Modem enabled
nvs_clear failed
nvs_setup failed
Writing 53.760241,-5.147095,1.023,11:20:22 to NVS
Bytes written to nvs: -22 at ID 0
Writing 53.760241,-5.147095,1.023,11:20:23 to NVS
Bytes written to nvs: -22 at ID 1
Writing 53.760241,-5.147095,1.023,11:20:24 to NVS
Bytes written to nvs: -22 at ID 2
Writing 53.760241,-5.147095,1.023,11:20:25 to NVS
Bytes written to nvs: -22 at ID 3
Writing 53.760241,-5.147095,1.023,11:20:26 to NVS
Bytes written to nvs: -22 at ID 4
Writing 53.760241,-5.147095,1.023,11:20:27 to NVS
Bytes written to nvs: -22 at ID 5
Writing 53.760241,-5.147095,1.023,11:20:28 to NVS
Bytes written to nvs: -22 at ID 6
Writing 53.760241,-5.147095,1.023,11:20:29 to NVS
Bytes written to nvs: -22 at ID 7
Writing 53.760241,-5.147095,1.023,11:20:30 to NVS
Bytes written to nvs: -22 at ID 8
Writing 53.760241,-5.147095,1.023,11:20:31 to NVS
Bytes written to nvs: -22 at ID 9
Writing 53.760241,-5.147095,1.023,11:20:32 to NVS
Bytes written to nvs: -22 at ID 10
Writing 53.760241,-5.147095,1.023,11:20:33 to NVS
Bytes written to nvs: -22 at ID 11
Remaining free space in nvs sector is -22 Bytes
[00:00:01.221,343] <inf> fs_nvs: 250 Sectors of 4096 bytes
[00:00:01.221,343] <inf> fs_nvs: alloc wra: 0, ff0
[00:00:01.221,343] <inf> fs_nvs: data wra: 0, 0
Bytes read from nvs: -22 at ID 0
Data read from nvs: UUUUUUUUUUUU at ID 0
Bytes read from nvs: -22 at ID 1
Data read from nvs: UUUUUUUUUUUU at ID 1
Bytes read from nvs: -22 at ID 2
Data read from nvs: UUUUUUUUUUUU at ID 2
Bytes read from nvs: -22 at ID 3
Data read from nvs: UUUUUUUUUUUU at ID 3
Bytes read from nvs: -22 at ID 4
Data read from nvs: UUUUUUUUUUUU at ID 4
Bytes read from nvs: -22 at ID 5
Data read from nvs: UUUUUUUUUUUU at ID 5
Bytes read from nvs: -22 at ID 6
Data read from nvs: UUUUUUUUUUUU at ID 6
Bytes read from nvs: -22 at ID 7
Data read from nvs: UUUUUUUUUUUU at ID 7
Bytes read from nvs: -22 at ID 8
Data read from nvs: UUUUUUUUUUUU at ID 8
Bytes read from nvs: -22 at ID 9
Data read from nvs: UUUUUUUUUUUU at ID 9
Bytes read from nvs: -22 at ID 10
Data read from nvs: UUUUUUUUUUUU at ID 10
Bytes read from nvs: -22 at ID 11
Data read from nvs: UUUUUUUUUUUU at ID 11

Is there something missing to enable the use of more than a few pages of flash?

Many thanks,

MJD093

Parents
  • Actually, I think I can answer the multi-page thing as well.

    If you print the fs.sector_size and DT_FLASH_AREA_STORAGE_OFFSET, you'll see that it's trying to use 4096-byte sectors located somewhere around flash offset 1024000. Simply put, don't have enough room for (4096*10) bytes of flash starting that high up.  I think you should have room for 6 at most.

  • Ah thank you, adding +1 to the length of the data properly returns the string without random characters. Knew there was something silly I was forgetting there.

    From your comments on the NVS offset, I decided to read back all the information stored in struct nvs_fs. You are right, the offset address is starting at 0x000FA000 or flash page 250 of 255 (from pg 21 of the manual). You are right in you assertion on the number of pages being 6 because of this.

    Modem enabled
    NVS offset: 000fa000
    NVS ate_wra: 00000ff0
    NVS data_wra: 00000000
    NVS Sector Size: 4096
    NVS sector_count: 6
    NVS write_block_size: 1
    NVS ready: 1
    Bytes written to nvs: 35 at ID 0
    Bytes written to nvs: 35 at ID 1
    Bytes written to nvs: 35 at ID 2
    Bytes written to nvs: 35 at ID 3
    Bytes written to nvs: 35 at ID 4
    Bytes written to nvs: 35 at ID 5
    Bytes written to nvs: 35 at ID 6
    Bytes written to nvs: 35 at ID 7
    Bytes written to nvs: 35 at ID 8
    Bytes written to nvs: 35 at ID 9
    Bytes written to nvs: 35 at ID 10
    Bytes written to nvs: 35 at ID 11
    NVS offset: 000fa000
    NVS ate_wra: 00000f90
    NVS data_wra: 000001a4
    NVS Sector Size: 4096
    NVS sector_count: 6
    NVS write_block_size: 1
    NVS ready: 1
    Remaining free space in nvs sector is 19924 Bytes
    [00:00:01.202,819] <inf> fs_nvs: 6 Sectors of 4096 bytes
    [00:00:01.202,819] <inf> fs_nvs: alloc wra: 0, ff0
    [00:00:01.202,850] <inf> fs_nvs: data wra: 0, 0

    From a question you posed 20 days ago, I had a look at the .dts files for the DK and can see the storage partition is set for this offset.

    		boot_partition: partition@0 {
    			label = "mcuboot";
    			reg = <0x00000000 0x10000>;
    		};
    		slot0_partition: partition@10000 {
    			label = "image-0";
    		};
    		slot0_ns_partition: partition@40000 {
    			label = "image-0-nonsecure";
    		};
    		slot1_partition: partition@80000 {
    			label = "image-1";
    		};
    		slot1_ns_partition: partition@b0000 {
    			label = "image-1-nonsecure";
    		};
    		scratch_partition: partition@f0000 {
    			label = "image-scratch";
    			reg = <0x000f0000 0xa000>;
    		};
    		storage_partition: partition@fa000 {
    			label = "storage";
    			reg = <0x000fa000 0x00006000>;
    		};

    I guess my question is now, I'd want to increase the size of the storage partition without making the others too small that they can't hold the application images. Is there some way to start telling just how much space is actually required for each individual partition, rather than just assigning a partition with a large number of flash sectors attributed to it.

  • Is there some way to start telling just how much space is actually required for each individual partition

    The size is printed when you build the project; e.g.

  • Thanks but I was aware of this feature in the build terminal, I've been using ninja flash so much in the past two months Sweat smile

    I'm not sure how that is supposed to help me determine adequate partition sizes, as unless I'm missing something, that is just the global amount of flash used rather than specific regions. In the end, I have manually reduced the size of the partitions by half (given the small amount of flash percentage used) except for the boot and scratch partition; ensuring that the partition sizes are still matching for slot0 and slot1 as per the Zephyr guidelines.

    I now have 102 sectors of flash avaliable, giving me 412888 Bytes of NVS space.

Reply
  • Thanks but I was aware of this feature in the build terminal, I've been using ninja flash so much in the past two months Sweat smile

    I'm not sure how that is supposed to help me determine adequate partition sizes, as unless I'm missing something, that is just the global amount of flash used rather than specific regions. In the end, I have manually reduced the size of the partitions by half (given the small amount of flash percentage used) except for the boot and scratch partition; ensuring that the partition sizes are still matching for slot0 and slot1 as per the Zephyr guidelines.

    I now have 102 sectors of flash avaliable, giving me 412888 Bytes of NVS space.

Children
No Data
Related