This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.

Dependencies:   NDefLib X_NUCLEO_NFC01A1 mbed

Fork of X-MBED-NFC1 by Giovanni Visentini

This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.

The available demos are:

  • SAMPLE_WRITE_URL: write a tag with the ST home page URL
  • SAMPLE_COUNT_CLICK: create a custom tag to count and report the user button clicks.
  • SAMPLE_WRITE_AND_CHANGE_ALL: write a tag with all the supported records and update the tag contents when the user button is pressed.
  • SAMPLE_LOCK_TAG_CONTENT: use the M24SR component API to set the NFC tag as read-only.

To enable the different demos comment/uncomment the SAMPLE_* macros provided in main.cpp .

Committer:
giovannivisentini
Date:
Mon Aug 21 12:12:30 2017 +0000
Revision:
20:ba95e0dc5975
Parent:
18:d596eb3f575f
update NDefLib and mbed libs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 13:685d95525ec8 1 /**
giovannivisentini 13:685d95525ec8 2 ******************************************************************************
Davidroid 18:d596eb3f575f 3 * @file SampleSync_countClick.cpp
giovannivisentini 13:685d95525ec8 4 * @author ST / Central Labs
giovannivisentini 13:685d95525ec8 5 * @date 03 Dic 2015
giovannivisentini 13:685d95525ec8 6 * @brief This demo define a custom record that contains the number of time the user
giovannivisentini 13:685d95525ec8 7 * press the user button
giovannivisentini 13:685d95525ec8 8 ******************************************************************************
giovannivisentini 13:685d95525ec8 9 *
giovannivisentini 13:685d95525ec8 10 * COPYRIGHT(c) 2015 STMicroelectronics
giovannivisentini 13:685d95525ec8 11 *
giovannivisentini 13:685d95525ec8 12 * Redistribution and use in source and binary forms, with or without modification,
giovannivisentini 13:685d95525ec8 13 * are permitted provided that the following conditions are met:
giovannivisentini 13:685d95525ec8 14 * 1. Redistributions of source code must retain the above copyright notice,
giovannivisentini 13:685d95525ec8 15 * this list of conditions and the following disclaimer.
giovannivisentini 13:685d95525ec8 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
giovannivisentini 13:685d95525ec8 17 * this list of conditions and the following disclaimer in the documentation
giovannivisentini 13:685d95525ec8 18 * and/or other materials provided with the distribution.
giovannivisentini 13:685d95525ec8 19 * 3. Neither the name of STMicroelectronics nor the names of its contributors
giovannivisentini 13:685d95525ec8 20 * may be used to endorse or promote products derived from this software
giovannivisentini 13:685d95525ec8 21 * without specific prior written permission.
giovannivisentini 13:685d95525ec8 22 *
giovannivisentini 13:685d95525ec8 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
giovannivisentini 13:685d95525ec8 24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
giovannivisentini 13:685d95525ec8 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
giovannivisentini 13:685d95525ec8 26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
giovannivisentini 13:685d95525ec8 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
giovannivisentini 13:685d95525ec8 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
giovannivisentini 13:685d95525ec8 29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
giovannivisentini 13:685d95525ec8 30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
giovannivisentini 13:685d95525ec8 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
giovannivisentini 13:685d95525ec8 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giovannivisentini 13:685d95525ec8 33 *
giovannivisentini 13:685d95525ec8 34 ******************************************************************************
giovannivisentini 13:685d95525ec8 35 */
giovannivisentini 13:685d95525ec8 36
giovannivisentini 13:685d95525ec8 37 #include "mbed.h"
giovannivisentini 13:685d95525ec8 38
giovannivisentini 17:d8d3d2088cac 39 #include "XNucleoNFC01A1.h"
giovannivisentini 13:685d95525ec8 40 #include "NDefLib/NDefNfcTag.h"
giovannivisentini 13:685d95525ec8 41 #include "MyRecord.h"
giovannivisentini 13:685d95525ec8 42
giovannivisentini 13:685d95525ec8 43 /* Write a nfc message into a tag
giovannivisentini 13:685d95525ec8 44 * @param nfcNucleo Board where write the data.
giovannivisentini 13:685d95525ec8 45 * @param msg Message to write.
giovannivisentini 13:685d95525ec8 46 */
giovannivisentini 17:d8d3d2088cac 47 static void write_message(XNucleoNFC01A1 *nfcNucleo,NDefLib::Message &msg){
giovannivisentini 17:d8d3d2088cac 48 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
giovannivisentini 13:685d95525ec8 49 //open the i2c session with the nfc chip
giovannivisentini 17:d8d3d2088cac 50 if(tag.open_session()){
giovannivisentini 15:94c98d2aa235 51 printf("Session opened\r\n");
giovannivisentini 15:94c98d2aa235 52
giovannivisentini 17:d8d3d2088cac 53 nfcNucleo->get_led1()=! nfcNucleo->get_led1();
giovannivisentini 13:685d95525ec8 54
giovannivisentini 13:685d95525ec8 55 //write the tag
giovannivisentini 13:685d95525ec8 56 if(tag.write(msg)){
giovannivisentini 17:d8d3d2088cac 57 printf("message wrote\r\n");
giovannivisentini 17:d8d3d2088cac 58 nfcNucleo->get_led2()=!nfcNucleo->get_led2();
giovannivisentini 13:685d95525ec8 59 }//if
giovannivisentini 13:685d95525ec8 60
giovannivisentini 13:685d95525ec8 61 //close the i2c session
giovannivisentini 17:d8d3d2088cac 62 if(tag.close_session()){
giovannivisentini 15:94c98d2aa235 63 printf("Session closed\r\n");
giovannivisentini 17:d8d3d2088cac 64 nfcNucleo->get_led3()=!nfcNucleo->get_led3();
giovannivisentini 15:94c98d2aa235 65 }
giovannivisentini 13:685d95525ec8 66 }//if open session
giovannivisentini 13:685d95525ec8 67 }//writeMessage
giovannivisentini 13:685d95525ec8 68
giovannivisentini 13:685d95525ec8 69
giovannivisentini 13:685d95525ec8 70 static volatile bool buttonPress=false; /// true when the user press the message
giovannivisentini 13:685d95525ec8 71
giovannivisentini 13:685d95525ec8 72 /**
giovannivisentini 13:685d95525ec8 73 * Call back called when the user press the button.
giovannivisentini 13:685d95525ec8 74 */
giovannivisentini 17:d8d3d2088cac 75 static void set_button_press(){
giovannivisentini 13:685d95525ec8 76 buttonPress=true;
giovannivisentini 13:685d95525ec8 77 }//if buttonPress
giovannivisentini 13:685d95525ec8 78
giovannivisentini 13:685d95525ec8 79 /**
giovannivisentini 13:685d95525ec8 80 * Write a castum record that count how many times the user press the button.
giovannivisentini 13:685d95525ec8 81 */
giovannivisentini 13:685d95525ec8 82 void sampleSync_countClick() {
giovannivisentini 13:685d95525ec8 83
giovannivisentini 13:685d95525ec8 84 //instance the board with the default paramiters
giovannivisentini 17:d8d3d2088cac 85 I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN);
giovannivisentini 17:d8d3d2088cac 86 XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel);
giovannivisentini 17:d8d3d2088cac 87
giovannivisentini 13:685d95525ec8 88 //set the button interrupt
giovannivisentini 17:d8d3d2088cac 89 #if defined(TARGET_STM)
giovannivisentini 17:d8d3d2088cac 90 InterruptIn userButton(USER_BUTTON);
giovannivisentini 17:d8d3d2088cac 91 #else
giovannivisentini 17:d8d3d2088cac 92 InterruptIn userButton(SW2);
giovannivisentini 17:d8d3d2088cac 93 #endif
giovannivisentini 17:d8d3d2088cac 94 userButton.fall(set_button_press);
giovannivisentini 13:685d95525ec8 95
giovannivisentini 15:94c98d2aa235 96 printf("Init Done\r\n");
giovannivisentini 15:94c98d2aa235 97
giovannivisentini 13:685d95525ec8 98 //create the NDef message and record
giovannivisentini 13:685d95525ec8 99 NDefLib::Message msg;
giovannivisentini 13:685d95525ec8 100 MyRecord rClickCount;
giovannivisentini 17:d8d3d2088cac 101 msg.add_record(&rClickCount);
giovannivisentini 13:685d95525ec8 102
giovannivisentini 17:d8d3d2088cac 103 write_message(nfcNucleo,msg);
giovannivisentini 13:685d95525ec8 104
giovannivisentini 15:94c98d2aa235 105 printf("Start Main Loop\r\n");
giovannivisentini 13:685d95525ec8 106 while(true){
giovannivisentini 13:685d95525ec8 107
giovannivisentini 13:685d95525ec8 108 if(buttonPress){
giovannivisentini 13:685d95525ec8 109 //change the record content
giovannivisentini 17:d8d3d2088cac 110 rClickCount.increment_click();
giovannivisentini 13:685d95525ec8 111 //write the new record content
giovannivisentini 17:d8d3d2088cac 112 write_message(nfcNucleo,msg);
giovannivisentini 13:685d95525ec8 113 //wait a new button press
giovannivisentini 13:685d95525ec8 114 buttonPress=false;
giovannivisentini 13:685d95525ec8 115 }//if
giovannivisentini 13:685d95525ec8 116 //wait next event
giovannivisentini 13:685d95525ec8 117 __WFE();
giovannivisentini 13:685d95525ec8 118 }//while
giovannivisentini 13:685d95525ec8 119
giovannivisentini 17:d8d3d2088cac 120 }//sample_countClick