NFC EEPROM sample application

NFC EEPROM Application based on X-NUCLEO-NFC04A1 expansion board

The application will write an URL to the EEPROM of the NFC tag on your board. This will be able to be read by any NFC device capable of reading NFC tags.

The example uses the EEPROM driver provided for the X-NUCLEO-NFC04A1 expansion board based on the ST25DV04K dynamic NFC/RFID tag.

Committer:
apalmieri
Date:
Thu Sep 17 13:44:38 2020 +0000
Revision:
0:f3d7ca3d45de
Initial commit of NFC EEPROM example for X-NUCLEO-NFC04A1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apalmieri 0:f3d7ca3d45de 1 /* mbed Microcontroller Library
apalmieri 0:f3d7ca3d45de 2 * Copyright (c) 2018-2018 ARM Limited
apalmieri 0:f3d7ca3d45de 3 *
apalmieri 0:f3d7ca3d45de 4 * Licensed under the Apache License, Version 2.0 (the "License");
apalmieri 0:f3d7ca3d45de 5 * you may not use this file except in compliance with the License.
apalmieri 0:f3d7ca3d45de 6 * You may obtain a copy of the License at
apalmieri 0:f3d7ca3d45de 7 *
apalmieri 0:f3d7ca3d45de 8 * http://www.apache.org/licenses/LICENSE-2.0
apalmieri 0:f3d7ca3d45de 9 *
apalmieri 0:f3d7ca3d45de 10 * Unless required by applicable law or agreed to in writing, software
apalmieri 0:f3d7ca3d45de 11 * distributed under the License is distributed on an "AS IS" BASIS,
apalmieri 0:f3d7ca3d45de 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
apalmieri 0:f3d7ca3d45de 13 * See the License for the specific language governing permissions and
apalmieri 0:f3d7ca3d45de 14 * limitations under the License.
apalmieri 0:f3d7ca3d45de 15 */
apalmieri 0:f3d7ca3d45de 16
apalmieri 0:f3d7ca3d45de 17 #include "events/EventQueue.h"
apalmieri 0:f3d7ca3d45de 18
apalmieri 0:f3d7ca3d45de 19 #include "nfc/ndef/MessageBuilder.h"
apalmieri 0:f3d7ca3d45de 20 #include "nfc/ndef/common/URI.h"
apalmieri 0:f3d7ca3d45de 21 #include "nfc/ndef/common/util.h"
apalmieri 0:f3d7ca3d45de 22
apalmieri 0:f3d7ca3d45de 23 #include "NFCEEPROM.h"
apalmieri 0:f3d7ca3d45de 24
apalmieri 0:f3d7ca3d45de 25 #include "EEPROMDriver.h"
apalmieri 0:f3d7ca3d45de 26 #include <cstdio>
apalmieri 0:f3d7ca3d45de 27
apalmieri 0:f3d7ca3d45de 28 using events::EventQueue;
apalmieri 0:f3d7ca3d45de 29
apalmieri 0:f3d7ca3d45de 30 using mbed::nfc::NFCEEPROM;
apalmieri 0:f3d7ca3d45de 31 using mbed::nfc::NFCEEPROMDriver;
apalmieri 0:f3d7ca3d45de 32 using mbed::Span;
apalmieri 0:f3d7ca3d45de 33
apalmieri 0:f3d7ca3d45de 34 using mbed::nfc::ndef::MessageBuilder;
apalmieri 0:f3d7ca3d45de 35 using mbed::nfc::ndef::common::URI;
apalmieri 0:f3d7ca3d45de 36 using mbed::nfc::ndef::common::span_from_cstr;
apalmieri 0:f3d7ca3d45de 37
apalmieri 0:f3d7ca3d45de 38 /* URL that will be written into the tag */
apalmieri 0:f3d7ca3d45de 39 const char url_string[] = "st.com";
apalmieri 0:f3d7ca3d45de 40
apalmieri 0:f3d7ca3d45de 41 class EEPROMExample : mbed::nfc::NFCEEPROM::Delegate
apalmieri 0:f3d7ca3d45de 42 {
apalmieri 0:f3d7ca3d45de 43 public:
apalmieri 0:f3d7ca3d45de 44 EEPROMExample(events::EventQueue& queue, NFCEEPROMDriver& eeprom_driver) :
apalmieri 0:f3d7ca3d45de 45 _ndef_buffer(),
apalmieri 0:f3d7ca3d45de 46 _eeprom(&eeprom_driver, &queue, _ndef_buffer),
apalmieri 0:f3d7ca3d45de 47 _queue(queue)
apalmieri 0:f3d7ca3d45de 48 { }
apalmieri 0:f3d7ca3d45de 49
apalmieri 0:f3d7ca3d45de 50 void run()
apalmieri 0:f3d7ca3d45de 51 {
apalmieri 0:f3d7ca3d45de 52 if (_eeprom.initialize() != NFC_OK) {
apalmieri 0:f3d7ca3d45de 53 printf("failed to initialise\r\n");
apalmieri 0:f3d7ca3d45de 54 _queue.break_dispatch();
apalmieri 0:f3d7ca3d45de 55 }
apalmieri 0:f3d7ca3d45de 56
apalmieri 0:f3d7ca3d45de 57 _eeprom.set_delegate(this);
apalmieri 0:f3d7ca3d45de 58
apalmieri 0:f3d7ca3d45de 59 _queue.call(&_eeprom, &NFCEEPROM::write_ndef_message);
apalmieri 0:f3d7ca3d45de 60 }
apalmieri 0:f3d7ca3d45de 61
apalmieri 0:f3d7ca3d45de 62 private:
apalmieri 0:f3d7ca3d45de 63 virtual void on_ndef_message_written(nfc_err_t result) {
apalmieri 0:f3d7ca3d45de 64 if (result == NFC_OK) {
apalmieri 0:f3d7ca3d45de 65 printf("message written successfully\r\n");
apalmieri 0:f3d7ca3d45de 66 } else {
apalmieri 0:f3d7ca3d45de 67 printf("failed to write (error: %d)\r\n", result);
apalmieri 0:f3d7ca3d45de 68 }
apalmieri 0:f3d7ca3d45de 69
apalmieri 0:f3d7ca3d45de 70 _queue.call(&_eeprom, &NFCEEPROM::read_ndef_message);
apalmieri 0:f3d7ca3d45de 71 }
apalmieri 0:f3d7ca3d45de 72
apalmieri 0:f3d7ca3d45de 73 virtual void on_ndef_message_read(nfc_err_t result) {
apalmieri 0:f3d7ca3d45de 74 if (result == NFC_OK) {
apalmieri 0:f3d7ca3d45de 75 printf("message read successfully\r\n");
apalmieri 0:f3d7ca3d45de 76 } else {
apalmieri 0:f3d7ca3d45de 77 printf("failed to read (error: %d)\r\n", result);
apalmieri 0:f3d7ca3d45de 78 }
apalmieri 0:f3d7ca3d45de 79 }
apalmieri 0:f3d7ca3d45de 80
apalmieri 0:f3d7ca3d45de 81 virtual void parse_ndef_message(const Span<const uint8_t> &buffer) {
apalmieri 0:f3d7ca3d45de 82 printf("Received an ndef message of size %d\r\n", buffer.size());
apalmieri 0:f3d7ca3d45de 83 }
apalmieri 0:f3d7ca3d45de 84
apalmieri 0:f3d7ca3d45de 85 virtual size_t build_ndef_message(const Span<uint8_t> &buffer) {
apalmieri 0:f3d7ca3d45de 86 printf("Building an ndef message\r\n");
apalmieri 0:f3d7ca3d45de 87
apalmieri 0:f3d7ca3d45de 88 /* create a message containing the URL */
apalmieri 0:f3d7ca3d45de 89
apalmieri 0:f3d7ca3d45de 90 MessageBuilder builder(buffer);
apalmieri 0:f3d7ca3d45de 91
apalmieri 0:f3d7ca3d45de 92 /* URI expected a non-null terminated string so we use a helper function that casts
apalmieri 0:f3d7ca3d45de 93 * the pointer into a Span of size smaller by one */
apalmieri 0:f3d7ca3d45de 94 URI uri(URI::HTTPS_WWW, span_from_cstr(url_string));
apalmieri 0:f3d7ca3d45de 95
apalmieri 0:f3d7ca3d45de 96 uri.append_as_record(builder, true);
apalmieri 0:f3d7ca3d45de 97
apalmieri 0:f3d7ca3d45de 98 return builder.get_message().size();
apalmieri 0:f3d7ca3d45de 99 }
apalmieri 0:f3d7ca3d45de 100
apalmieri 0:f3d7ca3d45de 101 private:
apalmieri 0:f3d7ca3d45de 102 uint8_t _ndef_buffer[1024];
apalmieri 0:f3d7ca3d45de 103 NFCEEPROM _eeprom;
apalmieri 0:f3d7ca3d45de 104 EventQueue& _queue;
apalmieri 0:f3d7ca3d45de 105 };
apalmieri 0:f3d7ca3d45de 106
apalmieri 0:f3d7ca3d45de 107 int main()
apalmieri 0:f3d7ca3d45de 108 {
apalmieri 0:f3d7ca3d45de 109 EventQueue queue;
apalmieri 0:f3d7ca3d45de 110 NFCEEPROMDriver& eeprom_driver = get_eeprom_driver(queue);
apalmieri 0:f3d7ca3d45de 111
apalmieri 0:f3d7ca3d45de 112 EEPROMExample example(queue, eeprom_driver);
apalmieri 0:f3d7ca3d45de 113
apalmieri 0:f3d7ca3d45de 114 example.run();
apalmieri 0:f3d7ca3d45de 115 queue.dispatch_forever();
apalmieri 0:f3d7ca3d45de 116
apalmieri 0:f3d7ca3d45de 117 return 0;
apalmieri 0:f3d7ca3d45de 118 }
apalmieri 0:f3d7ca3d45de 119