Example program for the X-NUCLEO-NFC04A1

Dependencies:   BSP libNDEF ST25DV

X-NUCLEO-NFC04A1 dynamic NFC/RFID tag IC expansion board Firmware Package

Introduction

This firmware package includes Components Device Drivers, Board Support Package and example applications for STMicroelectronics X-NUCLEO-NFC04A1 dynamic NFC/RFID tag IC expansion board.

Example Application

This program gives the User the possibility to read or write the tag of the X-NUCLEO-NFC04A1. The user needs an NFC enabled device as a smartphone that has to be at least 10 cm close to the antenna of the X-NUCLEO-NFC04A1. The LEDs indicate the operational mode:

  • LED1 is on: by reading the tag the User obtains the parameters of the WiFi access points.
  • LED2 is on: by reading the tag the User is redirected to a website through the custom browser of the smartphone.
  • LED3 is on: the User can overwrite the value of the tag through the use of any NFC application.

Tested Platforms

This firmware has been tested on STM32 NUCLEO-F401RE

Committer:
DiegoOstuni
Date:
Thu Nov 14 14:53:55 2019 +0000
Revision:
4:d55445a038d5
Parent:
0:3f1a69fc394e
Example application for the STMicroelectronics X-NUCLEO-NFC04A1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DiegoOstuni 0:3f1a69fc394e 1 #include "x_nucleo_nfc04.h"
DiegoOstuni 0:3f1a69fc394e 2
DiegoOstuni 0:3f1a69fc394e 3 extern sCCFileInfo CCFileStruct;
DiegoOstuni 0:3f1a69fc394e 4
DiegoOstuni 0:3f1a69fc394e 5 // This is a constructor
DiegoOstuni 0:3f1a69fc394e 6
DiegoOstuni 0:3f1a69fc394e 7 X_Nucleo_NFC04::X_Nucleo_NFC04(I2C* i2cChannel, DigitalOut* led_1, DigitalOut* led_2, DigitalOut* led_3, DigitalOut* LPD, DigitalIn* MISO){
DiegoOstuni 0:3f1a69fc394e 8 mi2cChannel = i2cChannel;
DiegoOstuni 0:3f1a69fc394e 9 mled_1 = led_1;
DiegoOstuni 0:3f1a69fc394e 10 mled_2 = led_2;
DiegoOstuni 0:3f1a69fc394e 11 mled_3 = led_3;
DiegoOstuni 0:3f1a69fc394e 12 mLPD = LPD;
DiegoOstuni 0:3f1a69fc394e 13 mMISO = MISO;
DiegoOstuni 0:3f1a69fc394e 14 }
DiegoOstuni 0:3f1a69fc394e 15
DiegoOstuni 0:3f1a69fc394e 16 /**
DiegoOstuni 0:3f1a69fc394e 17 * @brief This function initialize the device
DiegoOstuni 0:3f1a69fc394e 18 * @param mi2cChannel : I2C Channel
DiegoOstuni 0:3f1a69fc394e 19 * @param mLPD : A digital PIN
DiegoOstuni 0:3f1a69fc394e 20 * @retval status(int) : Return if the write operation is succesful
DiegoOstuni 0:3f1a69fc394e 21 */
DiegoOstuni 0:3f1a69fc394e 22
DiegoOstuni 0:3f1a69fc394e 23 int X_Nucleo_NFC04::begin(I2C* mi2cChannel, DigitalOut *mLPD) {
DiegoOstuni 0:3f1a69fc394e 24 int ret = 0;
DiegoOstuni 0:3f1a69fc394e 25
DiegoOstuni 0:3f1a69fc394e 26 // Light some leds
DiegoOstuni 0:3f1a69fc394e 27 ledOn(mled_1);
DiegoOstuni 0:3f1a69fc394e 28 wait_ms( 300 );
DiegoOstuni 0:3f1a69fc394e 29 ledOn(mled_2);
DiegoOstuni 0:3f1a69fc394e 30
DiegoOstuni 0:3f1a69fc394e 31 /* NFCTAG Init */
DiegoOstuni 0:3f1a69fc394e 32 ret = BSP_NFCTAG_Init(mi2cChannel, mLPD);
DiegoOstuni 0:3f1a69fc394e 33 if(ret != NDEF_OK)
DiegoOstuni 0:3f1a69fc394e 34 return ret;
DiegoOstuni 0:3f1a69fc394e 35
DiegoOstuni 0:3f1a69fc394e 36 /* Reset MBEN Dynamic and initializes the CCFile struct */
DiegoOstuni 0:3f1a69fc394e 37 BSP_NFCTAG_GetExtended_Drv()->ResetMBEN_Dyn( mi2cChannel );
DiegoOstuni 0:3f1a69fc394e 38 if( NfcType5_NDEFDetection( mi2cChannel) != NDEF_OK )
DiegoOstuni 0:3f1a69fc394e 39 {
DiegoOstuni 0:3f1a69fc394e 40 CCFileStruct.MagicNumber = NFCT5_MAGICNUMBER_E1_CCFILE;
DiegoOstuni 0:3f1a69fc394e 41 CCFileStruct.Version = NFCT5_VERSION_V1_0;
DiegoOstuni 0:3f1a69fc394e 42 CCFileStruct.MemorySize = ( ST25DV_MAX_SIZE / 8 ) & 0xFF;
DiegoOstuni 0:3f1a69fc394e 43 CCFileStruct.TT5Tag = 0x05;
DiegoOstuni 0:3f1a69fc394e 44 /* Init of the Type Tag 5 component (M24LR) */
DiegoOstuni 0:3f1a69fc394e 45 ret = NfcType5_TT5Init( mi2cChannel );
DiegoOstuni 0:3f1a69fc394e 46 if (ret != NDEF_OK)
DiegoOstuni 0:3f1a69fc394e 47 return ret;
DiegoOstuni 0:3f1a69fc394e 48 }
DiegoOstuni 0:3f1a69fc394e 49
DiegoOstuni 0:3f1a69fc394e 50 ledOff( mled_1 );
DiegoOstuni 0:3f1a69fc394e 51 wait_ms( 300 );
DiegoOstuni 0:3f1a69fc394e 52 ledOff( mled_2 );
DiegoOstuni 0:3f1a69fc394e 53 wait_ms( 300 );
DiegoOstuni 0:3f1a69fc394e 54 ledOff( mled_3 );
DiegoOstuni 0:3f1a69fc394e 55 wait_ms( 300 );
DiegoOstuni 0:3f1a69fc394e 56 return NDEF_OK;
DiegoOstuni 0:3f1a69fc394e 57 }
DiegoOstuni 0:3f1a69fc394e 58
DiegoOstuni 0:3f1a69fc394e 59
DiegoOstuni 0:3f1a69fc394e 60 /**
DiegoOstuni 0:3f1a69fc394e 61 * @brief This function light on selected Led
DiegoOstuni 0:3f1a69fc394e 62 * @param led : Led to be lit on
DiegoOstuni 0:3f1a69fc394e 63 * @retval None
DiegoOstuni 0:3f1a69fc394e 64 */
DiegoOstuni 0:3f1a69fc394e 65 void X_Nucleo_NFC04::ledOn(DigitalOut* led) {
DiegoOstuni 0:3f1a69fc394e 66 led -> write(1);
DiegoOstuni 0:3f1a69fc394e 67 }
DiegoOstuni 0:3f1a69fc394e 68
DiegoOstuni 0:3f1a69fc394e 69 /**
DiegoOstuni 0:3f1a69fc394e 70 * @brief This function light off selected Led
DiegoOstuni 0:3f1a69fc394e 71 * @param led : Led to be lit off
DiegoOstuni 0:3f1a69fc394e 72 * @retval None
DiegoOstuni 0:3f1a69fc394e 73 */
DiegoOstuni 0:3f1a69fc394e 74
DiegoOstuni 0:3f1a69fc394e 75 void X_Nucleo_NFC04::ledOff(DigitalOut* led) {
DiegoOstuni 0:3f1a69fc394e 76 *led = 0;
DiegoOstuni 0:3f1a69fc394e 77 }
DiegoOstuni 0:3f1a69fc394e 78
DiegoOstuni 0:3f1a69fc394e 79
DiegoOstuni 0:3f1a69fc394e 80 /**
DiegoOstuni 0:3f1a69fc394e 81 * @brief This function write an URI into the NFC Tag
DiegoOstuni 0:3f1a69fc394e 82 * @param protocol : Protocol of the uri to write
DiegoOstuni 0:3f1a69fc394e 83 * @param uri : String containing the uri to write in the NFC Tag
DiegoOstuni 0:3f1a69fc394e 84 * @param info : Additional info
DiegoOstuni 0:3f1a69fc394e 85 * @param mi2cChannel : Object of the I2C channel
DiegoOstuni 0:3f1a69fc394e 86 * @retval status(int) : Return if the write operation is succesful
DiegoOstuni 0:3f1a69fc394e 87 */
DiegoOstuni 0:3f1a69fc394e 88 int X_Nucleo_NFC04::writeURI(string protocol, string uri, string info, I2C* mi2cChannel) {
DiegoOstuni 0:3f1a69fc394e 89 sURI_Info _URI;
DiegoOstuni 0:3f1a69fc394e 90 strcpy( _URI.protocol,protocol.c_str() );
DiegoOstuni 0:3f1a69fc394e 91 strcpy( _URI.URI_Message,uri.c_str() );
DiegoOstuni 0:3f1a69fc394e 92 strcpy( _URI.Information,info.c_str() );
DiegoOstuni 0:3f1a69fc394e 93
DiegoOstuni 0:3f1a69fc394e 94
DiegoOstuni 0:3f1a69fc394e 95 return NDEF_WriteURI( &_URI, mi2cChannel );
DiegoOstuni 0:3f1a69fc394e 96 }
DiegoOstuni 0:3f1a69fc394e 97
DiegoOstuni 0:3f1a69fc394e 98 /**
DiegoOstuni 0:3f1a69fc394e 99 * @brief This function read an URI from the NFC Tag
DiegoOstuni 0:3f1a69fc394e 100 * @param mi2cChannel : Object of the I2C channel
DiegoOstuni 0:3f1a69fc394e 101 * @retval string : Return the string that has been read from the NFC Tag
DiegoOstuni 0:3f1a69fc394e 102 */
DiegoOstuni 0:3f1a69fc394e 103
DiegoOstuni 0:3f1a69fc394e 104 string X_Nucleo_NFC04::readURI( I2C* mi2cChannel) {
DiegoOstuni 0:3f1a69fc394e 105 sURI_Info uri = {"","",""};
DiegoOstuni 0:3f1a69fc394e 106 sRecordInfo_t recordInfo;
DiegoOstuni 0:3f1a69fc394e 107 // increase buffer size for bigger messages
DiegoOstuni 0:3f1a69fc394e 108 if(NDEF_ReadNDEF(NDEF_Buffer, mi2cChannel))
DiegoOstuni 0:3f1a69fc394e 109 return (string)NULL;
DiegoOstuni 0:3f1a69fc394e 110
DiegoOstuni 0:3f1a69fc394e 111 if(NDEF_IdentifyBuffer(&recordInfo, NDEF_Buffer))
DiegoOstuni 0:3f1a69fc394e 112 return (string)NULL;
DiegoOstuni 0:3f1a69fc394e 113 if(NDEF_ReadURI(&recordInfo,&uri))
DiegoOstuni 0:3f1a69fc394e 114 return (string)NULL;
DiegoOstuni 0:3f1a69fc394e 115 return string(uri.protocol) + string(uri.URI_Message);
DiegoOstuni 0:3f1a69fc394e 116 }
DiegoOstuni 0:3f1a69fc394e 117
DiegoOstuni 0:3f1a69fc394e 118
DiegoOstuni 0:3f1a69fc394e 119 /**
DiegoOstuni 0:3f1a69fc394e 120 * @brief This function write WiFi Credentials into the NFC Tag
DiegoOstuni 0:3f1a69fc394e 121 * @param NetworkSSID : SSID of the WiFi Network
DiegoOstuni 0:3f1a69fc394e 122 * @param AuthenticationType :
DiegoOstuni 0:3f1a69fc394e 123 * @param EncryptionType :
DiegoOstuni 0:3f1a69fc394e 124 * @param NetworkKey : Password of the WiFi
DiegoOstuni 0:3f1a69fc394e 125 * @param mi2cChannel : Object of the I2C channel
DiegoOstuni 0:3f1a69fc394e 126 * @retval status(int) : Return if the write operation is succesful
DiegoOstuni 0:3f1a69fc394e 127 */
DiegoOstuni 0:3f1a69fc394e 128 uint16_t X_Nucleo_NFC04::writeWiFi(string NetworkSSID, Ndef_Wifi_Authentication_t AuthenticationType, Ndef_Wifi_Encryption_t EncryptionType, string NetworkKey, I2C* mi2cChannel ){
DiegoOstuni 0:3f1a69fc394e 129
DiegoOstuni 0:3f1a69fc394e 130 sWifiTokenInfo _wifi;
DiegoOstuni 0:3f1a69fc394e 131
DiegoOstuni 0:3f1a69fc394e 132 strcpy( _wifi.NetworkSSID,NetworkSSID.c_str() );
DiegoOstuni 0:3f1a69fc394e 133 _wifi.AuthenticationType = AuthenticationType;
DiegoOstuni 0:3f1a69fc394e 134 _wifi.EncryptionType = EncryptionType;
DiegoOstuni 0:3f1a69fc394e 135 strcpy( _wifi.NetworkKey,NetworkKey.c_str() );
DiegoOstuni 0:3f1a69fc394e 136
DiegoOstuni 0:3f1a69fc394e 137
DiegoOstuni 0:3f1a69fc394e 138
DiegoOstuni 0:3f1a69fc394e 139 return NDEF_WriteWifiToken(&_wifi, mi2cChannel );
DiegoOstuni 0:3f1a69fc394e 140 }
DiegoOstuni 0:3f1a69fc394e 141
DiegoOstuni 0:3f1a69fc394e 142