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:
Thu Jan 14 09:17:03 2016 +0000
Revision:
12:c4a778f52d8a
Parent:
11:283df715f241
update dependency;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 8:a9ecd7687de7 1 /**
giovannivisentini 8:a9ecd7687de7 2 ******************************************************************************
giovannivisentini 8:a9ecd7687de7 3 * @file Sample_lockWriteTag.cpp
giovannivisentini 9:9742acd0c015 4 * @author ST / Central Labs
giovannivisentini 9:9742acd0c015 5 * @date 03 Dic 2015
giovannivisentini 8:a9ecd7687de7 6 * @brief This demo define a lock/unlock the tag content when the user press
giovannivisentini 8:a9ecd7687de7 7 * the user button
giovannivisentini 8:a9ecd7687de7 8 ******************************************************************************
giovannivisentini 8:a9ecd7687de7 9 *
giovannivisentini 8:a9ecd7687de7 10 * COPYRIGHT(c) 2015 STMicroelectronics
giovannivisentini 8:a9ecd7687de7 11 *
giovannivisentini 8:a9ecd7687de7 12 * Redistribution and use in source and binary forms, with or without modification,
giovannivisentini 8:a9ecd7687de7 13 * are permitted provided that the following conditions are met:
giovannivisentini 8:a9ecd7687de7 14 * 1. Redistributions of source code must retain the above copyright notice,
giovannivisentini 8:a9ecd7687de7 15 * this list of conditions and the following disclaimer.
giovannivisentini 8:a9ecd7687de7 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
giovannivisentini 8:a9ecd7687de7 17 * this list of conditions and the following disclaimer in the documentation
giovannivisentini 8:a9ecd7687de7 18 * and/or other materials provided with the distribution.
giovannivisentini 8:a9ecd7687de7 19 * 3. Neither the name of STMicroelectronics nor the names of its contributors
giovannivisentini 8:a9ecd7687de7 20 * may be used to endorse or promote products derived from this software
giovannivisentini 8:a9ecd7687de7 21 * without specific prior written permission.
giovannivisentini 8:a9ecd7687de7 22 *
giovannivisentini 8:a9ecd7687de7 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
giovannivisentini 8:a9ecd7687de7 24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
giovannivisentini 8:a9ecd7687de7 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
giovannivisentini 8:a9ecd7687de7 26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
giovannivisentini 8:a9ecd7687de7 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
giovannivisentini 8:a9ecd7687de7 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
giovannivisentini 8:a9ecd7687de7 29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
giovannivisentini 8:a9ecd7687de7 30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
giovannivisentini 8:a9ecd7687de7 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
giovannivisentini 8:a9ecd7687de7 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giovannivisentini 8:a9ecd7687de7 33 *
giovannivisentini 8:a9ecd7687de7 34 ******************************************************************************
giovannivisentini 8:a9ecd7687de7 35 */
giovannivisentini 8:a9ecd7687de7 36
giovannivisentini 8:a9ecd7687de7 37 #include "mbed.h"
giovannivisentini 8:a9ecd7687de7 38
giovannivisentini 8:a9ecd7687de7 39 #include "X_NUCLEO_NFC01A1.h"
giovannivisentini 8:a9ecd7687de7 40
giovannivisentini 11:283df715f241 41 static volatile bool buttonPress=false; /// true when the user press the message
giovannivisentini 8:a9ecd7687de7 42
giovannivisentini 11:283df715f241 43 /**
giovannivisentini 12:c4a778f52d8a 44 * Call back called when the user press the button.
giovannivisentini 11:283df715f241 45 */
giovannivisentini 8:a9ecd7687de7 46 static void setButtonPress(){
giovannivisentini 8:a9ecd7687de7 47 buttonPress=true;
giovannivisentini 8:a9ecd7687de7 48 }//if buttonPress
giovannivisentini 8:a9ecd7687de7 49
giovannivisentini 8:a9ecd7687de7 50 /**
giovannivisentini 12:c4a778f52d8a 51 * Return the led that tell if the tag is read only.
giovannivisentini 12:c4a778f52d8a 52 * @param nfcNucleo Board where the lead lives.
giovannivisentini 8:a9ecd7687de7 53 * @return let to switch on if the tag is read only
giovannivisentini 8:a9ecd7687de7 54 */
giovannivisentini 8:a9ecd7687de7 55 static inline DigitalOut& getReadOnlyLed(X_NUCLEO_NFC01A1 *nfcNucleo){
giovannivisentini 8:a9ecd7687de7 56 return nfcNucleo->getLed2();
giovannivisentini 8:a9ecd7687de7 57 }
giovannivisentini 8:a9ecd7687de7 58
giovannivisentini 8:a9ecd7687de7 59 /**
giovannivisentini 12:c4a778f52d8a 60 * Return the led that tell if the tag is write only.
giovannivisentini 12:c4a778f52d8a 61 * @param nfcNucleo Board where the lead lives.
giovannivisentini 8:a9ecd7687de7 62 * @return let to switch on if the tag is write only
giovannivisentini 8:a9ecd7687de7 63 */
giovannivisentini 8:a9ecd7687de7 64 static inline DigitalOut& getWriteableLed(X_NUCLEO_NFC01A1 *nfcNucleo){
giovannivisentini 8:a9ecd7687de7 65 return nfcNucleo->getLed3();
giovannivisentini 8:a9ecd7687de7 66 }
giovannivisentini 8:a9ecd7687de7 67
giovannivisentini 8:a9ecd7687de7 68 /**
giovannivisentini 12:c4a778f52d8a 69 * Set the tag as read only and switch the led on.
giovannivisentini 12:c4a778f52d8a 70 * @param nfcNucleo Board where the lead lives.
giovannivisentini 8:a9ecd7687de7 71 * @return true if the change has effect
giovannivisentini 8:a9ecd7687de7 72 */
giovannivisentini 9:9742acd0c015 73 bool enableReadOnly( X_NUCLEO_NFC01A1 *nfcNucleo){
giovannivisentini 8:a9ecd7687de7 74
giovannivisentini 9:9742acd0c015 75 if(nfcNucleo->getM24SR().enableReadOnly()==NFC_SUCCESS){
giovannivisentini 8:a9ecd7687de7 76 getReadOnlyLed(nfcNucleo)=true;
giovannivisentini 8:a9ecd7687de7 77 getWriteableLed(nfcNucleo)=false;
giovannivisentini 8:a9ecd7687de7 78 return true;
giovannivisentini 8:a9ecd7687de7 79 }else
giovannivisentini 8:a9ecd7687de7 80 return false;
giovannivisentini 8:a9ecd7687de7 81 }
giovannivisentini 8:a9ecd7687de7 82
giovannivisentini 8:a9ecd7687de7 83 /**
giovannivisentini 12:c4a778f52d8a 84 * Set the tag as read/write and switch the led on.
giovannivisentini 12:c4a778f52d8a 85 * @param nfcNucleo Board where the lead lives.
giovannivisentini 8:a9ecd7687de7 86 * @return true if the change has effect
giovannivisentini 8:a9ecd7687de7 87 */
giovannivisentini 9:9742acd0c015 88 bool disableReadOnly( X_NUCLEO_NFC01A1 *nfcNucleo){
giovannivisentini 8:a9ecd7687de7 89
giovannivisentini 9:9742acd0c015 90 if(nfcNucleo->getM24SR().disableReadOnly()==NFC_SUCCESS){
giovannivisentini 8:a9ecd7687de7 91 getReadOnlyLed(nfcNucleo)=false;
giovannivisentini 8:a9ecd7687de7 92 getWriteableLed(nfcNucleo)=true;
giovannivisentini 8:a9ecd7687de7 93 return true;
giovannivisentini 8:a9ecd7687de7 94 }else
giovannivisentini 8:a9ecd7687de7 95 return false;
giovannivisentini 8:a9ecd7687de7 96 }
giovannivisentini 8:a9ecd7687de7 97
giovannivisentini 11:283df715f241 98 /**
giovannivisentini 12:c4a778f52d8a 99 * When the user press the button enable/disable the tag write protetion.
giovannivisentini 11:283df715f241 100 */
giovannivisentini 8:a9ecd7687de7 101 void sample_lockTagContent() {
giovannivisentini 8:a9ecd7687de7 102 Serial console(SERIAL_TX, SERIAL_RX);
giovannivisentini 11:283df715f241 103
giovannivisentini 11:283df715f241 104 //instance the board with the default paramiters
giovannivisentini 8:a9ecd7687de7 105 I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);
giovannivisentini 8:a9ecd7687de7 106 X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel);
giovannivisentini 9:9742acd0c015 107
giovannivisentini 12:c4a778f52d8a 108 //retrieve the NdefLib interface
giovannivisentini 9:9742acd0c015 109 NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag();
giovannivisentini 8:a9ecd7687de7 110
giovannivisentini 8:a9ecd7687de7 111 //when the user press the button set buttonPress to true
giovannivisentini 8:a9ecd7687de7 112 InterruptIn userButton(USER_BUTTON);
giovannivisentini 8:a9ecd7687de7 113 userButton.fall(setButtonPress);
giovannivisentini 9:9742acd0c015 114
giovannivisentini 11:283df715f241 115 //restore the board as read/write, set the led1 if it is ok
giovannivisentini 8:a9ecd7687de7 116 tag.openSession(true);
giovannivisentini 11:283df715f241 117 nfcNucleo->getLed1()= (nfcNucleo->getM24SR().disableAllPassword()==NFC_SUCCESS);
giovannivisentini 8:a9ecd7687de7 118 tag.closeSession();
giovannivisentini 11:283df715f241 119
giovannivisentini 11:283df715f241 120 // now we are in read/write -> switch on the writable led
giovannivisentini 8:a9ecd7687de7 121 getReadOnlyLed(nfcNucleo)=false;
giovannivisentini 8:a9ecd7687de7 122 getWriteableLed(nfcNucleo)=true;
giovannivisentini 8:a9ecd7687de7 123 bool tagIsLock=false;
giovannivisentini 8:a9ecd7687de7 124
giovannivisentini 8:a9ecd7687de7 125 while(true){
giovannivisentini 8:a9ecd7687de7 126
giovannivisentini 8:a9ecd7687de7 127 if(buttonPress){
giovannivisentini 8:a9ecd7687de7 128 if(tag.openSession()){
giovannivisentini 8:a9ecd7687de7 129 bool changeStatusResult;
giovannivisentini 11:283df715f241 130 //switch the status
giovannivisentini 8:a9ecd7687de7 131 if(tagIsLock)
giovannivisentini 9:9742acd0c015 132 changeStatusResult=enableReadOnly(nfcNucleo);
giovannivisentini 8:a9ecd7687de7 133 else
giovannivisentini 9:9742acd0c015 134 changeStatusResult=disableReadOnly(nfcNucleo);
giovannivisentini 8:a9ecd7687de7 135
giovannivisentini 8:a9ecd7687de7 136 if(changeStatusResult)
giovannivisentini 8:a9ecd7687de7 137 tagIsLock=!tagIsLock;
giovannivisentini 8:a9ecd7687de7 138 else
giovannivisentini 12:c4a778f52d8a 139 console.printf("Error change the tag status\r\n");
giovannivisentini 8:a9ecd7687de7 140 tag.closeSession();
giovannivisentini 8:a9ecd7687de7 141 }else{
giovannivisentini 12:c4a778f52d8a 142 console.printf("Error opening the session, tag is in use\r\n");
giovannivisentini 8:a9ecd7687de7 143 }
giovannivisentini 8:a9ecd7687de7 144 buttonPress=false;
giovannivisentini 8:a9ecd7687de7 145 }//if
giovannivisentini 8:a9ecd7687de7 146
giovannivisentini 8:a9ecd7687de7 147 //wait next event
giovannivisentini 8:a9ecd7687de7 148 __WFE();
giovannivisentini 8:a9ecd7687de7 149 }//while
giovannivisentini 8:a9ecd7687de7 150
giovannivisentini 8:a9ecd7687de7 151 }//sample_countClick
giovannivisentini 8:a9ecd7687de7 152
giovannivisentini 8:a9ecd7687de7 153
giovannivisentini 8:a9ecd7687de7 154
giovannivisentini 8:a9ecd7687de7 155
giovannivisentini 8:a9ecd7687de7 156