API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
nfc_write.c

This is an example about how manage the NFC write.

/* @file main.c
* @author TST
* @version 0.1
* @date 13/12/2012
* @brief This is an example about how to write a NFC card.
*
* This example uses a NFC devices to write a NFC card.
*
*
*/
/* TSmarT header */
#include "tsmart.h"
#define TAG_ADDR 8
void vNFC(void *pvParameters){
uint8_t receive_data[100];
uint8_t write_data[16] = "123456789012345\0";
uint8_t i;
int8_t answ;
/* Typical Keys */
//uint8_t key[]={0xff,0xff,0xff,0xff,0xff,0xff};
//uint8_t key[]={0xa0,0xa1,0xa2,0xa3,0xa4,0xa5};
uint8_t key[]={0xd3,0xf7,0xd3,0xf7,0xd3,0xf7};
/* Wake up the NFC device */
TSMART_NFC_WakeUp(&tsmart_nfc, 100/portTICK_RATE_MS);
/* Configure device */
if(answ == TSMART_NFC_FAIL){
for(;;);
}
do{
/* Check NFC card */
answ = TSMART_NFC_GetId(&tsmart_nfc, &id, 100/portTICK_RATE_MS);
if(answ == TSMART_NFC_FAIL){
for(;;);
}
vTaskDelay(500/portTICK_RATE_MS);
}while(answ != 1);
/* Select NFC card */
answ = TSMART_NFC_InSelect(&tsmart_nfc, id.tg, 100/portTICK_RATE_MS);
if(answ != TSMART_NFC_PASS){
for(;;);
}
if (id.len_id == 4){//classic tags need authentication
/* Classic NFC cards need authentication */
answ = TSMART_NFC_AuthenticateB(&tsmart_nfc, id, TAG_ADDR, key, 100/portTICK_RATE_MS);
if(answ != TSMART_NFC_PASS){
for(;;);
}
/* Write 16 Bytes in card */
answ = TSMART_NFC_WriteCard16B(&tsmart_nfc, write_data, TAG_ADDR, 100/portTICK_RATE_MS);
if(answ == TSMART_NFC_FAIL){
for(;;);
}
}else{// Ultraligth tags don't need authentication
for(i=0;i<4;i++){
/* Write 4 Bytes in card */
answ = TSMART_NFC_WriteCard4B(&tsmart_nfc, &write_data[i*4], TAG_ADDR+i, 100/portTICK_RATE_MS);
if(answ == TSMART_NFC_FAIL){
for(;;);
}
}
}
/* Read a NFC card */
answ = TSMART_NFC_ReadCard(&tsmart_nfc, receive_data, TAG_ADDR, 100/portTICK_RATE_MS);
if(answ == TSMART_NFC_FAIL){
for(;;);
}
while(1);
}
/* @brief "Init()" function.
*
* This is the first thing that the user must do for using TSmarT.
* It initializes specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
if(xTaskCreate(vNFC, "NFC", 1024, NULL, 8, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}