Demo NFC

Dependencies:   NDefLib X_NUCLEO_NFC01A1 mbed

Fork of HelloWorld_NFC01A1 by ST

Committer:
giovannivisentini
Date:
Thu Jan 14 07:55:10 2016 +0000
Revision:
6:44204e733f96
Parent:
4:4145a2db3349
Child:
7:435297dd022d
update documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 0:151ae20d098f 1 /**
giovannivisentini 0:151ae20d098f 2 ******************************************************************************
giovannivisentini 0:151ae20d098f 3 * @file main.cpp
giovannivisentini 4:4145a2db3349 4 * @author ST Central Labs
giovannivisentini 6:44204e733f96 5 * @version V1.0.0
giovannivisentini 4:4145a2db3349 6 * @date 21 Dic 2015
giovannivisentini 6:44204e733f96 7 * @brief This demo writes an ndef message with an url inside.
giovannivisentini 0:151ae20d098f 8 ******************************************************************************
giovannivisentini 6:44204e733f96 9 * @attention
giovannivisentini 0:151ae20d098f 10 *
giovannivisentini 6:44204e733f96 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
giovannivisentini 0:151ae20d098f 12 *
giovannivisentini 0:151ae20d098f 13 * Redistribution and use in source and binary forms, with or without modification,
giovannivisentini 0:151ae20d098f 14 * are permitted provided that the following conditions are met:
giovannivisentini 0:151ae20d098f 15 * 1. Redistributions of source code must retain the above copyright notice,
giovannivisentini 0:151ae20d098f 16 * this list of conditions and the following disclaimer.
giovannivisentini 0:151ae20d098f 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
giovannivisentini 0:151ae20d098f 18 * this list of conditions and the following disclaimer in the documentation
giovannivisentini 0:151ae20d098f 19 * and/or other materials provided with the distribution.
giovannivisentini 0:151ae20d098f 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
giovannivisentini 0:151ae20d098f 21 * may be used to endorse or promote products derived from this software
giovannivisentini 0:151ae20d098f 22 * without specific prior written permission.
giovannivisentini 0:151ae20d098f 23 *
giovannivisentini 0:151ae20d098f 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
giovannivisentini 0:151ae20d098f 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
giovannivisentini 0:151ae20d098f 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
giovannivisentini 0:151ae20d098f 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
giovannivisentini 0:151ae20d098f 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
giovannivisentini 0:151ae20d098f 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
giovannivisentini 0:151ae20d098f 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
giovannivisentini 0:151ae20d098f 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
giovannivisentini 0:151ae20d098f 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
giovannivisentini 0:151ae20d098f 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giovannivisentini 0:151ae20d098f 34 *
giovannivisentini 0:151ae20d098f 35 ******************************************************************************
giovannivisentini 0:151ae20d098f 36 */
giovannivisentini 0:151ae20d098f 37
giovannivisentini 0:151ae20d098f 38 #include "mbed.h"
giovannivisentini 0:151ae20d098f 39
giovannivisentini 0:151ae20d098f 40 #include "X_NUCLEO_NFC01A1.h"
giovannivisentini 4:4145a2db3349 41 #include "NDefLib/NDefNfcTag.h"
giovannivisentini 0:151ae20d098f 42 #include "NDefLib/RecordType/RecordURI.h"
giovannivisentini 0:151ae20d098f 43
giovannivisentini 0:151ae20d098f 44 /**
giovannivisentini 6:44204e733f96 45 * Write a Ndef URI message linking to st.com site.
giovannivisentini 0:151ae20d098f 46 */
giovannivisentini 0:151ae20d098f 47 int main(void){
giovannivisentini 0:151ae20d098f 48 Serial pc(SERIAL_TX, SERIAL_RX);
giovannivisentini 0:151ae20d098f 49
giovannivisentini 6:44204e733f96 50 //use default board pinout
giovannivisentini 0:151ae20d098f 51 I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);
giovannivisentini 0:151ae20d098f 52 X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel);
giovannivisentini 0:151ae20d098f 53
giovannivisentini 6:44204e733f96 54 //retrieve the NdefLib interface
giovannivisentini 4:4145a2db3349 55 NDefLib::NDefNfcTag& tag =nfcNucleo->getM24SR().getNDefTag();
giovannivisentini 0:151ae20d098f 56 pc.printf("System Init done: !\n\r");
giovannivisentini 6:44204e733f96 57
giovannivisentini 0:151ae20d098f 58 //open the i2c session with the nfc chip
giovannivisentini 0:151ae20d098f 59 if(tag.openSession()){
giovannivisentini 1:eff1b8998438 60 pc.printf("Session open\n\r");
giovannivisentini 0:151ae20d098f 61 nfcNucleo->getLed1()=1;
giovannivisentini 0:151ae20d098f 62
giovannivisentini 0:151ae20d098f 63 //create the NDef message and record
giovannivisentini 0:151ae20d098f 64 NDefLib::Message msg;
giovannivisentini 0:151ae20d098f 65 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com");
giovannivisentini 0:151ae20d098f 66 msg.addRecord(&rUri);
giovannivisentini 0:151ae20d098f 67
giovannivisentini 0:151ae20d098f 68 //write the tag
giovannivisentini 0:151ae20d098f 69 if(tag.write(msg)){
giovannivisentini 0:151ae20d098f 70 pc.printf("Tag writed \n\r");
giovannivisentini 0:151ae20d098f 71 nfcNucleo->getLed2()=1;
giovannivisentini 1:eff1b8998438 72 }else{
giovannivisentini 1:eff1b8998438 73 pc.printf("Error writing \n\r");
giovannivisentini 1:eff1b8998438 74 }//if-else
giovannivisentini 0:151ae20d098f 75
giovannivisentini 0:151ae20d098f 76 //close the i2c session
giovannivisentini 1:eff1b8998438 77 if(tag.closeSession()){
giovannivisentini 1:eff1b8998438 78 pc.printf("Session closed\n\r");
giovannivisentini 1:eff1b8998438 79 nfcNucleo->getLed3()=1;
giovannivisentini 1:eff1b8998438 80 }else{
giovannivisentini 0:151ae20d098f 81 pc.printf("Error Closing the session\n\r");
giovannivisentini 1:eff1b8998438 82 }//if-else
giovannivisentini 1:eff1b8998438 83
giovannivisentini 0:151ae20d098f 84 }else
giovannivisentini 0:151ae20d098f 85 pc.printf("Error open Session\n\r");
giovannivisentini 0:151ae20d098f 86 }