2020-10-08 11:34 PM
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <netinet/in.h>
#include <linux/i2c-dev.h>
/* NDEF Text Record */
unsigned char NDEF_DATA[] =
{
/*NDEF Text Record*/
0x03, 0x0E, //NDEF message, 14 byte message
0xD1, //NDEF Record header: MB = 1b, ME = 1b, CF = 0b, SR = 1b, IL = 0b, TNF = 001b
0x01, 0x0A, //type length, payload length
0x54, //Type = Text
0x02, //Language code size
0x65, 0x6E, // Language = English, 'e', 'n',
/*Payload data = " EEwiki"*/
' ', 'E', 'E', 'w', 'i', 'k', 'i',
};
#define ADDRESS 0x53
int main() {
int fd;
unsigned char buffer[3];
unsigned char buffer1[2];
if ((fd = open("/dev/i2c-2", O_RDWR)) < 0) { printf("Couldn't open device! %d\n", fd); return 1; }
if (ioctl(fd, I2C_SLAVE, ADDRESS) < 0)
{
printf("Couldn't find device on address!\n"); return 1;
}
buffer[0] = 0x00;
buffer[1] = 0x00;
buffer[2] = NDEF_DATA[0];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x01;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[1];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x02;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[2];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x03;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[3];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x04;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[4];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x05;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[5];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x06;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[6];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x07;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[7];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x08;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[8];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x09;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[9];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x0A;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[10];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x0B;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[11];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x0C;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[12];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x0D;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[13];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x0E;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[14];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
buffer[0] = 0x00;
buffer[1] = 0x0F;
//buffer[2] = 0x00;
buffer[2] = NDEF_DATA[15];
if (write(fd, buffer, 3) != 3)
{
printf("ERROR HANDLING: i2c transcations failed\n");
}
else
printf("i2c write success\n");
usleep(5000);
close(fd);
return 0;
}
Solved! Go to Solution.
2020-11-02 01:28 AM
Hello,
I think that your problem is that you are missing the CCfile.
As defined in the NFC Forum specification, the first 4 bytes (or 8 bytes, depending on the tag memory size) must contains the CCFile, which indicates that the tag is an NFC Forum compatible device, and gives some information on the tag's capabilities, like memory size.
The NDEF File shall be written only after the CCFile, at Ii2C addess 0004h in your case, not at address 0000h
You can find the NFC Forum "Type 5 Tag Specification" explaining this at https://nfc-forum.org/our-work/specification-releases/specifications/nfc-forum-technical-specifications/ the (purchase needed).
You can also find explanation on how to format the tag with CCFIle in the following application note: https://www.st.com/resource/en/application_note/dm00328901-ndef-management-with-st25dvxxx-and-st25tvxxx-products-stmicroelectronics.pdf
Hope this helps.
Best regards.
2020-11-02 01:28 AM
Hello,
I think that your problem is that you are missing the CCfile.
As defined in the NFC Forum specification, the first 4 bytes (or 8 bytes, depending on the tag memory size) must contains the CCFile, which indicates that the tag is an NFC Forum compatible device, and gives some information on the tag's capabilities, like memory size.
The NDEF File shall be written only after the CCFile, at Ii2C addess 0004h in your case, not at address 0000h
You can find the NFC Forum "Type 5 Tag Specification" explaining this at https://nfc-forum.org/our-work/specification-releases/specifications/nfc-forum-technical-specifications/ the (purchase needed).
You can also find explanation on how to format the tag with CCFIle in the following application note: https://www.st.com/resource/en/application_note/dm00328901-ndef-management-with-st25dvxxx-and-st25tvxxx-products-stmicroelectronics.pdf
Hope this helps.
Best regards.