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
Diff: m24sr/NDefNfcTagM24SR.h
- Revision:
- 19:0b65a5813059
- Parent:
- 12:d1f5eaa85deb
- Child:
- 23:d07138541feb
--- a/m24sr/NDefNfcTagM24SR.h Fri Jan 22 09:04:51 2016 +0000
+++ b/m24sr/NDefNfcTagM24SR.h Thu Jan 28 14:01:18 2016 +0000
@@ -35,8 +35,10 @@
******************************************************************************
*/
-#ifndef NDEFNFCTAGSTM24_H_
-#define NDEFNFCTAGSTM24_H_
+#ifndef X_NUCLEO_NFC01A1_M24SR_NDEFNFCTAGM24SR_H_
+#define X_NUCLEO_NFC01A1_M24SR_NDEFNFCTAGM24SR_H_
+
+#include <cstdlib>
#include <stdint.h>
#include "NDefLib/NDefNfcTag.h"
@@ -44,78 +46,244 @@
#include "Nfc_class.h"
/**
- * Implement all NDefLib abstract methods. */
+ * Helper class to use the NDefLib
+ */
class NDefNfcTagM24SR: public NDefLib::NDefNfcTag {
public:
+
/**
- * Create the object.
- * @param Device device to write the Ndef tags into.
+ *
+ * @param device device to use
*/
NDefNfcTagM24SR(Nfc &device) :
- mDevice(device), mMaxReadBytes(0xFF), mMaxWriteBytes(
- 0xFF) {
+ NDefLib::NDefNfcTag(),
+ mDevice(device),mIsSessionOpen(false),
+ mMaxReadBytes(0xFF), mMaxWriteBytes(0xFF),
+ mOpenSessionCallback(*this),
+ mCloseSessionCallback(*this),
+ mWriteByteCallback(*this),
+ mReadByteCallback(*this){}
+
+ virtual bool openSession(bool force = false);
+
+ virtual bool closeSession();
+
+ virtual bool isSessionOpen(){
+ return mIsSessionOpen;
}
/**
- * Open the communication session with the nfc tag.
- * @par This method should be called at the end of an overriding implementation, just before returning.
- * @param Force force to open a communication.
- * @return true if success
- */
- virtual bool openSession(bool force = false);
-
- /**
- * Close the communication with the nfc tag.
- * @par This method should be called at the end of an overriding implementation, just before returning.
- * @return true if success
- */
- virtual bool closeSession();
-
- /**
- * Close the open session.
- */
- virtual ~NDefNfcTagM24SR() {
+ * Close the open session.
+ */
+ virtual ~NDefNfcTagM24SR(){
if(isSessionOpen())
closeSession();
- }//~Type4NfcTagM24SR
+ }//~NDefNfcTagM24SR
+
+ protected:
+
+ virtual bool writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset,
+ byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);
+
+ virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength,
+ uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);
+
+ private:
+
+ Nfc &mDevice;
+
+ /**
+ * true if the session is open
+ */
+ bool mIsSessionOpen;
+
+ /**
+ * Max length for a read operation
+ */
+ uint16_t mMaxReadBytes;
+
+ /**
+ * Max length for a write operation
+ */
+ uint16_t mMaxWriteBytes;
+
+ /**
+ * Class containing the callback needed to open a session and read the max
+ * read/write size
+ */
+ class OpenSessionCallBack: public Nfc::Callback{
+ public:
+ OpenSessionCallBack(NDefNfcTagM24SR &sender);
+ virtual void onSessionOpen(Nfc *nfc,NFC_StatusTypeDef status);
+ virtual void onSelectedApplication(Nfc *nfc,NFC_StatusTypeDef status);
+ virtual void onSelectedCCFile(Nfc *nfc,NFC_StatusTypeDef status);
+ virtual void onReadByte(Nfc *nfc,NFC_StatusTypeDef status,uint16_t offset,
+ uint8_t *readByte, uint16_t nReadByte);
+ virtual void onSelectedNDEFFile(Nfc *nfc,NFC_StatusTypeDef status);
-
+ private:
+ NDefNfcTagM24SR &mSender;
+ uint32_t mNTrials;
+ uint8_t CCFile[15];
+ };
+
+ OpenSessionCallBack mOpenSessionCallback;
+
+ /**
+ * Class containing the callback needed to close a session
+ */
+ class CloseSessionCallBack : public Nfc::Callback{
+ public:
+ CloseSessionCallBack(NDefNfcTagM24SR &sender):
+ mSender(sender){}
+
+ virtual void onDeselect(Nfc *nfc,NFC_StatusTypeDef status){
+ (void)nfc;
+ if(status==NFC_SUCCESS){
+ mSender.mIsSessionOpen=false;
+ mSender.mCallBack->onSessionClose(&mSender,true);
+ }else{
+ mSender.mCallBack->onSessionClose(&mSender,false);
+ }//if-else
+ }
+ private:
+ NDefNfcTagM24SR &mSender;
+ };
+
+ CloseSessionCallBack mCloseSessionCallback;
+
+ /**
+ * Class containing the callback needed to write a buffer
+ */
+ class WriteByteCallback : public Nfc::Callback{
+ public:
+
+ /**
+ *
+ * @param sender tag where write the buffer
+ */
+ WriteByteCallback(NDefNfcTagM24SR &sender):
+ mByteToWrite(NULL),
+ mNByteToWrite(0),
+ mByteWrote(0),
+ mCallback(NULL),
+ mCallbackParam(NULL),
+ mSender(sender){}
-protected:
-
- /**
- * Write a sequence of bytes to the NDEF file.
- * @param buffer Buffer to write.
- * @param length Number of bytes to write.
- * @param offset Write offset in bytes.
- * @return true if success
- */
- virtual bool writeByte(const uint8_t *buffer, const uint16_t length, uint16_t offset);
-
- /**
- * Read a sequence of bytes from the NDEF file.
- * @param byteOffset Read offsetin bytes.
- * @param byteLength Number of bytes to read.
- * @param[out] buffer Buffer to store the read data into.
- * @return true if success
- */
- virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength,
- uint8_t *buffer);
+ /**
+ * Set the buffer to write and the function to call when finish
+ * @param buffer buffer to write
+ * @param nByte number of bytes to write
+ * @param callback function to call when the write ends
+ * @param param parameter to pass to the callback function
+ */
+ void setTask(const uint8_t *buffer,uint16_t nByte,
+ byteOperationCallback_t callback,CallbackStatus_t *param){
+ mByteToWrite=buffer;
+ mNByteToWrite=nByte;
+ mByteWrote=0;
+ mCallback = callback;
+ mCallbackParam = param;
+ }
+
+ virtual void onUpdatedBinary(Nfc *nfc,NFC_StatusTypeDef status,
+ uint16_t startOffset,uint8_t *writeByte,uint16_t nWriteByte);
+
+
+ private:
+ /** buffer to write */
+ const uint8_t *mByteToWrite;
+ /** length of the buffer */
+ uint16_t mNByteToWrite;
+ /** number of byte already wrote */
+ uint16_t mByteWrote;
+
+ /** function to call when all the bytes are write */
+ byteOperationCallback_t mCallback;
+ /** parameter to pass to the callback function*/
+ CallbackStatus_t* mCallbackParam;
+
+ /** tag where write the buffer*/
+ NDefNfcTagM24SR &mSender;
+ };
+
+ WriteByteCallback mWriteByteCallback;
+
+ /**
+ * Class containing the callback needed to read a buffer
+ */
+ class ReadByteCallback : public Nfc::Callback{
+ public:
+
+ /**
+ *
+ * @param Sender tag where read the buffer
+ */
+ ReadByteCallback(NDefNfcTagM24SR &sender):
+ mBuffer(NULL),
+ mNByteToRead(0),
+ mByteRead(0),
+ mCallback(NULL),
+ mCallbackParam(NULL),
+ mSender(sender){}
-private:
+ /**
+ * Set the buffer where read the data and the function to call when finish
+ * @param buffer Buffer read
+ * @param nByte Number of bytes to read
+ * @param callback Function to call when the read ends
+ * @param param Parameter to pass to the callback function
+ */
+ void setTask(uint8_t *buffer,uint16_t nByte,
+ byteOperationCallback_t callback,CallbackStatus_t *param){
+ mBuffer=buffer;
+ mNByteToRead=nByte;
+ mByteRead=0;
+ mCallback = callback;
+ mCallbackParam = param;
+ }
- Nfc &mDevice;
+ virtual void onReadByte(Nfc *nfc,NFC_StatusTypeDef status,
+ uint16_t offset,uint8_t *readByte, uint16_t nReadByte);
+
+
+ private:
+
+ /**
+ * Buffer where read the data
+ */
+ uint8_t *mBuffer;
- /**
- * Max length for a read operation
- */
- uint16_t mMaxReadBytes;
+ /**
+ * Number of bytes to read
+ */
+ uint16_t mNByteToRead;
+
+ /**
+ * Number of bytes already read
+ */
+ uint16_t mByteRead;
+
+ /**
+ * Function to call when all the bytes are read
+ */
+ byteOperationCallback_t mCallback;
- /**
- * Max length for a write operation
- */
- uint16_t mMaxWriteBytes;
+ /**
+ * Parameter to pass to the callback function
+ */
+ CallbackStatus_t* mCallbackParam;
+
+ /**
+ * Tag where the data are read
+ */
+ NDefNfcTagM24SR &mSender;
+ };
+
+ ReadByteCallback mReadByteCallback;
+
+
};
-#endif /* NDEFNFCTAGSTM24_H_ */
+#endif /* X_NUCLEO_NFC01A1_M24SR_NDEFNFCTAGM24SR_H_ */

X-NUCLEO-NFC01A1 Dynamic NFC Tag