X_NUCLEO_NFC02A1 library for M24LR
Dependencies: ST_INTERFACES
Dependents: HelloWorld_NFC02A1_mbedOS HelloWorld_NFC02A1laatste HelloWorld_NFC02A1
Fork of X_NUCLEO_NFC02A1 by
X-NUCLEO-NFC02A1 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-NFC02A1 Dynamic NFC Tag Expansion Board based on M24LR.
Firmware Library
Class X_NUCLEO_NFC02A1 is intended to represent the Dynamic NFC Tag Expansion Board with the same name.
It provides an API to access to the M24LR component and to the three onboard LEDs.
It is intentionally implemented as a singleton because only one X_NUCLEO_NFC02A1 at a time might be deployed in a HW component stack.
The library also provides an implementation of the NDEF library API for M24LR, providing an simple way to read/write NDEF formatted messages from/to the M24LR dynamic NFC tag.
Example application
Hello World is a simple application to program and read an URI from the NFC tag.
Revision 0:71bff5ad0a49, committed 2016-09-28
- Comitter:
- rosarium
- Date:
- Wed Sep 28 11:30:09 2016 +0000
- Child:
- 1:b4e1cb04a87f
- Commit message:
- X_NUCLEO_NFC02A1 converted as an library
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Common/common.h Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,60 @@
+/**
+ ******************************************************************************
+ * @file common.h
+ * @author AST
+ * @version V1.0.0
+ * @date 1 April 2015
+ * @brief Header file containing generic component definitions
+ * and I/O functions.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
+ *
+ * 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.
+ *
+ ******************************************************************************
+ */
+
+
+/* Prevent recursive inclusion -----------------------------------------------*/
+
+#ifndef __COMMON_H__
+#define __COMMON_H__
+#include <stdint.h>
+
+/* Types ---------------------------------------------------------------------*/
+
+/**
+ * @brief NFCTAG status enumerator definition
+ */
+typedef enum
+{
+ NFCTAG_OK = 0,
+ NFCTAG_ERROR = 1,
+ NFCTAG_BUSY = 2,
+ NFCTAG_TIMEOUT = 3
+} NFCTAG_StatusTypeDef;
+typedef NFCTAG_StatusTypeDef (*ReadFnDataPtr)( uint8_t * const, const uint16_t, const uint16_t ) ;
+typedef NFCTAG_StatusTypeDef (*WriteFnDataPtr)( const uint8_t * const, const uint16_t, const uint16_t) ;
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Common/component.h Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,91 @@
+/**
+ ******************************************************************************
+ * @file component.h
+ * @author AST
+ * @version V1.0.0
+ * @date 1 April 2015
+ * @brief Header file containing generic component definitions
+ * and I/O functions.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
+ *
+ * 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.
+ *
+ ******************************************************************************
+ */
+
+
+/* Prevent recursive inclusion -----------------------------------------------*/
+
+#ifndef __COMPONENT_H__
+#define __COMPONENT_H__
+
+
+/* Types ---------------------------------------------------------------------*/
+
+/**
+ * @brief Component's Context structure definition.
+ */
+typedef struct
+{
+ /* Identity. */
+ uint8_t who_am_i;
+
+ /* ACTION ----------------------------------------------------------------*/
+ /* There should be only a unique identifier for each component, which */
+ /* should be the "who_am_i" parameter, hence this parameter is optional. */
+ /* -----------------------------------------------------------------------*/
+ /* Type. */
+ uint8_t type;
+
+ /* Configuration. */
+ uint8_t address;
+
+ /* Pointer to the Data. */
+ void *pData;
+
+ /* Pointer to the Virtual Table. */
+ void *pVTable;
+
+ /* ACTION ----------------------------------------------------------------*/
+ /* There should be only a unique virtual table for each component, which */
+ /* should be the "pVTable" parameter, hence this parameter is optional. */
+ /* -----------------------------------------------------------------------*/
+ /* Pointer to the Extended Virtual Table. */
+ void *pExtVTable;
+} DrvContextTypeDef;
+
+/**
+ * @brief Component's Status enumerator definition.
+ */
+typedef enum
+{
+ COMPONENT_OK = 0,
+ COMPONENT_ERROR,
+ COMPONENT_TIMEOUT,
+ COMPONENT_NOT_IMPLEMENTED
+} DrvStatusTypeDef;
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Interfaces/Component_class.h Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,75 @@
+/**
+ ******************************************************************************
+ * @file Component_class.h
+ * @author AST / EST
+ * @version V0.0.1
+ * @date 13-April-2015
+ * @brief This file contains the abstract class describing the interface of a
+ * generic component.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
+ *
+ * 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.
+ *
+ ******************************************************************************
+ */
+
+
+/* Define to prevent from recursive inclusion --------------------------------*/
+
+#ifndef __COMPONENT_CLASS_H
+#define __COMPONENT_CLASS_H
+
+
+/* Includes ------------------------------------------------------------------*/
+
+#include <stdint.h>
+
+
+/* Classes ------------------------------------------------------------------*/
+
+/** An abstract class for Generic components.
+ */
+class Component
+{
+public:
+ /**
+ * @brief Initializing the component.
+ * @param init pointer to device specific initalization structure.
+ * @retval "0" in case of success, an error code otherwise.
+ */
+ virtual int Init(void *init) = 0;
+
+ /**
+ * @brief Getting the ID of the component.
+ * @param[out] id pointer to an allocated variable to store the ID into.
+ * @retval "0" in case of success, an error code otherwise.
+ */
+ virtual int ReadID(uint8_t *id) = 0;
+};
+
+#endif /* __COMPONENT_CLASS_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Interfaces/X_NUCLEO_NFC02A1.cpp Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,75 @@
+/**
+ ******************************************************************************
+ * @file X_NUCLEO_NFC02A1.h
+ * @author AMG Central Lab
+ * @date 30 Aug 2016
+ * @brief Singleton class that controls all the electronics inside the
+ * X_NUCLEO_NFC02A1 expansion board
+ ******************************************************************************
+ *
+ * COPYRIGHT(c) 2016 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 <X_NUCLEO_NFC02A1.h>
+
+const uint8_t X_NUCLEO_NFC02A1::M24LR_ADDR= 0xAE; //0xAC; //rp
+const uint8_t X_NUCLEO_NFC02A1::M24LR_ADDR_DATA = 0xA6;
+const PinName X_NUCLEO_NFC02A1::DEFAULT_SDA_PIN=D14;
+const PinName X_NUCLEO_NFC02A1::DEFAULT_SDL_PIN=D15;
+
+
+const PinName X_NUCLEO_NFC02A1::DEFAULT_GPO_PIN=D12;
+const PinName X_NUCLEO_NFC02A1::DEFAULT_RF_DISABLE_PIN=D11;
+const PinName X_NUCLEO_NFC02A1::DEFAULT_LED1_PIN=D5;
+const PinName X_NUCLEO_NFC02A1::DEFAULT_LED2_PIN=D4;
+const PinName X_NUCLEO_NFC02A1::DEFAULT_LED3_PIN=D2;
+
+X_NUCLEO_NFC02A1 *X_NUCLEO_NFC02A1::mInstance = NULL;
+
+X_NUCLEO_NFC02A1* X_NUCLEO_NFC02A1::Instance(DevI2C &devI2C,
+ const PinName &gpoName,
+ const PinName &RFDisableName, const PinName &led1Name,
+ const PinName &led2Name, const PinName &led3Name) {
+ if (mInstance == NULL) { // the first time
+ mInstance = new X_NUCLEO_NFC02A1(devI2C,gpoName,
+ RFDisableName,led1Name,led2Name,led3Name);
+ if (mInstance != NULL) { //allocation ok
+ const int status = mInstance->mM24LR.Initialization();
+ //if (status != NFC_SUCCESS) { //initialization failed
+ if (status != 0) {
+ delete mInstance;
+ error(
+ "Failed to init X_NUCLEO_NFC02A1 expansion board!\r\nError:0x%X\r\n",
+ status);
+ } //if init
+ } //if instance !=NULL
+ } //if instance
+ return mInstance;
+}
+
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Interfaces/X_NUCLEO_NFC02A1.h Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,152 @@
+/**
+ ******************************************************************************
+ * @file X_NUCLEO_NFC02A1.cpp
+ * @author AMG Central Lab
+ * @version 1.0.0
+ * @date 30 Aug 2016
+ * @brief Singleton class that controls all the electronics inside the
+ * X_NUCLEO_NFC02A1 expansion board.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
+ *
+ * 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 X_NUCLEO_NFC02A1_H_
+#define X_NUCLEO_NFC02A1_H_
+#include <stdint.h>
+
+#include "mbed.h"
+
+#include "m24lr/m24lr.h"
+
+/**
+ * Singleton class that controls all the electronics inside the X_NUCLEO_NFC02A1 expansion board.
+ */
+class X_NUCLEO_NFC02A1 {
+
+public:
+
+private:
+ /**
+ * Ponter to the singleton instance, NULL if not allocated.
+ */
+ static X_NUCLEO_NFC02A1 *mInstance;
+ /**
+ * I2C address of the M24LR chip.
+ */
+ static const uint8_t M24LR_ADDR;
+
+ static const uint8_t M24LR_ADDR_DATA;
+
+ /**
+ * Constructor
+ * @param devI2C I2C channel used to communicate with the board.
+ * @param eventCallback Function that will be called when the gpo pin status change.
+ * @param gpoName Name of the gpio pin of the M24LR chip.
+ * @param RFDisableName Pin that controls the rf antenna status.
+ * @param led1Name Pin to control the led1 status.
+ * @param led2Name Pin to control the led1 status.
+ * @param led3Name Pin to control the led1 status.
+ */
+ X_NUCLEO_NFC02A1(DevI2C &devI2C,
+ const PinName &gpoName, const PinName &RFDisableName,
+ const PinName &led1Name, const PinName &led2Name,
+ const PinName &led3Name):
+ mM24LR(M24LR_ADDR, M24LR_ADDR_DATA, devI2C),
+ mNfcLed1(led1Name),mNfcLed2(led2Name),mNfcLed3(led3Name){}
+
+public:
+ static const PinName DEFAULT_SDA_PIN; //!< Default pin used for the M24LR SDA signal.
+ static const PinName DEFAULT_SDL_PIN; //!< Default pin used for the M24LR SDL signal.
+ static const PinName DEFAULT_GPO_PIN; //!< Default pin used for the M24LR GPO signal.
+ static const PinName DEFAULT_RF_DISABLE_PIN; //!< Default pin used for M24LR RF_DISABLE signal.
+ static const PinName DEFAULT_LED1_PIN; //!< Default pin to control the led 1.
+ static const PinName DEFAULT_LED2_PIN; //!< Default pin to control the led 2.
+ static const PinName DEFAULT_LED3_PIN; //!< Default pin to control the led 3.
+
+ /**
+ * Create or return an instance of X_NUCLEO_NFC02A1.
+ * @param devI2C I2C channel used to communicate with the board.
+ * @param eventCallback Function that will be called when the gpo pin status change.
+ * @param gpoName Name of the gpio pin of the M24LR chip.
+ * @param RFDisableName Pin that controls the rf antenna status.
+ * @param led1Name Pin to control the led1 status.
+ * @param led2Name Pin to control the led1 status.
+ * @param led3Name Pin to control the led1 status.
+ */
+ static X_NUCLEO_NFC02A1* Instance(DevI2C &devI2C,
+ const PinName &gpoName = DEFAULT_GPO_PIN,
+ const PinName &RFDisableName = DEFAULT_RF_DISABLE_PIN,
+ const PinName &led1Name = DEFAULT_LED1_PIN,
+ const PinName &led2Name = DEFAULT_LED2_PIN,
+ const PinName &led3Name = DEFAULT_LED3_PIN);
+
+ /**
+ * @return board led1.
+ */
+ DigitalOut& getLed1() {
+ return mNfcLed1;
+ }
+
+ /**
+ * @return board led2.
+ */
+ DigitalOut& getLed2() {
+ return mNfcLed2;
+ }
+
+ /**
+ * @return board led3.
+ */
+ DigitalOut& getLed3() {
+ return mNfcLed3;
+ }
+
+ /**
+ * @return NFC Chip.
+ */
+ M24LR& getNFCTagM24LR() {
+ return mM24LR;
+ }
+
+ M24LR& getM24LR() {
+ return mM24LR;
+ }
+
+ virtual ~X_NUCLEO_NFC02A1() {
+ }
+
+private:
+ M24LR mM24LR;
+
+ DigitalOut mNfcLed1;
+ DigitalOut mNfcLed2;
+ DigitalOut mNfcLed3;
+
+};
+#endif /* X_NUCLEO_NFC02A1_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/X_NUCLEO_COMMON/DevI2C/DevI2C.h Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,187 @@
+/**
+ ******************************************************************************
+ * @file DevI2C.h
+ * @author AST / EST / AMG-CL
+ * @version V0.0.2
+ * @date 25-July-2016
+ * @brief Header file for a special I2C class DevI2C which provides some
+ * helper function for on-board communication
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
+ *
+ * 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.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent from recursive inclusion --------------------------------*/
+#ifndef __DEV_I2C_H
+#define __DEV_I2C_H
+
+/* Includes ------------------------------------------------------------------*/
+#include "mbed.h"
+
+/* Classes -------------------------------------------------------------------*/
+/** Helper class DevI2C providing functions for multi-register I2C communication
+ * common for a series of I2C devices
+ */
+class DevI2C : public I2C
+{
+ public:
+
+ /** Create a DevI2C Master interface, connected to the specified pins
+ *
+ * @param sda I2C data line pin
+ * @param scl I2C clock line pin
+ */
+ DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {}
+
+ /**
+ * @brief Writes a buffer towards the I2C peripheral device.
+ * @param pBuffer pointer to the byte-array data to send
+ * @param DeviceAddr specifies the peripheral device slave address.
+ * @param RegisterAddr specifies the internal address register
+ * where to start writing to (must be correctly masked).
+ * @param NumByteToWrite number of bytes to be written.
+ * @retval 0 if ok,
+ * @retval -1 if an I2C error has occured, or
+ * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
+ * @note On some devices if NumByteToWrite is greater
+ * than one, the RegisterAddr must be masked correctly!
+ */
+ int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
+ uint16_t NumByteToWrite)
+ {
+ int ret;
+ uint8_t tmp[TEMP_BUF_SIZE];
+
+ if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
+
+ /* First, send device address. Then, send data and STOP condition */
+ tmp[0] = RegisterAddr;
+ memcpy(tmp+1, pBuffer, NumByteToWrite);
+
+ ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false);
+
+ if(ret) return -1;
+ return 0;
+ }
+
+ /**
+ * @brief Writes a buffer towards the I2C peripheral device.
+ * @param pBuffer pointer to the byte-array data to send
+ * @param DeviceAddr specifies the peripheral device slave address.
+ * @param RegisterAddr specifies the internal address register
+ * where to start writing to (must be correctly masked).
+ * @param NumByteToWrite number of bytes to be written.
+ * @retval 0 if ok,
+ * @retval -1 if an I2C error has occured, or
+ * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
+ * @note On some devices if NumByteToWrite is greater
+ * than one, the RegisterAddr must be masked correctly!
+ */
+ int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
+ uint16_t NumByteToWrite)
+ {
+ int ret;
+ uint8_t tmp[TEMP_BUF_SIZE];
+
+ if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
+
+ /* First, send device address. Then, send data and STOP condition */
+ tmp[0] = (RegisterAddr >> 8) & 0xFF;
+ tmp[1] = (RegisterAddr) & 0xFF;
+ memcpy(tmp+2, pBuffer, NumByteToWrite);
+
+ ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false);
+
+ if(ret) return -1;
+ return 0;
+ }
+ /**
+ * @brief Reads a buffer from the I2C peripheral device.
+ * @param pBuffer pointer to the byte-array to read data in to
+ * @param DaviceAddr specifies the peripheral device slave address.
+ * @param RegisterAddr specifies the internal address register
+ * where to start reading from (must be correctly masked).
+ * @param NumByteToRead number of bytes to be read.
+ * @retval 0 if ok,
+ * @retval -1 if an I2C error has occured
+ * @note On some devices if NumByteToWrite is greater
+ * than one, the RegisterAddr must be masked correctly!
+ */
+ int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
+ uint16_t NumByteToRead)
+ {
+ int ret;
+ uint8_t reg_addr[2];
+ reg_addr[0] = (RegisterAddr >> 8) & 0xFF;
+ reg_addr[1] = (RegisterAddr) & 0xFF;
+ /* Send device address, with no STOP condition */
+ ret = write(DeviceAddr, (const char*)reg_addr, 2, true);
+ if(!ret) {
+ /* Read data, with STOP condition */
+ ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
+ }
+
+ if(ret)
+ return -1;
+ else
+ return 0;
+ }
+
+ /**
+ * @brief Reads a buffer from the I2C peripheral device.
+ * @param pBuffer pointer to the byte-array to read data in to
+ * @param DaviceAddr specifies the peripheral device slave address.
+ * @param RegisterAddr specifies the internal address register
+ * where to start reading from (must be correctly masked).
+ * @param NumByteToRead number of bytes to be read.
+ * @retval 0 if ok,
+ * @retval -1 if an I2C error has occured
+ * @note On some devices if NumByteToWrite is greater
+ * than one, the RegisterAddr must be masked correctly!
+ */
+ int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
+ uint16_t NumByteToRead)
+ {
+ int ret;
+
+ /* Send device address, with no STOP condition */
+ ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true);
+ if(!ret) {
+ /* Read data, with STOP condition */
+ ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
+ }
+
+ if(ret) return -1;
+ return 0;
+ }
+
+private:
+ static const unsigned int TEMP_BUF_SIZE = 32;
+};
+
+#endif /* __DEV_I2C_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/m24lr/NDefNfcTagM24LR.cpp Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,492 @@
+/**
+ ******************************************************************************
+ * @file NdefNfcTagM24LR.cpp
+ * @author AMG Central Lab
+ * @version V1.0.0
+ * @date 30 Aug 2016
+ * @brief Wrapper class of the NDefLib library to write/read NDEF messages.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
+ *
+ * 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 <cmath>
+
+#include "NDefNfcTagM24LR.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 generally releases the session within the second, anyway the user can modify this value */
+#define OPENSESSION_NTRIALS 5
+
+#define CC_FILE_LENGTH_BYTE 15
+
+
+#define NFCT5_MAGICNUMBER_E1_CCFILE 0xE1
+#define NFCT5_MAGICNUMBER_E2_CCFILE 0xE2
+#define NFCT5_EXTENDED_CCFILE 0xFF
+#define NFCT5_VERSION_V1_0 0x40
+#define NFCT5_READ_ACCESS 0x0C
+#define NFCT5_WRITE_ACCESS 0x03
+
+#define NFCT5_NDEF_MSG_TLV 0x03
+#define NFCT5_PROPRIETARY_TLV 0xFD
+#define NFCT5_TERMINATOR_TLV 0xFE
+#define NFCT5_3_BYTES_L_TLV 0xFF
+
+
+#define NDEF_MAX_SIZE NFC_DEVICE_MAX_NDEFMEMORY
+
+#define NDEF_SIZE_OFFSET 0
+#define FIRST_RECORD_OFFSET 2
+
+#ifndef MIN
+#define MIN(x, y) (((x) < (y)) ? (x) : (y))
+#endif
+
+#define NFCTAG_4M_SIZE 0x200
+#define NFCTAG_16M_SIZE 0x800
+#define NFCTAG_64M_SIZE 0x2000
+
+
+#define MAX_NDEF_MEM 0x200
+#define M24LR_MAX_SIZE NFCTAG_4M_SIZE
+#define M24LR_NDEF_MAX_SIZE MIN(M24LR_MAX_SIZE,MAX_NDEF_MEM)
+#define NFC_DEVICE_MAX_NDEFMEMORY M24LR_NDEF_MAX_SIZE
+/**
+ * @brief This function read the data stored in NDEF file at defined offset.
+ * @param Offset : Offset in the NDEF file.
+ * @param DataSize : Number of byte to read.
+ * @param pData : pointer on buffer to store read data.
+ * @retval NDEF_ERROR_MEMORY_INTERNAL : Size not compatible with memory.
+ * @retval NDEF_ERROR_NOT_FORMATED : CCFile data not supported.
+ * @retval NDEF_ERROR : No NDEF in the tag.
+ * @retval NDEF_OK : The operation is completed.
+ */
+uint16_t NDefNfcTagM24LR::ReadData( uint16_t Offset , uint16_t DataSize , uint8_t* pData )
+{
+ uint16_t status = NDEF_ERROR;
+ uint8_t atlv_detect[4];
+ uint8_t index = 0;
+
+ /* Do not include length bytes */
+ DataSize -= FIRST_RECORD_OFFSET;
+
+ /* If too many data to write return error */
+ if( DataSize > NDEF_MAX_SIZE )
+ {
+ return NDEF_ERROR_MEMORY_INTERNAL;
+ }
+
+ /* Detect NDEF message in memory */
+ status = NfcType5_NDEFDetection( );
+ if( status != NDEF_OK )
+ {
+ return status;
+ }
+
+ /* Read TL of Type 5 */
+ status = NDefReadByte(CCFileStruct.NDEF_offset, 4, atlv_detect);
+ if( status != NDEF_OK )
+ {
+ return status;
+ }
+
+ /* Check if L is on 3 or 1 byte and update length in buffer */
+ if( atlv_detect[1] == NFCT5_3_BYTES_L_TLV )
+ {
+ pData[0] = atlv_detect[2];
+ pData[1] = atlv_detect[3];
+ index += 4;
+ }
+ else
+ {
+ pData[0] = 0x00;
+ pData[1] = atlv_detect[1];
+ index += 2;
+ }
+
+ /* Check CC file is in the correct mode to proceed */
+ if( CCFileStruct.State == TT5_INITIALIZED )
+ {
+ return NDEF_ERROR;
+ }
+
+ if( ((Offset == 0) && (DataSize > 0)) || (Offset > 0) )
+ {
+ /* Read NDEF */
+ if( NDefReadByte(CCFileStruct.NDEF_offset + index + Offset, DataSize, (pData + FIRST_RECORD_OFFSET)) != NFC_OK )
+ {
+ return NDEF_ERROR;
+ }
+ }
+
+ return NDEF_OK;
+}
+
+/**
+ * @brief This function writes data in NDEF file at defined offset.
+ * @param Offset : Offset in the NDEF file.
+ * @param DataSize : Number of byte to write.
+ * @param pData : pointer on buffer to copy.
+ * @retval NDEF_ERROR_MEMORY_INTERNAL : Size not compatible with memory.
+ * @retval NDEF_ERROR_NOT_FORMATED : CCFile data not supported.
+ * @retval NDEF_ERROR : No NDEF in the tag.
+ * @retval NDEF_OK : The operation is completed.
+ */
+uint16_t NDefNfcTagM24LR::WriteData( uint16_t Offset , uint32_t DataSize , uint8_t *pData )
+{
+ uint8_t atlv[4];
+ uint8_t index = 0;
+ uint16_t NDEF_Size = 0;
+ bool status;
+
+ /* Do not include length bytes */
+ DataSize -= FIRST_RECORD_OFFSET;
+
+ /* If too many data to write return error */
+ if( DataSize > NDEF_MAX_SIZE )
+ {
+ return NDEF_ERROR_MEMORY_INTERNAL;
+ }
+
+ /* Detect NDEF message in memory */
+ if( NfcType5_NDEFDetection( ) != NDEF_OK )
+ {
+ return NDEF_ERROR;
+ }
+
+ /* Extract NDEF Size from buffer */
+ NDEF_Size = (uint16_t)(pData[0] << 8);
+ NDEF_Size = NDEF_Size | (uint16_t)(pData[1] );
+
+ /* If entire NDEF is written, update Length of Type 5 */
+ if( DataSize == NDEF_Size )
+ {
+ /* Check if L is on 3 or 1 byte */
+ if( NDEF_Size >= NFCT5_3_BYTES_L_TLV )
+ {
+ /* First init L with 0, will be updated at the end */
+ atlv[1] = 0x00;
+ atlv[2] = 0x00;
+ atlv[3] = 0x00;
+
+ status = NDefWriteByte( (atlv + 1), 3, (CCFileStruct.NDEF_offset + 1));
+
+ index += 4;
+ }
+ else
+ {
+ /* First inti L with 0, will be updated at the end */
+ atlv[1] = 0x00;
+
+ status = NDefWriteByte( (atlv + 1), 1, (CCFileStruct.NDEF_offset + 1));
+
+ index += 2;
+ }
+ }
+
+ /* Start write NDEF message to EEPROM */
+ status = NDefWriteByte( (pData + FIRST_RECORD_OFFSET), DataSize, CCFileStruct.NDEF_offset + index + Offset);
+ if( status != NFC_OK )
+ {
+ return NDEF_ERROR;
+ }
+
+ /* If entire NDEF is written, update Length of Type 5 */
+ if( DataSize == NDEF_Size )
+ {
+ /* Check if L is on 3 or 1 byte */
+ if( NDEF_Size >= NFCT5_3_BYTES_L_TLV )
+ {
+ /* Update Length value */
+ atlv[1] = NFCT5_3_BYTES_L_TLV;
+ atlv[2] = pData[0];
+ atlv[3] = pData[1];
+
+ status = NDefWriteByte( (atlv + 1), 3, (CCFileStruct.NDEF_offset + 1));
+ }
+ else
+ {
+ /* Update Length value */
+ atlv[1] = pData[1];
+
+ status = NDefWriteByte( (atlv + 1), 1, (CCFileStruct.NDEF_offset + 1));
+ }
+
+ /* Write Terminator TLV */
+ atlv[0] = NFCT5_TERMINATOR_TLV;
+ status = NDefWriteByte( atlv, 1, CCFileStruct.NDEF_offset + index + NDEF_Size);
+ }
+
+ return NDEF_OK;
+}
+
+/**
+ * @brief This functions writes CCFile in EEPROM.
+ * @Param pCCBuffer : pointer on the buffer containnig CC file.
+ * @retval NFCTAG status.
+ */
+uint16_t NDefNfcTagM24LR::NfcType5_WriteCCFile( const uint8_t * const pCCBuffer )
+{
+ bool ret_value;
+
+ /* Write first block of CCFile */
+ ret_value = NDefWriteByte( pCCBuffer, 0x4, 0x00);
+
+ /* If extended memory writes the next 4 bytes */
+ if( (pCCBuffer[2] == 0x00) && (ret_value == NFC_OK) )
+ {
+ ret_value = NDefWriteByte( pCCBuffer + 4, 4, 0x04);
+ }
+
+ if( ret_value != NFC_OK )
+ {
+ return NDEF_ERROR;
+ }
+
+ return NDEF_OK;
+}
+
+
+
+
+uint16_t NDefNfcTagM24LR::NfcType5_ReadCCFile( uint8_t * const pCCBuffer )
+{
+ bool ret_value;
+
+ /* Read 4 bytes of CC File */
+ ret_value = NDefReadByte(0x00, 4, pCCBuffer);
+
+ /* If extended memory reads the next 4 bytes */
+ if( (pCCBuffer[2] == 0x00) && (ret_value == NFC_OK) )
+ {
+ ret_value = NDefReadByte(0x04, 4, pCCBuffer + 4 );
+ }
+
+ if( ret_value != NFC_OK )
+ {
+ return NDEF_ERROR;
+ }
+
+ return NDEF_OK;
+}
+uint16_t NDefNfcTagM24LR::NfcType5_TT5Init( void )
+{
+ bool ret_value = NFC_OK;
+ uint16_t status;
+ uint8_t accbuffer[8];
+ uint8_t cdata;
+
+ /* Prepare buffer to update CCFile */
+ accbuffer[0] = CCFileStruct.MagicNumber;
+ accbuffer[1] = CCFileStruct.Version;
+ accbuffer[2] = CCFileStruct.MemorySize;
+ accbuffer[3] = CCFileStruct.TT5Tag;
+ CCFileStruct.NDEF_offset = 0x04;
+
+ /* If extended memory prepare the length bytes */
+ if( CCFileStruct.MemorySize == NFCT5_EXTENDED_CCFILE )
+ {
+ accbuffer[6] = (uint8_t)(CCFileStruct.ExtMemorySize >> 8);
+ accbuffer[7] = (uint8_t)(CCFileStruct.ExtMemorySize & 0xFF);
+ CCFileStruct.NDEF_offset = 0x08;
+ }
+
+ /* Update CCFile */
+ status = NfcType5_WriteCCFile( accbuffer );
+ if( status != NDEF_OK )
+ {
+ return status;
+ }
+
+ /* Update NDEF TLV for INITIALIZED state */
+ /* Update T */
+ cdata = NFCT5_NDEF_MSG_TLV;
+ ret_value = NDefWriteByte( &cdata, 1, CCFileStruct.NDEF_offset);
+ if( ret_value != NFC_OK )
+ {
+ return NDEF_ERROR;
+ }
+
+ /* Update L */
+ cdata = 0x00;
+ ret_value = NDefWriteByte( &cdata, 1, (CCFileStruct.NDEF_offset + 1));
+ if( ret_value != NFC_OK )
+ {
+ return NDEF_ERROR;
+ }
+
+ return NDEF_OK;
+}
+
+
+uint16_t NDefNfcTagM24LR::NfcType5_NDEFDetection( void )
+{
+ uint8_t acc_buffer[8];
+ uint8_t atlv_detect[4];
+ uint16_t status;
+ uint32_t memory_size;
+
+ CCFileStruct.State = TT5_NO_NDEF;
+
+ /* Read CCFile */
+ status = NfcType5_ReadCCFile( acc_buffer );
+ if( status != NDEF_OK )
+ {
+ return status;
+ }
+
+ /* Check Byte 0 is equal to magic number */
+ if( ( acc_buffer[0] != NFCT5_MAGICNUMBER_E1_CCFILE ) && ( acc_buffer[0] != NFCT5_MAGICNUMBER_E2_CCFILE ) )
+ {
+ return NDEF_ERROR_NOT_FORMATED;
+ }
+ /* Check Version number */
+ else if( ( (acc_buffer[1]&0xFC) != 0x40 ) )
+ {
+ return NDEF_ERROR_NOT_FORMATED;
+ }
+
+ /* Check if CCFile is on 4 Bytes or 8 Bytes */
+ if( acc_buffer[2] == 0x00 )
+ {
+ /* Update CCFIle structure */
+ CCFileStruct.MemorySize = 0x0;
+ CCFileStruct.ExtMemorySize = (uint16_t)acc_buffer[6];
+ CCFileStruct.ExtMemorySize = ( CCFileStruct.ExtMemorySize << 8 ) | acc_buffer[7];
+ memory_size = CCFileStruct.ExtMemorySize;
+ CCFileStruct.NDEF_offset = 8;
+ }
+ else
+ {
+ /* Update CCFIle structure */
+ CCFileStruct.MemorySize = acc_buffer[2];
+ CCFileStruct.ExtMemorySize = 0x0;
+ memory_size = CCFileStruct.MemorySize;
+ CCFileStruct.NDEF_offset = 4;
+ }
+
+ /* Update CCFIle structure */
+ CCFileStruct.MagicNumber = acc_buffer[0];
+ CCFileStruct.Version = acc_buffer[1];
+ CCFileStruct.TT5Tag = acc_buffer[3];
+
+ /* Search for position of NDEF TLV in memory and tag status */
+ while( ( NDefReadByte(CCFileStruct.NDEF_offset, 4, atlv_detect) == NFC_OK ) && ( CCFileStruct.NDEF_offset < memory_size ) )
+ {
+ /* Detect first NDEF Message in memory */
+ if( atlv_detect[0] == NFCT5_NDEF_MSG_TLV )
+ {
+ if( atlv_detect[1] == 0x00 )
+ {
+ CCFileStruct.State = TT5_INITIALIZED;
+ }
+ else
+ {
+ if( CCFileStruct.Version & 0x3 )
+ {
+ CCFileStruct.State = TT5_READ;
+ }
+ else
+ {
+ CCFileStruct.State = TT5_READ_WRITE;
+ }
+ }
+ return NDEF_OK;
+ }
+ /* If Proprietary NDEF jump to end of proprietary message */
+ else if( atlv_detect[0] == NFCT5_PROPRIETARY_TLV )
+ {
+ if( atlv_detect[1] == NFCT5_3_BYTES_L_TLV )
+ {
+ CCFileStruct.NDEF_offset = CCFileStruct.NDEF_offset + ( (uint32_t)atlv_detect[2] << 8 ) + atlv_detect[3];
+ continue;
+ }
+ else
+ {
+ CCFileStruct.NDEF_offset = CCFileStruct.NDEF_offset + atlv_detect[1];
+ continue;
+ }
+ }
+ /* if Terminator no NDEF detected */
+ else if( atlv_detect[0] == NFCT5_TERMINATOR_TLV )
+ {
+ return NDEF_ERROR_NOT_FORMATED;
+ }
+
+ CCFileStruct.NDEF_offset++;
+ }
+
+ return NDEF_ERROR_NOT_FORMATED;
+}
+
+bool NDefNfcTagM24LR::openSession(bool force) {
+bool status;
+
+ status = NfcType5_NDEFDetection();
+
+ if ( status == NFC_SUCCESS )
+ {
+ CCFileStruct.MagicNumber = NFCT5_MAGICNUMBER_E1_CCFILE;
+ CCFileStruct.Version = NFCT5_VERSION_V1_0;
+ CCFileStruct.MemorySize = ( M24LR_MAX_SIZE / 8 ) & 0xFF;
+ CCFileStruct.TT5Tag = 0x05;
+ /* Init of the Type Tag 5 component (M24LR) */
+ while( NfcType5_TT5Init( ) != NFC_OK );
+ }
+ mIsSessionOpen = 1;
+ return NFC_SUCCESS;
+
+}
+
+bool NDefNfcTagM24LR::closeSession() {
+
+ return NFC_SUCCESS;
+}
+bool NDefNfcTagM24LR::writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset,
+ byteOperationCallback_t callback,CallbackStatus_t *callbackStatus){
+ return WriteData(offset + FIRST_RECORD_OFFSET, length, (uint8_t*)buffer);
+ }
+bool NDefNfcTagM24LR::readByte(const uint16_t byteOffset, const uint16_t length,
+ uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus){
+ return ReadData(byteOffset, length, (uint8_t*)buffer);
+ }
+
+bool NDefNfcTagM24LR::NDefWriteByte(const uint8_t *buffer, uint16_t length,uint16_t offset){
+ return mDevice.UpdateBinary(offset, length, (uint8_t*)buffer);
+}
+
+
+bool NDefNfcTagM24LR::NDefReadByte(const uint16_t byteOffset, const uint16_t length, uint8_t *buffer){
+ return mDevice.ReadBinary(byteOffset, length, (uint8_t*)buffer);
+}
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/m24lr/NDefNfcTagM24LR.h Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,158 @@
+/**
+ ******************************************************************************
+ * @file NdefNfcTagM24LR.h
+ * @author AMG Central Lab
+ * @version V1.0.0
+ * @date 30 Aug 2016
+ * @brief M24LR specific NDefLib derived class
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
+ *
+ * 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 X_NUCLEO_NFC02A1_M24LR_NDEFNFCTAGM24LR_H_
+#define X_NUCLEO_NFC02A1_M24LR_NDEFNFCTAGM24LR_H_
+
+#include <cstdlib>
+#include <stdint.h>
+
+#include "NDefNfcTag.h"
+#include "Nfc.h"
+
+#define NFC_OK 0
+#define NDEF_OK 0
+#define NDEF_ERROR 1
+#define NDEF_ERROR_MEMORY_TAG 2
+#define NDEF_ERROR_MEMORY_INTERNAL 3
+#define NDEF_ERROR_LOCKED 4
+#define NDEF_ERROR_NOT_FORMATED 5
+
+/**
+ * Helper class to use the NDefLib
+ */
+
+
+class NDefNfcTagM24LR: public NDefLib::NDefNfcTag {
+
+public:
+
+ /**
+ *
+ * @param device device to use
+ */
+ NDefNfcTagM24LR(Nfc &device) :
+ NDefLib::NDefNfcTag(),
+ mDevice(device),mIsSessionOpen(false),
+ mMaxReadBytes(0xFF), mMaxWriteBytes(0xFF){}
+
+ virtual bool openSession(bool force = false);
+
+ virtual bool closeSession();
+
+ virtual bool isSessionOpen(){
+ return mIsSessionOpen;
+ }
+
+ /**
+ * Close the open session.
+ */
+ virtual ~NDefNfcTagM24LR(){
+ if(isSessionOpen())
+ closeSession();
+ }//~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);
+ bool NDefWriteByte(const uint8_t *buffer, uint16_t length,uint16_t offset);
+
+
+
+
+ private:
+ bool NDefReadByte(const uint16_t byteOffset, const uint16_t length,
+ uint8_t *buffer);
+
+ uint16_t ReadData( uint16_t Offset , uint16_t DataSize , uint8_t* pData );
+ uint16_t WriteData( uint16_t Offset , uint32_t DataSize , uint8_t *pData );
+ uint16_t NfcType5_WriteCCFile( const uint8_t * const pCCBuffer );
+ uint16_t NfcType5_ReadCCFile( uint8_t * const pCCBuffer );
+ uint16_t NfcType5_TT5Init( void );
+ uint16_t NfcType5_NDEFDetection( void );
+ 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;
+ typedef enum
+ {
+ TT5_NO_NDEF = 0,
+ TT5_INITIALIZED,
+ TT5_READ_WRITE,
+ TT5_READ
+ } TT5_State;
+
+ /**
+ * @brief CCfile structure
+ */
+ typedef struct
+ {
+ uint8_t MagicNumber; /* Magic Number should be E1h or E2h */
+ uint8_t Version;
+ uint8_t MemorySize;
+ uint8_t TT5Tag;
+ uint8_t rsved1;
+ uint8_t rsved2;
+ uint16_t ExtMemorySize;
+ TT5_State State;
+ uint32_t NDEF_offset;
+ }sCCFileInfo;
+
+ sCCFileInfo CCFileStruct;
+
+ };
+
+#endif /* X_NUCLEO_NFC02A1_M24LR_NDEFNFCTAGM24LR_H_ */
+
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/m24lr/m24lr.cpp Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,1022 @@
+/**
+ ******************************************************************************
+ * @file m24lr.cpp
+ * @author AMG Central Lab
+ * @version V1.0.0
+ * @date 30 Aug 2016
+ * @brief M24LR driver file.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
+ *
+ * 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 "m24lr.h"
+
+uint8_t M24LR::NfctagInitialized = 0;
+
+
+/**
+ * @brief This function activate Energy Harvesting mode
+ */
+void M24LR::Enable_EnergyHarvesting( void )
+{
+ /* Initialise M24LR Board */
+
+ /* Enable Energy Harvesting */
+ i2c_SetEH( );
+
+ /* Store configuration in non Volatile Memory */
+ i2c_Enable_EH_mode();
+ i2c_WriteEH_Cfg( M24LR_EH_Cfg_6MA );
+
+}
+/**
+ * @brief Set M24LR nfctag Initialization
+ * @param None
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::Initialization( void )
+{
+ uint8_t nfctag_id = 0;
+
+ if( NfctagInitialized == 0 )
+ {
+
+ /* M24LR Init */
+ if( i2c_Init() != NFCTAG_OK )
+ {
+ return NFCTAG_ERROR;
+ }
+
+ /* Check M24LR driver ID */
+ i2c_ReadID(&nfctag_id);
+ if( (nfctag_id == I_AM_M24LR04) || (nfctag_id == I_AM_M24LR16) || (nfctag_id == I_AM_M24LR64) )
+ {
+ NfctagInitialized = 1;
+ // Nfctag_Drv = &M24lr_i2c_Drv;
+ // Nfctag_Drv->pData = &M24lr_i2c_ExtDrv;
+ }
+ else
+ {
+ NfctagInitialized = 0;
+ // Nfctag_Drv = NULL;
+ // NfctagInitialized = 0;
+ return NFCTAG_ERROR;
+ }
+ }
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Set M24LR Initialization
+ * @param None
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_Init( void )
+{
+ /* Configure the low level interface */
+ return(NFCTAG_OK);
+ // return mM24LR_IO.Init( );
+}
+
+/**
+ * @brief Read M24LR ID
+ * @param pICRef : pointer to store ID
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadID( uint8_t * const pData )
+{
+ uint8_t *pBuffer = (uint8_t *)pData;
+ NFCTAG_StatusTypeDef status;
+ /* Read ICRef on device */
+ //return M24LR_i2c_ReadRegister( pICRef, M24LR_ICREF_REG, 1 );
+ /* Before calling this function M24LR must be ready, here is a check to detect an issue */
+ status = i2c_ReadRegister(pBuffer, M24LR_ICREF_REG, 1);
+
+ if (status == 0)
+ return NFCTAG_OK;
+ return NFCTAG_TIMEOUT;
+
+}
+
+/**
+ * @brief Check M24LR availability
+ * @param Trials : number of max tentative tried
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_IsDeviceReady( const uint32_t Trials )
+{
+ /* Test i2c with M24LR */
+ // return mM24LR_IO.IsDeviceReady( M24LR_ADDR_DATA_I2C, Trials );
+ uint8_t status = 1;
+ char buffer;
+ while (status != 0) {
+ /* for device is ready address in M24Lr is M24LR_ADDR_DATA_I2C */
+ status = dev_I2C.read(i2c_address_data, &buffer, 1, false);
+ }
+ if ( status == 0 )
+ return NFCTAG_OK;
+ else
+ return NFCTAG_TIMEOUT;
+
+}
+
+/**
+ * @brief Configure M24LR GPO
+ * @param ITConf : 0x01 = RFBUSY, 0x02 = RFWIP
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ConfigureGPO( const uint16_t ITConf )
+{
+ NFCTAG_StatusTypeDef status = NFCTAG_ERROR;
+
+ /* Configure GPO function on M24LR */
+ if( (ITConf & M24LR_IT_BUSY_MASK) == M24LR_IT_BUSY_MASK )
+ {
+ status = i2c_SetRFBUSY( );
+ }
+ else if( (ITConf & M24LR_IT_WIP_MASK) == M24LR_IT_WIP_MASK )
+ {
+ status = i2c_SetRFWIP( );
+ }
+ return status;
+}
+
+/**
+ * @brief Configure GPO as RF WriteInProgress
+ * @param None
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_SetRFWIP( void )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+ status = i2c_ReadRegister(®_value, M24LR_CFG_REG, 1);
+
+
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Update register value for WIP configuration */
+ reg_value |= M24LR_CFG_WIPBUSY_MASK;
+
+ /* Write CFG register */
+ return i2c_WriteRegister( ®_value, M24LR_CFG_REG, 1 );
+}
+
+
+/**
+ * @brief Get Configuration of M24LR GPO
+ * @param GPOStatus : 0x01 = RFBUSY, 0x02 = RFWIP
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_GetGPOStatus( uint16_t * const pGPOStatus )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CFG register */
+ status = i2c_ReadRegister( ®_value, M24LR_CFG_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Extract RF WIP/BUSY information */
+ if( (reg_value & M24LR_CFG_WIPBUSY_MASK) == M24LR_CFG_WIPBUSY_MASK )
+ {
+ *pGPOStatus = M24LR_IT_WIP_MASK;
+ }
+ else
+ {
+ *pGPOStatus = M24LR_IT_BUSY_MASK;
+ }
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Read N bytes starting from specified I2C address
+ * @param pData : pointer of the data to store
+ * @param TarAddr : I2C data memory address to read
+ * @param NbByte : number of bytes to read
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadData( uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte )
+{
+ int status;
+ /* Before calling this function M24LR must be ready, here is a check to detect an issue */
+ if( i2c_IsDeviceReady( 1 ) != NFCTAG_OK )
+ {
+ return NFCTAG_TIMEOUT;
+ }
+ /* Rosarium : To check M24LR_ADDR_DATA_I2C is this case */
+ /* return M24lr_IO_MemRead( pData, M24LR_ADDR_DATA_I2C, TarAddr, NbByte ); */
+ status = dev_I2C.i2c_read(pData, i2c_address_data, TarAddr, NbByte);
+ if ( status == 0 )
+ return NFCTAG_OK;
+ else
+ return NFCTAG_TIMEOUT;
+}
+
+/**
+ * @brief Write N data bytes starting from specified I2C Address
+ * @brief if I2C_Write_Lock bit = 0 or I2C_Password present => ack (modification OK)
+ * @brief if I2C_Write_Lock bit = 1 and no I2C_Password present => No ack (no modification)
+ * @param pData : pointer of the data to write
+ * @param TarAddr : I2C data memory address to write
+ * @param NbByte : number of bytes to write
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_WriteData( const uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte )
+{
+ int status;
+ uint8_t align_mem_offset;
+ uint16_t bytes_to_write = NbByte;
+ uint16_t mem_addr = TarAddr;
+ uint8_t *pdata_index = (uint8_t *)pData;
+
+ /* Before calling this function M24LR must be ready, here is a check to detect an issue */
+ if( i2c_IsDeviceReady( 1 ) != NFCTAG_OK )
+ {
+ return NFCTAG_TIMEOUT;
+ }
+
+ /* M24LR can write a maximum of 4 bytes in EEPROM per i2c communication */
+ do
+ {
+ /* To write data in M24LR, data must be aligned on the same row in memory */
+ /* align_mem_offset is used to copy only Bytes that are on the same row in memory */
+ if( bytes_to_write > M24LR_PAGEWRITE_NBBYTE )
+ {
+ /* DataSize higher than max page write, copy data by page */
+ align_mem_offset = M24LR_PAGEWRITE_NBBYTE - (mem_addr % M24LR_PAGEWRITE_NBBYTE);
+ }
+ else
+ {
+ /* DataSize lower or equal to max page write, copy only last bytes */
+ align_mem_offset = bytes_to_write;
+ }
+ /* Write align_mem_offset bytes in memory */
+ /* Rosarium to Check as the address here is 0xA6 rather than 0xAE */
+ /* Rosarium dev_I2C.i2c_write(pdata_index, M24LR_ADDR_DATA_I2C, mem_addr, align_mem_offset); */
+ status = dev_I2C.i2c_write(pdata_index, i2c_address_data, mem_addr, align_mem_offset);
+
+ /* update index, dest address, size for next write */
+ pdata_index += align_mem_offset;
+ mem_addr += align_mem_offset;
+ bytes_to_write -= align_mem_offset;
+ /* Poll until EEPROM is available */
+ while( i2c_IsDeviceReady( 1 ) != NFCTAG_OK ) {};
+ }
+ while( ( bytes_to_write > 0 ) && ( status == NFCTAG_OK ) );
+ if ( status == 0 )
+ return NFCTAG_OK;
+ else
+ return NFCTAG_ERROR;
+}
+
+/**
+ * @brief Read N register bytes starting from specified I2C address
+ * @param pData : pointer of the data to store
+ * @param TarAddr : I2C memory address to read
+ * @param NbByte : number of bytes to read
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadRegister( uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte )
+{
+ /* Before calling read function M24LR must be ready, here is a check to detect an issue */
+ int status;
+
+ /* Before calling any read function M24LR must be ready, here is a check to detect an issue */
+ if( i2c_IsDeviceReady( 1 ) != NFCTAG_OK )
+ {
+ return NFCTAG_TIMEOUT;
+ }
+ /* Read actual value of register */
+ status = dev_I2C.i2c_read(pData, i2c_address_syst, TarAddr, NbByte);
+
+ if ( status == 0 )
+ return NFCTAG_OK;
+ else
+ return NFCTAG_TIMEOUT;
+}
+
+/**
+ * @brief Write N bytes to specific register
+ * @param pData : pointer of the data to write
+ * @param TarAddr : I2C register address to write
+ * @param NbByte : number of bytes to write
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_WriteRegister( const uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte )
+{
+ int status;
+ uint8_t align_mem_offset;
+ uint16_t bytes_to_write = NbByte;
+ uint16_t mem_addr = TarAddr;
+ uint8_t *pdata_index = (uint8_t *)pData;
+
+ /* Before calling this function M24LR must be ready, here is a check to detect an issue */
+ if( i2c_IsDeviceReady( 1 ) != NFCTAG_OK )
+ {
+ return NFCTAG_TIMEOUT;
+ }
+
+ /* M24LR can write a maximum of 4 bytes in EEPROM per i2c communication */
+ do
+ {
+ /* To write data in M24LR, data must be aligned on the same row in memory */
+ /* align_mem_offset is used to copy only Bytes that are on the same row in memory */
+ if( bytes_to_write > M24LR_PAGEWRITE_NBBYTE )
+ {
+ /* DataSize higher than max page write, copy data by page */
+ align_mem_offset = M24LR_PAGEWRITE_NBBYTE - (mem_addr % M24LR_PAGEWRITE_NBBYTE);
+ }
+ else
+ {
+ /* DataSize lower or equal to max page write, copy only last bytes */
+ align_mem_offset = bytes_to_write;
+ }
+ /* Write align_mem_offset bytes in register */
+ // status = M24lr_IO_MemWrite( pdata_index, M24LR_ADDR_SYST_I2C, mem_addr, align_mem_offset );
+ status = dev_I2C.i2c_write(pdata_index, i2c_address_syst, mem_addr, align_mem_offset);
+ /* update index, dest address, size for next write */
+ pdata_index += align_mem_offset;
+ mem_addr += align_mem_offset;
+ bytes_to_write -= align_mem_offset;
+ /* Poll until EEPROM is available */
+ while( i2c_IsDeviceReady( 1 ) != NFCTAG_OK ) {};
+ }
+ while( ( bytes_to_write > 0 ) && ( status == NFCTAG_OK ) );
+
+ if ( status == 0 )
+ return NFCTAG_OK;
+ else
+ return NFCTAG_ERROR;
+}
+
+/**
+ * @brief Read M24LR UID
+ * @param UID : M24LR_UID pointer of the UID to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadUID( M24LR_UID * const pUid )
+{
+ uint8_t areg_value[8];
+ uint8_t i;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of UID registers */
+ status = i2c_ReadRegister( areg_value, M24LR_UID_REG, 8 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Store information in 2 WORD */
+ pUid->MSB_UID = 0;
+
+ for( i = 0; i < 4; i++ )
+ {
+ pUid->MSB_UID = (pUid->MSB_UID << 8) | areg_value[7 - i];
+ }
+
+ pUid->LSB_UID = 0;
+
+ for( i = 0; i < 4; i++ )
+ {
+ pUid->LSB_UID = (pUid->LSB_UID << 8) | areg_value[3 - i];
+ }
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Read DSFID
+ * @param pData : pointer of the DSFID to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadDSFID( uint8_t * const pDsfid )
+{
+ /* Read actual value of DSFID register */
+ return i2c_ReadRegister( pDsfid, M24LR_DSFID_REG, 1 );
+}
+
+/**
+ * @brief Read AFI
+ * @param pData : pointer of the AFI to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadAFI( uint8_t * const pAfi )
+{
+ /* Read actual value of AFI register */
+ return i2c_ReadRegister( pAfi, M24LR_AFI_REG, 1 );
+}
+
+/**
+ * @brief Read status of I2C Lock Sectors
+ * @param Lock_sector : M24LR_Lock_Sectors pointer of the I2c lock sector status to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadI2CLockSector( M24LR_Lock_Sectors * const pLock_sector )
+{
+ uint8_t areg_value[8];
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of I2c Write Lock registers */
+ status = i2c_ReadRegister( areg_value, M24LR_LOCK_REG, 8 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Dispatch information to corresponding struct member */
+ pLock_sector->sectors_63_56 = areg_value[7];
+ pLock_sector->sectors_55_48 = areg_value[6];
+ pLock_sector->sectors_47_40 = areg_value[5];
+ pLock_sector->sectors_39_32 = areg_value[4];
+ pLock_sector->sectors_31_24 = areg_value[3];
+ pLock_sector->sectors_23_16 = areg_value[2];
+ pLock_sector->sectors_15_8 = areg_value[1];
+ pLock_sector->sectors_7_0 = areg_value[0];
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Lock I2C write on an EEPROM Sectors
+ * @brief Need a presentation of I2C Password to be effective
+ * @param Sector : EEPROM Sector number to lock (between 0 to 63)
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_I2CLockSector( const uint8_t Sector )
+{
+ NFCTAG_StatusTypeDef status;
+ uint8_t reg_value = 0;
+ uint16_t sector_write_lock_addr;
+
+ /* Compute register address */
+ sector_write_lock_addr = M24LR_LOCK_REG | (Sector >> 3);
+
+ /* Read actual WriteLockStatus */
+ status = i2c_ReadRegister( ®_value, sector_write_lock_addr, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Compute and update new WriteLockStatus */
+ reg_value |= 1 << ( Sector % 8 );
+
+ /* Write WriteLock register */
+ return i2c_WriteRegister( ®_value, sector_write_lock_addr, 1 );
+}
+
+/**
+ * @brief UnLock I2C write on a EEPROM Sector
+ * @brief Need an presentation of I2C Password to be effective
+ * @param pSector : EEPROM Sector number to unlock (between 0 to 63)
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_I2CUnlockSector( const uint8_t Sector )
+{
+ NFCTAG_StatusTypeDef status;
+ uint8_t reg_value = 0;
+ uint16_t sector_write_lock_addr;
+
+ /* Compute register address */
+ sector_write_lock_addr = M24LR_LOCK_REG | (Sector >> 3);
+
+ /* Read actual WriteLockStatus */
+ status = i2c_ReadRegister( ®_value, sector_write_lock_addr, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Compute and update new WriteLockStatus */
+ reg_value &= ~( 1 << ( Sector % 8 ) );
+
+ /* Write WriteLock register */
+ return i2c_WriteRegister( ®_value, sector_write_lock_addr, 1 );
+}
+
+/**
+ * @brief Present I2C password, authorize I2C write
+ * @param PassWord : Password value on 32bits
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_PresentI2CPassword( const uint32_t PassWord )
+{
+ uint8_t ai2c_message[9] = {0};
+ uint8_t i;
+
+ /* Build I2C Message with Password + Validation code 0x09 + Password */
+ ai2c_message[4] = 0x09;
+ i = 0;
+ while( i < 4 )
+ {
+ ai2c_message[i] = ( PassWord >> (i * 8) ) & 0xFF;
+ ai2c_message[i + 5] = ( PassWord >> (i * 8) ) & 0xFF;
+ i++;
+ };
+
+ /* Present password to M24LR */
+ return i2c_WriteRegister( ai2c_message, M24LR_I2C_PWD_REG, 9 );
+}
+
+/**
+ * @brief Write new I2C password
+ * @brief Need to present good I2CPassword before using this function
+ * @param PassWord : new I2C PassWord value on 32bits
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_WriteI2CPassword( const uint32_t PassWord )
+{
+ uint8_t ai2c_message[9] = {0};
+ uint8_t i;
+
+ /* Build I2C Message with Password + Validation code 0x07 + Password */
+ ai2c_message[4] = 0x07;
+ i = 0;
+ while( i < 4 )
+ {
+ ai2c_message[i] = ( PassWord >> (i * 8) ) & 0xFF;
+ ai2c_message[i + 5] = ( PassWord >> (i * 8) ) & 0xFF;
+ i++;
+ };
+
+ /* Write Password to register */
+ return i2c_WriteRegister( ai2c_message, M24LR_I2C_PWD_REG, 9 );
+}
+
+/**
+ * @brief Read SectorSecurityStatus (defining RF access allowed)
+ * @param SectorNb : Sector number to get RF security status
+ * @param pData : M24LR_SECTOR_SEC pointer of the data to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadSSSx( const uint8_t SectorNb, M24LR_SECTOR_SEC * const pData )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+ uint16_t sector_security_addr;
+
+ /* Compute Sector Security register address */
+ sector_security_addr = M24LR_SSS_REG | SectorNb;
+
+ /* Read actual value of SectorSecurityStatus register */
+ status = i2c_ReadRegister( ®_value, sector_security_addr, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Extract Sector Security Status configuration */
+ pData->SectorLock = reg_value & M24LR_SSS_LOCK_MASK;
+ pData->RW_Protection = (reg_value & M24LR_SSS_RW_MASK) >> 1;
+ pData->PassCtrl = (reg_value & M24LR_SSS_PASSCTRL_MASK) >> 3;
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Write SectorSecurityStatus (defining RF access allowed)
+ * @brief Need an presentation of I2C Password to be effective
+ * @param SectorNb : Sector number to set RF security
+ * @param pData : M24LR_SECTOR_SEC pointer of the data to write
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_WriteSSSx( const uint8_t SectorNb, const M24LR_SECTOR_SEC * const pData )
+{
+ uint8_t reg_value;
+ uint16_t sector_security_addr;
+
+ /* Compute Sector Security register address */
+ sector_security_addr = M24LR_SSS_REG | SectorNb;
+
+ /* Update Sector Security Status */
+ reg_value = (pData->PassCtrl << 3) & M24LR_SSS_PASSCTRL_MASK;
+ reg_value |= ((pData->RW_Protection << 1) & M24LR_SSS_RW_MASK);
+ reg_value |= (pData->SectorLock & M24LR_SSS_LOCK_MASK);
+
+ /* Write SectorSecurityStatus register */
+ return i2c_WriteRegister( ®_value, sector_security_addr, 1 );
+}
+
+/**
+ * @brief Read Memory Size info
+ * @param SizeInfo : M24LR_Mem_Size pointer of the data to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadMemSize( M24LR_Mem_Size * const pSizeInfo )
+{
+ uint8_t areg_value[3];
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of Mem_Size register */
+ status = i2c_ReadRegister( areg_value, M24LR_MEMSIZE_REG, 3 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Extract Mem information */
+ pSizeInfo->BlockSize = areg_value[2];
+ pSizeInfo->Mem_Size = areg_value[1];
+ pSizeInfo->Mem_Size = (pSizeInfo->Mem_Size << 8) | areg_value[0];
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Get GPO Configuration status
+ * @param Rf_Wip_Busy : M24LR_GPO_STATUS pointer of the data to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_GetRF_WIP_BUSY( M24LR_GPO_STATUS * const pRf_Wip_Busy )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CFG register */
+ status = i2c_ReadRegister( ®_value, M24LR_CFG_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Extract RF WIP/BUSY information */
+ if( (reg_value & M24LR_CFG_WIPBUSY_MASK) == M24LR_CFG_WIPBUSY_MASK )
+ {
+ *pRf_Wip_Busy = M24LR_GPO_WIP;
+ }
+ else
+ {
+ *pRf_Wip_Busy = M24LR_GPO_BUSY;
+ }
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Configure GPO as RF Busy
+ * @param None
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_SetRFBUSY( void )
+{
+ uint8_t reg_value;
+ int status;
+
+ /* Read actual value of CFG register */
+ status = dev_I2C.i2c_read( ®_value, i2c_address_syst, (uint16_t)M24LR_CFG_REG, 1 );
+ if( status != 0 )
+ {
+ return NFCTAG_TIMEOUT;
+ }
+
+ /* Update register value for BUSY configuration */
+ reg_value &= !M24LR_CFG_WIPBUSY_MASK;
+
+ /* Write CFG register */
+ status = i2c_WriteRegister( ®_value, M24LR_CFG_REG, 1 );
+ if ( status == 0 )
+ return NFCTAG_OK;
+ else
+ return NFCTAG_TIMEOUT;
+}
+
+
+/**
+ * @brief Get Energy harvesting mode status
+ * @param EH_mode : M24LR_EH_MODE_STATUS pointer of the data to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_Read_EH_mode( M24LR_EH_MODE_STATUS * const pEH_mode )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CFG register */
+ status = i2c_ReadRegister( ®_value, M24LR_CFG_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Extract EH_mode configuration */
+ if( (reg_value & M24LR_CFG_EHMODE_MASK) == M24LR_CFG_EHMODE_MASK )
+ {
+ *pEH_mode = M24LR_EH_MODE_DISABLE;
+ }
+ else
+ {
+ *pEH_mode = M24LR_EH_MODE_ENABLE;
+ }
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Enable Energy harvesting mode
+ * @param None
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_Enable_EH_mode( void )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CFG register */
+ status = i2c_ReadRegister( ®_value, M24LR_CFG_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Update EH_mode */
+ reg_value &= ~M24LR_CFG_EHMODE_MASK;
+
+ /* Write CFG register */
+ return i2c_WriteRegister( ®_value, M24LR_CFG_REG, 1 );
+}
+
+/**
+ * @brief Disable Energy harvesting mode
+ * @param None
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_Disable_EH_mode( void )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CFG register */
+ status = i2c_ReadRegister( ®_value, M24LR_CFG_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Update EH_mode */
+ reg_value |= M24LR_CFG_EHMODE_MASK;
+
+ /* Write CFG register */
+ return i2c_WriteRegister( ®_value, M24LR_CFG_REG, 1 );
+}
+
+/**
+ * @brief Read Vout sink current configuration status for Energy Harvesting
+ * @param EH_Cfg : M24LR_EH_CFG_VOUT pointer of the data to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ReadEH_Cfg( M24LR_EH_CFG_VOUT * const pEH_Cfg )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CFG register */
+ status = i2c_ReadRegister( ®_value, M24LR_CFG_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Extract Vout configuration for EH information */
+ reg_value &= (M24LR_CFG_EHCFG1_MASK | M24LR_CFG_EHCFG0_MASK);
+ switch( reg_value )
+ {
+ case 0:
+ *pEH_Cfg = M24LR_EH_Cfg_6MA;
+ break;
+ case 1:
+ *pEH_Cfg = M24LR_EH_Cfg_3MA;
+ break;
+ case 2:
+ *pEH_Cfg = M24LR_EH_Cfg_1MA;
+ break;
+ case 3:
+ *pEH_Cfg = M24LR_EH_Cfg_300UA;
+ break;
+
+ default:
+ *pEH_Cfg = M24LR_EH_Cfg_6MA;
+ }
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Write Vout sink current configuration status for Energy Harvesting
+ * @param EH_Cfg : M24LR_EH_CFG_VOUT value to configure Vout
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_WriteEH_Cfg( const M24LR_EH_CFG_VOUT EH_Cfg )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CFG register */
+ status = i2c_ReadRegister( ®_value, M24LR_CFG_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Update Vout configuration */
+ reg_value &= ~(M24LR_CFG_EHCFG1_MASK | M24LR_CFG_EHCFG0_MASK);
+ reg_value |= EH_Cfg;
+
+ /* Write CFG register */
+ return i2c_WriteRegister( ®_value, M24LR_CFG_REG, 1 );
+}
+
+/**
+ * @brief Get Energy Harvesting status
+ * @param EH_Val : M24LR_EH_STATUS pointer of the data to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_GetEH( M24LR_EH_STATUS * const pEH_Val )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CTRL register */
+ status = i2c_ReadRegister( ®_value, M24LR_CTRL_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Extract EH information */
+ if( (reg_value & M24LR_CTRL_EHEN_MASK) == M24LR_CTRL_EHEN_MASK )
+ {
+ *pEH_Val = M24LR_EH_ENABLE;
+ }
+ else
+ {
+ *pEH_Val = M24LR_EH_DISABLE;
+ }
+
+ return NFCTAG_OK;
+}
+
+/**
+ * @brief Enable Energy Harvesting
+ * @param None
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_SetEH( void )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CTRL register */
+ status = i2c_ReadRegister( ®_value, M24LR_CTRL_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Update EH configuration */
+ reg_value |= M24LR_CTRL_EHEN_MASK;
+
+ /* Write CTRL Register */
+ return i2c_WriteRegister( ®_value, M24LR_CTRL_REG, 1 );
+}
+
+/**
+ * @brief Disable Energy Harvesting
+ * @param None
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_ResetEH( void )
+{
+ uint8_t reg_value;
+ NFCTAG_StatusTypeDef status;
+
+ /* Read actual value of CTRL register */
+ status = i2c_ReadRegister( ®_value, M24LR_CTRL_REG, 1 );
+ if( status != NFCTAG_OK )
+ {
+ return status;
+ }
+
+ /* Update EH configuration */
+ reg_value &= ~M24LR_CTRL_EHEN_MASK;
+
+ /* Write CTRL register */
+ return i2c_WriteRegister( ®_value, M24LR_CTRL_REG, 1 );
+}
+
+/**
+ * @brief Check if RF Field is present in front of M24LR
+ * @param pRF_Field : M24LR_FIELD_STATUS pointer of the data to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_GetRFField( M24LR_FIELD_STATUS * const pRF_Field )
+{
+ NFCTAG_StatusTypeDef status;
+ uint8_t reg_value = 0;
+
+ /* Read actual value of CTRL register */
+ status = i2c_ReadRegister( ®_value, M24LR_CTRL_REG, 1 );
+
+ /* Extract RF Field information */
+ if( status == NFCTAG_OK )
+ {
+ if( (reg_value & M24LR_CTRL_FIELD_MASK) == M24LR_CTRL_FIELD_MASK )
+ {
+ *pRF_Field = M24LR_FIELD_ON;
+ }
+ else
+ {
+ *pRF_Field = M24LR_FIELD_OFF;
+ }
+
+ return NFCTAG_OK;
+ }
+
+ return status;
+}
+
+/**
+ * @brief Check if Write Timing is good
+ * @param pT_Prog : M24LR_T_PROG_STATUS pointer of the data to store
+ * @retval NFCTAG enum status
+ */
+NFCTAG_StatusTypeDef M24LR::i2c_GetTProg( M24LR_T_PROG_STATUS * const pT_Prog )
+{
+ NFCTAG_StatusTypeDef status;
+ uint8_t reg_value = 0;
+
+ /* Read actual value of CTRL register */
+ status = i2c_ReadRegister( ®_value, M24LR_CTRL_REG, 1 );
+
+ /* Extract T-Prog information */
+ if( status == NFCTAG_OK )
+ {
+ if( (reg_value & M24LR_CTRL_TPROG_MASK) == M24LR_CTRL_TPROG_MASK )
+ {
+ *pT_Prog = M24LR_T_PROG_OK;
+ }
+ else
+ {
+ *pT_Prog = M24LR_T_PROG_NO;
+ }
+
+ return NFCTAG_OK;
+ }
+ else
+ return status;
+}
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/m24lr/m24lr.h Wed Sep 28 11:30:09 2016 +0000
@@ -0,0 +1,294 @@
+/**
+ ******************************************************************************
+ * @file m24lr.h
+ * @author AMG Central Lab
+ * @version V1.0.0
+ * @date 30 Aug 2016
+ * @brief header file for M24LR driver .
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
+ *
+ * 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 __M24LR_H
+#define __M24LR_H
+
+#include "common.h"
+#include "DevI2C.h"
+#include "Nfc.h"
+#include "NDefNfcTag.h"
+#include "NDefNfcTagM24LR.h"
+
+//#include "lib_NDEF_URI.h"
+ /* Exported constants --------------------------------------------------------*/
+/** @defgroup M24LR_Exported_Constants
+ * @{
+ */
+#define I_AM_M24LR04 0x5A
+#define I_AM_M24LR16 0x4E
+#define I_AM_M24LR64 0x5E
+
+#ifndef NULL
+#define NULL (void *) 0
+#endif
+
+#define M24LR_PAGEWRITE_NBBYTE 4
+
+#define M24LR_ADDR_DATA_I2C 0xA6
+#define M24LR_ADDR_SYST_I2C 0xAE
+#define M24LR_I2C_TIMEOUT 200 /* I2C Time out (ms), this is the maximum time needed by M24LR to complete any command */
+
+#define M24LR_IT_BUSY_MASK 0x01
+#define M24LR_IT_WIP_MASK 0x02
+
+/* Registers address */
+#define M24LR_SSS_REG 0x0000
+#define M24LR_LOCK_REG 0x0800
+#define M24LR_I2C_PWD_REG 0x0900
+#define M24LR_CFG_REG 0x0910
+#define M24LR_AFI_REG 0x0912
+#define M24LR_DSFID_REG 0x0913
+#define M24LR_UID_REG 0x0914
+#define M24LR_ICREF_REG 0x091C
+#define M24LR_MEMSIZE_REG 0x091D
+#define M24LR_CTRL_REG 0x0920
+
+/* Registers mask */
+#define M24LR_SSS_LOCK_MASK 0x01
+#define M24LR_SSS_RW_MASK 0x06
+#define M24LR_SSS_PASSCTRL_MASK 0x18
+#define M24LR_LOCK_MASK 0x0F
+
+#define M24LR_CFG_EHCFG0_MASK 0x01
+#define M24LR_CFG_EHCFG1_MASK 0x02
+#define M24LR_CFG_EHMODE_MASK 0x04
+#define M24LR_CFG_WIPBUSY_MASK 0x08
+
+#define M24LR_CTRL_EHEN_MASK 0x01
+#define M24LR_CTRL_FIELD_MASK 0x02
+#define M24LR_CTRL_TPROG_MASK 0x80
+
+
+
+/**
+ * @brief M24LR VOUT Configuration enumerator definition
+ */
+typedef enum
+{
+ M24LR_EH_Cfg_6MA = 0,
+ M24LR_EH_Cfg_3MA,
+ M24LR_EH_Cfg_1MA,
+ M24LR_EH_Cfg_300UA
+} M24LR_EH_CFG_VOUT;
+
+/**
+ * @brief M24LR FIELD status enumerator definition
+ */
+typedef enum
+{
+ M24LR_FIELD_OFF = 0,
+ M24LR_FIELD_ON
+} M24LR_FIELD_STATUS;
+
+/**
+ * @brief M24LR TT-PROG status enumerator definition
+ */
+typedef enum
+{
+ M24LR_T_PROG_NO = 0,
+ M24LR_T_PROG_OK
+} M24LR_T_PROG_STATUS;
+
+/**
+ * @brief M24LR Energy Harvesting status enumerator definition
+ */
+typedef enum
+{
+ M24LR_EH_DISABLE = 0,
+ M24LR_EH_ENABLE
+} M24LR_EH_STATUS;
+
+/**
+ * @brief M24LR Energy Harvesting mode enumerator definition
+ */
+typedef enum
+{
+ M24LR_EH_MODE_ENABLE = 0,
+ M24LR_EH_MODE_DISABLE
+} M24LR_EH_MODE_STATUS;
+
+/**
+ * @brief M24LR GPO status enumerator definition
+ */
+typedef enum
+{
+ M24LR_GPO_BUSY = 0,
+ M24LR_GPO_WIP
+} M24LR_GPO_STATUS;
+
+/**
+ * @brief M24LR Memory information structure definition
+ */
+typedef struct
+{
+ uint8_t BlockSize;
+ uint16_t Mem_Size;
+} M24LR_Mem_Size;
+
+/**
+ * @brief M24LR I2C Write Lock register structure definition
+ */
+typedef struct
+{
+ uint8_t sectors_7_0;
+ uint8_t sectors_15_8;
+ uint8_t sectors_23_16;
+ uint8_t sectors_31_24;
+ uint8_t sectors_39_32;
+ uint8_t sectors_47_40;
+ uint8_t sectors_55_48;
+ uint8_t sectors_63_56;
+} M24LR_Lock_Sectors;
+
+/**
+ * @brief M24LR UID information structure definition
+ */
+typedef struct
+{
+ uint32_t MSB_UID;
+ uint32_t LSB_UID;
+} M24LR_UID;
+
+/**
+ * @brief M24LR Sector Security register structure definition
+ */
+typedef struct
+{
+ uint8_t SectorLock;
+ uint8_t RW_Protection;
+ uint8_t PassCtrl;
+} M24LR_SECTOR_SEC;
+
+
+ /**
+ * @}
+ */
+
+class M24LR : public Nfc {
+public:
+ NFCTAG_StatusTypeDef i2c_Init( void );
+ NFCTAG_StatusTypeDef i2c_ReadID( uint8_t * const pICRef );
+ NFCTAG_StatusTypeDef i2c_IsDeviceReady( const uint32_t Trials );
+ NFCTAG_StatusTypeDef i2c_ConfigureGPO( const uint16_t ITConf );
+ NFCTAG_StatusTypeDef i2c_GetGPOStatus( uint16_t * const pGPOStatus );
+ NFCTAG_StatusTypeDef i2c_ReadData( uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte );
+ NFCTAG_StatusTypeDef i2c_WriteData( const uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte );
+ NFCTAG_StatusTypeDef i2c_ReadRegister( uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte );
+ NFCTAG_StatusTypeDef i2c_WriteRegister( const uint8_t * const pData, const uint16_t TarAddr, const uint16_t NbByte );
+
+
+ /* Extended Functions */
+ NFCTAG_StatusTypeDef i2c_ReadUID( M24LR_UID * const pUid );
+ NFCTAG_StatusTypeDef i2c_ReadDSFID( uint8_t * const pDsfid );
+ NFCTAG_StatusTypeDef i2c_ReadAFI( uint8_t * const pAfi );
+ NFCTAG_StatusTypeDef i2c_ReadI2CLockSector( M24LR_Lock_Sectors * const pLock_sector );
+ NFCTAG_StatusTypeDef i2c_I2CLockSector( const uint8_t Sector );
+ NFCTAG_StatusTypeDef i2c_I2CUnlockSector( const uint8_t Sector );
+ NFCTAG_StatusTypeDef i2c_PresentI2CPassword( const uint32_t PassWord );
+ NFCTAG_StatusTypeDef i2c_WriteI2CPassword( const uint32_t PassWord );
+ NFCTAG_StatusTypeDef i2c_ReadSSSx( const uint8_t SectorNb, M24LR_SECTOR_SEC * const pData );
+ NFCTAG_StatusTypeDef i2c_WriteSSSx( const uint8_t SectorNb, const M24LR_SECTOR_SEC * const pData );
+ NFCTAG_StatusTypeDef i2c_ReadMemSize( M24LR_Mem_Size * const pSizeInfo );
+ NFCTAG_StatusTypeDef i2c_GetRF_WIP_BUSY( M24LR_GPO_STATUS * const pRf_Wip_Busy );
+ NFCTAG_StatusTypeDef i2c_SetRFBUSY( void );
+ NFCTAG_StatusTypeDef i2c_SetRFWIP( void );
+ NFCTAG_StatusTypeDef i2c_Read_EH_mode( M24LR_EH_MODE_STATUS * const pEH_mode );
+ NFCTAG_StatusTypeDef i2c_Enable_EH_mode( void );
+ NFCTAG_StatusTypeDef i2c_Disable_EH_mode( void );
+ NFCTAG_StatusTypeDef i2c_ReadEH_Cfg( M24LR_EH_CFG_VOUT * const pEH_Cfg );
+ NFCTAG_StatusTypeDef i2c_WriteEH_Cfg( const M24LR_EH_CFG_VOUT EH_Cfg );
+ NFCTAG_StatusTypeDef i2c_GetEH( M24LR_EH_STATUS * const pEH_Val );
+ NFCTAG_StatusTypeDef i2c_SetEH( void );
+ NFCTAG_StatusTypeDef i2c_ResetEH( void );
+ NFCTAG_StatusTypeDef i2c_GetRFField( M24LR_FIELD_STATUS * const pRF_Field );
+ NFCTAG_StatusTypeDef i2c_GetTProg( M24LR_T_PROG_STATUS * const pT_Prog );
+
+ void Enable_EnergyHarvesting( void );
+ NFCTAG_StatusTypeDef Initialization( void );
+
+ virtual int ReadBinary(uint16_t Offset, uint8_t NbByteToRead,
+ uint8_t *pBufferRead) {
+ return (NFC_StatusTypeDef) i2c_ReadData( pBufferRead, Offset, NbByteToRead );
+
+ }
+
+ virtual int UpdateBinary(uint16_t Offset,
+ uint8_t NbByteToWrite,uint8_t *pDataToWrite) {
+ return (NFC_StatusTypeDef) i2c_WriteData( pDataToWrite, Offset, NbByteToWrite );
+ }
+
+ M24LR(uint8_t const address, uint8_t const addressData, DevI2C &devI2C ):
+ NDefTagUtil(*this), i2c_address_syst(address), i2c_address_data(addressData), dev_I2C(devI2C) {}
+
+
+ NDefLib::NDefNfcTag& getNDefTag(){
+ return NDefTagUtil;
+ }
+
+
+ virtual int Init(void *ptr) {
+ (void)ptr;
+ return (NFC_StatusTypeDef) i2c_Init();
+ }
+
+ virtual int ReadID(uint8_t *id) {
+ return (NFC_StatusTypeDef) i2c_ReadID(id);
+ }
+ private:
+ static uint8_t NfctagInitialized;
+
+
+ /*
+ * Object implementing the interface to use the NDefLib.
+ */
+ NDefNfcTagM24LR NDefTagUtil;
+
+ uint8_t i2c_address_syst;
+ uint8_t i2c_address_data;
+ DevI2C dev_I2C;
+};
+
+
+
+
+#endif /* __M24LR_H */
+
+
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

X-NUCLEO-NFC02A1 Dynamic NFC tag