Set NVM Size

Hello,

  I need to persist ~200K of data on the internal flash of an NRF52832 chip.  I've run a memory report and I'm current taking up 185Kb and the chip has 512 Kb of flash so there is the space.  I've taken the code from teh sample nvm project to init that nvm filesystm as below:

int init_storage(){
    LOG_INF("Init storage");
    int rc = 0;
    	/* define the nvs file system by settings with:
	 *	sector_size equal to the pagesize,
	 *	3 sectors
	 *	starting at NVS_PARTITION_OFFSET
	 */
	fs.flash_device = NVS_PARTITION_DEVICE;
	if (!device_is_ready(fs.flash_device)) {
		LOG_ERR("Flash device %s is not ready\n", fs.flash_device->name);
		return -1;
	}

	fs.offset = NVS_PARTITION_OFFSET;
	rc = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &info);
	if (rc) {
		LOG_ERR("Unable to get page info, rc=%d\n", rc);
		return -1;
	}
    LOG_INF("Storage initialized size is %" PRIu32 " index is %" PRIu32, info.size, info.index);
    

	fs.sector_size = info.size;
	fs.sector_count = 2U;
    
    rc = nvs_mount(&fs);
	if (rc) {
		LOG_ERR("Flash Init failed, rc=%d\n", rc);
		return 0;
	}

    return 0;
}

  This works fine but it only gives me 4K worth of memory space.  If I increase the sector count to more than 3 I get an invalid address: 0x00080ff8 error on runtime.  How can I set the nvm filesystem to be 200Kb in size?

Cheers,

Neil

Parents
  • Hi Neil, 
    Could you send us your partitions.yml file in build folder ? 
    Note that the NVS system is allocated inside nvs_storage partition and by default this partition has the size of 0x6000 (24kB = 6 sectors)
    I don't know what you configured in your partition manager, but you may want to manually increase the size of the nvs_storage partition. You may need to adjust the size of other partition to make room for the nvs_storage partition. 

    This is what looks like in the nvs sample built for nRF52840: 

    app:
      address: 0x0
      end_address: 0xfa000
      region: flash_primary
      size: 0xfa000
    nvs_storage:
      address: 0xfa000
      end_address: 0x100000
      placement:
        before:
        - end
      region: flash_primary
      size: 0x6000
    sram_primary:
      address: 0x20000000
      end_address: 0x20040000
      region: sram_primary
      size: 0x40000
    

  • Thanks for your help, please see blow:

    app:
      address: 0x0
      end_address: 0x7a000
      region: flash_primary
      size: 0x7a000
    nvs_storage:
      address: 0x7a000
      end_address: 0x80000
      placement:
        before:
        - end
      region: flash_primary
      size: 0x6000
    sram_primary:
      address: 0x20000000
      end_address: 0x20010000
      region: sram_primary
      size: 0x10000
    

    Regards,

    Neil

  • Hi Neil, 
    From what I can see the nvs_storage is configured with 0x6000 in size (=24kB) so it should be able to handle more than 4kB. 
    Could you be more specific on the error you have ? Could you capture the log ? 

Reply Children
  • Hello,

      My apologies; I  did the above in a rush - please ignore the previous response the partitions file is as follows:

    app:
      address: 0x0
      end_address: 0x7e000
      region: flash_primary
      size: 0x7e000
    settings_storage:
      address: 0x7e000
      end_address: 0x80000
      placement:
        align:
          start: 0x1000
        before:
        - end
      region: flash_primary
      size: 0x2000
    sram_primary:
      address: 0x20000000
      end_address: 0x20010000
      region: sram_primary
      size: 0x10000
    

      So in fact I have nothing for the partitions at all.  I used the CONFIG setting of 

    CONFIG_PM_PARTITION_SIZE_NVS_STORAGE=0x30D40 (200,000 in decimal) but I guess that doesn't configure the partitions file?
    Cheers,
    Neil
  • Hello,

      Also please see the prj.conf file below:

    #
    # Copyright (c) 2018 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    
    #add GPIO
    CONFIG_GPIO=y
    
    #enable Coredump
    CONFIG_DEBUG_COREDUMP=n
    CONFIG_DEBUG_COREDUMP_BACKEND_LOGGING=n
    
    #adding compression algithmns
    CONFIG_LZ4=y
    
    #memory
    CONFIG_HEAP_MEM_POOL_SIZE=4026
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4026
    
    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DEVICE_NAME="BrikWiz_Base_Unit"
    CONFIG_BT_DEVICE_APPEARANCE=2688
    CONFIG_BT_MAX_CONN=1
    CONFIG_BT_MAX_PAIRED=1
    
    # Enable bonding
    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    
    CONFIG_NVS=y
    CONFIG_SETTINGS=y
    CONFIG_MPU_ALLOW_FLASH_WRITE=y
    CONFIG_SETTINGS_NVS=y
    CONFIG_PM_PARTITION_SIZE_NVS_STORAGE=0x30D40
    
    # Enable DK LED and Buttons library
    CONFIG_DK_LIBRARY=y
    
    # Make sure printk is printing to the UART console
    CONFIG_CONSOLE=y
    CONFIG_UART_CONSOLE=y
    
    # Config logger
    CONFIG_LOG=y
    CONFIG_LOG_BACKEND_RTT=n
    CONFIG_LOG_BACKEND_UART=y
    CONFIG_LOG_PRINTK=n
    CONFIG_LOG_BUFFER_SIZE=10240
    
    # Debugging configuration
    CONFIG_THREAD_NAME=y
    CONFIG_THREAD_ANALYZER=n
    CONFIG_THREAD_ANALYZER_AUTO=n
    CONFIG_THREAD_ANALYZER_RUN_UNLOCKED=n
    CONFIG_THREAD_ANALYZER_USE_PRINTK=n
    
    # add asserts
    CONFIG_ASSERT=y
    CONFIG_ASSERT_VERBOSE=y
    CONFIG_ASSERT_NO_COND_INFO=n
    CONFIG_ASSERT_NO_MSG_INFO=n
    CONFIG_RESET_ON_FATAL_ERROR=n
    CONFIG_THREAD_NAME=y

      Thanks.

    Regards,

    Neil

  • Hi Neil, 
    Could you show how you set up  the nvs_fs in the code ? 
    Which flash partition you assign it to ? 
    For example here is the code in the nvs sample: 

    #define NVS_PARTITION		storage_partition
    #define NVS_PARTITION_DEVICE	FIXED_PARTITION_DEVICE(NVS_PARTITION)
    #define NVS_PARTITION_OFFSET	FIXED_PARTITION_OFFSET(NVS_PARTITION)
    
    ...
    fs.flash_device = NVS_PARTITION_DEVICE;
    	if (!device_is_ready(fs.flash_device)) {
    		printk("Flash device %s is not ready\n", fs.flash_device->name);
    		return 0;
    	}
    	fs.offset = NVS_PARTITION_OFFSET;
    	rc = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &info);
    	if (rc) {
    		printk("Unable to get page info, rc=%d\n", rc);
    		return 0;
    	}
    	fs.sector_size = info.size;
    	fs.sector_count = 5U;
    
    	rc = nvs_mount(&fs);

  • Hello,

     I actually took it straight from that example, please see below:

    #include "include/bw_storage.h"
    #include <zephyr/logging/log.h>
    #include <zephyr/types.h>
    #include <stddef.h>
    #include <string.h>
    #include <errno.h>
    #include <zephyr/sys/printk.h>
    #include <zephyr/drivers/flash.h>
    #include <zephyr/storage/flash_map.h>
    #include <zephyr/fs/nvs.h>
    
    #define LOG_MODULE_NAME brikwiz_storage
    LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG);
    
    static struct nvs_fs fs;
    
    #define NVS_PARTITION		storage_partition
    #define NVS_PARTITION_DEVICE	FIXED_PARTITION_DEVICE(NVS_PARTITION)
    #define NVS_PARTITION_OFFSET	FIXED_PARTITION_OFFSET(NVS_PARTITION)
    #define NVS_PARTITION_SIZE	FIXED_PARTITION_SIZE(NVS_PARTITION)
    
    struct flash_pages_info info;
    
    uint16_t vid_file_id;
    
    int init_storage(){
        LOG_INF("Init storage");
        int rc = 0;
        	/* define the nvs file system by settings with:
    	 *	sector_size equal to the pagesize,
    	 *	3 sectors
    	 *	starting at NVS_PARTITION_OFFSET
    	 */
    	fs.flash_device = NVS_PARTITION_DEVICE;
    	if (!device_is_ready(fs.flash_device)) {
    		LOG_ERR("Flash device %s is not ready\n", fs.flash_device->name);
    		return -1;
    	}
    
    	fs.offset = NVS_PARTITION_OFFSET;
    	rc = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &info);
    	if (rc) {
    		LOG_ERR("Unable to get page info, rc=%d\n", rc);
    		return -1;
    	}
        LOG_INF("Storage size is %" PRIu32 " index is %" PRIu32, info.size, info.index);
        
        LOG_INF("Partition size is %" PRIu32 " offset is %" PRIu32, NVS_PARTITION_SIZE, NVS_PARTITION_OFFSET);
    	fs.sector_size = info.size;
    	fs.sector_count = 2U;
        
        rc = nvs_mount(&fs);
    	if (rc) {
    		LOG_ERR("Flash Init failed, rc=%d\n", rc);
    		return 0;
    	}
    
        return 0;
    }

      However that is the only thing I took directly; I also copied over the CONFIG options from the prj.conf file in the nvs example to my project's prj.conf file.

    Cheers,

    Neil

  • Hi Neil, 
    It seems that SETTINGS and your own nvs flash is sharing the same partition settings_storage. You may want to define your own partition for your nvs flash storage. 
    I would suggest to take a look at this sample from Sigurd: 
    RE: Setting and using NVS and BT Settings with Static Partition Manager

    To have your own dedicated partition for nvs, you can create a file named pm_static.yml and put the custom_nvs_storage there. 

Related