Complete mbed library/workspace for HelloWorld_NFC02A1

Dependencies:   NDefLib X_NUCLEO_NFC02A1 mbed

Fork of HelloWorld_NFC02A1 by ST Expansion SW Team

X-NUCLEO-NFC02A1 Dynamic NFC Tag Expansion Board Firmware Package

Introduction

This firmware package includes Components Device Drivers, Board Support Package and example applications for STMicroelectronics X-NUCLEO-NFC02A1 Dynamic NFC Tag Expansion Board based on M24LR.

Example Application

This is just a simple "hello world" style program for the X-NUCLEO-NFC02A1 Dynamic NFC Tag Expansion Board. The program writes a URI link to the M24LR dynamic tag using the synchronous programming model. It then reads back the URI from the tag to display it on terminal. The URI can also be retrieved from an NFC enabled smartphone/tablet.

Committer:
mapellil
Date:
Thu Aug 09 13:55:52 2018 +0000
Revision:
14:9db733e875fb
Parent:
13:17208d6d9971
Updated  Mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rosarium 0:892175366555 1 /**
rosarium 0:892175366555 2 ******************************************************************************
rosarium 0:892175366555 3 * @file main.cpp
rosarium 1:11ae12d41082 4 * @author AMG Central Lab
rosarium 0:892175366555 5 * @version V1.0.0
rosarium 1:11ae12d41082 6 * @date 30 Aug 2016
rosarium 0:892175366555 7 * @brief This demo writes an ndef message with an url inside.
rosarium 0:892175366555 8 ******************************************************************************
rosarium 0:892175366555 9 * @attention
rosarium 0:892175366555 10 *
rosarium 1:11ae12d41082 11 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
rosarium 0:892175366555 12 *
rosarium 0:892175366555 13 * Redistribution and use in source and binary forms, with or without modification,
rosarium 0:892175366555 14 * are permitted provided that the following conditions are met:
rosarium 0:892175366555 15 * 1. Redistributions of source code must retain the above copyright notice,
rosarium 0:892175366555 16 * this list of conditions and the following disclaimer.
rosarium 0:892175366555 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
rosarium 0:892175366555 18 * this list of conditions and the following disclaimer in the documentation
rosarium 0:892175366555 19 * and/or other materials provided with the distribution.
rosarium 0:892175366555 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
rosarium 0:892175366555 21 * may be used to endorse or promote products derived from this software
rosarium 0:892175366555 22 * without specific prior written permission.
rosarium 0:892175366555 23 *
rosarium 0:892175366555 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
rosarium 0:892175366555 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
rosarium 0:892175366555 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
rosarium 0:892175366555 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
rosarium 0:892175366555 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
rosarium 0:892175366555 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
rosarium 0:892175366555 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
rosarium 0:892175366555 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
rosarium 0:892175366555 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
rosarium 0:892175366555 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
rosarium 0:892175366555 34 *
rosarium 0:892175366555 35 ******************************************************************************
rosarium 0:892175366555 36 */
rosarium 0:892175366555 37
rosarium 0:892175366555 38 #include "mbed.h"
giovannivisentini 11:d0665d9c28a6 39 #include "XNucleoNFC02A1.h"
rosarium 0:892175366555 40 #include "DevI2C.h"
rosarium 1:11ae12d41082 41 #include "NDefLib/NDefNfcTag.h"
rosarium 1:11ae12d41082 42 #include "NDefLib/RecordType/RecordURI.h"
rosarium 8:9bdd16ebccfb 43
Davidroid 13:17208d6d9971 44 static volatile bool buttonPress = false; // true when the user press the message
Davidroid 13:17208d6d9971 45
Davidroid 13:17208d6d9971 46 /**
Davidroid 13:17208d6d9971 47 * Callback for button press.
Davidroid 13:17208d6d9971 48 */
Davidroid 13:17208d6d9971 49 static void set_button_press() {
Davidroid 13:17208d6d9971 50 buttonPress = true;
Davidroid 13:17208d6d9971 51 } //if buttonPress
rosarium 8:9bdd16ebccfb 52
Davidroid 13:17208d6d9971 53 /**
Davidroid 13:17208d6d9971 54 * Reading and printing NFC tag.
Davidroid 13:17208d6d9971 55 */
Davidroid 13:17208d6d9971 56 static void read_and_print_nfc_tag(NDefLib::NDefNfcTag &tag) {
Davidroid 13:17208d6d9971 57 using namespace NDefLib;
rosarium 8:9bdd16ebccfb 58
Davidroid 13:17208d6d9971 59 if (tag.open_session() == true) {
Davidroid 13:17208d6d9971 60 printf("Open Session\r\n");
Davidroid 13:17208d6d9971 61 NDefLib::Message readMsg;
rosarium 8:9bdd16ebccfb 62
Davidroid 13:17208d6d9971 63 tag.read(&readMsg);
Davidroid 13:17208d6d9971 64 printf("Message Read\r\n");
rosarium 8:9bdd16ebccfb 65
Davidroid 13:17208d6d9971 66 if (readMsg.get_N_records()==0) {
Davidroid 13:17208d6d9971 67 printf("Error Read\r\n");
Davidroid 13:17208d6d9971 68 } else {
Davidroid 13:17208d6d9971 69 for (uint32_t i=0;i<readMsg.get_N_records();i++) {
Davidroid 13:17208d6d9971 70 Record *r = readMsg[i];
Davidroid 13:17208d6d9971 71 //printRecord(r);
Davidroid 13:17208d6d9971 72 RecordURI *const temp = (RecordURI*)r;
Davidroid 13:17208d6d9971 73 printf("Read uriId: %d\r\n",temp->get_uri_id());
Davidroid 13:17208d6d9971 74 printf("Read uriType: %s\r\n",temp->get_uri_type().c_str());
Davidroid 13:17208d6d9971 75 printf("Read uriContent: %s\r\n",temp->get_content().c_str());
Davidroid 13:17208d6d9971 76 delete r;
Davidroid 13:17208d6d9971 77 } //for
Davidroid 13:17208d6d9971 78 } //if-else
Davidroid 13:17208d6d9971 79
Davidroid 13:17208d6d9971 80 tag.close_session();
Davidroid 13:17208d6d9971 81 printf("Close session\r\n");
Davidroid 13:17208d6d9971 82 } else {
Davidroid 13:17208d6d9971 83 printf("Error open read Session\n\r");
Davidroid 13:17208d6d9971 84 }
rosarium 8:9bdd16ebccfb 85 }
rosarium 8:9bdd16ebccfb 86
rosarium 0:892175366555 87 /**
Davidroid 13:17208d6d9971 88 * Writing a Ndef URI message linking to the "st.com" website.
rosarium 0:892175366555 89 */
rosarium 0:892175366555 90 int main(void)
rosarium 0:892175366555 91 {
Davidroid 13:17208d6d9971 92 /* Using default board pinout. */
Davidroid 13:17208d6d9971 93 DevI2C i2cChannel(XNucleoNFC02A1::DEFAULT_SDA_PIN,XNucleoNFC02A1::DEFAULT_SDL_PIN);
giovannivisentini 11:d0665d9c28a6 94 XNucleoNFC02A1 *nfcNucleo = XNucleoNFC02A1::instance(i2cChannel,
Davidroid 13:17208d6d9971 95 XNucleoNFC02A1::DEFAULT_GPO_PIN,XNucleoNFC02A1::DEFAULT_RF_DISABLE_PIN,
Davidroid 13:17208d6d9971 96 XNucleoNFC02A1::DEFAULT_LED1_PIN,XNucleoNFC02A1::DEFAULT_LED2_PIN,
Davidroid 13:17208d6d9971 97 XNucleoNFC02A1::DEFAULT_LED3_PIN);
rosarium 0:892175366555 98
giovannivisentini 11:d0665d9c28a6 99 NDefLib::NDefNfcTag& tag =nfcNucleo->get_M24LR().get_NDef_tag();
giovannivisentini 11:d0665d9c28a6 100 M24LR & mM24LRp = nfcNucleo->get_M24LR();
rosarium 2:6f1b1f7f8d12 101
Davidroid 13:17208d6d9971 102 /* Enabling Energy Harvesting. */
giovannivisentini 11:d0665d9c28a6 103 mM24LRp.enable_energy_harvesting();
rosarium 0:892175366555 104
rosarium 0:892175366555 105 printf("System Initialization done: !\n\r");
rosarium 0:892175366555 106
Davidroid 13:17208d6d9971 107 /* Opening the i2c session with the nfc chip. */
Davidroid 13:17208d6d9971 108 if (tag.open_session() == true) {
rosarium 2:6f1b1f7f8d12 109 printf("Session opened\n\r");
Davidroid 13:17208d6d9971 110 nfcNucleo->get_led1() = 1;
rosarium 2:6f1b1f7f8d12 111
Davidroid 13:17208d6d9971 112 /* Creating the NDef message and record. */
rosarium 2:6f1b1f7f8d12 113 NDefLib::Message msg;
rosarium 7:699836375630 114 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com/st25");
giovannivisentini 11:d0665d9c28a6 115 msg.add_record(&rUri);
rosarium 2:6f1b1f7f8d12 116
Davidroid 13:17208d6d9971 117 /* Writing the tag. */
Davidroid 13:17208d6d9971 118 if (tag.write(msg) == true) {
rosarium 2:6f1b1f7f8d12 119 printf("Tag written\n\r");
Davidroid 13:17208d6d9971 120 nfcNucleo->get_led2() = 1;
Davidroid 13:17208d6d9971 121 } else {
rosarium 2:6f1b1f7f8d12 122 printf("Error writing \n\r");
Davidroid 13:17208d6d9971 123 nfcNucleo->get_led1() = 0;
Davidroid 13:17208d6d9971 124 } //if-else
rosarium 2:6f1b1f7f8d12 125
Davidroid 13:17208d6d9971 126 /* Closing the i2c session. */
Davidroid 13:17208d6d9971 127 if (tag.close_session() == true) {
Davidroid 13:17208d6d9971 128 printf("Session closed\n\r");
Davidroid 13:17208d6d9971 129 nfcNucleo->get_led3() = 1;
Davidroid 13:17208d6d9971 130 } else {
Davidroid 13:17208d6d9971 131 printf("Error closing the session\n\r");
Davidroid 13:17208d6d9971 132 } //if-else
Davidroid 13:17208d6d9971 133 } else {
Davidroid 13:17208d6d9971 134 printf("Error opening the session\n\r");
rosarium 2:6f1b1f7f8d12 135 }
Davidroid 13:17208d6d9971 136
giovannivisentini 9:0bd2f0c13d3d 137 #if defined(TARGET_STM)
Davidroid 13:17208d6d9971 138 /* enable the button */
Davidroid 13:17208d6d9971 139 InterruptIn mybutton(USER_BUTTON);
Davidroid 13:17208d6d9971 140
Davidroid 13:17208d6d9971 141 mybutton.fall(set_button_press);
rosarium 8:9bdd16ebccfb 142
Davidroid 13:17208d6d9971 143 /* Each second change the led status and see if the user press the button. */
Davidroid 13:17208d6d9971 144 while(1) {
Davidroid 13:17208d6d9971 145 if (buttonPress) {
Davidroid 13:17208d6d9971 146 /* Writing the read message on console. */
Davidroid 13:17208d6d9971 147 read_and_print_nfc_tag(tag);
Davidroid 13:17208d6d9971 148 buttonPress=false;
rosarium 8:9bdd16ebccfb 149 }
Davidroid 13:17208d6d9971 150 }
giovannivisentini 9:0bd2f0c13d3d 151 #else
Davidroid 13:17208d6d9971 152 read_and_print_nfc_tag(tag);
rosarium 8:9bdd16ebccfb 153 #endif
rosarium 0:892175366555 154 }
rosarium 0:892175366555 155
rosarium 2:6f1b1f7f8d12 156
rosarium 2:6f1b1f7f8d12 157 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/