Hello,
Hardware: nRF5340DK
SDK: v2.6.1
Context: Creating a prototype using a Load Cell and transmitting its weight on BLE.
Disclaimer: I'm new to the settings subsystem
The problem is that I use personnal settings to store the scale parameters (offset+slope) and the Bluetooth Whitelist on the same project.
Here is the order of main.c execution:
- Load parameters for the scale
- Create my threads (Scale / IHM / UART)
- Create my BLE thread
When I order my program like this and build it using NSPE config (_ns), I get this error (SECURE FAULT).
When I build it using SPE config, I get this error (USAGE FAULT).
When restarting the DK, there is no error. So to reproduce the error a new "west flash" is needed.
When searching for an answer, I found this: Kconfig search (nordicsemi.com)
It stipulates that settings_load() should be called after bt_enable().
I personnally find it unnatural for my use case, because if I want to use another technology to read the weight (UART), I still need to call enable_ble().
I could use something like this: nRF SoC Internal Storage — Zephyr Project Documentation
But I find the settings to better match my needs.
However if there is no solution possible, I might use this last solution.
How to reproduce:
- Get this sample: bt-fund/lesson5/blefund_less5_exer2_solution at main · NordicDeveloperAcademy/bt-fund (github.com)
- Add settings.c
#include "settings.h" #include <string.h> LOG_MODULE_REGISTER(settings_log, LOG_LEVEL_INF); #define CONFIG_SETTINGS_SUBTREE "config" #define CONFIG_SETTINGS_OFFSET_NAME CONFIG_SETTINGS_SUBTREE"/offset" #define CONFIG_SETTINGS_SLOPE_NAME CONFIG_SETTINGS_SUBTREE"/slope" /* Let's create a structure that we want to save */ struct calib_param_t { uint32_t offset; double slope; }; struct calib_param_t my_params; static int settings_handle_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) { const char *next; size_t name_len; int rc; name_len = settings_name_next(name, &next); if (!next) { if (!strncmp(name, "offset", name_len)) { rc = read_cb(cb_arg, &my_params.offset, sizeof(my_params.offset)); return 0; } if (!strncmp(name, "slope", name_len)) { rc = read_cb(cb_arg, &my_params.slope, sizeof(my_params.slope)); return 0; } } return -ENOENT; } SETTINGS_STATIC_HANDLER_DEFINE(my_settings, CONFIG_SETTINGS_SUBTREE, NULL, settings_handle_set, NULL, NULL); int flash_init(){ int err = 0; err = settings_subsys_init(); if (err){ LOG_ERR("Error while initializing the subsys : err = %d", err); return -1; } // Loads the Whitelist for advertising + settings for param err = settings_load(); if (err){ LOG_ERR("Error while loading settings : err = %d", err); return -2; } return err; } /** * GETTERS + SETTERS */ uint32_t get_param_offset(void) { uint32_t offset = my_params.offset; return offset; } int set_param_offset(uint32_t value) { my_params.offset = value; int ret = settings_save_one(CONFIG_SETTINGS_OFFSET_NAME, &value, sizeof(value)); return ret; } double get_param_slope(void) { double slope = my_params.slope; return slope; } int set_param_slope(double value) { my_params.slope = value; int ret = settings_save_one(CONFIG_SETTINGS_SLOPE_NAME, &value, sizeof(value)); return ret; }
- Add settings.h
settings.h - Modify CMakeLists
cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(NONE) FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources}) zephyr_include_directories(src)
- Modify main.c
- include "settings.h"
- define TRIGGER_ERROR 1 (it allows to trigger or not the error
- add this code just before bt_enable in main():
#if TRIGGER_ERROR flash_init(); k_msleep(30); int local_offset = get_param_offset(); local_offset++; set_param_offset(local_offset); printk("offset: %d\n", local_offset); #endif
- After bt_enable(), replace the settings_load() line with
#if TRIGGER_ERROR /* STEP 1.3 - Add setting load function */ settings_load(); #else flash_init(); k_msleep(30); int local_offset = get_param_offset(); local_offset++; set_param_offset(local_offset); printk("offset: %d\n", local_offset); #endif
- Build and flash