Cannot read/write in nvs when gatt,Bt is active?

Hi,

iam trying to write the data in nvs which i give input through GATT.i  can read and write in nvs when not using gatt,BT.

I have 2 codes,1 is working but without GATT and BT.

another 1 is not working with GATT and BT.

Working code prj.conf

CONFIG_FLASH=y

CONFIG_NVS=y
CONFIG_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y
CONFIG_NVS_LOG_LEVEL_DBG=y
CONFIG_REBOOT=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y


CONFIG_FLASH_MAP=y

CONFIG_CJSON_LIB=y

Non working code prj.conf


#
# Copyright (c) 2018 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
#CONFIG_BOOTLOADER_MCUBOOT=n
#CONFIG_SRAM_SIZE=128
#CONFIG_SRAM_BASE_ADDRESS=0x20000000
#CONFIG_FLASH_SIZE=512
#CONFIG_FLASH_BASE_ADDRESS=0x00000000
#CONFIG_NCS_SAMPLES_DEFAULTS=y
CONFIG_BT=y
CONFIG_BT_LBS=y
CONFIG_BT_HCI=y
CONFIG_BT_ID_MAX=1
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
CONFIG_BT_SMP=y
CONFIG_BT_GATT_DYNAMIC_DB=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_SETTINGS_NVS=y
CONFIG_BT_KEYS_OVERWRITE_OLDEST=y


CONFIG_BT_SETTINGS=y
CONFIG_BT_HCI_VS=y
CONFIG_FLASH=y                 # Ensure flash storage is enabled if you are storing settings in flash


CONFIG_BT_GATT_SERVICE_CHANGED=y

CONFIG_BT_GATT_DYNAMIC_DB=y
CONFIG_BT_ATT_ERR_TO_STR=y
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_CTLR_ADVANCED_FEATURES=y
CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL=y
CONFIG_JSON_LIBRARY=y
CONFIG_DK_LIBRARY=y

#CONFIG_PM=y
#CONFIG_PM_SINGLE_IMAGE=y
#CONFIG_PM_LOG_LEVEL_DBG=y
#CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
CONFIG_BT_GATT_DM=y
CONFIG_BT_GATT_DM_DATA_PRINT=y
#CONFIG_BT_ADV_PROV_TX_POWER=y
#bt Update Data Length and MTU
#CONFIG_BT_USER_DATA_LEN_UPDATE=y
#CONFIG_BT_CTLR_DATA_LENGTH_MAX=251

#for import json
CONFIG_CJSON_LIB=y
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_NANO=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
#CONFIG_HEAP_MEM_POOL_SIZE=8192


#MTU update configurations
CONFIG_BT_USER_DATA_LEN_UPDATE=y
#CONFIG_BT_CTLR_DATA_LENGTH_MAX=100
#CONFIG_BT_BUF_ACL_RX_SIZE=251
#CONFIG_BT_BUF_ACL_TX_SIZE=251
#CONFIG_BT_L2CAP_TX_MTU=247
CONFIG_BT_PRIVACY=y


CONFIG_NVS=y
CONFIG_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y
CONFIG_NVS_LOG_LEVEL_DBG=y
CONFIG_REBOOT=y
CONFIG_SETTINGS_NVS=y

CONFIG_FLASH_MAP=y
CONFIG_SETTINGS_NVS_SECTOR_COUNT=3

working code partitions.yml

app:
  address: 0x0
  end_address: 0x78000
  region: flash_primary
  size: 0x78000
nvs_storage:
  address: 0x7a000
  end_address: 0x80000
  placement:
    before:
    - end
  region: flash_primary
  size: 0x6000
settings_storage:
  address: 0x78000
  end_address: 0x7a000
  placement:
    align:
      start: 0x1000
    before:
    - end
  region: flash_primary
  size: 0x2000
sram_primary:
  address: 0x20000000
  end_address: 0x20020000
  region: sram_primary
  size: 0x20000

non working code partitions.yml

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: 0x20020000
  region: sram_primary
  size: 0x20000

while initialise the nvs i got the following error,

[00:00:00.059,661] <err> flash_nrf: invalid address: 0x00080ff8:8
Error: Failed to write JSON data to NVS, rc=-22

in wrking code there is a separate partition for nvs,but in non working code there is not.i use the same build config for both the codes.is it because of enabling the GATT and BT?Please someone guide

  • Hi,

    There is no fundamental issue with wiring to flash and using BLE in the same project. As you use NVS I assume you write to the NVS partition, and that is missing in the non-working partitions.yml. Did you use a static partition layout here, or was it not added? Do you get any information about it in the build log? Can you share that?

  • EDIT:

    yes,I use/add pm_static.yml..my build log

    [0/1] Re-running CMake...
    Loading Zephyr default modules (Zephyr base (cached)).
    -- Application: C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs
    -- CMake version: 3.21.0
    -- Cache files will be written to: C:/ncs/v2.8.0/zephyr/.cache
    -- Zephyr version: 3.7.99 (C:/ncs/v2.8.0/zephyr)
    -- Found west (found suitable version "1.2.0", minimum required is "0.14.0")
    -- Board: nrf52833dk, qualifiers: nrf52833
    -- Found host-tools: zephyr 0.16.8 (C:/ncs/toolchains/2d382dcd92/opt/zephyr-sdk)
    -- Found toolchain: zephyr 0.16.8 (C:/ncs/toolchains/2d382dcd92/opt/zephyr-sdk)
    -- Found BOARD.dts: C:/ncs/v2.8.0/zephyr/boards/nordic/nrf52833dk/nrf52833dk_nrf52833.dts
    -- Generated zephyr.dts: C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/build/tx_chaneg_wout_nvs/zephyr/zephyr.dts
    -- Generated devicetree_generated.h: C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/build/tx_chaneg_wout_nvs/zephyr/include/generated/zephyr/devicetree_generated.h
    -- Including generated dts.cmake file: C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/build/tx_chaneg_wout_nvs/zephyr/dts.cmake

    warning: PM (defined at C:/ncs/v2.8.0/zephyr/soc/silabs\silabs_s2\efr32bg22\Kconfig.defconfig:12,
    C:/ncs/v2.8.0/zephyr/soc/silabs\silabs_s2\efr32bg27\Kconfig.defconfig:12,
    C:/ncs/v2.8.0/zephyr/soc/silabs\silabs_s2\efr32mg24\Kconfig.defconfig:12,
    C:/ncs/v2.8.0/zephyr/soc/renesas/smartbond\da1469x\Kconfig.defconfig:39, subsys/pm/Kconfig:13) was
    assigned the value 'y' but got the value 'n'. Check these unsatisfied dependencies:
    (SOC_SERIES_EFR32BG22 || SOC_SERIES_EFR32BG27 || SOC_SERIES_EFR32MG24 || (SOC_SERIES_DA1469X &&
    SOC_FAMILY_RENESAS_SMARTBOND) || (SYS_CLOCK_EXISTS && HAS_PM)) (=n). See
    docs.zephyrproject.org/.../kconfig.html and/or look up PM in the
    menuconfig/guiconfig interface. The Application Development Primer, Setting Configuration Values,
    and Kconfig - Tips and Best Practices sections of the manual might be helpful too.


    warning: BT_CTLR_ADVANCED_FEATURES (defined at subsys/bluetooth\controller\Kconfig.ll_sw_split:292)
    was assigned the value 'y' but got the value 'n'. Check these unsatisfied dependencies:
    BT_LL_SW_SPLIT (=n). See
    docs.zephyrproject.org/.../kconfig.html and/or look up
    BT_CTLR_ADVANCED_FEATURES in the menuconfig/guiconfig interface. The Application Development Primer,
    Setting Configuration Values, and Kconfig - Tips and Best Practices sections of the manual might be
    helpful too.


    warning: The choice symbol PM_LOG_LEVEL_DBG (defined at
    subsys/logging/Kconfig.template.log_config:20) was selected (set =y), but no symbol ended up as the
    choice selection. See docs.zephyrproject.org/.../kconfig.html
    and/or look up PM_LOG_LEVEL_DBG in the menuconfig/guiconfig interface. The Application Development
    Primer, Setting Configuration Values, and Kconfig - Tips and Best Practices sections of the manual
    might be helpful too.


    warning: Experimental symbol BT_ATT_ERR_TO_STR is enabled.

    Parsing C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/Kconfig
    Loaded configuration 'C:/ncs/v2.8.0/zephyr/boards/nordic/nrf52833dk/nrf52833dk_nrf52833_defconfig'
    Merged configuration 'C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/prj.conf'
    Merged configuration 'C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/build/tx_chaneg_wout_nvs/zephyr/.config.sysbuild'
    No change to configuration in 'C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/build/tx_chaneg_wout_nvs/zephyr/.config'
    No change to Kconfig header in 'C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/build/tx_chaneg_wout_nvs/zephyr/include/generated/zephyr/autoconf.h'
    -- Configuring done
    -- Generating done
    -- Build files have been written to: C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/build/tx_chaneg_wout_nvs
    [7/7] Linking C executable zephyr\zephyr.elf
    Memory region         Used Size  Region Size  %age Used
               FLASH:      206244 B       504 KB     39.96%
                 RAM:       29240 B       128 KB     22.31%
            IDT_LIST:          0 GB        32 KB      0.00%
    Generating files from C:/Users/Sara/Desktop/NRF/tx_chaneg_wout_nvs/build/tx_chaneg_wout_nvs/zephyr/zephyr.elf for board: nrf52833dk
     *  Terminal will be reused by tasks, press any key to close it.

    pm_static.yml below,

    app:
      address: 0x0
      end_address: 0x78000
      region: flash_primary
      size: 0x78000
    nvs_storage:
      address: 0x7a000
      end_address: 0x80000
      placement:
        before:
        - end
      region: flash_primary
      size: 0x6000
    settings_storage:
      address: 0x78000
      end_address: 0x7a000
      placement:
        align:
          start: 0x1000
        before:
        - end
      region: flash_primary
      size: 0x2000
    sram_primary:
      address: 0x20000000
      end_address: 0x20020000
      region: sram_primary
      size: 0x20000
  • Hi,

    Thank you. I wonder why the settings_storage region is not part of the Bleutooth application in this case. Can you send the generated .config for both your non-working (bluetooth) application and the working one? Or perhaps the full build folders for both so that I can have a look?

  • working build 1581.build.zip

    non working non working build.zip

    in non working code,error is below while initialise the nvs.........

    [00:00:00.017,883] <dbg> fs_nvs: nvs_recover_last_ate: Recovering last ate from sector 0
    [00:00:00.032,897] <dbg> fs_nvs: nvs_add_gc_done_ate: Adding gc done ate at ff0
    [00:00:00.041,503] <inf> fs_nvs: 2 Sectors of 4096 bytes
    [00:00:00.047,546] <inf> fs_nvs: alloc wra: 0, fe8
    [00:00:00.053,070] <inf> fs_nvs: data wra: 0, 0
    [00:00:00.058,380] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision:
                                                fe 2c f9 6a 7f 36 22 2e  a0 79 c0 40 be 2c 03 20 |.,.j.6". .y.@.,.
                                                40 c2 f3 32                                      |@..2             
    [00:00:00.090,728] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
    [00:00:00.098,907] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
    [00:00:00.120,819] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 254.63788 Build 573996906
    [00:00:00.132,934] <inf> bt_hci_core: No ID address. App must call settings_load()
    Bluetooth initialized
    [00:00:00.143,341] <dbg> fs_nvs: nvs_recover_last_ate: Recovering last ate from sector 2
    [00:00:00.156,921] <inf> fs_nvs: GC Done marker found
    [00:00:00.162,658] <dbg> fs_nvs: nvs_flash_erase_sector: Erasing flash at 78000, len 4096
    [00:00:00.260,711] <inf> fs_nvs: 3 Sectors of 4096 bytes
    [00:00:00.266,784] <inf> fs_nvs: alloc wra: 2, fe0
    [00:00:00.272,277] <inf> fs_nvs: data wra: 2, 24
    [00:00:00.277,679] <err> os: ***** MPU FAULT *****
    [00:00:00.283,416] <err> os:   Stacking error (context area might be not valid)
    [00:00:00.291,748] <err> os:   Data Access Violation
    [00:00:00.297,668] <err> os:   MMFAR Address: 0x20005abc
    [00:00:00.303,985] <err> os: r0/a1:  0x1dc9c047  r1/a2:  0xe1eb6cf1  r2/a3:  0xe5622bcc
    [00:00:00.313,140] <err> os: r3/a4:  0x4eac21c5 r12/ip:  0x7b07b752 r14/lr:  0x7fd4bf01
    [00:00:00.322,265] <err> os:  xpsr:  0xe9f43a00
    [00:00:00.327,789] <err> os: Faulting instruction address (r15/pc): 0x3adafb9e
    [00:00:00.336,059] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
    [00:00:00.344,360] <err> os: Current thread: 0x20002b78 (unknown)
    [00:00:00.351,501] <err> os: Halting system

  • Hi,

    Looking at the configs I notice that the non-working does not have CONFIG_PM_PARTITION_SIZE_NVS_STORAGE defined. Is the issue resolve by adding CONFIG_PM_PARTITION_SIZE_NVS_STORAGE=0x6000 to your prj.conf? This is skipped by default when using settings.

Related