0

Dependencies:   mbed NDefLib X_NUCLEO_NFC01A1

Dependents:   Projekt KPG Program_KPG_2019

Committer:
Davidroid
Date:
Wed Jul 12 14:22:42 2017 +0000
Revision:
23:05ef41b77f9b
Parent:
20:3a6e6ae2d691
Updated to fit ARM coding style.

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 20:3a6e6ae2d691 5 * @version V2.0.0
giovannivisentini 20:3a6e6ae2d691 6 * @date 28 Apr 2017
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 11:925a5a491a5c 37
giovannivisentini 0:151ae20d098f 38 #include "mbed.h"
giovannivisentini 20:3a6e6ae2d691 39 #include "XNucleoNFC01A1.h"
giovannivisentini 4:4145a2db3349 40 #include "NDefLib/NDefNfcTag.h"
giovannivisentini 0:151ae20d098f 41 #include "NDefLib/RecordType/RecordURI.h"
giovannivisentini 0:151ae20d098f 42
giovannivisentini 0:151ae20d098f 43 /**
giovannivisentini 6:44204e733f96 44 * Write a Ndef URI message linking to st.com site.
giovannivisentini 17:22e6c00f3a4c 45 * Write an NDef message with a Uri record linking the st.com site
giovannivisentini 17:22e6c00f3a4c 46 * @param nfcNucleo expansion board where write the NDef message
giovannivisentini 17:22e6c00f3a4c 47 */
Davidroid 23:05ef41b77f9b 48 static void write_url(XNucleoNFC01A1 *nfcNucleo) {
giovannivisentini 6:44204e733f96 49 //retrieve the NdefLib interface
giovannivisentini 20:3a6e6ae2d691 50 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
giovannivisentini 6:44204e733f96 51
giovannivisentini 0:151ae20d098f 52 //open the i2c session with the nfc chip
Davidroid 23:05ef41b77f9b 53 if (tag.open_session()) {
giovannivisentini 14:1b99908360c5 54 printf("Session opened\n\r");
giovannivisentini 20:3a6e6ae2d691 55 nfcNucleo->get_led1()=1;
giovannivisentini 0:151ae20d098f 56
giovannivisentini 0:151ae20d098f 57 //create the NDef message and record
giovannivisentini 0:151ae20d098f 58 NDefLib::Message msg;
giovannivisentini 0:151ae20d098f 59 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com");
giovannivisentini 20:3a6e6ae2d691 60 msg.add_record(&rUri);
giovannivisentini 0:151ae20d098f 61
giovannivisentini 0:151ae20d098f 62 //write the tag
Davidroid 23:05ef41b77f9b 63 if (tag.write(msg)) {
giovannivisentini 14:1b99908360c5 64 printf("Tag written\n\r");
giovannivisentini 20:3a6e6ae2d691 65 nfcNucleo->get_led2()=1;
Davidroid 23:05ef41b77f9b 66 } else {
giovannivisentini 7:435297dd022d 67 printf("Error writing \n\r");
giovannivisentini 1:eff1b8998438 68 }//if-else
giovannivisentini 0:151ae20d098f 69
giovannivisentini 0:151ae20d098f 70 //close the i2c session
Davidroid 23:05ef41b77f9b 71 if (tag.close_session()) {
giovannivisentini 7:435297dd022d 72 printf("Session closed\n\r");
giovannivisentini 20:3a6e6ae2d691 73 nfcNucleo->get_led3()=1;
Davidroid 23:05ef41b77f9b 74 } else {
giovannivisentini 14:1b99908360c5 75 printf("Error closing the session\n\r");
giovannivisentini 1:eff1b8998438 76 }//if-else
giovannivisentini 1:eff1b8998438 77
Davidroid 23:05ef41b77f9b 78 } else {
giovannivisentini 14:1b99908360c5 79 printf("Error opening the session\n\r");
Davidroid 23:05ef41b77f9b 80 }
giovannivisentini 17:22e6c00f3a4c 81 }
giovannivisentini 17:22e6c00f3a4c 82
giovannivisentini 17:22e6c00f3a4c 83 /**
giovannivisentini 17:22e6c00f3a4c 84 * Read a NDef message and print content and type of all the uri record inside
giovannivisentini 17:22e6c00f3a4c 85 * the message
giovannivisentini 17:22e6c00f3a4c 86 * @param nfcNucleo expansion board from where read the message
giovannivisentini 17:22e6c00f3a4c 87 */
Davidroid 23:05ef41b77f9b 88 static void read_url(XNucleoNFC01A1 *nfcNucleo) {
giovannivisentini 17:22e6c00f3a4c 89 //retrieve the NdefLib interface
giovannivisentini 20:3a6e6ae2d691 90 NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
giovannivisentini 17:22e6c00f3a4c 91
giovannivisentini 17:22e6c00f3a4c 92 //open the i2c session with the nfc chip
Davidroid 23:05ef41b77f9b 93 if (tag.open_session()) {
giovannivisentini 17:22e6c00f3a4c 94 printf("Session opened\n\r");
Davidroid 23:05ef41b77f9b 95 nfcNucleo->get_led1() = 1;
giovannivisentini 17:22e6c00f3a4c 96
giovannivisentini 17:22e6c00f3a4c 97 //create the NDef message and record
giovannivisentini 17:22e6c00f3a4c 98 NDefLib::Message msg;
giovannivisentini 17:22e6c00f3a4c 99
giovannivisentini 17:22e6c00f3a4c 100 //read the tag
Davidroid 23:05ef41b77f9b 101 if (tag.read(&msg)) {
giovannivisentini 20:3a6e6ae2d691 102 const uint32_t nRecords = msg.get_N_records();
Davidroid 23:05ef41b77f9b 103 printf("Tag read: %d record\n\r", msg.get_N_records());
Davidroid 23:05ef41b77f9b 104 for (int i =0 ;i<nRecords ;i++) {
Davidroid 23:05ef41b77f9b 105 if (msg[i]->get_type()== NDefLib::Record::TYPE_URI) {
giovannivisentini 17:22e6c00f3a4c 106 NDefLib::RecordURI *rUri = (NDefLib::RecordURI *)msg[i];
giovannivisentini 17:22e6c00f3a4c 107 printf("UriType: %x\n\rUriContent: %s\n\r",
giovannivisentini 20:3a6e6ae2d691 108 rUri->get_uri_id(),
giovannivisentini 20:3a6e6ae2d691 109 rUri->get_content().c_str());
giovannivisentini 17:22e6c00f3a4c 110 }//if
giovannivisentini 17:22e6c00f3a4c 111 }//for
giovannivisentini 17:22e6c00f3a4c 112 //free the read records
giovannivisentini 20:3a6e6ae2d691 113 NDefLib::Message::remove_and_delete_all_record(msg);
Davidroid 23:05ef41b77f9b 114 } else {
giovannivisentini 17:22e6c00f3a4c 115 printf("Error Reading \n\r");
giovannivisentini 17:22e6c00f3a4c 116 }//if-else
giovannivisentini 17:22e6c00f3a4c 117
giovannivisentini 17:22e6c00f3a4c 118 //close the i2c session
Davidroid 23:05ef41b77f9b 119 if (tag.close_session()) {
giovannivisentini 17:22e6c00f3a4c 120 printf("Session closed\n\r");
Davidroid 23:05ef41b77f9b 121 nfcNucleo->get_led3() = 1;
Davidroid 23:05ef41b77f9b 122 } else {
giovannivisentini 17:22e6c00f3a4c 123 printf("Error closing the session\n\r");
giovannivisentini 17:22e6c00f3a4c 124 }//if-else
giovannivisentini 17:22e6c00f3a4c 125
Davidroid 23:05ef41b77f9b 126 } else {
giovannivisentini 17:22e6c00f3a4c 127 printf("Error opening the session\n\r");
Davidroid 23:05ef41b77f9b 128 }
giovannivisentini 17:22e6c00f3a4c 129 }
giovannivisentini 17:22e6c00f3a4c 130
giovannivisentini 17:22e6c00f3a4c 131 static volatile bool buttonPress=false;
giovannivisentini 17:22e6c00f3a4c 132
giovannivisentini 20:3a6e6ae2d691 133 static void set_button_press(){
giovannivisentini 17:22e6c00f3a4c 134 buttonPress=true;
giovannivisentini 17:22e6c00f3a4c 135 }//if buttonPress
giovannivisentini 17:22e6c00f3a4c 136
giovannivisentini 17:22e6c00f3a4c 137 /**
giovannivisentini 17:22e6c00f3a4c 138 * Write a Ndef URI message linking to st.com site and than read the message from
giovannivisentini 17:22e6c00f3a4c 139 * the Nfc tag.
giovannivisentini 17:22e6c00f3a4c 140 *
giovannivisentini 17:22e6c00f3a4c 141 */
giovannivisentini 17:22e6c00f3a4c 142 int main(void){
giovannivisentini 17:22e6c00f3a4c 143
giovannivisentini 17:22e6c00f3a4c 144 //use default board pinout
giovannivisentini 20:3a6e6ae2d691 145 I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN);
giovannivisentini 20:3a6e6ae2d691 146 XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel,NULL,
Davidroid 23:05ef41b77f9b 147 XNucleoNFC01A1::DEFAULT_GPO_PIN,XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN,
Davidroid 23:05ef41b77f9b 148 XNucleoNFC01A1::DEFAULT_LED1_PIN,XNucleoNFC01A1::DEFAULT_LED2_PIN,
Davidroid 23:05ef41b77f9b 149 XNucleoNFC01A1::DEFAULT_LED3_PIN);
giovannivisentini 17:22e6c00f3a4c 150
giovannivisentini 17:22e6c00f3a4c 151 printf("System Init done: !\n\r");
giovannivisentini 17:22e6c00f3a4c 152
giovannivisentini 17:22e6c00f3a4c 153 // write an URI message
giovannivisentini 20:3a6e6ae2d691 154 write_url(nfcNucleo);
giovannivisentini 17:22e6c00f3a4c 155
giovannivisentini 17:22e6c00f3a4c 156 //if run on a nucleo board enable the user button for read the ndef record
giovannivisentini 17:22e6c00f3a4c 157 #if defined(TARGET_STM)
giovannivisentini 17:22e6c00f3a4c 158 InterruptIn userButton(USER_BUTTON);
giovannivisentini 20:3a6e6ae2d691 159 userButton.fall(set_button_press);
giovannivisentini 17:22e6c00f3a4c 160 while(true){
Davidroid 23:05ef41b77f9b 161 if (buttonPress) {
giovannivisentini 20:3a6e6ae2d691 162 read_url(nfcNucleo);
giovannivisentini 17:22e6c00f3a4c 163 buttonPress=false;
giovannivisentini 17:22e6c00f3a4c 164 }//if
giovannivisentini 17:22e6c00f3a4c 165 //wait next event
giovannivisentini 17:22e6c00f3a4c 166 __WFE();
giovannivisentini 17:22e6c00f3a4c 167 }//while
giovannivisentini 17:22e6c00f3a4c 168 #else
giovannivisentini 20:3a6e6ae2d691 169 read_url(nfcNucleo);
giovannivisentini 17:22e6c00f3a4c 170 #endif
giovannivisentini 17:22e6c00f3a4c 171 }
Davidroid 23:05ef41b77f9b 172
Davidroid 23:05ef41b77f9b 173
Davidroid 23:05ef41b77f9b 174 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/