Bluetooth: Peripheral Memfault Diagnostic Service (MDS)
The Peripheral Memfault Diagnostic Service sample demonstrates how to use the Memfault Diagnostic Service (MDS) with the Memfault SDK in an nRF Connect SDK Bluetooth application to collect core dumps and metrics. The Memfault diagnostic data is sent through a Bluetooth gateway.
To get started with Memfault integration in nRF Connect SDK, see nRF Cloud powered by Memfault integration.
Requirements
The sample supports the following development kits:
Hardware platforms |
PCA |
Board name |
|
|---|---|---|---|
nRF54LV10 DK |
PCA10188 |
|
|
PCA10184 |
|
||
nRF54LC10 DK |
PCA10226 |
nrf54lc10dk |
|
PCA10156 |
|
||
PCA10156 |
|
||
PCA10156 |
|
||
PCA10095 |
|
||
PCA10040 |
|
||
PCA10056 |
|
||
PCA10100 |
|
For more security, it is recommended to use the */ns variant of the board target.
When built for this variant, the sample is configured to compile and run as a non-secure application using security by separation.
Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.
Overview
In this sample, the Memfault SDK is used as a module in the nRF Connect SDK to collect core dumps, reboot reasons, metrics, and trace events from devices and send them through a Bluetooth gateway to the Memfault cloud. See Memfault terminology for more details on the various Memfault concepts. The sample also includes the BAS functionalities.
Metrics
The sample shows how to capture user-specific metrics. It defines the following metrics:
button_press_count- The number of Button 3 presses.battery_soc_pct- The simulated battery level.button_elapsed_time_ms- The time measured between two Button 1 presses.
These metrics are defined in the samples/bluetooth/peripheral_mds/memfault_config/memfault_metrics_heartbeat_config.def file.
For more details about the metrics, see Memfault: Collecting Device Metrics.
There are also metrics that are specific to nRF Connect SDK. Memfault adds these system proprietary metrics. The following metrics are enabled by default in this sample:
Bluetooth metrics, enabled and disabled using the
CONFIG_MEMFAULT_NCS_BT_METRICSKconfig option.ncs_bt_connection_count- Number of Bluetooth connections.ncs_bt_connection_time_ms- Bluetooth connection time.Time with at least one live Bluetooth connection.
ncs_bt_bond_count- Number of Bluetooth bonds.Stack usage metrics shows the free stack space in bytes. Configurable by the
CONFIG_MEMFAULT_NCS_STACK_METRICSKconfig option.ncs_bt_rx_unused_stack- HCI RX thread stack.ncs_bt_tx_unused_stack- HCI TX thread stack.
The sample shows how to capture user-specific metrics. It defines the following metrics:
button_press_count- The number of Button 2 presses.battery_soc_pct- The simulated battery level.button_elapsed_time_ms- The time measured between two Button 0 presses.
These metrics are defined in the samples/bluetooth/peripheral_mds/memfault_config/memfault_metrics_heartbeat_config.def file.
For more details about the metrics, see Memfault: Collecting Device Metrics.
There are also metrics that are specific to nRF Connect SDK. Memfault adds these system proprietary metrics. The following metrics are enabled by default in this sample:
Bluetooth metrics, enabled and disabled using the
CONFIG_MEMFAULT_NCS_BT_METRICSKconfig option.ncs_bt_connection_count- Number of Bluetooth connections.ncs_bt_connection_time_ms- Bluetooth connection time.Time with at least one live Bluetooth connection.
ncs_bt_bond_count- Number of Bluetooth bonds.Stack usage metrics shows the free stack space in bytes. Configurable by the
CONFIG_MEMFAULT_NCS_STACK_METRICSKconfig option.ncs_bt_rx_unused_stack- HCI RX thread stack.ncs_bt_tx_unused_stack- HCI TX thread stack.
Error tracking with trace events
The sample implements the following user-defined trace reason for demonstration purposes:
button_state_changed - Collected every time when Button 2 changes its state.
The trace events are defined in the samples/bluetooth/peripheral_mds/memfault_config/memfault_trace_reason_user_config.def file.
See Memfault: Error Tracking with Trace Events for more details about trace events.
The sample implements the following user-defined trace reason for demonstration purposes:
button_state_changed - Collected every time when Button 1 changes its state.
The trace events are defined in the samples/bluetooth/peripheral_mds/memfault_config/memfault_trace_reason_user_config.def file.
See Memfault: Error Tracking with Trace Events for more details about trace events.
Core dumps
You can trigger core dumps in this sample using the following methods:
Button 4 - Triggers a hardfault exception by division by zero.
mfl crashshell command - Triggers an assertion fail.
When a fault occurs, it results in crashes that are captured by Memfault. After your development kit reboots and reconnects with the Bluetooth gateway, it sends core dump data to the Memfault cloud for further inspection and analysis.
You can trigger core dumps in this sample using the following methods:
Button 3 - Triggers a hardfault exception by division by zero.
mfl crashshell command - Triggers an assertion fail.
When a fault occurs, it results in crashes that are captured by Memfault. After your development kit reboots and reconnects with the Bluetooth gateway, it sends core dump data to the Memfault cloud for further inspection and analysis.
Memfault shell
The Bluetooth MDS sample enables a shell interface by default. You can use it instead of the development kit buttons to trigger a separate event.
For a list of available commands, see Memfault Demo CLI.
User interface
The sample supports a simple user interface. You can control the sample using predefined buttons, while LEDs are used to display information.
- LED 1:
Blinks, toggling on/off every second, when the main loop is running and the device is advertising.
- LED 2:
Lit when the development kit is connected.
- Button 1:
Press this button to start time measuring. The second press stops time measuring.
- Button 2:
Triggers the
button_state_changedtrace event.- Button 3:
Every press of this button is counted under the
button_press_countmetric.- Button 4:
Simulate a development kit crash by triggering a hardfault exception by division by zero.
- LED 0:
Blinks, toggling on/off every second, when the main loop is running and the device is advertising.
- LED 1:
Lit when the development kit is connected.
- Button 0:
Press this button to start time measuring. The second press stops time measuring.
- Button 1:
Triggers the
button_state_changedtrace event.- Button 2:
Every press of this button is counted under the
button_press_countmetric.- Button 3:
Simulate a development kit crash by triggering a hardfault exception by division by zero.
Configuration
See Configuring and building for information about how to permanently or temporarily change the configuration.
The Memfault SDK allows configuring some of its options using Kconfig.
For the options not configurable using Kconfig, use the samples/bluetooth/peripheral_mds/memfault_config/memfault_platform_config.h file.
See Memfault SDK for more information.
To send data to the Memfault cloud through a Bluetooth gateway, you must configure a project key using the CONFIG_MEMFAULT_NCS_PROJECT_KEY Kconfig option.
You can find your project key in the project settings at Memfault Dashboards.
Use the CONFIG_MEMFAULT_NCS_DEVICE_ID Kconfig option to set a static device ID.
For this sample, the device ID is ncs-ble-testdevice by default.
Building and running
This sample can be found under samples/bluetooth/peripheral_mds in the nRF Connect SDK folder structure.
For more security, it is recommended to use the */ns variant of the board target (see the Requirements section above.)
When built for this variant, the sample is configured to compile and run as a non-secure application using security by separation.
Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.
To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.
Note
When building repository applications in the SDK repositories, building with sysbuild is enabled by default.
If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.
When building this sample with Sysbuild for an SoC that has a network core, the IPC radio firmware is automatically applied to the build.
The IPC radio is one of the companion components in the nRF Connect SDK and allows to use the radio peripheral from another core in a multicore device.
If needed, you can modify the IPC radio configuration in the prj.conf source file in the sample’s sysbuild/ipc_radio directory.
Testing
Before testing, ensure that your device is configured with the project key of your Memfault project.
After programming the sample to your development kit, complete the following steps to test it:
Testing with nRF Memfault mobile applications
You can use the nRF Connect Device Manager application for testing the Memfault Diagnostic Service (MDS). You can also use it for your custom applications using the Memfault Diagnostic Service.
Open a serial port connection to the kit using a terminal emulator that supports VT100/ANSI escape characters (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.
Reset your development kit.
Observe that the sample starts.
On your mobile phone with access to the Internet, open the nRF Connect Device Manager application and allow it a moment to scan for devices.
Select your device from the list. If you use the settings defined in the
prj.conffile of this sample, it is titled “Nordic_Memfault.” If your device requires a PIN for a pairing request, it is visible through a serial terminal connection. If you are having trouble connecting to your device, forget the device’s entry from your phone’s Bluetooth settings and try again.Once connected, navigate to the Logs and Stats or Diagnostics tab (labeled differently depending on your platform). You should see “Status: Awaiting New Chunks” or a message such as “Uploaded: 2 chunk(s), 152 bytes” (or both).
Upload the symbol file generated from your build to your Memfault account so that the information from your application can be parsed. The symbol file is located in the build folder:
peripheral_mds/build/peripheral_mds/zephyr/zephyr.elf:Open Memfault in a web browser.
Log in to your account and select the project you created earlier.
Navigate to Fleet > Devices in the left side menu. You can see your newly connected device and the software version in the list.
Select the software version number for your device and click Upload to upload the symbol file.
Return to the terminal and press Tab to confirm that the Memfault shell is working. The shell commands available are displayed.
To learn about the Memfault shell commands, issue command
mflt help.Use the buttons to trigger Memfault crashes, traces and metrics collection.
See User interface for details about button functions.
Explore the Memfault user interface to see the errors and metrics sent from your device.
When you have finished collecting diagnostic data, tap Disconnect to close the connection with your development kit.
As the bond information is preserved, you can tap Connect again to immediately reconnect to the device.
Testing with MDS BLE gateway script
Open a serial port connection to the kit using a terminal emulator that supports VT100/ANSI escape characters (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.
Reset your development kit.
Observe that the sample starts.
Run the following command in the nRF Connect SDK root directory to install the MDS BLE gateway script dependencies:
pip install --user -r scripts/memfault/requirements.txtConnect the nRF52 development kit to your PC that uses the MDS BLE gateway script.
Start the
mds_ble_gateway.pyscript with the correct parameters, for example:python3 mds_ble_gateway.py --snr 682900407 --com COM0Wait for the script to establish a connection with your development kit.
Upon connection, data already collected by the Memfault SDK is forwarded to the cloud for further analysis. When connected, the new data is periodically transferred to the cloud with the interval configured in the
CONFIG_BT_MDS_DATA_POLL_INTERVALKconfig option.On the terminal running the script, you can observe the Memfault chunk counter:
Sending.. Forwarded 2 Memfault Chunks
Upload the symbol file generated from your build to your Memfault account so that the information from your application can be parsed. The symbol file is located in the build folder:
peripheral_mds/build/peripheral_mds/zephyr/zephyr.elf:Open Memfault in a web browser.
Log in to your account and select the project you created earlier.
Navigate to Fleet > Devices in the left side menu. You can see your newly connected device and the software version in the list.
Select the software version number for your device and click Upload to upload the symbol file.
Return to the terminal and press the Tab button on your keyboard to confirm that the Memfault shell is working. The shell commands available are displayed.
To learn about the Memfault shell commands, issue command
mflt helpUse the buttons to trigger Memfault crashes, traces and metrics collection.
See User interface for details about button functions.
Explore the Memfault user interface to see the errors and metrics sent from your device.
Open a serial port connection to the kit using a terminal emulator that supports VT100/ANSI escape characters (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.
Reset your development kit.
Observe that the sample starts.
Run the following command in the nRF Connect SDK root directory to install the MDS BLE gateway script dependencies:
pip install --user -r scripts/memfault/requirements.txtConnect the nRF52 development kit to your PC that uses the MDS BLE gateway script.
Start the
mds_ble_gateway.pyscript with the correct parameters, for example:python3 mds_ble_gateway.py --snr 682900407 --com COM0Wait for the script to establish a connection with your development kit.
Upon connection, data already collected by the Memfault SDK is forwarded to the cloud for further analysis. When connected, the new data is periodically transferred to the cloud with the interval configured in the
CONFIG_BT_MDS_DATA_POLL_INTERVALKconfig option.On the terminal running the script, you can observe the Memfault chunk counter:
Sending.. Forwarded 2 Memfault Chunks
Upload the symbol file generated from your build to your Memfault account so that the information from your application can be parsed. The symbol file is located in the build folder:
peripheral_mds/build/peripheral_mds/zephyr/zephyr.elf:Open Memfault in a web browser.
Log in to your account and select the project you created earlier.
Navigate to Fleet > Devices in the left side menu. You can see your newly connected device and the software version in the list.
Select the software version number for your device and click Upload to upload the symbol file.
Return to the terminal and press the Tab button on your keyboard to confirm that the Memfault shell is working. The shell commands available are displayed.
To learn about the Memfault shell commands, issue command
mflt helpUse the buttons to trigger Memfault crashes, traces and metrics collection.
See User interface for details about button functions.
Explore the Memfault user interface to see the errors and metrics sent from your device.
Testing with Memfault WebBluetooth Client
Note
The Web Bluetooth API used by the Memfault WebBluetooth Client is an experimental feature. The functionality depends on your browser and computer OS compatibility.
Open a serial port connection to the kit using a terminal emulator that supports VT100/ANSI escape characters (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.
Reset your development kit.
Observe that the sample starts.
Open a recent version of the Google Chrome browser.
Run the Memfault WebBluetooth Client script to forward Memfault diagnostic data to the cloud. For more details, see the Memfault WebBluetooth Client source code.
Make sure that your development kit is advertising.
In the browser, click the Connect button and select your device from the list.
Upon connection, data already collected by the Memfault SDK is forwarded to the cloud for further the analysis. When connected, the new data is periodically flushed to the cloud with the interval configured by the Kconfig option
CONFIG_BT_MDS_DATA_POLL_INTERVAL.Upload the symbol file generated from your build to your Memfault account so that the information from your application can be parsed. The
zephyr.elfsymbol file is located in the build folderperipheral_mds/build/peripheral_mds/zephyr.In a web browser, navigate to Memfault.
Log in to your account and select the project you created earlier.
Navigate to Fleet > Devices in the left side menu. You can see your newly connected device and the software version in the list.
Select the software version number for your device and click Upload to upload the symbol file.
Return to the terminal and press the Tab button on your keyboard to confirm that the Memfault shell is working. The shell commands available are displayed.
To learn about the Memfault shell commands, issue command
mflt help.Use the buttons to trigger Memfault crashes, traces and metrics collection. See User interface for details about button functions.
Explore the Memfault user interface to see the errors and metrics sent from your device.
Note
The Web Bluetooth API used by the Memfault WebBluetooth Client is an experimental feature. The functionality depends on your browser and computer OS compatibility.
Open a serial port connection to the kit using a terminal emulator that supports VT100/ANSI escape characters (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.
Reset your development kit.
Observe that the sample starts.
Open a recent version of the Google Chrome browser.
Run the Memfault WebBluetooth Client script to forward Memfault diagnostic data to the cloud. For more details, see the Memfault WebBluetooth Client source code.
Make sure that your development kit is advertising.
In the browser, click the Connect button and select your device from the list.
Upon connection, data already collected by the Memfault SDK is forwarded to the cloud for further the analysis. When connected, the new data is periodically flushed to the cloud with the interval configured by the Kconfig option
CONFIG_BT_MDS_DATA_POLL_INTERVAL.Upload the symbol file generated from your build to your Memfault account so that the information from your application can be parsed. The
zephyr.elfsymbol file is located in the build folderperipheral_mds/build/peripheral_mds/zephyr.In a web browser, navigate to Memfault.
Log in to your account and select the project you created earlier.
Navigate to Fleet > Devices in the left side menu. You can see your newly connected device and the software version in the list.
Select the software version number for your device and click Upload to upload the symbol file.
Return to the terminal and press the Tab button on your keyboard to confirm that the Memfault shell is working. The shell commands available are displayed.
To learn about the Memfault shell commands, issue command
mflt help.Use the buttons to trigger Memfault crashes, traces and metrics collection. See User interface for details about button functions.
Explore the Memfault user interface to see the errors and metrics sent from your device.
Dependencies
This sample uses the following nRF Connect SDK libraries:
In addition, it uses the following Zephyr libraries:
API:
include/bluetooth/bluetooth.hinclude/bluetooth/conn.hsamples/bluetooth/gatt/bas.h
The sample also uses the following secure firmware component: