Firmware library for the X-NUCLEO-NFC01A1 Dynamic NFC Tag board.
Dependencies: M24SR
Dependents: NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more
Fork of X_NUCLEO_NFC01A1 by
X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board Firmware Package
Introduction
This firmware package includes Components Device Drivers, Board Support Package and example applications for STMicroelectronics X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board based on M24SR.
Firmware Library
Class X_NUCLEO_NFC01A1 is intended to represent the Dynamic NFC Tag Expansion Board with the same name.
It provides an API to access to the M24SR component and to the three onboard LEDs.
It is intentionally implemented as a singleton because only one X_NUCLEO_NFC01A1 at a time might be deployed in a HW component stack.
The library also provides an implementation of the NDEF library API for M24SR, providing an simple way to read/write NDEF formatted messages from/to the M24SR dynamic NFC tag.
Example applications
1. Hello World
2. Asynchronous Hello World
Revision 6:96389fb79676, committed 2015-12-22
- Comitter:
- giovannivisentini
- Date:
- Tue Dec 22 15:34:24 2015 +0000
- Parent:
- 5:71d13a8d9ce0
- Child:
- 7:19fc04b8fec6
- Commit message:
- add the NdefNdfTagM24SR
Changed in this revision
--- a/Interfaces/Nfc_class.h Tue Dec 22 08:30:06 2015 +0000 +++ b/Interfaces/Nfc_class.h Tue Dec 22 15:34:24 2015 +0000 @@ -1,7 +1,7 @@ /** ****************************************************************************** * @file Nfc_class.h - * @author AST / EST + * @author ST Central Labs * @version V0.0.1 * @date 13-April-2015 * @brief This file contains the abstract class describing the interface of a
--- a/X_NUCLEO_NFC01A1.cpp Tue Dec 22 08:30:06 2015 +0000 +++ b/X_NUCLEO_NFC01A1.cpp Tue Dec 22 15:34:24 2015 +0000 @@ -1,7 +1,8 @@ /** ****************************************************************************** * @file X_NUCLEO_NFC01A1.h - * @date 05/11/2015 + * @author ST Central Labs + * @date 05 Nov 2015 * @brief singleton class that controls all the electronics inside the * X_NUCLEO_NFC01A1 expansion board ******************************************************************************
--- a/X_NUCLEO_NFC01A1.h Tue Dec 22 08:30:06 2015 +0000 +++ b/X_NUCLEO_NFC01A1.h Tue Dec 22 15:34:24 2015 +0000 @@ -1,7 +1,8 @@ /** ****************************************************************************** * @file X_NUCLEO_NFC01A1.h - * @date 05/11/2015 + * @author ST Central Labs + * @date 05 Nov 2015 * @brief singleton class that controls all the electronics inside the * X_NUCLEO_NFC01A1 expansion board ******************************************************************************
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/m24sr/NDefNfcTagM24SR.cpp Tue Dec 22 15:34:24 2015 +0000
@@ -0,0 +1,156 @@
+/**
+ ******************************************************************************
+ * @file NdefNfcTagSTM24SR.cpp
+ * @author ST Central Labs
+ * @date 05 Nov 2015
+ * @brief wrapper class for use the NDefLib library for write/read ndef message
+ ******************************************************************************
+ *
+ * COPYRIGHT(c) 2015 STMicroelectronics
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of STMicroelectronics nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+#include "NDefNfcTagM24SR.h"
+#include "m24sr_class.h"
+/* wait 1sec, driver is configured to let 200ms for command to complete */
+/* which is enough for all commands except GetSession if RF session is already opened */
+/* Smartphone generaly release the session within the second, but customer can modify this value */
+#define OPENSESSION_NTRIALS 5
+
+#define CC_FILE_LENGTH_BYTE 15
+
+bool NDefNfcTagM24SR::openSession(bool force) {
+
+ if (isSessionOpen())
+ return true;
+ //else
+ if (force)
+ mDevice.KillSession();
+
+ int8_t nTrials = OPENSESSION_NTRIALS;
+ NFC_StatusTypeDef status = NFC_ERROR;
+ while (status != NFC_SUCCESS && nTrials) {
+ status = mDevice.GetSession();
+ nTrials--;
+ }
+
+ if (status != NFC_SUCCESS) {
+ return false;
+ } //else
+
+ //Select the NFC type 4 application
+ if (mDevice.SelectApplication() != NFC_SUCCESS)
+ return false;
+
+ if (mDevice.SelectCCfile() != NFC_SUCCESS)
+ return false;
+
+ uint8_t CCFile[CC_FILE_LENGTH_BYTE];
+ /* read the first 15 bytes of the CC file */
+ if (mDevice.ReadBinary(0x0000, CC_FILE_LENGTH_BYTE, CCFile) != NFC_SUCCESS)
+ return false;
+
+ //read the ndefFileId
+ uint16_t ndefFileId = (uint16_t) ((CCFile[0x09] << 8) | CCFile[0x0A]);
+ mMaxReadBytes = (uint16_t) ((CCFile[0x03] << 8) | CCFile[0x04]);
+ mMaxWriteBytes = (uint16_t) ((CCFile[0x05] << 8) | CCFile[0x06]);
+
+ //open it
+ if (mDevice.SelectNDEFfile(ndefFileId) != NFC_SUCCESS)
+ return false;
+
+ NDefNfcTag::openSession();
+ return true;
+}
+
+bool NDefNfcTagM24SR::closeSession() {
+
+ //no open session
+ if (!isSessionOpen())
+ return true;
+
+ //close the CC file
+ if (mDevice.Deselect() != NFC_SUCCESS)
+ return false;
+
+ NDefNfcTag::closeSession();
+ return true;
+}
+
+bool NDefNfcTagM24SR::writeByte(const uint8_t *buffer, uint16_t length, uint16_t offset) {
+
+ //no open session
+ if (!isSessionOpen())
+ return false;
+
+ NFC_StatusTypeDef status = NFC_SUCCESS;
+ bool prevSessionStatus = isSessionOpen();
+ if (!prevSessionStatus) {
+ //try to acquire a session or fail
+ if (openSession(false))
+ return false;
+ } //if
+
+ if (length > mMaxWriteBytes) {
+ do {
+ status = mDevice.UpdateBinary(offset, mMaxWriteBytes,(uint8_t*) buffer);
+ offset += mMaxWriteBytes;
+ buffer += mMaxWriteBytes;
+ length -= mMaxWriteBytes;
+ } while (length > mMaxWriteBytes && status == NFC_SUCCESS);
+ } //if
+
+ //finish to write the buffer
+ if (status == NFC_SUCCESS && mMaxWriteBytes)
+ status = mDevice.UpdateBinary(offset, length,(uint8_t*) buffer);
+
+ return status==NFC_SUCCESS;
+}
+
+bool NDefNfcTagM24SR::readByte(uint16_t byteOffset, uint16_t byteLength,
+ uint8_t *buffer) {
+
+ //no open session
+ if (!isSessionOpen())
+ return false;
+
+ NFC_StatusTypeDef status = NFC_SUCCESS;
+ uint16_t offset = byteOffset;
+ if (byteLength > mMaxReadBytes) {
+ do {
+ status = mDevice.ReadBinary(offset, mMaxReadBytes, buffer);
+ offset += mMaxReadBytes;
+ buffer += mMaxReadBytes;
+ byteLength -= mMaxReadBytes;
+ } while (byteLength > mMaxWriteBytes && status == NFC_SUCCESS);
+ } //if
+ //finish to write the buffer
+ if (status == NFC_SUCCESS && mMaxWriteBytes)
+ status = mDevice.ReadBinary(offset, byteLength, buffer);
+
+ return status == NFC_SUCCESS;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/m24sr/NDefNfcTagM24SR.h Tue Dec 22 15:34:24 2015 +0000
@@ -0,0 +1,94 @@
+/**
+ ******************************************************************************
+ * @file Type4NfcTagM24SR.h
+ * @author ST Central Labs
+ * @date 05 Nov 2015
+ * @brief wrapper class for use the NDefLib library for write/read ndef message
+ ******************************************************************************
+ *
+ * COPYRIGHT(c) 2015 STMicroelectronics
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of STMicroelectronics nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+#ifndef NDEFNFCTAGSTM24_H_
+#define NDEFNFCTAGSTM24_H_
+#include <stdint.h>
+
+#include "NDefLib/NDefNfcTag.h"
+
+class M24SR;
+
+/**
+ * implement the abstract method for use the NDefLib,
+ * all method must be called after opening a session
+ */
+class NDefNfcTagM24SR: public NDefLib::NDefNfcTag {
+
+public:
+ /**
+ *
+ * @param device device where write the Ndef tags
+ */
+ NDefNfcTagM24SR(M24SR &device) :
+ mDevice(device), mMaxReadBytes(0xFF), mMaxWriteBytes(
+ 0xFF) {
+ }
+
+
+ virtual bool openSession(bool force = false);
+ virtual bool closeSession();
+
+ /**
+ * close the open session
+ */
+ virtual ~NDefNfcTagM24SR() {
+ if(isSessionOpen())
+ closeSession();
+ }//~Type4NfcTagM24SR
+
+
+
+protected:
+ virtual bool writeByte(const uint8_t *buffer, const uint16_t length, uint16_t offset);
+ virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength,
+ uint8_t *buffer);
+
+private:
+
+ M24SR &mDevice;
+
+ /**
+ * max length for a read operation
+ */
+ uint16_t mMaxReadBytes;
+
+ /**
+ * max length for a write operation
+ */
+ uint16_t mMaxWriteBytes;
+};
+
+#endif /* NDEFNFCTAGSTM24_H_ */
--- a/m24sr/m24sr_class.cpp Tue Dec 22 08:30:06 2015 +0000
+++ b/m24sr/m24sr_class.cpp Tue Dec 22 15:34:24 2015 +0000
@@ -1,7 +1,8 @@
/**
******************************************************************************
* @file m24sr_class.cpp
- * @date 05/11/2015
+ * @author ST Central Labs
+ * @date 05 Nov 2015
* @brief This file provides a set of functions to interface with the M24SR
* device.
******************************************************************************
@@ -1230,4 +1231,79 @@
Error: return NFC_IO_ERROR_I2CTIMEOUT;
}
+////////////////////////hight level utility function //////////////////////////
+
+NFC_StatusTypeDef M24SR::enableReadPassword(const uint8_t* pCurrentWritePassword,
+ const uint8_t* pNewPassword) {
+ errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
+ /* Set new password */
+ errchk(ChangeReferenceData(M24SR::ReadPwd, pNewPassword));
+ return EnableVerificationRequirement(M24SR::ReadPwd);
+}
+
+NFC_StatusTypeDef M24SR::disableReadPassword(const uint8_t* pCurrentWritePassword) {
+ errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
+ return DisableVerificationRequirement(M24SR::ReadPwd);
+}
+
+NFC_StatusTypeDef M24SR::enableWritePassword(const uint8_t* pCurrentWritePassword,
+ uint8_t* pNewPassword) {
+ /* check we have the good password */
+ errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
+ /* Set new password */
+ errchk(ChangeReferenceData(M24SR::WritePwd, pNewPassword));
+ return EnableVerificationRequirement(M24SR::WritePwd);
+}
+
+NFC_StatusTypeDef M24SR::disableWritePassword(const uint8_t* pCurrentWritePassword) {
+ errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
+ return(DisableVerificationRequirement(M24SR::WritePwd));
+}
+
+NFC_StatusTypeDef M24SR::disableAllPassword(const uint8_t* pSuperUserPassword){
+ errchk(Verify(M24SR::I2CPwd, 0x10, pSuperUserPassword));
+
+ errchk(DisablePermanentState(M24SR::ReadPwd));
+ errchk(DisablePermanentState(M24SR::WritePwd));
+
+ errchk(DisableVerificationRequirement(M24SR::ReadPwd));
+ errchk(DisableVerificationRequirement(M24SR::WritePwd));
+
+ /* reset password */
+ errchk(ChangeReferenceData(M24SR::ReadPwd, M24SR::DEFAULT_PASSWORD));
+ return(ChangeReferenceData(M24SR::WritePwd, M24SR::DEFAULT_PASSWORD));
+}
+
+NFC_StatusTypeDef M24SR::enableReadOnly(const uint8_t* pCurrentWritePassword){
+
+ errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
+ /* lock write to have read only */
+ return EnablePermanentState(M24SR::WritePwd);
+}
+
+NFC_StatusTypeDef M24SR::disableReadOnly() {
+
+ errchk(Verify(M24SR::I2CPwd, 0x10, M24SR::DEFAULT_PASSWORD));
+ /* disable write protection to disable read only mode */
+ errchk(DisablePermanentState(M24SR::WritePwd));
+ return(DisableVerificationRequirement(M24SR::WritePwd));
+}
+
+
+NFC_StatusTypeDef M24SR::enableWriteOnly(const uint8_t* pCurrentWritePassword) {
+
+ errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
+ /* disable read access and keep write */
+ return(EnablePermanentState(M24SR::ReadPwd));
+
+}
+
+NFC_StatusTypeDef M24SR::disableWriteOnly() {
+
+ errchk(Verify(M24SR::I2CPwd, 0x10, M24SR::DEFAULT_PASSWORD));
+ /* disable write only -> enable write acces */
+ errchk(DisablePermanentState(M24SR::ReadPwd))
+ return DisableVerificationRequirement(M24SR::ReadPwd);
+}
+
/******************* (C) COPYRIGHT 2013 STMicroelectronics *****END OF FILE****/
--- a/m24sr/m24sr_class.h Tue Dec 22 08:30:06 2015 +0000
+++ b/m24sr/m24sr_class.h Tue Dec 22 15:34:24 2015 +0000
@@ -1,7 +1,8 @@
/**
******************************************************************************
* @file m24sr_class.h
- * @date 05/11/2015
+ * @author ST Central Labs
+ * @date 05 Nov 2015
* @brief This file provides a set of functions to interface with the M24SR
* device.
******************************************************************************
@@ -52,6 +53,7 @@
#include "m24sr.h"
#include "Interfaces/Nfc_class.h"
+#include "NDefNfcTagM24SR.h"
/* Classes -------------------------------------------------------------------*/
@@ -73,7 +75,7 @@
const PinName& RFDISPinName) :
Nfc(), address(address), dev_i2c(i2c), GPOPin(GPOPinName), RFDisablePin(
RFDISPinName), answerReadyInterrupt(GPOPinName), interruptIsFired(
- false) {
+ false),NDefTagUtil(*this) {
memset(uM24SRbuffer, 0, 0xFF * sizeof(int8_t));
syncMode = M24SR_WAITINGTIME_POLLING;
@@ -210,6 +212,63 @@
return (NFC_StatusTypeDef) M24SR_SendInterrupt();
}
+ /////////////////// hight level/utility function /////////////////////
+
+ NDefLib::NDefNfcTag& getNDefTag(){
+ return NDefTagUtil;
+ }
+
+
+ NFC_StatusTypeDef enableReadPassword(const uint8_t* pCurrentWritePassword,
+ const uint8_t* pNewPassword);
+
+ NFC_StatusTypeDef disableReadPassword(const uint8_t* pCurrentWritePassword=M24SR::DEFAULT_PASSWORD);
+
+ NFC_StatusTypeDef enableWritePassword(const uint8_t* pCurrentWritePassword,
+ uint8_t* pNewPassword);
+
+ NFC_StatusTypeDef disableWritePassword(const uint8_t* pCurrentWritePassword=M24SR::DEFAULT_PASSWORD);
+
+ /**
+ * @brief This function deactivate the need of read and write password for next access
+ * @param pSuperUserPassword : I2C super user password to overwrite read and write password
+ * @retval SUCCESS : M24SR access is now free (no password needed)
+ * @retval ERROR : operation does not complete
+ */
+ NFC_StatusTypeDef disableAllPassword(const uint8_t* pSuperUserPassword=M24SR::DEFAULT_PASSWORD);
+
+ /**
+ * @brief This function enable read only mode
+ * @param pCurrentWritePassword : Write password is needed to have right to enable read only mode
+ * @retval SUCCESS : M24SR access is now forbidden in write mode
+ * @retval ERROR : operation does not complete
+ */
+ NFC_StatusTypeDef enableReadOnly(const uint8_t* pCurrentWritePassword=M24SR::DEFAULT_PASSWORD);
+
+ /**
+ * @brief This fonction disable read only mode
+ * @param pCurrentWritePassword : Write password is needed to have right to disable read only mode
+ * @retval SUCCESS : M24SR write access is now allowed
+ * @retval ERROR : operation does not complete
+ */
+ NFC_StatusTypeDef disableReadOnly();
+
+ /**
+ * @brief This fonction enable write only mode
+ * @param pCurrentWritePassword : Write password is needed to have right to enable write only mode
+ * @retval SUCCESS : M24SR access is now forbidden in read mode
+ * @retval ERROR : operation does not complete
+ */
+ NFC_StatusTypeDef enableWriteOnly(const uint8_t* pCurrentWritePassword=M24SR::DEFAULT_PASSWORD);
+
+ /**
+ * @brief This fonction disable write only mode
+ * @param pCurrentWritePassword : Write password is needed to have right to disable write only mode
+ * @retval SUCCESS : M24SR read access is now allowed
+ * @retval ERROR : operation does not complete
+ */
+ NFC_StatusTypeDef disableWriteOnly();
+
protected:
/*** Protected Component Related Methods ***/
@@ -326,6 +385,8 @@
* state variable change when the interrupt is fired
*/
volatile bool interruptIsFired;
+
+ NDefNfcTagM24SR NDefTagUtil;
};
#endif // __M24SR_CLASS_H

X-NUCLEO-NFC01A1 Dynamic NFC Tag