Demonstration of possible usage of the NFC EEPROM class.
NFC EEPROM usage example
Demonstration of possible usage of the NFC EEPROM class.
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 needs to supply a driver to the eeprom. This example uses the M24SR driver on the DISCO_L475VG_IOT01A and target and the PN512 driver on the NUCLEO_F401RE target. You may wish to add your own driver or update the configuration if you're using a different board.
Running the application
Verification of the sample application can be seen on any a smartphone with an NFC reader. After running you will be able to read the tag with an NFC tag reader application.
Information about activity is also printed over the serial connection - please have a client open. You may use:
Tera Term - https://ttssh2.osdn.jp/index.html.en
You will also need to supply the driver for the EEPROM. This example is known to work on DISCO_L475VG_IOT01A which uses M24SR. The driver is downloaded during deployment step (mbed deploy). This is based on the contents of the eeprom_driver.lib file. The build process will pick up this library and build it as part of the mbed-os build. If you want to use a different driver, please update the contents of eeprom_driver.lib to point at the github repository containing the driver.
source/main.cpp@0:86f202e07059, 2018-09-24 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Sep 24 17:09:39 2018 +0100
- Revision:
- 0:86f202e07059
Add mbed deploy into the SmartPoster instructions.
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-nfc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:86f202e07059 | 1 | /* mbed Microcontroller Library |
mbed_official | 0:86f202e07059 | 2 | * Copyright (c) 2018-2018 ARM Limited |
mbed_official | 0:86f202e07059 | 3 | * |
mbed_official | 0:86f202e07059 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 0:86f202e07059 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 0:86f202e07059 | 6 | * You may obtain a copy of the License at |
mbed_official | 0:86f202e07059 | 7 | * |
mbed_official | 0:86f202e07059 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:86f202e07059 | 9 | * |
mbed_official | 0:86f202e07059 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 0:86f202e07059 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 0:86f202e07059 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 0:86f202e07059 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 0:86f202e07059 | 14 | * limitations under the License. |
mbed_official | 0:86f202e07059 | 15 | */ |
mbed_official | 0:86f202e07059 | 16 | |
mbed_official | 0:86f202e07059 | 17 | #include "events/EventQueue.h" |
mbed_official | 0:86f202e07059 | 18 | |
mbed_official | 0:86f202e07059 | 19 | #include "nfc/ndef/MessageBuilder.h" |
mbed_official | 0:86f202e07059 | 20 | #include "nfc/ndef/common/URI.h" |
mbed_official | 0:86f202e07059 | 21 | #include "nfc/ndef/common/util.h" |
mbed_official | 0:86f202e07059 | 22 | |
mbed_official | 0:86f202e07059 | 23 | #include "NFCEEPROM.h" |
mbed_official | 0:86f202e07059 | 24 | |
mbed_official | 0:86f202e07059 | 25 | #include "EEPROMDriver.h" |
mbed_official | 0:86f202e07059 | 26 | |
mbed_official | 0:86f202e07059 | 27 | using events::EventQueue; |
mbed_official | 0:86f202e07059 | 28 | |
mbed_official | 0:86f202e07059 | 29 | using mbed::nfc::NFCEEPROM; |
mbed_official | 0:86f202e07059 | 30 | using mbed::nfc::NFCEEPROMDriver; |
mbed_official | 0:86f202e07059 | 31 | using mbed::Span; |
mbed_official | 0:86f202e07059 | 32 | |
mbed_official | 0:86f202e07059 | 33 | using mbed::nfc::ndef::MessageBuilder; |
mbed_official | 0:86f202e07059 | 34 | using mbed::nfc::ndef::common::URI; |
mbed_official | 0:86f202e07059 | 35 | using mbed::nfc::ndef::common::span_from_cstr; |
mbed_official | 0:86f202e07059 | 36 | |
mbed_official | 0:86f202e07059 | 37 | /* URL that will be written into the tag */ |
mbed_official | 0:86f202e07059 | 38 | const char url_string[] = "mbed.com"; |
mbed_official | 0:86f202e07059 | 39 | |
mbed_official | 0:86f202e07059 | 40 | class EEPROMExample : mbed::nfc::NFCEEPROM::Delegate |
mbed_official | 0:86f202e07059 | 41 | { |
mbed_official | 0:86f202e07059 | 42 | public: |
mbed_official | 0:86f202e07059 | 43 | EEPROMExample(events::EventQueue& queue, NFCEEPROMDriver& eeprom_driver) : |
mbed_official | 0:86f202e07059 | 44 | _ndef_buffer(), |
mbed_official | 0:86f202e07059 | 45 | _eeprom(&eeprom_driver, &queue, _ndef_buffer), |
mbed_official | 0:86f202e07059 | 46 | _queue(queue) |
mbed_official | 0:86f202e07059 | 47 | { } |
mbed_official | 0:86f202e07059 | 48 | |
mbed_official | 0:86f202e07059 | 49 | void run() |
mbed_official | 0:86f202e07059 | 50 | { |
mbed_official | 0:86f202e07059 | 51 | if (_eeprom.initialize() != NFC_OK) { |
mbed_official | 0:86f202e07059 | 52 | printf("failed to initialise\r\n"); |
mbed_official | 0:86f202e07059 | 53 | _queue.break_dispatch(); |
mbed_official | 0:86f202e07059 | 54 | } |
mbed_official | 0:86f202e07059 | 55 | |
mbed_official | 0:86f202e07059 | 56 | _eeprom.set_delegate(this); |
mbed_official | 0:86f202e07059 | 57 | |
mbed_official | 0:86f202e07059 | 58 | _queue.call(&_eeprom, &NFCEEPROM::write_ndef_message); |
mbed_official | 0:86f202e07059 | 59 | } |
mbed_official | 0:86f202e07059 | 60 | |
mbed_official | 0:86f202e07059 | 61 | private: |
mbed_official | 0:86f202e07059 | 62 | virtual void on_ndef_message_written(nfc_err_t result) { |
mbed_official | 0:86f202e07059 | 63 | if (result == NFC_OK) { |
mbed_official | 0:86f202e07059 | 64 | printf("message written successfully\r\n"); |
mbed_official | 0:86f202e07059 | 65 | } else { |
mbed_official | 0:86f202e07059 | 66 | printf("failed to write (error: %d)\r\n", result); |
mbed_official | 0:86f202e07059 | 67 | } |
mbed_official | 0:86f202e07059 | 68 | |
mbed_official | 0:86f202e07059 | 69 | _queue.call(&_eeprom, &NFCEEPROM::read_ndef_message); |
mbed_official | 0:86f202e07059 | 70 | } |
mbed_official | 0:86f202e07059 | 71 | |
mbed_official | 0:86f202e07059 | 72 | virtual void on_ndef_message_read(nfc_err_t result) { |
mbed_official | 0:86f202e07059 | 73 | if (result == NFC_OK) { |
mbed_official | 0:86f202e07059 | 74 | printf("message read successfully\r\n"); |
mbed_official | 0:86f202e07059 | 75 | } else { |
mbed_official | 0:86f202e07059 | 76 | printf("failed to read (error: %d)\r\n", result); |
mbed_official | 0:86f202e07059 | 77 | } |
mbed_official | 0:86f202e07059 | 78 | } |
mbed_official | 0:86f202e07059 | 79 | |
mbed_official | 0:86f202e07059 | 80 | virtual void parse_ndef_message(const Span<const uint8_t> &buffer) { |
mbed_official | 0:86f202e07059 | 81 | printf("Received an ndef message of size %d\r\n", buffer.size()); |
mbed_official | 0:86f202e07059 | 82 | } |
mbed_official | 0:86f202e07059 | 83 | |
mbed_official | 0:86f202e07059 | 84 | virtual size_t build_ndef_message(const Span<uint8_t> &buffer) { |
mbed_official | 0:86f202e07059 | 85 | printf("Building an ndef message\r\n"); |
mbed_official | 0:86f202e07059 | 86 | |
mbed_official | 0:86f202e07059 | 87 | /* create a message containing the URL */ |
mbed_official | 0:86f202e07059 | 88 | |
mbed_official | 0:86f202e07059 | 89 | MessageBuilder builder(buffer); |
mbed_official | 0:86f202e07059 | 90 | |
mbed_official | 0:86f202e07059 | 91 | /* URI expected a non-null terminated string so we use a helper function that casts |
mbed_official | 0:86f202e07059 | 92 | * the pointer into a Span of size smaller by one */ |
mbed_official | 0:86f202e07059 | 93 | URI uri(URI::HTTPS_WWW, span_from_cstr(url_string)); |
mbed_official | 0:86f202e07059 | 94 | |
mbed_official | 0:86f202e07059 | 95 | uri.append_as_record(builder, true); |
mbed_official | 0:86f202e07059 | 96 | |
mbed_official | 0:86f202e07059 | 97 | return builder.get_message().size(); |
mbed_official | 0:86f202e07059 | 98 | } |
mbed_official | 0:86f202e07059 | 99 | |
mbed_official | 0:86f202e07059 | 100 | private: |
mbed_official | 0:86f202e07059 | 101 | uint8_t _ndef_buffer[1024]; |
mbed_official | 0:86f202e07059 | 102 | NFCEEPROM _eeprom; |
mbed_official | 0:86f202e07059 | 103 | EventQueue& _queue; |
mbed_official | 0:86f202e07059 | 104 | }; |
mbed_official | 0:86f202e07059 | 105 | |
mbed_official | 0:86f202e07059 | 106 | int main() |
mbed_official | 0:86f202e07059 | 107 | { |
mbed_official | 0:86f202e07059 | 108 | EventQueue queue; |
mbed_official | 0:86f202e07059 | 109 | NFCEEPROMDriver& eeprom_driver = get_eeprom_driver(queue); |
mbed_official | 0:86f202e07059 | 110 | |
mbed_official | 0:86f202e07059 | 111 | EEPROMExample example(queue, eeprom_driver); |
mbed_official | 0:86f202e07059 | 112 | |
mbed_official | 0:86f202e07059 | 113 | example.run(); |
mbed_official | 0:86f202e07059 | 114 | queue.dispatch_forever(); |
mbed_official | 0:86f202e07059 | 115 | |
mbed_official | 0:86f202e07059 | 116 | return 0; |
mbed_official | 0:86f202e07059 | 117 | } |
mbed_official | 0:86f202e07059 | 118 |