From 61760cc85340f49633e4b36ac893847a7d26cf9f Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Mon, 14 Sep 2009 15:05:02 +0000 Subject: [PATCH] =?UTF-8?q?Allow=20ARYGON=20APDB=20=C2=B5C=20equipped=20(A?= =?UTF-8?q?PDB2UA33)=20to=20work=20with=20libnfc.=20(WARNING:=20some=20sta?= =?UTF-8?q?bility=20issues=20remains=20and=20it=20works=20only=20on=20POSI?= =?UTF-8?q?X).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dev_arygon.c | 33 +++++++++++++++++++++++++-------- src/rs232.c | 45 ++++++++++++++++++++++++++++++++------------- src/rs232.h | 4 ++++ src/types.h | 4 ++-- 4 files changed, 63 insertions(+), 23 deletions(-) diff --git a/src/dev_arygon.c b/src/dev_arygon.c index 8cf8930..e8c8846 100644 --- a/src/dev_arygon.c +++ b/src/dev_arygon.c @@ -58,6 +58,16 @@ along with this program. If not, see 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; uiDevNrport, 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)"); diff --git a/src/rs232.c b/src/rs232.c index abce517..20bab51 100644 --- a/src/rs232.c +++ b/src/rs232.c @@ -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); diff --git a/src/rs232.h b/src/rs232.h index e1c6518..690462c 100644 --- a/src/rs232.h +++ b/src/rs232.h @@ -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); diff --git a/src/types.h b/src/types.h index 45b61d1..cfea0c8 100644 --- a/src/types.h +++ b/src/types.h @@ -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 {