Demonstration of possible usage of the NFC EEPROM class (including support for ST25DV NFC chip)

Dependencies:   eeprom_driver

Committer:
apalmieri
Date:
Tue Jan 28 16:08:05 2020 +0000
Revision:
6:462737ad2938
Parent:
0:86f202e07059
Add ST25DV driver support

Who changed what in which revision?

UserRevisionLine numberNew 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