Storing a few parameter bytes in flash

I need to store just and handful of bytes in flash, some config data. Looking around the forum I've seen recommendation to read and write from/to by using the nvs functionality, but that looks massively overcomplicated at first glance. I don't need or want a filesystem. On an STM32 it's really simple and easy to put aside a sector and program data within it, without needing a file system you can do all that with just a handful of lines of code. Is it not possible to do the same with this chip?

Am using that VSC Zephyr setup and taken the central uart project to do my UART stuff after a lot of hand holding. Now I just want to save a few bytes of data at a particular point and then read that data at boot. Just six bytes.

How do I do this without the over complication of a file system please?

  • So, trawling through endless posts here, it seems maybe you can't just reference this nvs code and you need to add something to this .dts file thing. I've tried looking through some more example files and looks like maybe you need something like this:

    &flash0 {

        partitions {
            compatible = "fixed-partitions";
            #address-cells = <1>;
            #size-cells = <1>;

            boot_partition: partition@0 {
                label = "mcuboot";
                reg = <0x00000000 0x0000C000>;
            };
            slot0_partition: partition@c000 {
                label = "image-0";
                reg = <0x0000C000 0x00076000>;
            };
            slot1_partition: partition@82000 {
                label = "image-1";
                reg = <0x00082000 0x00076000>;
            };

            /*
             * The flash starting at 0x000f8000 and ending at
             * 0x000fffff is reserved for use by the application.
             */

            /*
             * Storage partition will be used by FCB/LittleFS/NVS
             * if enabled.
             */
            storage_partition: partition@f8000 {
                label = "storage";
                reg = <0x000f8000 0x00008000>;
            };
        };
    };

    But if I put that in mine it stops compiling. Can I even  just take another flash segment from a different project's dts file?

  • I feel like I'm shouting into the void. More investigation seems like maybe I need one of these overlay files? With something like this:

    /delete-node/ &storage_partition;

    &flash0 {
        partitions {
            /* Set 48KB of storage at the beginning of bank2 in order to have 3 sectors smaller than 32K
             *     (nvs.h: uint16_t sector_size)
             */
            storage_partition: partition@100000 {
                label = "storage";
                reg = <0x000100000 DT_SIZE_K(48)>;
            };
        };
    };

    Is that right? If so, which one? I finally worked out how to make a new project with a 52840 using nvs project and the dts file has this:

    &flash0 {
    /*
    * For more information, see:
    */
    partitions {
    compatible = "fixed-partitions";
    #address-cells = <1>;
    #size-cells = <1>;

    boot_partition: partition@0 {
    label = "mcuboot";
    reg = <0x000000000 0xd000>;
    };
    slot0_partition: partition@d000 {
    label = "image-0";
    reg = <0x0000d000 0x30000>;
    };
    slot1_partition: partition@3d000 {
    label = "image-1";
    reg = <0x0003d000 0x1000>;
    };
    storage_partition: partition@3e000 {
    label = "storage";
    reg = <0x0003e000 0x00002000>;
    };
    };
    };

    So could I create a custom overlay file with this in?
    Would I need all the guff or just this:

    storage_partition: partition@3e000 {
    label = "storage";
    reg = <0x0003e000 0x00002000>;

    ?
    I just can't get my head around how my original project doesn't complain
    I don't seem to have
    storage_partition
    defined anywhere. What's the score here?

    Any chance of some guidance here please?
  • A little more shouting into the void. Tried putting this in an overlay file

    /delete-node/ &storage_partition;

    &flash0 {
    partitions {
    /* Set 48KB of storage at the beginning of bank2 in order to have 3 sectors smaller than 32K
    * (nvs.h: uint16_t sector_size)
    */
    storage_partition: partition@f8000 {
    label = "storage";
    reg = <0x000f8000 0x00008000>;
    };
    };
    };
    No difference, still -22 error.
  • Hello, earth calling Nordic. Is there no-one there who can offer some assistance on this? I can see no difference between any of the config files in a plain write to flash demo (which works) and my modified central uart demo (which just returns -22 error. I've spent a week trying to get six bytes written to on-chip flash,this is madness.

  • Hello,

    Presuming you reserve a flash page (either in an dts overlay or using partition manager (if partition manager is enabled the partitioning is done in pm_static.yml file (dts is ignored))).

    Then in theory you should be able to do something like:

    #define FLASH_ADDR 0x000F8000
    
    uint32_t value;
    
    // Read value
    value = *((volatile uint32_t *)FLASH_ADDR);
    
    // Erase page
    (void) nrfx_nvmc_page_erase(FLASH_ADDR);
    
    //Write value
    nrfx_nvmc_word_write(FLASH_ADDR, value);

    Kenneth

Related