NFC: Writable NDEF message

The Writable NDEF message sample shows how to use the NFC tag to expose an NDEF message that can be overwritten with any other NDEF message by an NFC device. It uses the NFC Data Exchange Format (NDEF).

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

nRF54L15 PDK

PCA10156

nrf54l15pdk

nrf54l15pdk/nrf54l15/cpuapp

nRF54H20 DK

PCA10175

nrf54h20dk

nrf54h20dk/nrf54h20/cpuapp

nRF5340 DK

PCA10095

nrf5340dk

nrf5340dk/nrf5340/cpuapp/ns nrf5340dk/nrf5340/cpuapp

nRF52 DK

PCA10040

nrf52dk

nrf52dk/nrf52832

nRF52840 DK

PCA10056

nrf52840dk

nrf52840dk/nrf52840

When built for a board target with the */ns variant, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

The sample also requires a smartphone or tablet with NFC Tools application (or equivalent).

Overview

When the sample starts, it initializes the NFC tag and loads the NDEF message from the file in flash memory. If the NDEF message file does not exist, a default message is generated. It is a URI message with a URI record containing the URL “nordicsemi.com”. The sample then sets up the NFC library for the Type 4 Tag platform, which uses the NDEF message and senses the external NFC field.

The library works in Read-Write emulation mode. In this mode, procedures for reading and updating an NDEF message are handled internally by the NFC library. Any changes to the NDEF message update the NDEF message file stored in flash memory.

User interface

LED 1:

Indicates if an NFC field is present.

LED 2:

Indicates that the NDEF message was updated.

LED 4:

Indicates that the NDEF message was read.

Button 1:

Press during startup to restore the default NDEF message.

Building and running

This sample can be found under samples/nfc/writable_ndef_msg in the nRF Connect SDK folder structure.

When built as firmware image for a board target with the */ns variant, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M). To read more about CMSE, see Processing environments.

To build the sample, follow the instructions in Configuring and 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.

Note

Programming the nRF54H20 SoC can sometimes fail due to conflicts in the resource configuration. This can happen if, for example, an application programmed to the nRF54H20 SoC configured the UICRs for one or more cores in a way that is incompatible with the configuration required by the application you are trying to program on the SoC.

To fix this error and erase the UICR for the application core, run the following command:

nrfutil device recover --core Application

If your sample also uses the radio core, you must also erase the UICRs for the radio core. To erase the UICR for the radio core, run the following command:

nrfutil device recover --core Network

For more information on the command, run:

nrfutil device recover --help

You can then run west flash to program your application.

Note

If you are using debug messages in the NFCT driver, the driver might not be working properly if you have CONFIG_LOG_MODE_IMMEDIATE enabled. The NFCT driver is part of the nrfx driver package. For more information about this driver, see the NFCT driver page in the nrfx repository.

Testing

After programming the sample to your development kit, complete the following steps to test it:

  1. Touch the NFC antenna with the smartphone or tablet and observe that LED 1 and LED 4 are lit.

  2. Observe that the smartphone or tablet tries to open the URL “http://www.nordicsemi.com” in a web browser.

  3. Use a proper application (for example, NFC Tools for Android) to overwrite the existing NDEF message with your own message.

  4. Restart your development kit and touch the antenna again. Observe that the new message is displayed.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the Type 4 Tag library from sdk-nrfxlib:

It uses the following Zephyr libraries:

  • include/atomic.h

  • include/zephyr.h

  • include/device.h

  • include/nvs/nvs.h

  • GPIO Interface

The sample also uses the following secure firmware component: