Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot initiate nvs in NRF52840 dongle #83037

Open
Saranya-karan opened this issue Dec 16, 2024 · 11 comments
Open

cannot initiate nvs in NRF52840 dongle #83037

Saranya-karan opened this issue Dec 16, 2024 · 11 comments
Assignees
Labels
bug The issue is a bug, or the PR is fixing a bug platform: nRF Nordic nRFx priority: low Low impact/importance bug

Comments

@Saranya-karan
Copy link

Saranya-karan commented Dec 16, 2024

iam new to nrf52840 DONGLE.i want to initiate nvs and read/write in it.
but it kept on failed.my main.c

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/fs/nvs.h>
#include <cJSON.h>
#include <string.h>
#include <zephyr/drivers/flash.h>

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 JSON_DATA_ID 10
#define SLEEP_TIME   2000  // 2 seconds

void store_json_data(struct nvs_fs *fs) {
    cJSON *root = cJSON_CreateObject();

    // Add actual numeric values
    cJSON_AddStringToObject(root, "name", "Bluetooth");

	char tx_power_str[16]; 
    sprintf(tx_power_str, "%d", -40);  // Example TX power value as string
    cJSON_AddStringToObject(root, "tx_power", tx_power_str);

    const char *json_string = cJSON_PrintUnformatted(root);
    printk("JSON data stored: %s\n", json_string);

    // Write to NVS
    int rc = nvs_write(fs, JSON_DATA_ID, json_string, strlen(json_string) + 1);
    if (rc < 0) {
        printk("Error: Failed to write JSON data to NVS, rc=%d\n", rc);
    } else {
        printk("JSON data successfully written to NVS\n");
    }

    cJSON_Delete(root);
}

#include <stdlib.h>  // For strtol

void read_json_data(struct nvs_fs *fs) {
    char json_buffer[256]; 
    
    int rc = nvs_read(fs, JSON_DATA_ID, json_buffer, sizeof(json_buffer));
    
    if (rc > 0) {
        json_buffer[rc] = '\0';  // Null-terminate the string
        printk("JSON data read: %s\n", json_buffer);

        cJSON *root = cJSON_Parse(json_buffer);
        if (root != NULL) {
            const cJSON *name = cJSON_GetObjectItem(root, "name");
            const cJSON *tx_power_str = cJSON_GetObjectItem(root, "tx_power");

            if (name && tx_power_str) {
                printk("Parsed JSON: name=%s, tx_power=%s\n",
                       name->valuestring,  
                       tx_power_str->valuestring 
                       );

                // Convert tx_power and battery_level to integers using strtol
                char *endptr;
                long tx_power = strtol(tx_power_str->valuestring, &endptr, 10);  // Convert to integer
              

                printk("Converted values: tx_power=%ld\n", tx_power);
            } else {
                printk("Error: JSON parsing error - missing fields\n");
            }

            cJSON_Delete(root);
        } else {
            printk("Error: Failed to parse JSON data\n");
        }
    } else if (rc == 0) {
        printk("Warning: No JSON data found in NVS\n");
    } else {
        printk("Error: Failed to read JSON data, rc=%d\n", rc);
    }
}

int main(void) {
    int rc;
    struct flash_pages_info info;

    // Initialize NVS file system
    fs.flash_device = NVS_PARTITION_DEVICE;
    if (!device_is_ready(fs.flash_device)) {
        printk("Error: Flash device not ready\n");
        return -1;
    }

    fs.offset = NVS_PARTITION_OFFSET;
    rc = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &info);
    if (rc) {
        printk("Error: Failed to get page info, rc=%d\n", rc);
        return -1;
    }

    fs.sector_size = info.size;
    fs.sector_count = 3U;

    rc = nvs_mount(&fs);
    if (rc) {
        printk("Error: Failed to mount NVS, rc=%d\n", rc);
        return -1;
    }

    // Store and read JSON data
    store_json_data(&fs);
    read_json_data(&fs);

    while (1) {
        k_msleep(SLEEP_TIME);
    }

    return 0;
}

prj.conf

CONFIG_FLASH=y
CONFIG_LOG=y
CONFIG_UART_CONSOLE=y
CONFIG_CONSOLE=y
CONFIG_PM=y
CONFIG_PM_LOG_LEVEL_DBG=y

CONFIG_NVS=y

CONFIG_LOG_MODE_IMMEDIATE=y
CONFIG_NVS_LOG_LEVEL_DBG=y
CONFIG_REBOOT=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y

CONFIG_FLASH_MAP=y
CONFIG_USB_DEVICE_STACK=y

CONFIG_CJSON_LIB=y

@Saranya-karan Saranya-karan added the bug The issue is a bug, or the PR is fixing a bug label Dec 16, 2024
Copy link

Hi @Saranya-karan! We appreciate you submitting your first issue for our open-source project. 🌟

Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. 🤖💙

@Laczen
Copy link
Collaborator

Laczen commented Dec 16, 2024

Hi @Saranya-karan, this will probably be more a discussion than a bug. In the issue you don't mention what is failing in main. Is it the mounting, or something else ? What error code is returned?

@Saranya-karan
Copy link
Author

Saranya-karan commented Dec 16, 2024

Hi @Saranya-karan, this will probably be more a discussion than a bug. In the issue you don't mention what is failing in main. Is it the mounting, or something else ? What error code is returned?

i really dont know.in build there is no errors.after uploading hex file through programmer tool,i cannot open the uart port in putty(so cannot see the error log). when uploading other code related to BT,GATT..i can open the PORT in putty.

i need the guidance..if anyother way to see the errors ...also why my nvs not getting initiated

@Saranya-karan
Copy link
Author

Hi @Saranya-karan, this will probably be more a discussion than a bug. In the issue you don't mention what is failing in main. Is it the mounting, or something else ? What error code is returned?

the same code i already worked with NRF52833DK, it worked file

@Laczen
Copy link
Collaborator

Laczen commented Dec 16, 2024

I can't comment on how to debug firmware on the nrf52840 dongle (I don't have such a device). Maybe adding some delay (10s) to the start of main will allow getting print info. Also instead of returning -1 you could stay in main and print the error in the while loop every 2 seconds.

@kartben
Copy link
Collaborator

kartben commented Dec 17, 2024

@Saranya-karan would you be able to bisect this to figure out when your code stopped working? Thanks!

@kartben kartben added the priority: low Low impact/importance bug label Dec 17, 2024
@Laczen
Copy link
Collaborator

Laczen commented Dec 17, 2024

@Saranya-karan would you be able to bisect this to figure out when your code stopped working? Thanks!

I don't think it ever worked, the author is referring to a different device. The nrf52480 dongle is a bit of an annoying device as most users will want to use the usb to debug. This takes some time to get up and as the authors code is direct returning on error there might never be anything visible.

I think the problem is related to a storage partition that already contains data. Doing a partial erase on these devices might also be a problem.

@nordic-piks nordic-piks assigned de-nordic and unassigned anangl and masz-nordic Jan 16, 2025
@nordic-piks nordic-piks moved this from To triage to Backlog in nRF platform Jan 16, 2025
@otzy
Copy link

otzy commented Feb 28, 2025

The problem is the address of the storage partition. It does not take into account that all addresses on the dongle are 0x1000 shifted because of MBR.
I had the same problem and solved it by fixing zephyr/boards/nordic/nrf52840dongle/fstab-stock.dtsi

               slot1_partition: partition@76000 {
			label = "image-1";
			reg = <0x00076000 0x00066000>;
		};
		storage_partition: partition@db000 {
			label = "storage";
			reg = <0x000db000 0x00004000>;
		};

namely the size of slot1_partition was reduced and the start of the storage changed to 0xdb000

@de-nordic
Copy link
Collaborator

Referring to the solution by @otzy: I do not think that the default partitioning in the fstab-stock.dtsi is correct in the tree.
The dongle is by default loaded with the bootloader, which is even mentioned in the dtsi, so the default partition layout should account fr all partitions that are already used, and have them defined, because by default a Zephyr app will be built to be placed after the NRF bootloader.
This is a problem I have probably mentioned more than once, in general, that some configuration use start offset provided via Kconfig and some by zephyr,code-partition to position application, which is problematic, specifically if app uses other partitions in DTS, because linker, for app using Kconfig only, will not be aware of that and may allow app to cross into other partitions, as it only sees flash size as boundary.

@otzy
Copy link

otzy commented Mar 3, 2025

Hello, I have another problem. After I change the optimization level, it does not work again.
I changed from debug to optimize for speed, it stopped working. I thought it is somehow related to this and switched back to the Optimize for debugging. It is still not working.
The addresses in the compiled device tree are correct:

                                        slot1_partition: partition@76000 {
                                                label = "image-1";
                                                reg = <0x00076000 0x00065000>;
                                        };

                                        storage_partition: partition@db000 {
                                                label = "storage";
                                                reg = <0x000db000 0x00004000>;
                                        };

But the device works only if I set CONFIG_SETTINGS=n

Is there anything I forgot/missed to change?

UPDATE:
somehow sysbuild option activated and enabled Partition Manager, which is doing it's own magic

@de-nordic
Copy link
Collaborator

UPDATE: somehow sysbuild option activated and enabled Partition Manager, which is doing it's own magic

Yeah, When using sdk-nrf, not upstream Zephyr, the Partition Manager may be enabled by default for some of configurations, like building app with MCUboot child image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug, or the PR is fixing a bug platform: nRF Nordic nRFx priority: low Low impact/importance bug
Projects
Status: Backlog
Development

No branches or pull requests

8 participants