V Click_NFC_Tag_2 Milos Vidojevic milos.vidojevic@mikroe.com www.mikroe.com Library for NFC Tag 2 click mikroC PRO for AVR
NFC_Tag_2_HAL nfc_tag2_hal C_String TWI .\Uses\nfc_tag2_hal.c; /**
* @file nfc_tag2_hal.c
*
* @cond INTERNAL_DOC
* @brief NFC tag 2
*
* Version : 0.0.1
*
* Revision Log :
* - 0.0.1 (Sep/2016) Module created Milos Vidojevic
*
* To Do List :
* @todo
*
******************************************************************************/
#include "nfc_tag2_hal.h"
/******************************************************************************
* Module Preprocessor Constants
*******************************************************************************/
#define READ_BIT 1
#define WRITE_BIT 0
/******************************************************************************
* Module Variable Definitions
*******************************************************************************/
#if defined( __MIKROC_PRO_FOR_PIC32__ ) || \
defined( __MIKROC_PRO_FOR_DSPIC__ )
static uint8_t NACK_BIT = 0x01;
static uint8_t ACK_BIT = 0x00;
#elif defined( __MIKROC_PRO_FOR_PIC__ ) || \
defined( __MIKROC_PRO_FOR_AVR__ )
static uint8_t NACK_BIT = 0x00;
static uint8_t ACK_BIT = 0x01;
#endif
#if defined( __MIKROC_PRO_FOR_ARM__ )
#if defined( __STM32__ ) || defined( __KINETIS__ ) || defined( __MCHP__ )
static unsigned int ( *start_i2c_p ) ( void );
static unsigned int ( *write_i2c_p ) ( unsigned char slave_address,
unsigned char *buffer,
unsigned long count,
unsigned long end_mode );
static void ( *read_i2c_p ) ( unsigned char slave_address,
unsigned char *buffer,
unsigned long count,
unsigned long end_mode );
#elif defined( __TI__ )
static void ( *enable_i2c_p ) ( void );
static void ( *disable_i2c_p ) ( void );
static void ( *set_slave_address_i2c_p )( char slave_address,
char dir );
static unsigned char( *write_i2c_p ) ( unsigned char data_in,
unsigned char mode );
static unsigned char( *read_i2c_p ) ( unsigned char *data_out,
unsigned char mode );
#endif
#elif defined( __MIKROC_PRO_FOR_AVR__ )
static unsigned char( *busy_i2c_p ) ( void );
static unsigned char( *status_i2c_p ) ( void );
static unsigned char( *start_i2c_p ) ( void );
static void ( *stop_i2c_p ) ( void );
static void ( *close_i2c_p ) ( void );
static void ( *write_i2c_p ) ( unsigned char data_out );
static unsigned char( *read_i2c_p ) ( unsigned char ack );
#elif defined( __MIKROC_PRO_FOR_PIC__ )
static unsigned char( *is_idle_i2c_p ) ( void );
static unsigned char( *start_i2c_p ) ( void );
static void ( *stop_i2c_p ) ( void );
static void ( *restart_i2c_p ) ( void );
static unsigned char( *write_i2c_p ) ( unsigned char data_out );
static unsigned char( *read_i2c_p ) ( unsigned char ack );
#elif defined( __MIKROC_PRO_FOR_PIC32__ )
static unsigned int ( *is_idle_i2c_p ) ( void );
static unsigned int ( *start_i2c_p ) ( void );
static void ( *stop_i2c_p ) ( void );
static unsigned int ( *restart_i2c_p ) ( void );
static unsigned int ( *write_i2c_p ) ( unsigned char data_out );
static unsigned char( *read_i2c_p ) ( unsigned int ack );
#elif defined( __MIKROC_PRO_FOR_DSPIC__ )
static unsigned int ( *is_idle_i2c_p ) ( void );
static unsigned int ( *start_i2c_p ) ( void );
static void ( *stop_i2c_p ) ( void );
static void ( *restart_i2c_p ) ( void );
static unsigned int ( *write_i2c_p ) ( unsigned char data_out );
static unsigned char( *read_i2c_p ) ( unsigned int ack );
#elif defined( __MIKROC_PRO_FOR_8051__ )
static unsigned char( *status_i2c_p ) ( void );
static unsigned char( *start_i2c_p ) ( void );
static void ( *stop_i2c_p ) ( void );
static void ( *close_i2c_p ) ( void );
static void ( *write_i2c_p ) ( unsigned char data_out );
static unsigned char( *read_i2c_p ) ( unsigned char ack );
#elif defined( __MIKROC_PRO_FOR_FT90x__ )
static void ( *soft_reset_i2c_p ) ( void );
static void ( *set_slave_address_i2c_p )( unsigned char slave_address );
static unsigned char( *write_i2c_p ) ( unsigned char *buffer,
unsigned int count );
static unsigned char( *read_i2c_p ) ( unsigned char *buffer,
unsigned int count );
#endif
/******************************************************************************
* Function Definitions
*******************************************************************************/
void hal_delay( uint16_t ms )
{
#if defined(__MIKROC_PRO_FOR_ARM__) || \
defined( __MIKROC_PRO_FOR_FT90x__ ) || \
defined( __MIKROC_PRO_FOR_AVR__ ) || \
defined( __MIKROC_PRO_FOR_8051__ ) || \
defined( __MIKROC_PRO_FOR_DSPIC__ ) || \
defined( __MIKROC_PRO_FOR_PIC32__ ) || \
defined( __MIKROC_PRO_FOR_PIC__ )
while( ms-- )
Delay_1ms();
#endif
}
int hal_nfctag2_init( void )
{
#if defined( __MIKROC_PRO_FOR_ARM__ )
#if defined( __STM32__ ) || defined( __KINETIS__ ) || defined( __MCHP__ )
start_i2c_p = I2C_Start_Ptr;
write_i2c_p = I2C_Write_Ptr;
read_i2c_p = I2C_Read_Ptr;
#elif defined( __TI__ )
enable_i2c_p = I2C_Enable_Ptr;
disable_i2c_p = I2C_Disable_Ptr;
set_slave_address_i2c_p = I2C_Master_Slave_Addr_Set_Ptr;
write_i2c_p = I2C_Write_Ptr;
read_i2c_p = I2C_Read_Ptr;
#endif
#elif defined( __MIKROC_PRO_FOR_AVR__ )
#if defined( __LT64__ )
busy_i2c_p = TWI_Busy;
status_i2c_p = TWI_Status;
close_i2c_p = TWI_Close;
start_i2c_p = TWI_Start;
stop_i2c_p = TWI_Stop;
write_i2c_p = TWI_Write;
read_i2c_p = TWI_Read;
#elif defined( __GT64__ )
busy_i2c_p = TWIC_Busy;
status_i2c_p = TWIC_Status;
close_i2c_p = TWIC_Close;
start_i2c_p = TWIC_Start;
stop_i2c_p = TWIC_Stop;
write_i2c_p = TWIC_Write;
read_i2c_p = TWIC_Read;
#endif
#elif defined( __MIKROC_PRO_FOR_PIC__ )
is_idle_i2c_p = I2C1_Is_Idle;
start_i2c_p = I2C1_Start;
stop_i2c_p = I2C1_Stop;
restart_i2c_p = I2C1_Repeated_Start;
write_i2c_p = I2C1_Wr;
read_i2c_p = I2C1_Rd;
#elif defined( __MIKROC_PRO_FOR_PIC32__ )
is_idle_i2c_p = I2C_Is_Idle_Ptr;
start_i2c_p = I2C_Start_Ptr;
stop_i2c_p = I2C_Stop_Ptr;
restart_i2c_p = I2C_Restart_Ptr;
write_i2c_p = I2C_Write_Ptr;
read_i2c_p = I2C_Read_Ptr;
#elif defined( __MIKROC_PRO_FOR_DSPIC__ )
is_idle_i2c_p = I2C_Is_Idle_Ptr;
start_i2c_p = I2C_Start_Ptr;
stop_i2c_p = I2C_Stop_Ptr;
restart_i2c_p = I2C_Restart_Ptr;
write_i2c_p = I2C_Write_Ptr;
read_i2c_p = I2C_Read_Ptr;
#elif defined( __MIKROC_PRO_FOR_8051__ )
status_i2c_p = TWI_Status;
close_i2c_p = TWI_Close;
start_i2c_p = TWI_Start;
stop_i2c_p = TWI_Stop;
write_i2c_p = TWI_Write;
read_i2c_p = TWI_Read;
#elif defined( __MIKROC_PRO_FOR_FT90x__ )
soft_reset_i2c_p = I2CM_Soft_Reset_Ptr;
set_slave_address_i2c_p = I2CM_Set_Slave_Address_Ptr;
write_i2c_p = I2CM_Write_Bytes_Ptr;
read_i2c_p = I2CM_Read_Bytes_Ptr;
#endif
return 0;
}
int hal_nfctag2_write
(
uint8_t i2c_address,
uint8_t* buffer,
uint16_t count
)
{
int res = 0;
uint8_t *ptr = buffer;
#if defined(__MIKROC_PRO_FOR_ARM__)
#if defined( __STM32__ ) || defined( __KINETIS__ ) || defined( __MCHP__ )
res |= start_i2c_p();
res |= write_i2c_p( i2c_address, ptr, count, END_MODE_STOP );
#elif defined( __TI__ )
set_slave_address_i2c_p( i2c_address, _I2C_DIR_MASTER_TRANSMIT );
if( count == 1 ){
res |= write_i2c_p( *ptr, _I2C_MASTER_MODE_SINGLE_SEND );
}
else if( count == 2 ){
res |= write_i2c_p( *ptr++, _I2C_MASTER_MODE_BURST_SEND_START );
res |= write_i2c_p( *ptr, _I2C_MASTER_MODE_BURST_SEND_FINISH );
} else {
res |= write_i2c_p( *ptr++, _I2C_MASTER_MODE_BURST_SEND_START );
while( count-- > 1 )
res |= write_i2c_p( *ptr++, _I2C_MASTER_MODE_BURST_SEND_CONT );
res |= write_i2c_p( *ptr, _I2C_MASTER_MODE_BURST_SEND_FINISH );
}
#endif
#elif defined( __MIKROC_PRO_FOR_FT90x__ )
set_slave_address_i2c_p( i2c_address );
res |= write_i2c_p( ptr, count );
#elif defined( __MIKROC_PRO_FOR_AVR__ ) || \
defined( __MIKROC_PRO_FOR_8051__ ) || \
defined( __MIKROC_PRO_FOR_DSPIC__ ) || \
defined( __MIKROC_PRO_FOR_PIC32__ ) || \
defined( __MIKROC_PRO_FOR_PIC__ )
res |= start_i2c_p();
res |= write_i2c_p( ( i2c_address << 1 ) | WRITE_BIT );
while( count-- )
res |= write_i2c_p( *ptr++ );
stop_i2c_p();
#endif
return res;
}
int hal_nfctag2_read
(
uint8_t i2c_address,
uint8_t* buffer,
uint16_t count
)
{
int res = 0;
uint8_t *ptr = buffer;
#if defined(__MIKROC_PRO_FOR_ARM__)
#if defined( __STM32__ ) || defined( __KINETIS__ ) || defined( __MCHP__ )
res |= start_i2c_p();
res |= write_i2c_p( i2c_address, ptr, 1, END_MODE_STOP );
res |= start_i2c_p();
read_i2c_p( i2c_address, ptr, count, END_MODE_STOP );
#elif defined( __TI__ )
set_slave_address_i2c_p( i2c_address, _I2C_DIR_MASTER_TRANSMIT );
res |= write_i2c_p( *ptr, _I2C_MASTER_MODE_SINGLE_SEND );
set_slave_address_i2c_p( i2c_address, _I2C_DIR_MASTER_RECEIVE );
if( count == 1 ) {
res |= read_i2c_p( ptr, _I2C_MASTER_MODE_SINGLE_RECEIVE );
}
else if( count == 2 ){
res |= read_i2c_p( ptr++, _I2C_MASTER_MODE_BURST_RECEIVE_START );
res |= read_i2c_p( ptr, _I2C_MASTER_MODE_BURST_RECEIVE_FINISH );
}
else {
res |= read_i2c_p( ptr++ , _I2C_MASTER_MODE_BURST_RECEIVE_START );
while( --count > 1 )
res |= read_i2c_p( ptr++ , _I2C_MASTER_MODE_BURST_SEND_CONT );
res |= read_i2c_p( ptr, _I2C_MASTER_MODE_BURST_SEND_FINISH );
}
#endif
#elif defined( __MIKROC_PRO_FOR_FT90x__ )
set_slave_address_i2c_p( i2c_address );
res |= write_i2c_p( ptr, 1 );
res |= read_i2c_p( ptr, count );
#elif defined( __MIKROC_PRO_FOR_AVR__ ) || \
defined( __MIKROC_PRO_FOR_PIC32__ ) || \
defined( __MIKROC_PRO_FOR_8051__ ) || \
defined( __MIKROC_PRO_FOR_PIC__ ) || \
defined( __MIKROC_PRO_FOR_DSPIC__ )
res |= start_i2c_p();
res |= write_i2c_p( ( i2c_address << 1 ) | WRITE_BIT );
res |= write_i2c_p( *ptr );
stop_i2c_p();
Delay_ms( 5 );
res |= start_i2c_p();
res |= write_i2c_p( ( i2c_address << 1 ) | READ_BIT );
while( --count )
*ptr++ = read_i2c_p( ACK_BIT );
*ptr = read_i2c_p( NACK_BIT );
stop_i2c_p();
#endif
return res;
}
int hal_nfctag2_transfer
(
uint8_t i2c_address,
uint8_t* input,
uint16_t in_count,
uint8_t* output,
uint16_t out_count
)
{
int res = 0;
uint8_t *in = input;
uint8_t *out = output;
#if defined(__MIKROC_PRO_FOR_ARM__)
#if defined( __STM32__ ) || defined( __KINETIS__ ) || defined( __MCHP__ )
res |= start_i2c_p();
res |= write_i2c_p( i2c_address, in, in_count, END_MODE_STOP );
res |= start_i2c_p();
read_i2c_p( i2c_address, out, out_count, END_MODE_STOP );
#elif defined( __TI__ )
set_slave_address_i2c_p( i2c_address, _I2C_DIR_MASTER_TRANSMIT );
if( in_count == 1 ) {
res |= write_i2c_p( *in, _I2C_MASTER_MODE_SINGLE_SEND );
}
if( in_count == 2 ) {
res |= write_i2c_p( *in++, _I2C_MASTER_MODE_BURST_SEND_START );
res |= write_i2c_p( *in, _I2C_MASTER_MODE_BURST_SEND_FINISH );
}
else {
res |= write_i2c_p( *in++, _I2C_MASTER_MODE_BURST_SEND_START );
while( in_count-- > 1 )
res |= write_i2c_p( *in++, _I2C_MASTER_MODE_BURST_SEND_CONT );
res |= write_i2c_p( *in, _I2C_MASTER_MODE_BURST_SEND_FINISH );
}
set_slave_address_i2c_p( _i2c_hw_address, _I2C_DIR_MASTER_RECEIVE );
if( out_count == 1 ) {
res |= read_i2c_p( out, _I2C_MASTER_MODE_SINGLE_RECEIVE );
}
else if( out_count == 2 ){
res |= read_i2c_p( out++, _I2C_MASTER_MODE_BURST_RECEIVE_START );
res |= read_i2c_p( out, _I2C_MASTER_MODE_BURST_RECEIVE_FINISH );
}
else {
res |= read_i2c_p( out++ , _I2C_MASTER_MODE_BURST_RECEIVE_START );
while( --out_count > 1 )
res |= read_i2c_p( out++ , _I2C_MASTER_MODE_BURST_SEND_CONT );
res |= read_i2c_p( out, _I2C_MASTER_MODE_BURST_SEND_FINISH );
}
#endif
#elif defined( __MIKROC_PRO_FOR_FT90x__ )
set_slave_address_i2c_p( i2c_address );
res |= write_i2c_p( in, in_count );
set_slave_address_i2c_p( i2c_address );
res |= read_i2c_p( out, out_count );
#elif defined( __MIKROC_PRO_FOR_AVR__ ) || \
defined( __MIKROC_PRO_FOR_8051__ ) || \
defined( __MIKROC_PRO_FOR_DSPIC__ ) || \
defined( __MIKROC_PRO_FOR_PIC32__ ) || \
defined( __MIKROC_PRO_FOR_PIC__ )
res |= start_i2c_p();
res |= write_i2c_p( ( i2c_address << 1 ) | WRITE_BIT );
while( in_count-- )
res|= write_i2c_p( *in++ );
stop_i2c_p();
res |= start_i2c_p();
res |= write_i2c_p( ( i2c_address << 1 ) | READ_BIT );
while( --out_count )
*out++ = read_i2c_p( ACK_BIT );
*out = read_i2c_p( NACK_BIT );
stop_i2c_p();
#endif
return res;
}
/*************** END OF FUNCTIONS *********************************************/ .\Uses\nfc_tag2_hal.h /**
* @file nfc_tag2_hal.h
* @brief
HAL layer
*
******************************************************************************/
#ifndef NFC_TAG2_HAL_H_
#define NFC_TAG2_HAL_H_
#include
/******************************************************************************
* Function Prototypes
*******************************************************************************/
#ifdef __cplusplus
extern "C"{
#endif
void hal_delay( uint16_t ms );
/**
* @brief HAL Initialization
*
* @par
* Initialization of HAL layer used to initialize function pointers. This
* Function is usually caled from HW layer initialization so user should not
* care about it in that case.
*/
int hal_nfctag2_init( void );
/**
* @brief HAL Write
*
* @param[in] uint8_t i2c_address
* @param[in] uint8_t* buffer - data buffer
* @param[in] uint16_t count - data size in bytes
*
* @par
* Generic write function slightly adapted for NFC Tag 2 click.
*
*/
int hal_nfctag2_write
(
uint8_t i2c_address,
uint8_t *buffer,
uint16_t count
);
/**
* @brief HAL Read
*
* @par
* Generic read function slightly adapted for NFC Tag 2 click.
*
* @note
* Buffer caries command ( register address ) at first byte which will be
* rewritten after reading.
*
* @param[in/out] buffer - data buffer
* @param[in] count - data size in bytes
*/
int hal_nfctag2_read
(
uint8_t i2c_address,
uint8_t *buffer,
uint16_t count
);
/**
* @brief HAL Transfer
*
* @param input
* @param in_count
* @param output
* @param out_count
* @return
*/
int hal_nfctag2_transfer
(
uint8_t i2c_address,
uint8_t* input,
uint16_t in_count,
uint8_t* output,
uint16_t out_count
);
#ifdef __cplusplus
} // extern "C"
#endif
#endif
/*** End of File **************************************************************/
k
AT90CAN128 AT90CAN32 AT90CAN64 AT90USB1286 AT90USB1287
AT90USB646
AT90USB647 ATmega128
ATmega1280
ATmega1281
ATmega1284 ATmega1284P
ATmega128A ATmega16 ATmega163 ATmega164
ATmega164A
ATmega164P ATmega164PA ATmega168
ATmega168A
ATmega168P ATmega168PA ATMEGA168PB ATmega16A ATmega16HVB
ATmega16U4
ATmega2560
ATmega2561 ATmega32 ATmega323 ATmega324
ATmega324A
ATmega324P ATmega324PA ATmega328
ATmega328P ATmega32A ATmega32HVB
ATmega32U4
ATmega32U6 ATmega406 ATmega48 ATmega48A ATmega48P
ATmega48PA ATmega64 ATmega640 ATmega644
ATmega644A
ATmega644P ATmega644PA ATmega64A ATmega8
ATmega8535 ATmega88 ATmega88A ATmega88P
ATmega88PA ATmega8A ATtiny48 ATtiny88 ATxmega128A1
ATXMEGA128A1U ATxmega128A3
ATXMEGA128A3U
ATXMEGA128A4U ATXMEGA128B1 ATXMEGA128B3 ATXMEGA128C3 ATxmega128D3 ATXMEGA128D4 ATxmega16A4 ATXMEGA16A4U ATXMEGA16C4 ATxmega16D4 ATXMEGA16E5 ATxmega192A3
ATXMEGA192A3U ATXMEGA192C3 ATxmega192D3 ATxmega256A3
ATxmega256A3B ATXMEGA256A3BU
ATXMEGA256A3U ATXMEGA256C3 ATxmega256D3 ATxmega32A4 ATXMEGA32A4U ATXMEGA32C3 ATXMEGA32C4 ATXMEGA32D3 ATxmega32D4 ATXMEGA32E5 ATXMEGA384C3 ATXMEGA384D3 ATxmega64A1 ATXMEGA64A1U ATxmega64A3 ATXMEGA64A3U ATXMEGA64A4U ATXMEGA64B1 ATXMEGA64B3 ATXMEGA64C3 ATxmega64D3 ATXMEGA64D4
ATXMEGA8E5 NFC_Tag_2 nfc_tag2
NFC_Tag_2_HAL .\Uses\nfc_tag2.c /*
* nfc_tag2.c
*
* Version : 0.0.1
*
* Revision Log :
* 0.0.1 (Sep/2016) Module created Milos Vidojevic
*
* To Do List :
* ( 0 Block / 0 Byte ) write always current I2C Address
*
******************************************************************************/
#include "nfc_tag2.h"
#define _NT2_USER_MEMORY_ 0x01
#define _NT2_CONFIG_REG_ 0x3A
#define _NT2_SESSION_REG_ 0xFE
#define _NT2_BLOCK_SIZE_ 16
/*
* Memory map type
*/
typedef uint8_t NT2_MEM_MAP[ 3 ];
/*
* Memory map properties
*/
enum NT2_MAP_PROP
{
MM_BLOCK, MM_OFFSET, MM_SIZE
};
/*
* Memory map with properties mapable and accessable by NFCTAG2_REG enum.
*/
static const NT2_MEM_MAP MEMORY[ 5 ] =
{
{ 0, 0, 7 }, /*< Serial Number */
{ 0, 10, 2 }, /*< Static Lock */
{ 0, 12, 4 }, /*< Capability Container */
{ 56, 8, 3 }, /*< Dynamic Lock */
{ 248, 0, 64 } /*< SRAM Memory */
};
static uint8_t _i2c_address;
/*
* Private Function Prototypes
******************************************************************************/
static int nt2_memblock_rd( uint8_t block, uint8_t *value );
static int nt2_memblock_wr( uint8_t block, uint8_t *value );
static int nt2_memreg_rd
(
uint8_t block,
uint8_t offset,
uint16_t len,
uint8_t *out
);
static int nt2_memreg_wr
(
uint8_t block,
uint8_t offset,
uint16_t len,
uint8_t *in
);
/*
* Private Function Definitions
******************************************************************************/
static int nt2_memblock_rd
(
uint8_t block,
uint8_t *value
)
{
uint8_t temp[ _NT2_BLOCK_SIZE_ ] = { 0 };
temp[ 0 ] = block;
if( hal_nfctag2_read( _i2c_address, temp, sizeof( temp ) ) )
return 1;
memcpy( ( void* )value, ( void* )temp, _NT2_BLOCK_SIZE_ );
hal_delay( 5 );
return 0;
}
static int nt2_memblock_wr
(
uint8_t block,
uint8_t *value
)
{
uint8_t temp[ _NT2_BLOCK_SIZE_ + 1 ] = { 0 };
temp[ 0 ] = block;
memcpy( ( void* )( temp + 1 ), ( void* )value , _NT2_BLOCK_SIZE_ );
/*if( temp[ 0 ] == 0 )
temp[ 1 ] = ( _i2c_address >> 1 ) ;*/
if( hal_nfctag2_write( _i2c_address, temp, sizeof( temp ) ) )
return 1;
hal_delay( 5 );
return 0;
}
static int nt2_memreg_rd
(
uint8_t block,
uint8_t offset,
uint16_t len,
uint8_t *out
)
{
uint8_t temp[ _NT2_BLOCK_SIZE_ ] = { 0 };
while( ( offset + len ) > _NT2_BLOCK_SIZE_ )
{
if( nt2_memblock_rd( block++, temp ) )
return 1;
memcpy( ( void* )out,
( void* )( temp + offset ), _NT2_BLOCK_SIZE_ - offset );
len -= _NT2_BLOCK_SIZE_ - offset;
out += _NT2_BLOCK_SIZE_ - offset;
offset = 0;
}
if( nt2_memblock_rd( block, temp ) )
return 1;
memcpy( ( void* )out, ( void* )( temp + offset ), len );
return 0;
}
static int nt2_memreg_wr
(
uint8_t block,
uint8_t offset,
uint16_t len,
uint8_t *in
)
{
uint8_t temp[ _NT2_BLOCK_SIZE_ ] = { 0 };
while( ( offset + len ) > _NT2_BLOCK_SIZE_ )
{
if( nt2_memblock_rd( block, temp ) )
return 1;
memcpy( ( void* )( temp + offset ),
( void* )in, _NT2_BLOCK_SIZE_ - offset );
if( nt2_memblock_wr( block++, temp ) )
return 1;
len -= _NT2_BLOCK_SIZE_ - offset;
in += _NT2_BLOCK_SIZE_ - offset;
offset = 0;
}
if( nt2_memblock_rd( block, temp ) )
return 1;
memcpy( ( void* )( temp + offset ), ( void* )in, len );
if( nt2_memblock_wr( block, temp ) )
return 1;
return 0;
}
/*
* Public Function Definitions
******************************************************************************/
int nfctag2_init( uint8_t i2c_address )
{
_i2c_address = i2c_address >> 1;
if( hal_nfctag2_init() )
return -1;
return NT2_OK;
}
int nfctag2_i2c_addres( uint8_t new_address )
{
uint8_t tmp = new_address;
if( nt2_memreg_wr( 0, 0, 1, &tmp ) )
return NT2_BLOCK_WRITE_ERR;
_i2c_address = tmp >> 1;
return NT2_OK;
}
int nfctag2_get( NFCTAG2_SET reg, uint8_t *output )
{
uint8_t *ptr = output;
if( nt2_memreg_rd( MEMORY[ reg ][ MM_BLOCK ], MEMORY[ reg ][ MM_OFFSET ],
MEMORY[ reg ][ MM_SIZE ], ptr ) )
return NT2_BLOCK_READ_ERR;
return NT2_OK;
}
int nfctag2_set( NFCTAG2_SET reg, uint8_t *value )
{
uint8_t *ptr = value;
if((MEMORY[ reg ][ MM_BLOCK ] == 0) && (MEMORY[ reg ][ MM_OFFSET ] == 0))
{
*ptr = _i2c_address;
}
if( nt2_memreg_wr( MEMORY[ reg ][ MM_BLOCK ], MEMORY[ reg ][ MM_OFFSET ],
MEMORY[ reg ][ MM_SIZE ], ptr ) )
return NT2_BLOCK_WRITE_ERR;
return NT2_OK;
}
uint8_t nfctag2_config_get( NFCTAG2_CFG reg )
{
uint8_t regdat;
if( nt2_memreg_rd( _NT2_CONFIG_REG_, reg, 1, ®dat ) )
return NT2_BLOCK_READ_ERR;
return regdat;
}
int nfctag2_config_set( NFCTAG2_CFG reg, uint8_t input )
{
if( nt2_memreg_wr( _NT2_CONFIG_REG_, reg, 1, &input ) )
return NT2_BLOCK_WRITE_ERR;
return NT2_OK;
}
uint8_t nfctag2_session_get( NFCTAG2_CFG reg )
{
uint8_t regdat;
uint8_t temp[ 2 ] = { 0 };
temp[ 0 ] = _NT2_SESSION_REG_;
temp[ 1 ] = reg;
if( hal_nfctag2_transfer( _i2c_address, temp, 2, ®dat, 1 ) )
return NT2_BLOCK_WRITE_ERR;
hal_delay( 5 );
return regdat;
}
int nfctag2_session_set( NFCTAG2_CFG reg, uint8_t mask, uint8_t value )
{
uint8_t temp[ 4 ] = { 0 };
temp[ 0 ] = _NT2_SESSION_REG_;
temp[ 1 ] = reg;
temp[ 2 ] = mask;
temp[ 3 ] = value;
if( hal_nfctag2_write( _i2c_address, temp, 4 ) )
return NT2_BLOCK_WRITE_ERR;
hal_delay( 5 );
return NT2_OK;
}
int nfctag2_memory_read( uint16_t mem_offset, uint8_t *buffer, uint16_t count )
{
uint8_t blk = ( mem_offset / _NT2_BLOCK_SIZE_ ) + _NT2_USER_MEMORY_;
uint8_t off = mem_offset % _NT2_BLOCK_SIZE_;
uint8_t *ptr = buffer;
if( nt2_memreg_rd( blk, off, count, ptr ) )
return NT2_BLOCK_READ_ERR;
return NT2_OK;
}
int nfctag2_memory_write( uint16_t mem_offset, uint8_t *buffer, uint16_t count )
{
uint8_t blk = ( mem_offset / _NT2_BLOCK_SIZE_ ) + _NT2_USER_MEMORY_;
uint8_t off = mem_offset % _NT2_BLOCK_SIZE_;
uint8_t *ptr = buffer;
if( nt2_memreg_wr( blk, off, count, ptr ) )
return NT2_BLOCK_WRITE_ERR;
return NT2_OK;
}
/**
* End of File
******************************************************************************/ .\Uses\nfc_tag2.h8 /**
* @file nfc_tag2.h
* @brief NFC tag 2 driver
*
* @defgroup NT2 NFC Tag 2
* @brief NFC Tag 2 click driver
*
* Driver for NFC Tag click board which caries NT3H1101 energy harvesting
* NFC Forum Type 2 Tag with field detection pin and I2C interface.
*
* @{
*
* ### Device Description ###
*
* @par
* The NTAG I2C is the first product of NXP’s NTAG family offering both
* contactless and contact interfaces. In addition to the
* passive NFC Forum compliant contactless interface, the IC features an I2C
* contact interface, which can communicate with a microcontroller if the
* NTAG I2C is powered from an external power supply. An additional externally
* powered SRAM mapped into the memory allows a fast data transfer between the
* RF and I2C interfaces and vice versa, without the write cycle limitations
* of the EEPROM memory.
*
* @par
* The NTAG I2C product features a configurable field detection pin, which
* provides a trigger to an external device depending on the activities at the
* RF interface.
*
* @par
* The NTAG I2C product can also supply power to external (low power) devices
* (e.g. a microcontroller) via the embedded energy harvesting circuitry.
*
* ### Features ###
*
* @par
* - RF interface NFC Forum Type 2 Tag compliant
* - I2C interface
* - Configurable field detection pin based on open drain implementation that
* can be triggered upon the following events:
* + RF field presence
* + First start of communication
* + Selection of the tag only
* - 64 byte SRAM buffer for fast transfer of data (Pass-through mode) between
* the RF and the I2C interfaces located outside the User Memory
* - Wake up signal at the field detect pin when:
* + New data has arrived from one interface
* + Data has been read by the receiving interface
* - Clear arbitration between RF and I2C interfaces:
* + First come, first serve strategy
* + Status flag bits to signal if one interface is busy writing to or reading
* data from the EEPROM
* - Energy harvesting functionality to power external devices
*
* ### Library Desription ###
*
* @par
* Library proveds routines for easy building of the higher layers and control
* of the NT3H1101 module.
*
* @example NFC_Tag2_ARM.c
* @example NFC_Tag2_PIC.c
* @example NFC_Tag2_PIC32.c
* @example NFC_Tag2_dsPIC.c
* @example NFC_Tag2_AVR.c
*
******************************************************************************/
#ifndef NFC_TAG2_H
#define NFC_TAG2_H
#include
#include
#include "nfc_tag2_hal.h"
/**
* @name Device Properties
*
******************************************************************************/
///@{
#define NT2_SERIAL_NUM_SIZE 7
#define NT2_STATIC_LOCK_SIZE 2
#define NT2_CAP_CONTAINER_SIZE 4
#define NT2_DYNAMIC_LOCK_SIZE 3
#define NT2_USER_CONFIG_SIZE 8
#define NT2_SRAM_SIZE 64
#define NT2_USER_MEMORY_SIZE 888
///@}
/**
* @name Return values
*
******************************************************************************/
///@{
#define NT2_OK 0
#define NT2_BLOCK_READ_ERR (-2)
#define NT2_BLOCK_WRITE_ERR (-3)
#define NT2_INVALID_MEMORY_ENTRY (-4)
///@}
/**
* @name Common Configuration Values
*
******************************************************************************/
///@{
#define NT2_I2C_RST_ON 0x80
#define NT2_I2C_RST_OFF 0x00
#define NT2_FD_OFF_MODE_0 0x00
#define NT2_FD_OFF_MODE_1 0x10
#define NT2_FD_OFF_MODE_2 0x20
#define NT2_FD_OFF_MODE_3 0x30
#define NT2_FD_ON_MODE_0 0x00
#define NT2_FD_ON_MODE_1 0x04
#define NT2_FD_ON_MODE_2 0x08
#define NT2_FD_ON_MODE_3 0x0C
#define NT2_DIRECTION_RF_TO_I2C 0x01
#define NT2_DIRECTION_I2C_TO_RF 0x00
#define NT2_I2C_CLK_STR_ON 0x01
#define NT2_I2C_CLK_STR_OFF 0x00
///@}
/**
* @name Session Configuration Masks
*
******************************************************************************/
///@{
#define NT2_MASK_NC_I2C_RST 0x80
#define NT2_MASK_NC_PTHRU 0x40
#define NT2_MASK_NC_FD_OFF 0x30
#define NT2_MASK_NC_FD_ON 0x0C
#define NT2_MASK_NC_SRAM_MIRROR 0x02
#define NT2_MASK_NC_DIRECTION_MASK 0x01
///@}
/**
* @name Session Configuration Values for NS reg
*
******************************************************************************/
///@{
#define NT2_SESSION_PTHRU_ON 0x40
#define NT2_SESSION_PTHRU_OFF 0x00
#define NT2_SESSION_SRAM_MIRROR_ON 0x20
#define NT2_SESSION_SRAM_MIRROR_OFF 0x00
///@}
/**
* @name NS Configuration Masks
*
******************************************************************************/
///@{
#define NT2_MASK_NS_I2C_LOCK_MASK 0x40
#define NT2_MASK_NS_EEPROM_MASK 0x04
///@}
/**
* @name NS Configuraton Values
*
******************************************************************************/
///@{
#define NT2_EEPROM_ERR_CLEAR 0x00
#define NT2_I2C_LOCK_ON 0x40
#define NT2_I2C_LOCK_OFF 0x00
///@}
/**
* @name NS Status Values
*
******************************************************************************/
///@{
#define NT2_STATUS_NDEF_DATA 0x80
#define NT2_STATUS_I2C_LOCK 0x40
#define NT2_STATUS_RF_LOCK 0x20
#define NT2_STATUS_SRAM_I2C_READY 0x10
#define NT2_STATUS_SRAM_RF_READY 0x08
#define NT2_STATUS_EPPROM_ERR 0x04
#define NT2_STATUS_EEPROM_BUSY 0x02
#define NT2_STATUS_RF_FIELD_DETECTED 0x01
///@}
/**
* @name Types
*
******************************************************************************/
///@{
/**
* @enum NFCTAG2_SET
* @brief NFC Tag 2 Setable Registers
*
* Enum is usable as first argument with functions :
* + @link nfctag2_get @endlink
* + @link nfctag2_get @endlink
*
* Additional infromations about @c NT2_USER_CONFIG register can be found in
* @link Configuration values @endlink part related to this module.
* For even more informations about registers and how to manipulate with them
* check the NT3H1101 datasheet.
*/
typedef enum {
NT2_SET_SERIAL_NO = 0,
NT2_SET_STATIC_LOCK = 1,
NT2_SET_CAP_CONTAINER = 2,
NT2_SET_DYNAMIC_LOCK = 3,
NT2_SET_SRAM_MEMORY = 4
}NFCTAG2_SET;
/**
* @enum NFCTAG2_CFG
* @brief NFC Tag 2 User Configuration
*
* Enum is usable as first argument with functions :
* + @link nfctag2_config_get @endlink
* + @link nfctag2_config_set @endlink
* + @link nfctag2_session_get @endlink
* + @link nfctag2_session_set @endlink
*
* Value represents the register offset inside the block for the config and
* session function calls.
*
* @note
* @c NT2_SES_NS_REG should be used only in case of config function call
* @c NT2_CFG_LOCK should be used only in case of session function call
*
* Additional infromations about function descriptions.
* For even more informations about registers and how to manipulate with them
* check the NT3H1101 datasheet.
*/
typedef enum {
NT2_CFG_NC_REG = 0,
NT2_CFG_LAST_NDEF = 1,
NT2_CFG_SRAM_MIRROR = 2,
NT2_CFG_WDT_LS = 3,
NT2_CFG_WDT_MS = 4,
NT2_CFG_STR = 5,
NT2_CFG_LOCK = 6,
NT2_SES_NS_REG = 6,
}NFCTAG2_CFG;
/**
* @struct nfctag2_click
* @brief NFC Tag 2 click
*
* @par
* Every structure member represents some memory area inside device. It is not
* necessary to use this struct inside your implementation of the higher layers.
*/
typedef struct {
/**
* @brief Device Hardware Address */
uint8_t i2c_address;
/**
* @brief Device Serial Number */
uint8_t serial_num[ NT2_SERIAL_NUM_SIZE ];
/**
* @brief Device Static Lock */
uint8_t static_lock[ NT2_STATIC_LOCK_SIZE ];
/**
* @brief Device Dynamic Lock */
uint8_t dynamic_lock[ NT2_DYNAMIC_LOCK_SIZE ];
/**
* @brief Capability Container */
uint8_t cap_containter[ NT2_CAP_CONTAINER_SIZE ];
/**
* @brief SRAM Memory */
uint8_t sram_memory[ NT2_SRAM_SIZE ];
/**
* @brief Device Configuration */
uint8_t config[ NT2_USER_CONFIG_SIZE ];
/**
* @brief Device Session Settings */
uint8_t session[ NT2_USER_CONFIG_SIZE ];
/**
* @brief Device User Memory */
uint8_t user_memory[ NT2_USER_MEMORY_SIZE ];
}nfctag2_click;
///@}
/**
* @name Functions
******************************************************************************/
///@{
#ifdef __cplusplus
extern "C"{
#endif
/**
* @brief NFC Tag 2 Initialization
*
* @retval NT2_OK - Success
* @retval NT2_INVALID_BLOCK_READ - Read Error
* @retval NT2_INVALID_BLOCK_WRITE - Write Error
*
* Initializes the library. Executes HAL initialization and assigns the provided
* I2C address to the private variable. Function must be called before any other
* function which uses device.
*/
int nfctag2_init( uint8_t i2c_address );
/**
* @brief Change Device I2C Address
*
* @param[in] uint8_t new_address
*
* @retval NT2_OK - Success
* @retval NT2_INVALID_BLOCK_READ - Read Error
* @retval NT2_INVALID_BLOCK_WRITE - Write Error
*
* Function dynamicly changes the device I2C address.
*
* @warning
* Changes are writen inside non volatile memory so device will have new address
* which can not be read because the reading of the first byte of the device
* EEPROM always returns 04.
*/
int nfctag2_i2c_addres( uint8_t new_address );
/**
* @brief Common Get Function
*
* @param[in] NFCTAG2_SET setting - setting
* @param[out] uint8_t* output - buffer
*
* @retval NT2_OK - Success
* @retval NT2_INVALID_BLOCK_READ - Read Error
* @retval NT2_INVALID_BLOCK_WRITE - Write Error
*
* @par
* Get (Read) function usable for settings defined by
* @link NFCTAG2_SET @endlink enum. Depend on the size of the registers buffer
* with the proper size should be pointed with output argument.
*/
int nfctag2_get( NFCTAG2_SET setting, uint8_t *output );
/**
* @brief Common Get Function
*
* @param[in] NFCTAG2_SET setting
* @param[out] uint8_t* output
*
* @retval NT2_OK - Success
* @retval NT2_INVALID_BLOCK_READ - Read Error
* @retval NT2_INVALID_BLOCK_WRITE - Write Error
*
* @par
* Set (Write) function usable for settings defined by
* @link NFCTAG2_SET @endlink enum. Depend on the size of the registers buffer
* with the proper size should be pointed with output argument.
*
* @bug
* After the execution of this function HW address is changed to 0x04.
*
* @todo
* Block writting of the 0 byte.
*/
int nfctag2_set( NFCTAG2_SET setting, uint8_t *input );
/**
* @brief Configuration Read Function
*
* @param[in] NFCTAG2_CFG reg
*
* @return uint8_t Content of the specific configuration register
*
* @par
* Reads the content of the specified configuration register.
*/
uint8_t nfctag2_config_get( NFCTAG2_CFG reg );
/**
* @brief Configuration Write Function
*
* @param[in] NFCTAG2_CFG reg
* @param[in] uint8_t input
*
* @retval NT2_OK - Success
* @retval NT2_INVALID_BLOCK_READ - Read Error
* @retval NT2_INVALID_BLOCK_WRITE - Write Error
*
* @par
* Writes new data to specified configuration register.
*/
int nfctag2_config_set( NFCTAG2_CFG reg, uint8_t input );
/**
* @brief Session Configuration Read Function
*
* @param[in] NFCTAG2_CFG reg
*
* @return uint8_t Content of the specific session register
*
* Reads the status of session registers. NS reg is mostly status register
* which can be used to read current status of the device.
*/
uint8_t nfctag2_session_get( NFCTAG2_CFG reg );
/**
* @brief Session Configuration Write Function
*
* @param[in] NFCTAG2_CFG reg
* @param[in] uint8_t mask
* @param[in] uint8_t value
*
* @retval NT2_OK - Success
* @retval NT2_INVALID_BLOCK_READ - Read Error
* @retval NT2_INVALID_BLOCK_WRITE - Write Error
*
* @par
* Session configuration setup. Writes data to provided seesion register. Usage
* of mask is needed in case of NC and NS reg setup.
*/
int nfctag2_session_set( NFCTAG2_CFG reg, uint8_t mask, uint8_t value );
/**
* @brief Read User Memory
*
* @param[in] uint16_t mem_offset
* @param[out] uint8_t* buffer
* @param[in] uint8_t count
*
* @retval NT2_OK - Success
* @retval NT2_INVALID_BLOCK_READ - Read Error
* @retval NT2_INVALID_BLOCK_WRITE - Write Error
*
* @par
* Reads user memory area. Buffer size must not exceed @c NT2_USER_MEMORY_SIZE,
* also ( mem_offset + count ) must not be bigger than NT2_USER_MEMORY_SIZE.
*/
int nfctag2_memory_read( uint16_t mem_offset, uint8_t *buffer, uint16_t count );
/**
* @brief Write User Memory
*
* @param[in] uint16_t mem_offset
* @param[in] uint8_t* buffer
* @param[in] uint8_t count
*
* @retval NT2_OK - Success
* @retval NT2_INVALID_BLOCK_READ - Read Error
* @retval NT2_INVALID_BLOCK_WRITE - Write Error
*
* @par
* Writes user memory area. Buffer size must not exceed @c NT2_USER_MEMORY_SIZE,
* also ( mem_offset + count ) must not be bigger than NT2_USER_MEMORY_SIZE.
*/
int nfctag2_memory_write( uint16_t mem_offset, uint8_t *buffer, uint16_t count );
#ifdef __cplusplus
} // extern "C"
#endif
#endif
///@}
/**
* @} End of File
******************************************************************************/
k
AT90CAN128 AT90CAN32 AT90CAN64 AT90USB1286 AT90USB1287
AT90USB646
AT90USB647 ATmega128
ATmega1280
ATmega1281
ATmega1284 ATmega1284P
ATmega128A ATmega16 ATmega163 ATmega164
ATmega164A
ATmega164P ATmega164PA ATmega168
ATmega168A
ATmega168P ATmega168PA ATMEGA168PB ATmega16A ATmega16HVB
ATmega16U4
ATmega2560
ATmega2561 ATmega32 ATmega323 ATmega324
ATmega324A
ATmega324P ATmega324PA ATmega328
ATmega328P ATmega32A ATmega32HVB
ATmega32U4
ATmega32U6 ATmega406 ATmega48 ATmega48A ATmega48P
ATmega48PA ATmega64 ATmega640 ATmega644
ATmega644A
ATmega644P ATmega644PA ATmega64A ATmega8
ATmega8535 ATmega88 ATmega88A ATmega88P
ATmega88PA ATmega8A ATtiny48 ATtiny88 ATxmega128A1
ATXMEGA128A1U ATxmega128A3
ATXMEGA128A3U
ATXMEGA128A4U ATXMEGA128B1 ATXMEGA128B3 ATXMEGA128C3 ATxmega128D3 ATXMEGA128D4 ATxmega16A4 ATXMEGA16A4U ATXMEGA16C4 ATxmega16D4 ATXMEGA16E5 ATxmega192A3
ATXMEGA192A3U ATXMEGA192C3 ATxmega192D3 ATxmega256A3
ATxmega256A3B ATXMEGA256A3BU
ATXMEGA256A3U ATXMEGA256C3 ATxmega256D3 ATxmega32A4 ATXMEGA32A4U ATXMEGA32C3 ATXMEGA32C4 ATXMEGA32D3 ATxmega32D4 ATXMEGA32E5 ATXMEGA384C3 ATXMEGA384D3 ATxmega64A1 ATXMEGA64A1U ATxmega64A3 ATXMEGA64A3U ATXMEGA64A4U ATXMEGA64B1 ATXMEGA64B3 ATXMEGA64C3 ATxmega64D3 ATXMEGA64D4
ATXMEGA8E5 .\Help\nfc_tag_2_doc.chm
ITSF ` " |{ "|{ "` x T
ITSP T
j].! "T PMGL / /#IDXHDRK /#ITBITS /#STRINGSG5/#SYSTEM 6(/#TOPICSKP/#URLSTRwP/#URLTBL\ /#WINDOWSgL/$FIftiMain0 /$OBJINST\?/$WWAssociativeLinks/ /$WWAssociativeLinks/PropertyX/$WWKeywordLinks/ /$WWKeywordLinks/BTree3L/$WWKeywordLinks/Data/$WWKeywordLinks/Map*/$WWKeywordLinks/Property8 $/_n_f_c__tag2__a_r_m_8c-example.html=q$/_n_f_c__tag2__a_v_r_8c-example.html.T&/_n_f_c__tag2__p_i_c32_8c-example.html8R$/_n_f_c__tag2__p_i_c_8c-example.htmlpH&/_n_f_c__tag2_ds_p_i_c_8c-example.htmln/annotated.html-7 /bc_s.png2$ /bdwn.png /bug.html9R
/classes.htmld/closed.png*/dir_255fa8f554e95a439f260058328638ab.html)*/dir_9bb0cdb2fc6936e925cffa69d90b0c80.html-*/dir_e3d620c6b6fdb93ed3bc6186215bde2e.html5[/doxygen.pngVC/doxygen_1.css[ /dynsections.jsD/e_logo.png
/examples.html/functions.html}D/functions_vars.htmlAD/group___n_t2.html
/index.hhcM
/index.hhkU5/index.html9+
/jquery.js|/mikroe.pnga
/modules.html%
/nav_f.png
/nav_h.png7b/nfc__tag2_8h_source.html
/nfc__tag2__hal_8h_source.htmlz /open.png{/pages.htmldA/structnfctag2__click.html
/sync_off.pngxU/sync_on.png+M
/tab_a.png
/tab_b.png%)
/tab_h.pngN1
/tab_s.png8 /tabs.cssq/tabs_1.cssd
/todo.html 0::DataSpace/NameList <(::DataSpace/Storage/MSCompressed/Content ^,::DataSpace/Storage/MSCompressed/ControlData j)::DataSpace/Storage/MSCompressed/SpanInfo b/::DataSpace/Storage/MSCompressed/Transform/List <&_::DataSpace/Storage/MSCompressed/Transform/{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/ i::DataSpace/Storage/MSCompressed/Transform/{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/ResetTable 0 .Jg > E U n c o m p r e s s e d M S C o m p r e s s e d { 7 F C 2 8 9 4 0 - 9 D 3 1 - 1 1 D 0 l LZXC ( l
v \' ) 0; vF [ @` &