![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
M2M im2ag M2PGI
Dependencies: mbed X_NUCLEO_NFC01A1 NDefLib
main.cpp@8:ccf083f26f3a, 2016-03-22 (annotated)
- Committer:
- Bayle38
- Date:
- Tue Mar 22 14:19:59 2016 +0000
- Revision:
- 8:ccf083f26f3a
- Parent:
- 7:536aa5009d53
- Child:
- 9:3e718f4a0fbc
Prout
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThibaudB | 2:7183a6189b65 | 1 | |
hellmak | 0:4d794bbc5de1 | 2 | #include "mbed.h" |
hellmak | 0:4d794bbc5de1 | 3 | |
ThibaudB | 2:7183a6189b65 | 4 | #include "X_NUCLEO_NFC01A1.h" |
ThibaudB | 2:7183a6189b65 | 5 | #include "NDefLib/NDefNfcTag.h" |
hellmak | 4:47b9a09a74bf | 6 | #include "NDefLib/Message.h" |
ThibaudB | 2:7183a6189b65 | 7 | #include "NDefLib/RecordType/RecordURI.h" |
hellmak | 4:47b9a09a74bf | 8 | Serial pc(SERIAL_TX, SERIAL_RX); |
hellmak | 4:47b9a09a74bf | 9 | DigitalOut myled(LED1); |
Bayle38 | 7:536aa5009d53 | 10 | InterruptIn interrupt(D12); |
ThibaudB | 2:7183a6189b65 | 11 | /** |
ThibaudB | 2:7183a6189b65 | 12 | * Write a Ndef URI message linking to st.com site. |
ThibaudB | 2:7183a6189b65 | 13 | */ |
hellmak | 4:47b9a09a74bf | 14 | void init(){ |
hellmak | 4:47b9a09a74bf | 15 | pc.baud(115200); |
hellmak | 4:47b9a09a74bf | 16 | pc.format(8,SerialBase::None, 1); |
hellmak | 4:47b9a09a74bf | 17 | } |
hellmak | 4:47b9a09a74bf | 18 | |
Bayle38 | 6:67e0ae676619 | 19 | void tagDetect(){ |
Bayle38 | 6:67e0ae676619 | 20 | pc.printf("Detected !!! \n"); |
Bayle38 | 6:67e0ae676619 | 21 | } |
Bayle38 | 6:67e0ae676619 | 22 | |
ThibaudB | 2:7183a6189b65 | 23 | int main(void){ |
hellmak | 4:47b9a09a74bf | 24 | init(); |
hellmak | 4:47b9a09a74bf | 25 | pc.printf("System Init now !\n"); |
hellmak | 4:47b9a09a74bf | 26 | myled = 0; // LED is OFF |
ThibaudB | 2:7183a6189b65 | 27 | |
ThibaudB | 2:7183a6189b65 | 28 | //use default board pinout |
ThibaudB | 2:7183a6189b65 | 29 | I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); |
Bayle38 | 8:ccf083f26f3a | 30 | X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,NULL, |
ThibaudB | 2:7183a6189b65 | 31 | X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN, |
ThibaudB | 2:7183a6189b65 | 32 | X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN, |
ThibaudB | 2:7183a6189b65 | 33 | X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN); |
Bayle38 | 7:536aa5009d53 | 34 | nfcNucleo->getLed1()=0; |
Bayle38 | 7:536aa5009d53 | 35 | nfcNucleo->getM24SR().ManageI2CGPO(I2C_ANSWER_READY); |
ThibaudB | 2:7183a6189b65 | 36 | |
ThibaudB | 2:7183a6189b65 | 37 | //retrieve the NdefLib interface |
ThibaudB | 2:7183a6189b65 | 38 | NDefLib::NDefNfcTag& tag =nfcNucleo->getM24SR().getNDefTag(); |
hellmak | 4:47b9a09a74bf | 39 | printf("System Init done: !\n"); |
ThibaudB | 2:7183a6189b65 | 40 | |
ThibaudB | 2:7183a6189b65 | 41 | //open the i2c session with the nfc chip |
ThibaudB | 2:7183a6189b65 | 42 | if(tag.openSession()){ |
hellmak | 4:47b9a09a74bf | 43 | printf("Session opened\n"); |
Bayle38 | 7:536aa5009d53 | 44 | interrupt.fall(&tagDetect); |
Bayle38 | 6:67e0ae676619 | 45 | |
ThibaudB | 2:7183a6189b65 | 46 | nfcNucleo->getLed1()=1; |
ThibaudB | 2:7183a6189b65 | 47 | |
ThibaudB | 2:7183a6189b65 | 48 | //create the NDef message and record |
ThibaudB | 2:7183a6189b65 | 49 | NDefLib::Message msg; |
ThibaudB | 2:7183a6189b65 | 50 | NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com"); |
ThibaudB | 2:7183a6189b65 | 51 | msg.addRecord(&rUri); |
ThibaudB | 2:7183a6189b65 | 52 | |
ThibaudB | 2:7183a6189b65 | 53 | //write the tag |
ThibaudB | 2:7183a6189b65 | 54 | if(tag.write(msg)){ |
hellmak | 4:47b9a09a74bf | 55 | printf("Tag written\n"); |
ThibaudB | 2:7183a6189b65 | 56 | nfcNucleo->getLed2()=1; |
hellmak | 4:47b9a09a74bf | 57 | |
ThibaudB | 2:7183a6189b65 | 58 | }else{ |
hellmak | 4:47b9a09a74bf | 59 | printf("Error writing \n"); |
ThibaudB | 2:7183a6189b65 | 60 | }//if-else |
hellmak | 4:47b9a09a74bf | 61 | NDefLib::Message message; |
hellmak | 4:47b9a09a74bf | 62 | while(!tag.read(&message)){ |
hellmak | 4:47b9a09a74bf | 63 | } |
hellmak | 5:b75a06d210fd | 64 | |
hellmak | 4:47b9a09a74bf | 65 | |
hellmak | 4:47b9a09a74bf | 66 | uint8_t buffer[64]; |
hellmak | 4:47b9a09a74bf | 67 | buffer[0]=0; |
hellmak | 4:47b9a09a74bf | 68 | buffer[1]=3; |
hellmak | 4:47b9a09a74bf | 69 | |
hellmak | 4:47b9a09a74bf | 70 | message.write(buffer); |
hellmak | 4:47b9a09a74bf | 71 | buffer[63]=0; |
hellmak | 4:47b9a09a74bf | 72 | for(int i=0; i< 63; i++){ |
hellmak | 4:47b9a09a74bf | 73 | printf("%c",buffer[i]); |
hellmak | 4:47b9a09a74bf | 74 | } |
hellmak | 4:47b9a09a74bf | 75 | printf("\n"); |
ThibaudB | 2:7183a6189b65 | 76 | //close the i2c session |
ThibaudB | 2:7183a6189b65 | 77 | if(tag.closeSession()){ |
hellmak | 4:47b9a09a74bf | 78 | myled = 1; // LED is ON |
hellmak | 4:47b9a09a74bf | 79 | printf("Session closed\n"); |
ThibaudB | 2:7183a6189b65 | 80 | nfcNucleo->getLed3()=1; |
ThibaudB | 2:7183a6189b65 | 81 | }else{ |
hellmak | 4:47b9a09a74bf | 82 | printf("Error closing the session\n"); |
ThibaudB | 2:7183a6189b65 | 83 | }//if-else |
ThibaudB | 2:7183a6189b65 | 84 | |
ThibaudB | 2:7183a6189b65 | 85 | }else |
hellmak | 4:47b9a09a74bf | 86 | printf("Error opening the session\n"); |
hellmak | 4:47b9a09a74bf | 87 | |
ThibaudB | 2:7183a6189b65 | 88 | } |