STMicroelectronics' M24SR NFC Dynamic Tag Library.

Dependencies:   ST_INTERFACES

Dependents:   X_NUCLEO_NFC01A1

Fork of M24SR by ST Expansion SW Team

M24SR series Dynamic NFC Tags

The M24SR series provides an NFC forum tag type 4 RF interface and supports the NFC data exchange format (NDEF). This enables NFC use cases such as simple Bluetooth pairing and other connection handovers, automatic links to URLs, storage of Vcard and other types of information. It can be used in a wide variety of applications, including consumer electronics, computer peripherals, home appliances, industrial automation and healthcare products.

  • NFC forum tag type 4 based on ISO 14443 RF interface
  • 1 MHz I²C serial interface operating from 2.7 to 5.5 V
  • EEPROM memory density from 2 Kbits to 64 Kbits with built-in NDEF message support
  • RF disable pin allowing the application to control RF access from NFC phones
  • 128-bit password protection
  • General-purpose output pin allowing flexibility for the applications (wake up on several types of events)
  • Simple antenna design, backward compatible with M24LR series

For further information and ordering please refer to the ST Page.

HelloWorld application

Import programHelloWorld_Async_M24SR

M24SR NFC example. Simple application to asynchronously write and read an URL from a M24SR tag.

m24sr_def.h

Committer:
nikapov
Date:
2017-07-31
Revision:
0:11161008d77a

File content as of revision 0:11161008d77a:

/**
 ******************************************************************************
 * @file    m24sr.h
 * @author  MMY Application Team
 * @version V2.0.0
 * @date    28 Apr 2017
 * @brief   This file provides a set of functions needed to manage M24SR
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
 *
 * Licensed under MMY-ST Liberty SW License Agreement V2, (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *        http://www.st.com/software_license_agreement_liberty_v2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __DRV_M24SR_H
#define __DRV_M24SR_H

/* Includes ------------------------------------------------------------------*/
#include "Nfc.h"

#ifdef __cplusplus
extern "C" {
#endif

/** @addtogroup M24SR_Driver
 * @{
 */

/** @addtogroup drv_M24SR
 * @{
 */

/* Exported types ------------------------------------------------------------*/

/**
 * @brief  APDU-Header command structure
 */
typedef struct {
	uint8_t CLA; /* Command class */
	uint8_t INS; /* Operation code */
	uint8_t P1; /* Selection Mode */
	uint8_t P2; /* Selection Option */
} C_APDU_Header;

/**
 * @brief  APDU-Body command structure
 */
typedef struct {
	uint8_t LC; /* Data field length */
	const uint8_t *pData; /* Command parameters */
	uint8_t LE; /* Expected length of data to be returned */
} C_APDU_Body;

/**
 * @brief  APDU Command structure
 */
typedef struct {
	C_APDU_Header Header;
	C_APDU_Body Body;
} C_APDU;

/**
 * @brief  SC response structure
 */
typedef struct {
	uint8_t *pData; /* Data returned from the card */ // pointer on the transceiver buffer = ReaderRecBuf[CR95HF_DATA_OFFSET ];
	uint8_t SW1; /* Command Processing status */
	uint8_t SW2; /* Command Processing qualification */
} R_APDU;

/**
 * @brief  GPO mode structure
 */
typedef enum {
	RF_GPO = 0, I2C_GPO = 1
} M24SR_GPO_MODE;

typedef enum {
	M24SR_WAITINGTIME_POLLING,
	M24SR_INTERRUPT_GPO
} M24SR_WAITINGTIME_MGMT;

/* Exported constants --------------------------------------------------------*/

/** @defgroup lib_M24SR_Exported_Constants
 * @{
 */

/* ---------------------- status code ----------------------------------------*/
#define UB_STATUS_OFFSET										4
#define LB_STATUS_OFFSET  									3
#define I_AM_M24SR                ((uint8_t)0xB4)
#define I_AM_M24SR_AUTOMOTIVE                ((uint8_t)0xBC)

#define M24SR_NBBYTE_INVALID								0xFFFE

/** @defgroup drv_M24SR_File_Identifier
 * @{
 */
#define SYSTEM_FILE_ID_BYTES {0xE1,0x01}
#define CC_FILE_ID_BYTES {0xE1,0x03}
#define NDEF_FILE_ID						0x0001	 
/**
 * @}
 */

/** @defgroup drv_M24SR_Password_Management
 * @{
 */
#define READ_PWD								0x0001
#define WRITE_PWD								0x0002
#define I2C_PWD									0x0003

/*-------------------------- Verify command answer ----------------------------*/
/**
 * @}
 */

/** @defgroup drv_M24SR_Command_Management
 * @{
 */

/* special M24SR command ----------------------------------------------------------------------*/
#define M24SR_OPENSESSION_COMMAND {0x26}
#define M24SR_KILLSESSION_COMMAND {0x52}

/* APDU Command: class list -------------------------------------------*/
#define C_APDU_CLA_DEFAULT	  0x00
#define C_APDU_CLA_ST					0xA2

/*------------------------ Data Area Management Commands ---------------------*/
#define C_APDU_SELECT_FILE     0xA4
#define C_APDU_GET_RESPONCE    0xC0
#define C_APDU_STATUS          0xF2
#define C_APDU_UPDATE_BINARY   0xD6
#define C_APDU_READ_BINARY     0xB0
#define C_APDU_WRITE_BINARY    0xD0
#define C_APDU_UPDATE_RECORD   0xDC
#define C_APDU_READ_RECORD     0xB2

/*-------------------------- Safety Management Commands ----------------------*/
#define C_APDU_VERIFY          0x20
#define C_APDU_CHANGE          0x24
#define C_APDU_DISABLE         0x26
#define C_APDU_ENABLE          0x28

/*-------------------------- Gpio Management Commands ------------------------*/
#define C_APDU_INTERRUPT       0xD6

/*  Length	----------------------------------------------------------------------------------*/
#define M24SR_STATUS_NBBYTE												2
#define M24SR_CRC_NBBYTE													2
#define M24SR_STATUSRESPONSE_NBBYTE								5
#define M24SR_DESELECTREQUEST_COMMAND	{0xC2,0xE0,0xB4}
#define M24SR_DESELECTRESPONSE_NBBYTE							3
#define M24SR_WATINGTIMEEXTRESPONSE_NBBYTE				4
#define M24SR_PASSWORD_NBBYTE											0x10
#define M24SR_SELECTAPPLICATION_COMMAND	{0xD2,0x76,0x00,0x00,0x85,0x01,0x01}
/*  Command structure	------------------------------------------------------------------------*/
#define M24SR_CMDSTRUCT_SELECTAPPLICATION					0x01FF
#define M24SR_CMDSTRUCT_SELECTCCFILE							0x017F
#define M24SR_CMDSTRUCT_SELECTNDEFFILE						0x017F
#define M24SR_CMDSTRUCT_READBINARY								0x019F
#define M24SR_CMDSTRUCT_UPDATEBINARY							0x017F
#define M24SR_CMDSTRUCT_VERIFYBINARYWOPWD					0x013F
#define M24SR_CMDSTRUCT_VERIFYBINARYWITHPWD				0x017F
#define M24SR_CMDSTRUCT_CHANGEREFDATA							0x017F
#define M24SR_CMDSTRUCT_ENABLEVERIFREQ						0x011F
#define M24SR_CMDSTRUCT_DISABLEVERIFREQ						0x011F
#define M24SR_CMDSTRUCT_SENDINTERRUPT							0x013F
#define M24SR_CMDSTRUCT_GPOSTATE									0x017F

/*  Command structure Mask -------------------------------------------------------------------*/
#define M24SR_PCB_NEEDED				0x0001		/* PCB byte present or not */
#define M24SR_CLA_NEEDED				0x0002 		/* CLA byte present or not */
#define M24SR_INS_NEEDED				0x0004 		/* Operation code present or not*/ 
#define M24SR_P1_NEEDED					0x0008		/* Selection Mode  present or not*/
#define M24SR_P2_NEEDED					0x0010		/* Selection Option present or not*/
#define M24SR_LC_NEEDED					0x0020		/* Data field length byte present or not */
#define M24SR_DATA_NEEDED				0x0040		/* Data present or not */
#define M24SR_LE_NEEDED					0x0080		/* Expected length present or not */
#define M24SR_CRC_NEEDED				0x0100		/* 2 CRC bytes present	or not */

#define M24SR_DID_NEEDED				0x08			/* DID byte present or not */

/**
 * @}
 */

/*  Offset	----------------------------------------------------------------------------------*/
#define M24SR_OFFSET_PCB													0
#define M24SR_OFFSET_CLASS												1
#define M24SR_OFFSET_INS													2
#define M24SR_OFFSET_P1														3

/*  mask	------------------------------------------------------------------------------------*/
#define M24SR_MASK_BLOCK													0xC0
#define M24SR_MASK_IBLOCK													0x00
#define M24SR_MASK_RBLOCK													0x80
#define M24SR_MASK_SBLOCK													0xC0

/**
 * @}
 */

/**
 * @}
 */

/* Exported macro ------------------------------------------------------------*/

/** @brief Get Most Significant Byte
 * @param  val: number where MSB must be extracted
 * @retval MSB
 */
#define GETMSB(val) 		( (uint8_t) ((val & 0xFF00 )>>8) ) 

/** @brief Get Least Significant Byte
 * @param  val: number where LSB must be extracted
 * @retval LSB
 */
#define GETLSB(val) 		( (uint8_t) (val & 0x00FF )) 

/** @brief Used to toggle the block number by adding 0 or 1 to default block number value
 * @param  val: number to know if incrementation is needed
 * @retval  0 or 1 if incrementation needed
 */
#define TOGGLE(val) 		((val != 0x00)? 0x00 : 0x01)

typedef struct {
	C_APDU command;
	//static R_APDU 						Response;
	uint8_t dataBuffer[0xFF];
	uint8_t uM24SRbuffer[0xFF];
	uint8_t uDIDbyte;
} M24SR_DrvDataTypeDef;

typedef void* M24SR_InitTypeDef;

extern NFC_t M24SR_IO_Init(void *handle);
extern NFC_t M24SR_IO_IsAnswerReady(void *handle);
extern NFC_t M24SR_IO_SendI2Ccommand(void *handle, uint8_t NbByte,
		uint8_t *pBuffer);
extern NFC_t M24SR_IO_ReceiveI2Cresponse(void *handle,
		uint8_t NbByte, uint8_t *pBuffer);
extern NFC_t M24SR_IO_PollI2C(void *handle);
//extern void M24SR_IO_GPO_ReadPin(void *handle, GPIO_PinState *pPinState);
//extern void M24SR_IO_RFDIS_WritePin(void *handle, GPIO_PinState PinState);
extern void M24SR_IO_GPO_ReadPin(void *handle, uint8_t *pPinState);
extern void M24SR_IO_RFDIS_WritePin(void *handle, uint8_t PinState);
//extern void M24SR_IO_SetI2CSynchroMode(void *handle,
//		M24SR_WAITINGTIME_MGMT mode);

/**
 * @}
 */

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif /* __DRV_M24SR_H */

/******************* (C) COPYRIGHT 2013 STMicroelectronics *****END OF FILE****/