mbed OS 5 example application using X-NUCLEO-NFC02A1

Dependencies:   NDefLib X_NUCLEO_NFC02A1

Fork of HelloWorld_NFC02A1 by ST

X_NUCLEO_NFC02A1/m24lr/m24lr.h

Committer:
rosarium
Date:
2016-08-30
Revision:
1:11ae12d41082
Parent:
0:892175366555
Child:
2:6f1b1f7f8d12

File content as of revision 1:11ae12d41082:

/**
  ******************************************************************************
  * @file       m24lr.h
  * @author     AMG Central Lab
  * @version    V1.0.0
  * @date       30 Aug 2016
  * @brief      header file for M24LR driver .
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; 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_class.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 NFC_StatusTypeDef ReadBinary(uint16_t Offset, uint8_t NbByteToRead,
            uint8_t *pBufferRead) {
           return  (NFC_StatusTypeDef) i2c_ReadData( pBufferRead, Offset, NbByteToRead );
        
    }

   virtual NFC_StatusTypeDef 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) {}
    
   static uint8_t NfctagInitialized;
   
   friend class NDEF_URI;
   
    /*
     * Object implementing the interface to use the NDefLib.
     */
   NDefNfcTagM24LR NDefTagUtil;
   
   uint8_t i2c_address_syst;
   uint8_t i2c_address_data;
   DevI2C dev_I2C;
   
   NDefLib::NDefNfcTag& getNDefTag(){
      return NDefTagUtil;
  }
  
  // Not Required from Nfc Class
  /**
     * Open a I2C connection with the tag if an RF connection isn't already open.
     * @return operation status
     */
  virtual NFC_StatusTypeDef GetSession(void){
    return(NFC_SUCCESS);
  }
    
    /**
     * Force to open an I2C connection , abort the RF connection.
     * @return NFC_SUCCESS if the session is acquired
     */
  virtual NFC_StatusTypeDef ForceGetSession(void){
     return(NFC_SUCCESS);
  }

    /**
     * Close an I2C connection.
     * @return NFC_SUCCESS if the session is release
     */
  virtual NFC_StatusTypeDef Deselect(void){
     return(NFC_SUCCESS);
  }

    /**
     * Select the application file.
     * @return NFC_SUCCESS if the application is selected
     */
   virtual NFC_StatusTypeDef SelectApplication(void){
      return(NFC_SUCCESS);
   }

    /**
     * Select the CC file.
     * @return NFC_SUCCESS if the CC file is selected.
     */
   virtual NFC_StatusTypeDef SelectCCfile(void){
      return(NFC_SUCCESS);
   }

    /**
     * Select the NDEF file.
     * @param NDEFfileId File id to open.
     * @return NFC_SUCCESS if the file is selected
     */
   virtual NFC_StatusTypeDef SelectNDEFfile(uint16_t NDEFfileId){
     (void)NDEFfileId;
      return(NFC_SUCCESS);
   }

    /**
     * Select the system file.
     * @return NFC_SUCCESS if the system file is selected
     */
   virtual NFC_StatusTypeDef SelectSystemfile(void){
      return(NFC_SUCCESS);
        }





    /**
     * Check that the password is correct.
     * @param uPwdId Password type.
     * @param NbPwdByte Password size ( must be 0x10).
     * @param pPwd Password buffer.
     * @return NFC_SUCCESS if no errors and write/read permission granted
     */
   virtual NFC_StatusTypeDef Verify(PasswordType_t uPwdId, uint8_t NbPwdByte,const uint8_t *pPwd){
     (void)uPwdId; (void)NbPwdByte; (void)pPwd;
      return(NFC_SUCCESS);
   }

    /**
     * Replace the read or write password.
     * @param uPwdId Password to change.
     * @param pPwd New password.
     * @return NFC_SUCCESS if no errors
     */
   virtual NFC_StatusTypeDef ChangeReferenceData(PasswordType_t uPwdId,const uint8_t *pPwd){
     (void)uPwdId;  (void)pPwd;
      return(NFC_SUCCESS);
   }
    
    /**
     *  Activate NDEF file password protection.
     *  When this command is successful, read or write access to the NDEF file is protected by a 128-bit password.
     *  @param uReadOrWrite Read or write password.
     *  @return NFC_SUCCESS if no errors
     */
   virtual NFC_StatusTypeDef EnableVerificationRequirement(PasswordType_t uReadOrWrite){
     (void)uReadOrWrite;
      return(NFC_SUCCESS);
   }
    
    /**
     * Deactivate NDEF file password protection.
     * When this command is successful, read or write access to the NDEF file is granted.
     * with no security enforcement.
     * @param uReadOrWrite Read or write password.
     * @return NFC_SUCCESS if no errors
     */     
   virtual NFC_StatusTypeDef DisableVerificationRequirement(PasswordType_t uReadOrWrite){
     (void)uReadOrWrite;
      return(NFC_SUCCESS);
   }
    
    /**
     * Same as {@link NFC#ReadBinary}, however permitting to read more bytes than available.
     * @param Offset read offset.
     * @param NbByteToRead Number of bytes to read.
     * @param[out] pBufferRead Buffer to store the read data into.
     * @return NFC_SUCCESS if no errors
     */
   virtual NFC_StatusTypeDef STReadBinary(uint16_t Offset, uint8_t NbByteToRead, uint8_t *pBufferRead){
     (void)Offset; (void)NbByteToRead; (void)pBufferRead;
      return(NFC_SUCCESS);
   }
    
    /**
     * Set the tag as read/write only, stored in the eeprom.
     * @param  uReadOrWrite ReadPwd if write only, WritePwd if read only.
     * @return NFC_SUCCESS if no errors
     */
   virtual NFC_StatusTypeDef EnablePermanentState(PasswordType_t uReadOrWrite){
     (void)uReadOrWrite;
      return(NFC_SUCCESS);
   }
    
    /**
     * Unset the tag as read/write only, stored in the eeprom.
     * @param  uReadOrWrite ReadPwd if write only, WritePwd if read only.
     * @return NFC_SUCCESS if no errors
     * @par Caller must have I2Csuper user permissions to call this function.
     */
   virtual NFC_StatusTypeDef DisablePermanentState(PasswordType_t uReadOrWrite){
     (void)uReadOrWrite;
      return(NFC_SUCCESS);
   }
    
    /**
     * Set the gpo output pin.
     * @param uSetOrReset New pin status.
     * @return NFC_SUCCESS if no errors
     */
   virtual NFC_StatusTypeDef StateControl(uint8_t uSetOrReset){
     (void)uSetOrReset;
      return(NFC_SUCCESS);
   }
    
    /**
     * @brief  This function configures GPO for I2C session.
     * @param  GPO_I2Cconfig GPO configuration to set.
     * @return NFC_SUCCESS if no errors
     * @par if the configuration is I2C_ANSWER_READY, the component will start to work
     * in async mode.
     */
   virtual NFC_StatusTypeDef ManageI2CGPO(NFC_GPO_MGMT GPO_I2Cconfig){
     (void)GPO_I2Cconfig;
      return(NFC_SUCCESS);
   }
    
    /**
    * @brief  This function configures GPO for RF session.
    * @param  GPO_RFconfig GPO configuration to set.
    * @return NFC_SUCCESS if no errors
    */
   virtual NFC_StatusTypeDef ManageRFGPO(uint8_t GPO_RFconfig){
     (void)GPO_RFconfig;
      return(NFC_SUCCESS);
   }
    
    /**
     * @brief  This function enables or disables the RF communication.
     * @param  OnOffChoice GPO configuration to set.
     * @return NFC_SUCCESS if no errors
     */
   virtual NFC_StatusTypeDef RFConfig(uint8_t OnOffChoice){
     (void)OnOffChoice;
      return(NFC_SUCCESS);
   }

    /**
     * Generates a negative pulse on the GPO pin.
     * Pulse starts immediately after the command is issued and ends at the end of the RF response.
     * @return NFC_SUCCESS if no errors
     */
    virtual NFC_StatusTypeDef SendInterrupt(void){
           return(NFC_SUCCESS);
        }

   virtual NFC_StatusTypeDef ManageEvent(){
      return(NFC_SUCCESS);
   }
   
   virtual int Init(void *ptr) {
                (void)ptr;
        return (NFC_StatusTypeDef) i2c_Init();
                 //return(NFC_SUCCESS);
    }

  virtual int ReadID(uint8_t *id) {
        return (NFC_StatusTypeDef) i2c_ReadID(id);
    // return(NFC_SUCCESS);
    }
  
};
  
  

  
#endif /* __M24LR_H */