ST Expansion SW Team / eeprom_driver

Dependents:   mbed-os-example-nfc-EEPROM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers st25dv_driver.h Source File

st25dv_driver.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    st25dv_driver.h
00004  * @author  ST Central Labs
00005  * @brief   This file provides a set of functions to interface with the ST25DV
00006  *          device.
00007  ******************************************************************************
00008  * @attention
00009  *
00010  * <h2><center>&copy; COPYRIGHT(c) 2020 STMicroelectronics</center></h2>
00011  *
00012  * Redistribution and use in source and binary forms, with or without modification,
00013  * are permitted provided that the following conditions are met:
00014  *   1. Redistributions of source code must retain the above copyright notice,
00015  *      this list of conditions and the following disclaimer.
00016  *   2. Redistributions in binary form must reproduce the above copyright notice,
00017  *      this list of conditions and the following disclaimer in the documentation
00018  *      and/or other materials provided with the distribution.
00019  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00020  *      may be used to endorse or promote products derived from this software
00021  *      without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00026  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00027  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00028  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00029  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00031  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  ******************************************************************************
00035  */
00036 
00037 #ifndef ST25DV_H
00038 #define ST25DV_H
00039 
00040 #include <stdint.h>
00041 #include <mbed.h>
00042 #include "I2C.h"
00043 #include "NFCEEPROMDriver.h"
00044 #include "EventQueue.h"
00045 #include "st25dv.h"
00046 
00047 #if defined MBED_CONF_X_NUCLEO_NFC04A1
00048 
00049 #define ST25DV_I2C_SDA_PIN     D14
00050 #define ST25DV_I2C_SCL_PIN     D15
00051 #define ST25DV_LPD_PIN         D7
00052 #define ST25DV_GPO_PIN         D12
00053 #define ST25DV_LED1_PIN        D5
00054 #define ST25DV_LED2_PIN        D4
00055 #define ST25DV_LED3_PIN        D2
00056 
00057 #else
00058 
00059 #define ST25DV_I2C_SDA_PIN     NC
00060 #define ST25DV_I2C_SCL_PIN     NC
00061 #define ST25DV_LPD_PIN         NC
00062 #define ST25DV_GPO_PIN         NC
00063 #define ST25DV_LED1_PIN        NC
00064 #define ST25DV_LED2_PIN        NC
00065 #define ST25DV_LED3_PIN        NC
00066 
00067 #endif
00068 
00069 namespace mbed {
00070 namespace nfc {
00071 namespace vendor {
00072 namespace ST {
00073 
00074 #ifndef MIN
00075 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
00076 #endif
00077 
00078 #define NFCTAG_4K_SIZE            ((uint32_t) 0x200)
00079 #define NFCTAG_16K_SIZE           ((uint32_t) 0x800)
00080 #define NFCTAG_64K_SIZE           ((uint32_t) 0x2000)
00081 
00082 #define MAX_NDEF_MEM                 0x200
00083 #define ST25DV_MAX_SIZE              NFCTAG_4K_SIZE
00084 #define ST25DV_NDEF_MAX_SIZE         MIN(ST25DV_MAX_SIZE,MAX_NDEF_MEM)
00085 #define NFC_DEVICE_MAX_NDEFMEMORY    ST25DV_NDEF_MAX_SIZE
00086 
00087 /* Error codes for Higher level */
00088 #define NDEF_OK                     0
00089 #define NDEF_ERROR                  1
00090 #define NDEF_ERROR_MEMORY_TAG       2
00091 #define NDEF_ERROR_MEMORY_INTERNAL  3
00092 #define NDEF_ERROR_LOCKED           4
00093 #define NDEF_ERROR_NOT_FORMATTED    5
00094 
00095 #define NDEF_MAX_SIZE               NFC_DEVICE_MAX_NDEFMEMORY
00096 #define NDEF_RECORD_MAX_SIZE        NFC_DEVICE_MAX_NDEFMEMORY
00097 
00098 /** @brief Memory size value indicating that this is a 8-bytes Capability Container */
00099 #define NFCT5_EXTENDED_CCFILE             0x00
00100 /** @brief Capability container version 1.0 */
00101 #define NFCT5_VERSION_V1_0                0x40
00102 /** @brief Read access condition mask for the Capability Container byte1 */
00103 #define NFCT5_READ_ACCESS                 0x0C
00104 /** @brief Write access condition mask for the Capability Container byte1 */
00105 #define NFCT5_WRITE_ACCESS                0x03
00106 
00107 /** @brief Type5 Tag NDEF message TLV-Type. */
00108 #define NFCT5_NDEF_MSG_TLV                ((uint8_t) 0x03)
00109 /** @brief Type5 Tag Proprietary message TLV-Type. */
00110 #define NFCT5_PROPRIETARY_TLV             ((uint8_t) 0xFD)
00111 /** @brief Type5 Tag Terminator TLV-Type. */
00112 #define NFCT5_TERMINATOR_TLV              ((uint8_t) 0xFE)
00113 /** @brief TLV-Length indicating a 4-bytes TLV (Length coded on 2 bytes). */
00114 #define NFCT5_3_BYTES_L_TLV               ((uint8_t) 0xFF)
00115 
00116 #define MAX_NDEF_SIZE         NFC_DEVICE_MAX_NDEFMEMORY
00117 
00118 typedef enum
00119 {
00120   TT5_NO_NDEF = 0,  /**< No data detected in the tag. */
00121   TT5_INITIALIZED,  /**< Capability container detected. */
00122   TT5_READ_WRITE,   /**< Read-Write data detected. */
00123   TT5_READ          /**< Read-Only data message detected. */
00124 } TT5_State;
00125 
00126 /** @brief Type5 Tag Capability Container Magic numbers as defined by the NFC Forum. */
00127 typedef enum {
00128   NFCT5_MAGICNUMBER_E1_CCFILE = 0xE1, /**<  Complete data area can be read by 1-byte block adrdess commands. */
00129   NFCT5_MAGICNUMBER_E2_CCFILE = 0xE2  /**<  Last part of the data area can be only read by 2-bytes block address commands.\n
00130                                             The first 256 blocks can be read by 1-byte block address commands. */
00131 } TT5_MagicNumber_t;
00132 
00133 /**
00134   * @brief  Type5 Tag Capability Container structure.
00135   */
00136 typedef struct
00137 {
00138   TT5_MagicNumber_t MagicNumber;  /**< CCfile[0]: Magic Number should be E1h or E2h (for extended API) */
00139   uint8_t Version;                /**< CCfile[1]: Capability container version (b7-b4) and access conditions (b3-b0) */
00140   uint8_t MemorySize;             /**< CCfile[2]: Memory size, expressed in 8 bytes blocks, set to 0 if tag size is greater than 16kbits. */
00141   uint8_t TT5Tag;                 /**< CCfile[3]: Additionnal information on the Type5 Tag:\n
00142                                                   b0: supports `read multiple block` commands\n
00143                                                   b1: RFU\n
00144                                                   b2: RFU\n
00145                                                   b3: supports `lock block` commands\n
00146                                                   b4: requires the `special frame` format
00147                                     */
00148   uint8_t rsved1;                 /**< RFU */
00149   uint8_t rsved2;                 /**< RFU */
00150   uint16_t ExtMemorySize;         /**< CCfile[6],CCfile[7]: Memory size, expressed in 8 bytes blocks, when tag size is greater than 16kbits. */
00151   TT5_State State;                /**< Indicates if a NDEF message is present. */
00152   uint32_t NDEF_offset;           /**< Indicates the address of a NDEF message in the tag. */
00153 }sCCFileInfo;
00154 
00155 /** @brief Type5 Tag Type-Length-Value structure as defined by the NFC Forum */
00156 typedef struct
00157 {
00158   uint8_t   Type;     /**< NFC Forum message Type */
00159   uint8_t   Length;   /**< Message length if lesser than 255 bytes */
00160   uint16_t  Length16; /**< Message length if greater than or equal to 255 bytes */
00161 } TT5_TLV_t;
00162 
00163 class ST25dvDriver : public NFCEEPROMDriver {
00164 
00165 public:
00166     /** Create the driver, default pin names will be used appropriate for the board.
00167      *  @param i2c_data_pin I2C data pin name.
00168      *  @param i2c_clock_pin I2C clock pin name.
00169      *  @param gpo_pin I2C GPO pin name.
00170      *  @param rf_disable_pin pin name for breaking the RF connection.
00171      */
00172     ST25dvDriver(PinName i2c_data_pin = ST25DV_I2C_SDA_PIN,
00173                 PinName i2c_clock_pin = ST25DV_I2C_SCL_PIN,
00174                 PinName led1_pin = ST25DV_LED1_PIN,
00175                 PinName led2_pin = ST25DV_LED2_PIN,
00176                 PinName led3_pin = ST25DV_LED3_PIN,
00177                 PinName lpd_pin = ST25DV_LPD_PIN,
00178                 PinName gpo_pin = ST25DV_GPO_PIN);
00179 
00180     virtual ~ST25dvDriver() { }
00181 
00182     /** @see NFCEEPROMDriver::reset
00183      */
00184     virtual void reset() {
00185         printf("reset\r\n");
00186         begin();
00187     }
00188 
00189     /** @see NFCEEPROMDriver::get_max_size
00190      */
00191     virtual size_t read_max_size() {
00192         return MAX_NDEF_SIZE;
00193     }
00194 
00195     /** @see NFCEEPROMDriver::start_session
00196      */
00197     virtual void start_session(bool force = true) {
00198         printf("start_session\r\n");
00199         if(_is_device_inited) {
00200             _is_session_started = true;
00201             delegate()->on_session_started(true);
00202         } else {
00203             delegate()->on_session_started(false);
00204         }
00205     }
00206 
00207     /** @see NFCEEPROMDriver::end_session
00208      */
00209     virtual void end_session() {
00210         printf("end_session\r\n");
00211         if(_is_session_started) {
00212             _is_session_started = false;
00213             delegate()->on_session_ended(true);
00214         } else {
00215             delegate()->on_session_ended(false);
00216         }
00217     }
00218 
00219     /** @see NFCEEPROMDriver::read_bytes
00220      */
00221     virtual void read_bytes(uint32_t address, uint8_t* bytes, size_t count) {
00222         int ret;
00223         printf("read_bytes\r\n");
00224 
00225         if (address > _ndef_size) {
00226             delegate()->on_bytes_read(0);
00227             return;
00228         }
00229 
00230         ret = read_data(address, bytes, count);
00231         printf("read_bytes read_data ret =%d count=%d bytes=%s\r\n", ret, count, bytes);
00232         if(ret != 0) {
00233             delegate()->on_bytes_read(0);
00234         } else {
00235             delegate()->on_bytes_read(count);
00236         }
00237     }
00238 
00239     /** @see NFCEEPROMDriver::write_bytes
00240      */
00241     virtual void write_bytes(uint32_t address, const uint8_t* bytes, size_t count) {
00242         int ret;
00243         printf("write_bytes\r\n");
00244         if (address > _ndef_size) {
00245             delegate()->on_bytes_written(0);
00246             printf("write_bytes error (address > _ndef_size)\r\n");
00247             return;
00248         }
00249 
00250         ret = write_data(address, bytes, count);
00251         printf("write_bytes write_data ret =%d count=%d bytes=%s\r\n", ret, count, bytes);
00252         if(ret != 0) {
00253             delegate()->on_bytes_written(0);
00254         } else {
00255             delegate()->on_bytes_written(count);
00256         }
00257     }
00258 
00259     /** @see NFCEEPROMDriver::write_size
00260      */
00261     virtual void write_size(size_t count) {
00262         int ret;
00263         printf("write_size (count=%d)\r\n", count);
00264         if (!_is_session_started) {
00265             delegate()->on_size_written(false);
00266             return;
00267         }
00268         _ndef_size = count;
00269 
00270         ret = set_size(count);
00271         if(ret != 0) {
00272             delegate()->on_size_written(false);
00273         } else {
00274             delegate()->on_size_written(true);
00275         }
00276     }
00277 
00278     /** @see NFCEEPROMDriver::read_size
00279      */
00280     virtual void read_size() {
00281         int ret;
00282         printf("read_size\r\n");
00283         if (!_is_session_started) {
00284             delegate()->on_size_read(false, 0);
00285             return;
00286         }
00287 
00288         ret = get_size();
00289         if(ret != 0) {
00290             delegate()->on_size_read(false, 0);
00291         } else {
00292             delegate()->on_size_read(true, _ndef_size);
00293         }
00294         printf("read_size _ndef_size=%d\r\n", _ndef_size);
00295     }
00296 
00297     /** @see NFCEEPROMDriver::erase_bytes
00298      */
00299     virtual void erase_bytes(uint32_t address, size_t size) {
00300         printf("erase_bytes\r\n");
00301         write_data(address, NULL, size);
00302     }
00303 
00304 private:
00305     int begin();
00306     int read_data(uint32_t address, uint8_t* bytes, size_t count);
00307     int write_data(uint32_t address, const uint8_t* bytes, size_t count);
00308     int get_size(void);
00309     int set_size(size_t count);
00310 
00311     NFCTAG_StatusTypeDef NFCTAG_Init(void);
00312     NFCTAG_StatusTypeDef NFCTAG_ReadData(uint8_t * const pData, const uint16_t TarAddr, const uint16_t Size);
00313     NFCTAG_StatusTypeDef NFCTAG_WriteData(const uint8_t * const pData, const uint16_t TarAddr, const uint16_t Size);
00314     uint32_t NFCTAG_GetByteSize(void);
00315     NFCTAG_ExtDrvTypeDef *NFCTAG_GetExtended_Drv(void);
00316     uint16_t NfcType5_NDEFDetection(void);
00317     uint16_t NfcType5_TT5Init(void);
00318     uint16_t NfcType5_WriteCCFile( const uint8_t * const pCCBuffer );
00319     uint16_t NfcType5_ReadCCFile( uint8_t * const pCCBuffer );
00320     uint16_t NfcType5_ReadNDEF(uint8_t* pData);
00321     uint16_t NfcType5_WriteNDEF(uint16_t Length, uint8_t* pData);
00322     uint16_t NfcType5_GetLength(uint16_t* Length);
00323     uint16_t NfcType5_SetLength(uint16_t Length);
00324 
00325     void ledOn(DigitalOut led);
00326     void ledOff(DigitalOut led);
00327 
00328     I2C _i2c_channel;
00329     NFCTAG_DrvTypeDef *Nfctag_Drv;
00330     sCCFileInfo CCFileStruct;
00331 
00332     DigitalOut _led1_pin;
00333     DigitalOut _led2_pin;
00334     DigitalOut _led3_pin;
00335     DigitalOut _lpd_pin;
00336     DigitalIn _gpo_pin;
00337 
00338     uint16_t _ndef_size;
00339 
00340     bool _is_device_inited;
00341     bool _is_session_started;
00342 };
00343 
00344 } //ST
00345 } //vendor
00346 } //nfc
00347 } //mbed
00348 
00349 #endif // ST25DV_H
00350 
00351 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/