Topic: Add support for pn532 chip
Hi, I'm trying to add support for pn532 chip to libnfc but I have some issues with the serial port communication.
I've added dev_pn532.h and dev_pn532.c to the source tree.
This is my current work-in-progress code (linux-based):
#include "defines.h"
#include <stdio.h>
#include <string.h>
#include "dev_pn532.h"
#include "rs232.h"
#include "bitutils.h"
#define BUFFER_LENGTH 256
static char buffer[BUFFER_LENGTH] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff"
typedef struct {
serial_port sp;
} dev_spec_pn532;
dev_info* dev_pn532_connect(const uint32_t uiIndex)
{
dev_info* pdi = INVALID_DEVICE_INFO;
dev_spec_pn532* pdsp;
dev_spec_pn532 dsp;
uint32_t uiDevIndex;
// Initialize the device index we are seaching for
uiDevIndex = uiIndex;
// Open the PN532 device
serial_port sp = rs232_open("/dev/ttyUSB0");
if (!sp)
{
#ifdef DEBUG
printf("Invalid serial port name\n");
#endif
return INVALID_DEVICE_INFO;
}
dsp.sp = sp;
#ifdef DEBUG
printf("Found PN532 device\n");
#endif
// Allocate memory for the device info and specification, fill it and return the info
pdsp = malloc(sizeof(dev_spec_pn532));
*pdsp = dsp;
pdi = malloc(sizeof(dev_info));
strcpy(pdi->acName,"PN532");
pdi->ct = CT_PN532;
pdi->ds = (dev_spec)pdsp;
pdi->bActive = true;
pdi->bCrc = true;
pdi->bPar = true;
pdi->ui8TxBits = 0;
return pdi;
}
void dev_pn532_disconnect(dev_info* pdi)
{
dev_spec_pn532* pdsp = (dev_spec_pn532*)pdi->ds;
rs232_close(pdsp->sp);
free(pdi->ds);
free(pdi);
}
bool dev_pn532_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen)
{
uint32_t uiPos = 0;
int ret = 0;
char buf[BUFFER_LENGTH];
dev_spec_pn532* pdsp = (dev_spec_pn532*)ds;
// Packet length = data length (len) + checksum (1) + end of stream marker (1)
buffer[3] = uiTxLen;
// Packet length checksum
buffer[4] = BUFFER_LENGTH - buffer[3];
// Copy the PN53X command into the packet buffer
memmove(buffer+5,pbtTx,uiTxLen);
// Calculate data payload checksum
buffer[uiTxLen+5] = 0;
for(uiPos=0; uiPos < uiTxLen; uiPos++)
{
buffer[uiTxLen+5] -= buffer[uiPos+5];
}
// End of stream marker
buffer[uiTxLen+6] = 0;
#ifdef DEBUG
printf("Tx: ");
print_hex((byte_t*)buffer,uiTxLen+7);
#endif
ret = rs232_send(pdsp->sp, buffer, uiTxLen+7);
if (!ret)
{
#ifdef DEBUG
printf("rs232_send failed\n");
#endif
return false;
}
ret = rs232_receive(pdsp->sp, buf, puiRxLen);
if (!ret)
{
#ifdef DEBUG
printf( "rs232_receive failed\n");
#endif
return false;
}
#ifdef DEBUG
printf("Rx: ");
print_hex((byte_t*)buf,puiRxLen);
#endif
// When the answer should be ignored, just return a succesful result
if(pbtRx == NULL || puiRxLen == NULL) return true;
// Only succeed when the result is at least 00 00 FF xx Fx Dx xx .. .. .. xx 00 (x = variable)
if(ret < 9) return false;
memcpy( pbtRx, buf + 7, *puiRxLen);
return true;
}I've connected a NXP demo board to my computer (at /dev/ttyUSB0). When I launch the nfc-list program with debug information, the result is:
Found PN532 device
Tx: 00 00 ff 04 fc d4 06 00 00 26 00
rs232_receive failed
Tx: 00 00 ff 02 fe d4 02 2a 00
rs232_receive failed
Error connecting NFC readerPlease, help me ![]()
PS: the serial port works in "loop-back mode" and the NXP demo board works too with its Windows SDK.
Last edited by zuck (2009-08-14 10:49:31)