Allow ARYGON APDB µC equipped (APDB2UA33) to work with libnfc. (WARNING: some stability issues remains and it works only on POSIX).

This commit is contained in:
Romuald Conty 2009-09-14 15:05:02 +00:00
parent 0977dd1c0b
commit 61760cc853
4 changed files with 63 additions and 23 deletions

View file

@ -58,6 +58,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
static byte_t abtTxBuf[BUFFER_LENGTH] = { DEV_ARYGON_PROTOCOL_TAMA, 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff"
#define SERIAL_DEFAULT_PORT_SPEED 9600
/**
* @note ARYGON-ADRA (PN531): ???,n,8,1
* @note ARYGON-ADRB (PN532): ???,n,8,1
* @note ARYGON-APDA (PN531): 9600,n,8,1
* @note ARYGON-APDB1UA33N (PN532): 115200,n,8,1
* @note ARYGON-APDB2UA33 (PN532 + ARYGON µC): 9600,n,8,1
*/
dev_info* dev_arygon_connect(const nfc_device_desc_t* device_desc)
{
uint32_t uiDevNr;
@ -70,18 +80,22 @@ dev_info* dev_arygon_connect(const nfc_device_desc_t* device_desc)
INFO("Sorry, serial auto-probing have been disabled at compile time.");
return INVALID_DEVICE_INFO;
#else
DBG("Trying to find ARYGON device on serial port: %s#",SERIAL_STRING);
DBG("Trying to find ARYGON device on serial port: %s# at %d bauds.",SERIAL_STRING, SERIAL_DEFAULT_PORT_SPEED);
// I have no idea how MAC OS X deals with multiple devices, so a quick workaround
for (uiDevNr=0; uiDevNr<MAX_DEVICES; uiDevNr++)
{
#ifdef __APPLE__
strcpy(acConnect,SERIAL_STRING);
sp = rs232_open(acConnect);
#else
sprintf(acConnect,"%s%d",SERIAL_STRING,uiDevNr);
sp = rs232_open(acConnect);
#endif /* __APPLE__ */
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) break;
sp = rs232_open(acConnect);
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT))
{
rs232_set_speed(sp, SERIAL_DEFAULT_PORT_SPEED);
break;
}
#ifdef DEBUG
if (sp == INVALID_SERIAL_PORT) DBG("Invalid serial port: %s",acConnect);
if (sp == CLAIMED_SERIAL_PORT) DBG("Serial port already claimed: %s",acConnect);
@ -91,11 +105,14 @@ dev_info* dev_arygon_connect(const nfc_device_desc_t* device_desc)
// Test if we have found a device
if (uiDevNr == MAX_DEVICES) return INVALID_DEVICE_INFO;
} else {
DBG("Connecting to: %s at %d bauds.",device_desc->port, device_desc->speed);
strcpy(acConnect,device_desc->port);
sp = rs232_open(acConnect);
if (sp == INVALID_SERIAL_PORT) ERR("Invalid serial port: %s",acConnect);
if (sp == CLAIMED_SERIAL_PORT) ERR("Serial port already claimed: %s",acConnect);
if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return INVALID_DEVICE_INFO;
rs232_set_speed(sp, device_desc->speed);
}
DBG("Successfully connected to: %s",acConnect);
@ -150,20 +167,20 @@ bool dev_arygon_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_
return false;
}
/** @todo Find why this delay are needed. Maybe in PN532 datasheet, but I (Romuald) haven't it... */
/** @note ARYGON-APDB need 20ms between sending and receiving frame. No information regarding this in ARYGON datasheet... */
/** @todo Find why this delay are needed. (in PN532 datasheet, but I (Romuald) haven't it...) */
/** @note PN532 need 20ms between sending and receiving frame. No information regarding this in ARYGON datasheet... */
#ifdef _WIN32
Sleep(20);
#else
usleep(20000);
#endif
/** @note ARYGON-APDB need 30ms more to be stable (report correctly present tag, at each try: 20ms seems to be enought for one shot...) */
/** @note PN532 need 30ms more to be stable (report correctly present tag, at each try: 20ms seems to be enought for one shot...) */
#ifdef _WIN32
Sleep(30);
#else
usleep(30000);
#endif
#endif
if (!rs232_receive((serial_port)ds,abtRxBuf,&uiRxBufLen)) {
ERR("Unable to receive data. (RX)");

View file

@ -81,19 +81,6 @@ serial_port rs232_open(const char* pcPortName)
sp->tiNew.c_oflag = 0;
sp->tiNew.c_lflag = 0;
/**
* @note ARYGON-ADRA (PN531): ???,n,8,1
* @note ARYGON-ADRB (PN532): ???,n,8,1
* @note ARYGON-APDA (PN531): 9600,n,8,1
* @note ARYGON-APDB (PN532): 115200,n,8,1
*/
/** @todo provide this settings dynamically */
#define DEBUG__TRY_ARYGON_APDB
#ifdef DEBUG__TRY_ARYGON_APDB
cfsetispeed(&(sp->tiNew), B115200);
cfsetospeed(&(sp->tiNew), B115200);
#endif
sp->tiNew.c_cc[VMIN] = 0; // block until n bytes are received
sp->tiNew.c_cc[VTIME] = 0; // block until a timer expires (n * 100 mSec.)
if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1)
@ -104,6 +91,38 @@ serial_port rs232_open(const char* pcPortName)
return sp;
}
void rs232_set_speed(const serial_port sp, const uint32_t uiPortSpeed)
{
DBG("Serial port speed requested to be set to %d bauds.", uiPortSpeed);
// Set port speed (Input and Output)
speed_t portSpeed = B9600;
switch(uiPortSpeed) {
case 9600: portSpeed = B9600;
break;
case 19200: portSpeed = B19200;
break;
case 38400: portSpeed = B38400;
break;
case 57600: portSpeed = B57600;
break;
case 115200: portSpeed = B115200;
break;
case 230400: portSpeed = B230400;
break;
case 460800: portSpeed = B460800;
break;
default:
ERR("Unable to set serial port speed to %d bauds. Speed value must be one of these constants: 9600 (default), 19200, 38400, 57600, 115200, 230400 or 460800.", uiPortSpeed);
};
const serial_port_unix* spu = (serial_port_unix*)sp;
cfsetispeed(&spu->tiNew, portSpeed);
cfsetospeed(&spu->tiNew, portSpeed);
if( tcsetattr(spu->fd, TCSADRAIN, &spu->tiNew) == -1)
{
ERR("Unable to apply new speed settings.");
}
}
void rs232_close(const serial_port sp)
{
tcsetattr(((serial_port_unix*)sp)->fd,TCSANOW,&((serial_port_unix*)sp)->tiOld);

View file

@ -48,7 +48,11 @@ typedef void* serial_port;
serial_port rs232_open(const char* pcPortName);
void rs232_close(const serial_port sp);
void rs232_set_speed(const serial_port sp, uint32_t uiPortSpeed);
bool rs232_cts(const serial_port sp);
bool rs232_receive(const serial_port sp, byte_t* pbtRx, uint32_t* puiRxLen);
bool rs232_send(const serial_port sp, const byte_t* pbtTx, const uint32_t uiTxLen);

View file

@ -80,6 +80,8 @@ typedef struct {
char* driver;
/** Port (i.e. /dev/ttyUSB2) */
char* port;
/** Port speed (i.e. 115200) */
uint32_t speed;
/** Device index for backward compatibility (used to choose one specific device in USB or PSCS devices list) */
uint32_t index;
} nfc_device_desc_t;
@ -142,8 +144,6 @@ typedef enum {
IM_ACTIVE_DEP = 0x05,
/** Passive DEP */
IM_PASSIVE_DEP = 0x06,
}init_modulation;
typedef struct {