This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

52840 NVS inconsistent storage when writing custom data + using BLE bonding

Hi Nordic,

NCS v1.8.0

Controller IC 1: Thingy91 - NRF52840

Sensor IC: Laird BT510 - NRF52840

Intro: I currently have 2 devices. A sensor and a controller. For the sensor I have chosen the Laird BT510 based on the nrf52840 IC and for the controller we have chosen to base this around the Thingy91. In this situation I have found that both the Sensor and Controller are suffering from the same thing, though the sensor outside of the development environment will never really be subject to changing its NVS.

What I am finding is when I bond the controller that previously has a bonding with another device. I initially remove the old bonding, immediately clear a section of old data containing information about the old pairing for my user app. Store the newly cleared data overwriting the old and then begin pairing/bonding with the new device.

BT_SCAN_CB_INIT(scan_cb, scan_filter_match, NULL, scan_connecting_error, scan_connecting);

static void scan_filter_match(struct bt_scan_device_info *device_info, struct bt_scan_filter_match *filter_match, bool connectable) {
....
    const int STORAGE_ADDRESS_ID = 0x11;    //randomly chosen
    bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY);   //unbond old device
    memset(&appData.Storage, 0, sizeof(appData.Storage));   //reset old IDs
    int wrote = nvs_write(&fs, STORAGE_ADDRESS_ID, data, sizeof(Storage_t)); //store new IDs
    int err = bt_conn_le_create(device_info->recv_info->addr, BT_CONN_LE_CREATE_CONN, device_info->conn_param, &conn);  //create connection to filtered device
....
}
I have found that by interacting with the NVS in this way. Sometimes the old bonded device never gets cleared, or more specifically after bonding the new device, it still contains the old device information. Then when I write to the NVS to update the user data, I actually loop through 5 times and find that it usually takes 3 loops to successfully store the data as intended.
for (int i = 0; i < force; i++) {
	int wrote = nvs_write(&fs, STORAGE_ADDRESS_ID, data, sizeof(Storage_t));
	if (wrote == 0) {
		sprintf(buf, "Config success after %d attempts\n", i + 1);
		Debug_Write(buf);
		return true;
	}
	else if (wrote < 0) {
		sprintf(buf, "Config err, got %d (0x%x). \n", wrote, wrote);
		Debug_Write(buf);
		return false;
	}
	else if (wrote != sizeof(Storage_t)) {
		sprintf(buf, "Config err: unable to write all data, wrote %d/%d\n", wrote, sizeof(Storage_t));
		Debug_Write(buf);
		return false;
	}
}
I have previously seen this on my Laird sensor. Here I found that when sending updated configurations from the controller -> Sensor that it would often take me several writes before it would be stored. This was of course when it was not interacting with the bonded information at all.
My Thingy91 Controller firmware is based on the "bridge" example and simply extended. My prj.conf is as follows:
# General
CONFIG_REBOOT=y
CONFIG_HEAP_MEM_POOL_SIZE=8192
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
CONFIG_MAIN_STACK_SIZE=2048
CONFIG_GPIO=y
CONFIG_POLL=y
CONFIG_BOOTLOADER_MCUBOOT=n
CONFIG_EVENT_MANAGER=y
CONFIG_LINKER_ORPHAN_SECTION_PLACE=y
CONFIG_SPEED_OPTIMIZATIONS=y
CONFIG_PM_DEVICE=y

# Features
CONFIG_BRIDGE_CDC_ENABLE=y

 # USB
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_MANUFACTURER="Nordic Semiconductor"
CONFIG_USB_DEVICE_PRODUCT="Thingy:91 UART"
CONFIG_USB_DEVICE_VID=0x1915
CONFIG_USB_DEVICE_PID=0x9100
CONFIG_USB_DEVICE_SN="THINGY91 12PLACEHLDRS" # This is overridden at runtime
CONFIG_USB_COMPOSITE_DEVICE=y
CONFIG_USB_CDC_ACM_RINGBUF_SIZE=16384

# Disk and file system
#CONFIG_DISK_ACCESS=y
#CONFIG_DISK_DRIVER_RAM=y
#CONFIG_DISK_RAM_VOLUME_SIZE=64
#CONFIG_FILE_SYSTEM=y
#CONFIG_FAT_FILESYSTEM_ELM=y
#CONFIG_FS_FATFS_LFN=y

# Settings
#CONFIG_FCB=y

CONFIG_BT_SETTINGS=y
#CONFIG_SETTINGS_FCB=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_NVS=y
CONFIG_NVS=y
#CONFIG_MPU_ALLOW_FLASH_WRITE=y

# Logging
CONFIG_LOG=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_LOG_BACKEND_RTT=y
CONFIG_LOG_BACKEND_RTT_MODE_DROP=y

# Event logs
CONFIG_BRIDGE_LOG_MODULE_STATE_EVENT=n
CONFIG_BRIDGE_LOG_UART_DATA_EVENT=n
CONFIG_BRIDGE_LOG_CDC_DATA_EVENT=n
CONFIG_BRIDGE_LOG_BLE_DATA_EVENT=n
CONFIG_BRIDGE_LOG_BLE_CTRL_EVENT=n
CONFIG_BRIDGE_LOG_PEER_CONN_EVENT=n
CONFIG_BRIDGE_LOG_FS_EVENT=n
CONFIG_BRIDGE_LOG_POWER_DOWN_EVENT=n

 # UART
CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_0_INTERRUPT_DRIVEN=n
CONFIG_UART_1_INTERRUPT_DRIVEN=n
CONFIG_UART_LINE_CTRL=y
CONFIG_UART_ASYNC_API=y
CONFIG_UART_0_ASYNC=y
CONFIG_UART_1_ASYNC=y
CONFIG_UART_0_NRF_HW_ASYNC=y
CONFIG_UART_1_NRF_HW_ASYNC=y
CONFIG_UART_0_NRF_HW_ASYNC_TIMER=1
CONFIG_UART_1_NRF_HW_ASYNC_TIMER=2
CONFIG_NRFX_UARTE=y
CONFIG_NRFX_UARTE0=y
CONFIG_NRFX_UARTE1=y
CONFIG_NRFX_TIMER=y
CONFIG_NRFX_TIMER1=y
CONFIG_NRFX_TIMER2=y
CONFIG_NRFX_PPI=y

# BLE
CONFIG_BT=y
#CONFIG_BT_DEVICE_NAME="Thingy:91 UART"
#CONFIG_BT_DEVICE_NAME_DYNAMIC=y
#CONFIG_BT_PERIPHERAL=y
#CONFIG_BT_PERIPHERAL_PREF_MIN_INT=6
#CONFIG_BT_PERIPHERAL_PREF_MAX_INT=16
#CONFIG_BT_PERIPHERAL_PREF_LATENCY=0
#CONFIG_BT_PERIPHERAL_PREF_TIMEOUT=400
CONFIG_BT_BONDABLE=y
#CONFIG_BT_LL_SOFTDEVICE=y
CONFIG_BT_AUTO_PHY_UPDATE=y
CONFIG_BT_DATA_LEN_UPDATE=y
CONFIG_BT_HCI_ACL_FLOW_CONTROL=y
CONFIG_BT_L2CAP_TX_MTU=247
CONFIG_BT_BUF_ACL_RX_SIZE=251
#CONFIG_BT_NUS=y
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_SMP=y
CONFIG_BT_CTLR=y
CONFIG_BT_CTLR_RX_BUFFERS=10
CONFIG_BT_BUF_ACL_TX_COUNT=10
CONFIG_BT_BUF_ACL_TX_SIZE=251
CONFIG_BT_CTLR_DATA_LENGTH_MAX=251

#can't force level 4 only because weather station won't be
#CONFIG_BT_SMP_SC_ONLY=y
CONFIG_BT_DEBUG_LOG=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_GATT_DM=y

CONFIG_BT_MAX_CONN=2
CONFIG_BT_SCAN=y
CONFIG_BT_SCAN_FILTER_ENABLE=y
CONFIG_BT_SCAN_UUID_CNT=1
CONFIG_BT_SCAN_ADDRESS_CNT=2
CONFIG_BT_SCAN_NAME_CNT=1

#CONFIG_SENSOR=y
#CONFIG_LIS2DH=y
CONFIG_I2C_NRFX=y
CONFIG_I2C=y
and my Laird (Sensor) was originally based on the heart rate example to I could pivot off the working bonding security lvl 4 work.
#
# Copyright (c) 2018 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
CONFIG_NCS_SAMPLES_DEFAULTS=y

CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEVICE_NAME="Neo Trigger"

# Enable the LBS service
CONFIG_BT_LBS=n
CONFIG_DK_LIBRARY=n

CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048

CONFIG_BT_SMP=y
CONFIG_BT_SMP_SC_ONLY=y
CONFIG_BT_FIXED_PASSKEY=y

CONFIG_BT_REMOTE_INFO=y

# CONFIG_BT_DCS_LOG_LEVEL_OFF is not set
# CONFIG_BT_DCS_LOG_LEVEL_ERR is not set
# CONFIG_BT_DCS_LOG_LEVEL_WRN is not set
# CONFIG_BT_DCS_LOG_LEVEL_INF=y
CONFIG_BT_DCS_LOG_LEVEL_DBG=y

CONFIG_ADC=y
CONFIG_PM=y
# Required to disable default behavior of deep sleep on timeout
CONFIG_PM_DEVICE=y
CONFIG_GPIO=y
#CONFIG_BT_PRIVACY=y

#console related
CONFIG_NEWLIB_LIBC=y
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_HANDLER=y
CONFIG_CONSOLE_GETCHAR=y
CONFIG_CONSOLE_GETCHAR_BUFSIZE=256

#floating point support for debug messages
CONFIG_CBPRINTF_FP_SUPPORT=y

#accelerometer value
CONFIG_SENSOR=y
CONFIG_LIS2DH=y

CONFIG_SI7055=y
CONFIG_SI7055_ENABLE_CHECKSUM=y
I hope you're able to just tell me that I am doing something wrong or my stack size isn't big enough and its not some fundemental bug!
Looking forward to your reply,
Ryan.
Parents Reply Children
No Data
Related