mbed OS 5 example application using X-NUCLEO-NFC02A1

Dependencies:   NDefLib X_NUCLEO_NFC02A1

Fork of HelloWorld_NFC02A1 by ST

Committer:
Davidroid
Date:
Tue Jul 11 15:26:04 2017 +0000
Revision:
12:85bf9d143843
Parent:
11:d0665d9c28a6
Child:
13:17208d6d9971
Updating with the new mbed library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rosarium 0:892175366555 1 /**
rosarium 0:892175366555 2 ******************************************************************************
rosarium 0:892175366555 3 * @file main.cpp
rosarium 1:11ae12d41082 4 * @author AMG Central Lab
rosarium 0:892175366555 5 * @version V1.0.0
rosarium 1:11ae12d41082 6 * @date 30 Aug 2016
rosarium 0:892175366555 7 * @brief This demo writes an ndef message with an url inside.
rosarium 0:892175366555 8 ******************************************************************************
rosarium 0:892175366555 9 * @attention
rosarium 0:892175366555 10 *
rosarium 1:11ae12d41082 11 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
rosarium 0:892175366555 12 *
rosarium 0:892175366555 13 * Redistribution and use in source and binary forms, with or without modification,
rosarium 0:892175366555 14 * are permitted provided that the following conditions are met:
rosarium 0:892175366555 15 * 1. Redistributions of source code must retain the above copyright notice,
rosarium 0:892175366555 16 * this list of conditions and the following disclaimer.
rosarium 0:892175366555 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
rosarium 0:892175366555 18 * this list of conditions and the following disclaimer in the documentation
rosarium 0:892175366555 19 * and/or other materials provided with the distribution.
rosarium 0:892175366555 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
rosarium 0:892175366555 21 * may be used to endorse or promote products derived from this software
rosarium 0:892175366555 22 * without specific prior written permission.
rosarium 0:892175366555 23 *
rosarium 0:892175366555 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
rosarium 0:892175366555 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
rosarium 0:892175366555 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
rosarium 0:892175366555 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
rosarium 0:892175366555 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
rosarium 0:892175366555 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
rosarium 0:892175366555 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
rosarium 0:892175366555 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
rosarium 0:892175366555 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
rosarium 0:892175366555 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
rosarium 0:892175366555 34 *
rosarium 0:892175366555 35 ******************************************************************************
rosarium 0:892175366555 36 */
rosarium 0:892175366555 37
rosarium 0:892175366555 38 #include "mbed.h"
rosarium 0:892175366555 39
giovannivisentini 11:d0665d9c28a6 40 #include "XNucleoNFC02A1.h"
rosarium 0:892175366555 41 #include "DevI2C.h"
rosarium 1:11ae12d41082 42 #include "NDefLib/NDefNfcTag.h"
rosarium 1:11ae12d41082 43 #include "NDefLib/RecordType/RecordURI.h"
rosarium 8:9bdd16ebccfb 44
rosarium 8:9bdd16ebccfb 45 static volatile bool buttonPress=false; // true when the user press the message
rosarium 8:9bdd16ebccfb 46
giovannivisentini 11:d0665d9c28a6 47 static void set_button_press(){
rosarium 8:9bdd16ebccfb 48 buttonPress=true;
rosarium 8:9bdd16ebccfb 49 }//if buttonPress
rosarium 8:9bdd16ebccfb 50
giovannivisentini 11:d0665d9c28a6 51 static void read_and_print_nfc_tag(NDefLib::NDefNfcTag &tag){
rosarium 8:9bdd16ebccfb 52 using namespace NDefLib;
rosarium 8:9bdd16ebccfb 53
giovannivisentini 11:d0665d9c28a6 54 if(tag.open_session() == true){
rosarium 8:9bdd16ebccfb 55 printf("Open Session\r\n");
rosarium 8:9bdd16ebccfb 56 NDefLib::Message readMsg;
rosarium 8:9bdd16ebccfb 57
rosarium 8:9bdd16ebccfb 58 tag.read(&readMsg);
rosarium 8:9bdd16ebccfb 59 printf("Message Read\r\n");
rosarium 8:9bdd16ebccfb 60
giovannivisentini 11:d0665d9c28a6 61 if(readMsg.get_N_records()==0){
rosarium 8:9bdd16ebccfb 62 printf("Error Read\r\n");
rosarium 8:9bdd16ebccfb 63 }else{
giovannivisentini 11:d0665d9c28a6 64 for(uint32_t i=0;i<readMsg.get_N_records();i++){
rosarium 8:9bdd16ebccfb 65 Record *r = readMsg[i];
rosarium 8:9bdd16ebccfb 66 // printRecord(r);
rosarium 8:9bdd16ebccfb 67 //-----
rosarium 8:9bdd16ebccfb 68 RecordURI *const temp = (RecordURI*)r;
giovannivisentini 11:d0665d9c28a6 69 printf("Read uriId: %d\r\n",temp->get_uri_id());
giovannivisentini 11:d0665d9c28a6 70 printf("Read uriType: %s\r\n",temp->get_uri_type().c_str());
giovannivisentini 11:d0665d9c28a6 71 printf("Read uriContent: %s\r\n",temp->get_content().c_str());
rosarium 8:9bdd16ebccfb 72 //------
rosarium 8:9bdd16ebccfb 73 delete r;
rosarium 8:9bdd16ebccfb 74 }//for
rosarium 8:9bdd16ebccfb 75 }//if-else
rosarium 8:9bdd16ebccfb 76
giovannivisentini 11:d0665d9c28a6 77 tag.close_session();
rosarium 8:9bdd16ebccfb 78 printf("Close session\r\n");
rosarium 8:9bdd16ebccfb 79 }else{
rosarium 8:9bdd16ebccfb 80 printf("Error open read Session\n\r");
rosarium 8:9bdd16ebccfb 81 }
rosarium 8:9bdd16ebccfb 82 }
rosarium 8:9bdd16ebccfb 83
rosarium 0:892175366555 84 /**
rosarium 0:892175366555 85 * Write a Ndef URI message linking to st.com site.
rosarium 0:892175366555 86 */
rosarium 8:9bdd16ebccfb 87
rosarium 0:892175366555 88 int main(void)
rosarium 0:892175366555 89 {
rosarium 0:892175366555 90
rosarium 0:892175366555 91 /*use default board pinout*/
Davidroid 12:85bf9d143843 92 DevI2C *i2cChannel = new DevI2C(XNucleoNFC02A1::DEFAULT_SDA_PIN,XNucleoNFC02A1::DEFAULT_SDL_PIN);
giovannivisentini 11:d0665d9c28a6 93 XNucleoNFC02A1 *nfcNucleo = XNucleoNFC02A1::instance(i2cChannel,
giovannivisentini 11:d0665d9c28a6 94 XNucleoNFC02A1::DEFAULT_GPO_PIN,XNucleoNFC02A1::DEFAULT_RF_DISABLE_PIN,
giovannivisentini 11:d0665d9c28a6 95 XNucleoNFC02A1::DEFAULT_LED1_PIN,XNucleoNFC02A1::DEFAULT_LED2_PIN,
giovannivisentini 11:d0665d9c28a6 96 XNucleoNFC02A1::DEFAULT_LED3_PIN);
rosarium 0:892175366555 97
giovannivisentini 11:d0665d9c28a6 98 NDefLib::NDefNfcTag& tag =nfcNucleo->get_M24LR().get_NDef_tag();
giovannivisentini 11:d0665d9c28a6 99 M24LR & mM24LRp = nfcNucleo->get_M24LR();
rosarium 2:6f1b1f7f8d12 100
rosarium 0:892175366555 101 /* Enable Energy Harvesting */
giovannivisentini 11:d0665d9c28a6 102 mM24LRp.enable_energy_harvesting();
rosarium 0:892175366555 103
rosarium 0:892175366555 104 printf("System Initialization done: !\n\r");
rosarium 0:892175366555 105
rosarium 2:6f1b1f7f8d12 106 /*open the i2c session with the nfc chip*/
giovannivisentini 11:d0665d9c28a6 107 if(tag.open_session() == true)
rosarium 2:6f1b1f7f8d12 108 {
rosarium 2:6f1b1f7f8d12 109 printf("Session opened\n\r");
giovannivisentini 11:d0665d9c28a6 110 nfcNucleo->get_led1()=1;
rosarium 2:6f1b1f7f8d12 111
rosarium 2:6f1b1f7f8d12 112 /*create the NDef message and record*/
rosarium 2:6f1b1f7f8d12 113 NDefLib::Message msg;
rosarium 7:699836375630 114 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com/st25");
giovannivisentini 11:d0665d9c28a6 115 msg.add_record(&rUri);
rosarium 2:6f1b1f7f8d12 116
rosarium 2:6f1b1f7f8d12 117 /*write the tag*/
rosarium 8:9bdd16ebccfb 118 if(tag.write(msg) == true)
rosarium 8:9bdd16ebccfb 119 {
rosarium 2:6f1b1f7f8d12 120 printf("Tag written\n\r");
giovannivisentini 11:d0665d9c28a6 121 nfcNucleo->get_led2()=1;
rosarium 8:9bdd16ebccfb 122 }
rosarium 8:9bdd16ebccfb 123 else
rosarium 8:9bdd16ebccfb 124 {
rosarium 2:6f1b1f7f8d12 125 printf("Error writing \n\r");
giovannivisentini 11:d0665d9c28a6 126 nfcNucleo->get_led1()=0;
rosarium 2:6f1b1f7f8d12 127 }//if-else
rosarium 2:6f1b1f7f8d12 128
rosarium 2:6f1b1f7f8d12 129 /*close the i2c session*/
giovannivisentini 11:d0665d9c28a6 130 if(tag.close_session() == true)
rosarium 1:11ae12d41082 131 {
rosarium 2:6f1b1f7f8d12 132 printf("Session closed\n\r");
giovannivisentini 11:d0665d9c28a6 133 nfcNucleo->get_led3()=1;
rosarium 1:11ae12d41082 134 }
rosarium 1:11ae12d41082 135 else
rosarium 2:6f1b1f7f8d12 136 {
rosarium 2:6f1b1f7f8d12 137 printf("Error closing the session\n\r");
rosarium 2:6f1b1f7f8d12 138 }//if-else
rosarium 2:6f1b1f7f8d12 139
rosarium 2:6f1b1f7f8d12 140 }
rosarium 2:6f1b1f7f8d12 141 else
rosarium 2:6f1b1f7f8d12 142 printf("Error opening the session\n\r");
giovannivisentini 9:0bd2f0c13d3d 143
giovannivisentini 9:0bd2f0c13d3d 144 #if defined(TARGET_STM)
rosarium 8:9bdd16ebccfb 145 /* enable the button */
rosarium 8:9bdd16ebccfb 146 InterruptIn mybutton(USER_BUTTON);
rosarium 8:9bdd16ebccfb 147
giovannivisentini 11:d0665d9c28a6 148 mybutton.fall(set_button_press);
rosarium 8:9bdd16ebccfb 149
rosarium 8:9bdd16ebccfb 150 /*each second change the led status and see if the user press the button*/
rosarium 8:9bdd16ebccfb 151 while(1)
rosarium 8:9bdd16ebccfb 152 {
rosarium 8:9bdd16ebccfb 153 if(buttonPress)
rosarium 8:9bdd16ebccfb 154 {
rosarium 8:9bdd16ebccfb 155 /*write the read message on console*/
giovannivisentini 11:d0665d9c28a6 156 read_and_print_nfc_tag(tag);
rosarium 8:9bdd16ebccfb 157 buttonPress=false;
rosarium 8:9bdd16ebccfb 158 }
rosarium 8:9bdd16ebccfb 159 }
giovannivisentini 9:0bd2f0c13d3d 160 #else
giovannivisentini 11:d0665d9c28a6 161 read_and_print_nfc_tag(tag);
rosarium 8:9bdd16ebccfb 162 #endif
rosarium 0:892175366555 163 }
rosarium 0:892175366555 164
rosarium 2:6f1b1f7f8d12 165
rosarium 2:6f1b1f7f8d12 166
rosarium 8:9bdd16ebccfb 167
rosarium 2:6f1b1f7f8d12 168 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/