Example application for the X-Nucleo-NFC01A1 Dynamic NFC Tag board.
Dependencies: NDefLib X_NUCLEO_NFC01A1 mbed
Fork of HelloWorld_NFC01A1 by
main.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file main.cpp 00004 * @author ST Central Labs 00005 * @version V2.0.0 00006 * @date 28 Apr 2017 00007 * @brief This demo writes an ndef message with an url inside. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 #include "mbed.h" 00039 #include "XNucleoNFC01A1.h" 00040 #include "NDefLib/NDefNfcTag.h" 00041 #include "NDefLib/RecordType/RecordURI.h" 00042 00043 /** 00044 * Write a Ndef URI message linking to st.com site. 00045 * Write an NDef message with a Uri record linking the st.com site 00046 * @param nfcNucleo expansion board where write the NDef message 00047 */ 00048 static void write_url(XNucleoNFC01A1 *nfcNucleo) { 00049 //retrieve the NdefLib interface 00050 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag(); 00051 00052 //open the i2c session with the nfc chip 00053 if (tag.open_session()) { 00054 printf("Session opened\n\r"); 00055 nfcNucleo->get_led1()=1; 00056 00057 //create the NDef message and record 00058 NDefLib::Message msg; 00059 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com"); 00060 msg.add_record(&rUri); 00061 00062 //write the tag 00063 if (tag.write(msg)) { 00064 printf("Tag written\n\r"); 00065 nfcNucleo->get_led2()=1; 00066 } else { 00067 printf("Error writing \n\r"); 00068 }//if-else 00069 00070 //close the i2c session 00071 if (tag.close_session()) { 00072 printf("Session closed\n\r"); 00073 nfcNucleo->get_led3()=1; 00074 } else { 00075 printf("Error closing the session\n\r"); 00076 }//if-else 00077 00078 } else { 00079 printf("Error opening the session\n\r"); 00080 } 00081 } 00082 00083 /** 00084 * Read a NDef message and print content and type of all the uri record inside 00085 * the message 00086 * @param nfcNucleo expansion board from where read the message 00087 */ 00088 static void read_url(XNucleoNFC01A1 *nfcNucleo) { 00089 //retrieve the NdefLib interface 00090 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag(); 00091 00092 //open the i2c session with the nfc chip 00093 if (tag.open_session()) { 00094 printf("Session opened\n\r"); 00095 nfcNucleo->get_led1() = 1; 00096 00097 //create the NDef message and record 00098 NDefLib::Message msg; 00099 00100 //read the tag 00101 if (tag.read(&msg)) { 00102 const uint32_t nRecords = msg.get_N_records(); 00103 printf("Tag read: %d record\n\r", msg.get_N_records()); 00104 for (int i =0 ;i<nRecords ;i++) { 00105 if (msg[i]->get_type()== NDefLib::Record::TYPE_URI) { 00106 NDefLib::RecordURI *rUri = (NDefLib::RecordURI *)msg[i]; 00107 printf("UriType: %x\n\rUriContent: %s\n\r", 00108 rUri->get_uri_id(), 00109 rUri->get_content().c_str()); 00110 }//if 00111 }//for 00112 //free the read records 00113 NDefLib::Message::remove_and_delete_all_record(msg); 00114 } else { 00115 printf("Error Reading \n\r"); 00116 }//if-else 00117 00118 //close the i2c session 00119 if (tag.close_session()) { 00120 printf("Session closed\n\r"); 00121 nfcNucleo->get_led3() = 1; 00122 } else { 00123 printf("Error closing the session\n\r"); 00124 }//if-else 00125 00126 } else { 00127 printf("Error opening the session\n\r"); 00128 } 00129 } 00130 00131 static volatile bool buttonPress=false; 00132 00133 static void set_button_press(){ 00134 buttonPress=true; 00135 }//if buttonPress 00136 00137 /** 00138 * Write a Ndef URI message linking to st.com site and than read the message from 00139 * the Nfc tag. 00140 * 00141 */ 00142 int main(void){ 00143 00144 //use default board pinout 00145 I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN); 00146 XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel,NULL, 00147 XNucleoNFC01A1::DEFAULT_GPO_PIN,XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN, 00148 XNucleoNFC01A1::DEFAULT_LED1_PIN,XNucleoNFC01A1::DEFAULT_LED2_PIN, 00149 XNucleoNFC01A1::DEFAULT_LED3_PIN); 00150 00151 printf("System Init done: !\n\r"); 00152 00153 // write an URI message 00154 write_url(nfcNucleo); 00155 00156 //if run on a nucleo board enable the user button for read the ndef record 00157 #if defined(TARGET_STM) 00158 InterruptIn userButton(USER_BUTTON); 00159 userButton.fall(set_button_press); 00160 while(true){ 00161 if (buttonPress) { 00162 read_url(nfcNucleo); 00163 buttonPress=false; 00164 }//if 00165 //wait next event 00166 __WFE(); 00167 }//while 00168 #else 00169 read_url(nfcNucleo); 00170 #endif 00171 } 00172 00173 00174 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed Jul 13 2022 11:33:16 by 1.7.2