Demo NFC

Dependencies:   NDefLib X_NUCLEO_NFC01A1 mbed

Fork of HelloWorld_NFC01A1 by ST

Committer:
anitarusso88_
Date:
Fri Feb 05 09:59:52 2016 +0000
Revision:
17:b5fab6850e5c
Parent:
14:1b99908360c5
Demo NFC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 0:151ae20d098f 1 #include "mbed.h"
giovannivisentini 0:151ae20d098f 2
giovannivisentini 0:151ae20d098f 3 #include "X_NUCLEO_NFC01A1.h"
giovannivisentini 4:4145a2db3349 4 #include "NDefLib/NDefNfcTag.h"
giovannivisentini 0:151ae20d098f 5 #include "NDefLib/RecordType/RecordURI.h"
anitarusso88_ 17:b5fab6850e5c 6 #include "NDefLib/RecordType/RecordText.h"
giovannivisentini 0:151ae20d098f 7
anitarusso88_ 17:b5fab6850e5c 8
anitarusso88_ 17:b5fab6850e5c 9 int updateM24SR(X_NUCLEO_NFC01A1* nfcNucleo, char* data){
anitarusso88_ 17:b5fab6850e5c 10
anitarusso88_ 17:b5fab6850e5c 11 //retrieve the NdefLib interface
anitarusso88_ 17:b5fab6850e5c 12 NDefLib::NDefNfcTag& tag =nfcNucleo->getM24SR().getNDefTag();
anitarusso88_ 17:b5fab6850e5c 13 printf("System Init done: !\n\r");
anitarusso88_ 17:b5fab6850e5c 14 /*SESSION 1*/
anitarusso88_ 17:b5fab6850e5c 15 //open the i2c session with the nfc chip
anitarusso88_ 17:b5fab6850e5c 16 if(tag.openSession()){
anitarusso88_ 17:b5fab6850e5c 17 printf("Session opened\n\r");
anitarusso88_ 17:b5fab6850e5c 18 nfcNucleo->getLed1()=1;
anitarusso88_ 17:b5fab6850e5c 19
anitarusso88_ 17:b5fab6850e5c 20 //create the NDef message and record
anitarusso88_ 17:b5fab6850e5c 21 NDefLib::Message msg;
anitarusso88_ 17:b5fab6850e5c 22 NDefLib::RecordText rText(data);
anitarusso88_ 17:b5fab6850e5c 23 msg.addRecord(&rText);
anitarusso88_ 17:b5fab6850e5c 24
anitarusso88_ 17:b5fab6850e5c 25 //write the tag
anitarusso88_ 17:b5fab6850e5c 26 if(tag.write(msg)){
anitarusso88_ 17:b5fab6850e5c 27 printf("Tag written\n\r");
anitarusso88_ 17:b5fab6850e5c 28 nfcNucleo->getLed2()=1;
anitarusso88_ 17:b5fab6850e5c 29
anitarusso88_ 17:b5fab6850e5c 30 }else{
anitarusso88_ 17:b5fab6850e5c 31 printf("Error writing \n\r");
anitarusso88_ 17:b5fab6850e5c 32 }//if-else
anitarusso88_ 17:b5fab6850e5c 33
anitarusso88_ 17:b5fab6850e5c 34 //close the i2c session
anitarusso88_ 17:b5fab6850e5c 35 if(tag.closeSession()){
anitarusso88_ 17:b5fab6850e5c 36 printf("Session closed\n\r");
anitarusso88_ 17:b5fab6850e5c 37 nfcNucleo->getLed3()=1;
anitarusso88_ 17:b5fab6850e5c 38 }else{
anitarusso88_ 17:b5fab6850e5c 39 printf("Error closing the session\n\r");
anitarusso88_ 17:b5fab6850e5c 40 }//if-else
anitarusso88_ 17:b5fab6850e5c 41
anitarusso88_ 17:b5fab6850e5c 42 }else
anitarusso88_ 17:b5fab6850e5c 43 printf("Error opening the session\n\r");
anitarusso88_ 17:b5fab6850e5c 44 }
anitarusso88_ 17:b5fab6850e5c 45
giovannivisentini 0:151ae20d098f 46 int main(void){
giovannivisentini 0:151ae20d098f 47
giovannivisentini 6:44204e733f96 48 //use default board pinout
giovannivisentini 0:151ae20d098f 49 I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);
giovannivisentini 14:1b99908360c5 50 X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,NULL,
giovannivisentini 14:1b99908360c5 51 X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN,
giovannivisentini 14:1b99908360c5 52 X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN,
giovannivisentini 14:1b99908360c5 53 X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN);
giovannivisentini 0:151ae20d098f 54
giovannivisentini 6:44204e733f96 55 //retrieve the NdefLib interface
giovannivisentini 4:4145a2db3349 56 NDefLib::NDefNfcTag& tag =nfcNucleo->getM24SR().getNDefTag();
giovannivisentini 7:435297dd022d 57 printf("System Init done: !\n\r");
giovannivisentini 6:44204e733f96 58
anitarusso88_ 17:b5fab6850e5c 59 updateM24SR(nfcNucleo,"CIAO ANITA");
anitarusso88_ 17:b5fab6850e5c 60
giovannivisentini 11:925a5a491a5c 61 }