This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.

Dependencies:   NDefLib X_NUCLEO_NFC01A1 mbed

Fork of X-MBED-NFC1 by Giovanni Visentini

This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.

The available demos are:

  • SAMPLE_WRITE_URL: write a tag with the ST home page URL
  • SAMPLE_COUNT_CLICK: create a custom tag to count and report the user button clicks.
  • SAMPLE_WRITE_AND_CHANGE_ALL: write a tag with all the supported records and update the tag contents when the user button is pressed.
  • SAMPLE_LOCK_TAG_CONTENT: use the M24SR component API to set the NFC tag as read-only.

To enable the different demos comment/uncomment the SAMPLE_* macros provided in main.cpp .

Committer:
giovannivisentini
Date:
Thu Nov 19 08:50:18 2015 +0000
Revision:
0:674813bd5ec9
Child:
1:6d202b62ed68
first commit,

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 0:674813bd5ec9 1 /*
giovannivisentini 0:674813bd5ec9 2 * Type4NfcTagSTM24SR.cpp
giovannivisentini 0:674813bd5ec9 3 *
giovannivisentini 0:674813bd5ec9 4 * Created on: Nov 5, 2015
giovannivisentini 0:674813bd5ec9 5 * Author: giovanni visentini
giovannivisentini 0:674813bd5ec9 6 */
giovannivisentini 0:674813bd5ec9 7
giovannivisentini 0:674813bd5ec9 8 #include <Type4NfcTagSTM24SR.h>
giovannivisentini 0:674813bd5ec9 9
giovannivisentini 0:674813bd5ec9 10 /* wait 1sec, driver is configured to let 200ms for command to complete */
giovannivisentini 0:674813bd5ec9 11 /* which is enough for all commands except GetSession if RF session is already opened */
giovannivisentini 0:674813bd5ec9 12 /* Smartphone generaly release the session within the second, but customer can modify this value */
giovannivisentini 0:674813bd5ec9 13 #define OPENSESSION_NTRIALS 5
giovannivisentini 0:674813bd5ec9 14
giovannivisentini 0:674813bd5ec9 15 #define CC_FILE_LENGTH_BYTE 15
giovannivisentini 0:674813bd5ec9 16
giovannivisentini 0:674813bd5ec9 17 bool Type4NfcTagSTM24SR::openSession(bool force){
giovannivisentini 0:674813bd5ec9 18
giovannivisentini 0:674813bd5ec9 19 if(mSessionIsOpen)
giovannivisentini 0:674813bd5ec9 20 return true;
giovannivisentini 0:674813bd5ec9 21 //else
giovannivisentini 0:674813bd5ec9 22 if(force)
giovannivisentini 0:674813bd5ec9 23 mDevice.KillSession();
giovannivisentini 0:674813bd5ec9 24
giovannivisentini 0:674813bd5ec9 25
giovannivisentini 0:674813bd5ec9 26 int8_t nTrials=OPENSESSION_NTRIALS;
giovannivisentini 0:674813bd5ec9 27 NFC_StatusTypeDef status = NFC_ERROR;
giovannivisentini 0:674813bd5ec9 28 while(status != NFC_OK && nTrials){
giovannivisentini 0:674813bd5ec9 29 status = mDevice.GetSession( );
giovannivisentini 0:674813bd5ec9 30 nTrials--;
giovannivisentini 0:674813bd5ec9 31 }
giovannivisentini 0:674813bd5ec9 32
giovannivisentini 0:674813bd5ec9 33 if (status != NFC_OK){
giovannivisentini 0:674813bd5ec9 34 return false;
giovannivisentini 0:674813bd5ec9 35 }//else
giovannivisentini 0:674813bd5ec9 36
giovannivisentini 0:674813bd5ec9 37 //Select the NFC type 4 application
giovannivisentini 0:674813bd5ec9 38 if(mDevice.SelectApplication()!=NFC_OK)
giovannivisentini 0:674813bd5ec9 39 return false;
giovannivisentini 0:674813bd5ec9 40
giovannivisentini 0:674813bd5ec9 41 if(mDevice.SelectCCfile()!=NFC_OK)
giovannivisentini 0:674813bd5ec9 42 return false;
giovannivisentini 0:674813bd5ec9 43
giovannivisentini 0:674813bd5ec9 44 uint8_t CCFile[CC_FILE_LENGTH_BYTE];
giovannivisentini 0:674813bd5ec9 45 /* read the first 15 bytes of the CC file */
giovannivisentini 0:674813bd5ec9 46 if(mDevice.ReadBinary(0x0000,CC_FILE_LENGTH_BYTE,CCFile)!=NFC_OK)
giovannivisentini 0:674813bd5ec9 47 return false;
giovannivisentini 0:674813bd5ec9 48
giovannivisentini 0:674813bd5ec9 49 //read the ndefFileId
giovannivisentini 0:674813bd5ec9 50 uint16_t ndefFileId = (uint16_t) ((CCFile[0x09]<<8) | CCFile[0x0A]);
giovannivisentini 0:674813bd5ec9 51 mMaxReadBytes = (uint16_t) ((CCFile[0x03]<<8) | CCFile[0x04]);
giovannivisentini 0:674813bd5ec9 52 mMaxWriteBytes =(uint16_t) ((CCFile[0x05]<<8) | CCFile[0x06]);
giovannivisentini 0:674813bd5ec9 53
giovannivisentini 0:674813bd5ec9 54 //openit
giovannivisentini 0:674813bd5ec9 55 if(mDevice.SelectNDEFfile(ndefFileId)!=NFC_OK)
giovannivisentini 0:674813bd5ec9 56 return false;
giovannivisentini 0:674813bd5ec9 57
giovannivisentini 0:674813bd5ec9 58 mSessionIsOpen=true;
giovannivisentini 0:674813bd5ec9 59 return true;
giovannivisentini 0:674813bd5ec9 60 }
giovannivisentini 0:674813bd5ec9 61
giovannivisentini 0:674813bd5ec9 62
giovannivisentini 0:674813bd5ec9 63 bool Type4NfcTagSTM24SR::closeSession(){
giovannivisentini 0:674813bd5ec9 64
giovannivisentini 0:674813bd5ec9 65 //no open session
giovannivisentini 0:674813bd5ec9 66 if(!mSessionIsOpen)
giovannivisentini 0:674813bd5ec9 67 return true;
giovannivisentini 0:674813bd5ec9 68
giovannivisentini 0:674813bd5ec9 69 //close the CC file
giovannivisentini 0:674813bd5ec9 70 if(mDevice.Deselect()!=NFC_OK)
giovannivisentini 0:674813bd5ec9 71 return false;
giovannivisentini 0:674813bd5ec9 72
giovannivisentini 0:674813bd5ec9 73 mSessionIsOpen=false;
giovannivisentini 0:674813bd5ec9 74 return true;
giovannivisentini 0:674813bd5ec9 75 }
giovannivisentini 0:674813bd5ec9 76
giovannivisentini 0:674813bd5ec9 77 bool Type4NfcTagSTM24SR::writeByte(uint8_t *buffer,uint16_t length){
giovannivisentini 0:674813bd5ec9 78
giovannivisentini 0:674813bd5ec9 79 NFC_StatusTypeDef status=NFC_OK;
giovannivisentini 0:674813bd5ec9 80 uint16_t offset=0;
giovannivisentini 0:674813bd5ec9 81 bool prevSessionStatus = mSessionIsOpen;
giovannivisentini 0:674813bd5ec9 82 if(!prevSessionStatus){
giovannivisentini 0:674813bd5ec9 83 //try to acquire a session or fail
giovannivisentini 0:674813bd5ec9 84 if(openSession(false))
giovannivisentini 0:674813bd5ec9 85 return false;
giovannivisentini 0:674813bd5ec9 86 }//if
giovannivisentini 0:674813bd5ec9 87
giovannivisentini 0:674813bd5ec9 88 if( length > mMaxWriteBytes){
giovannivisentini 0:674813bd5ec9 89 do{
giovannivisentini 0:674813bd5ec9 90 status = mDevice.UpdateBinary ( offset, mMaxWriteBytes , buffer);
giovannivisentini 0:674813bd5ec9 91 offset += mMaxWriteBytes;
giovannivisentini 0:674813bd5ec9 92 buffer += mMaxWriteBytes;
giovannivisentini 0:674813bd5ec9 93 length -= mMaxWriteBytes;
giovannivisentini 0:674813bd5ec9 94 }while( length > mMaxWriteBytes && status == NFC_OK);
giovannivisentini 0:674813bd5ec9 95 }//if
giovannivisentini 0:674813bd5ec9 96
giovannivisentini 0:674813bd5ec9 97 //finish to write the buffer
giovannivisentini 0:674813bd5ec9 98 if( status == NFC_OK && mMaxWriteBytes)
giovannivisentini 0:674813bd5ec9 99 status = mDevice.UpdateBinary ( offset,length , buffer);
giovannivisentini 0:674813bd5ec9 100
giovannivisentini 0:674813bd5ec9 101 if(status==NFC_OK){
giovannivisentini 0:674813bd5ec9 102 if(!prevSessionStatus) // if the session was closed, try close it
giovannivisentini 0:674813bd5ec9 103 return closeSession();
giovannivisentini 0:674813bd5ec9 104 else
giovannivisentini 0:674813bd5ec9 105 return true;
giovannivisentini 0:674813bd5ec9 106 }else
giovannivisentini 0:674813bd5ec9 107 return false;
giovannivisentini 0:674813bd5ec9 108
giovannivisentini 0:674813bd5ec9 109 }
giovannivisentini 0:674813bd5ec9 110