From 412c326c3a125d12e08dee85799dcc3e27c45067 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 4 May 2011 14:14:18 +0000 Subject: [PATCH] better fix for timed fcts --- libnfc/chips/pn53x.c | 57 +++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 10ef840..78cf632 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -859,7 +859,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, (void) pbtTxPar; (void) pbtRxPar; unsigned int i; - uint8_t sz, sz2; + uint8_t sz; // Sorry, no arbitrary parity bits support for now if (!pnd->bPar) { @@ -894,19 +894,26 @@ pn53x_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, pn53x_write_register (pnd, REG_CIU_BIT_FRAMING, 0xFF, SYMBOL_START_SEND | ((szTxBits % 8) & SYMBOL_TX_LAST_BITS)); // Recv data - pn53x_read_register (pnd, REG_CIU_FIFOLEVEL, &sz); - *pszRxBits = (sz & SYMBOL_FIFO_LEVEL) * 8; - for (i=0; i< sz; i++) { - pn53x_read_register (pnd, REG_CIU_FIFODATA, &(pbtRx[i])); + *pszRxBits = 0; + // we've to watch for coming data until we decide to timeout. + // our PN53x timer saturates after 4.8ms so this function shouldn't be used for + // responses coming very late anyway. + // Ideally we should implement a real timer here too but looping a few times is good enough. + for (i=0; i<4; i++) { + pn53x_read_register (pnd, REG_CIU_FIFOLEVEL, &sz); + if (sz > 0) + break; } - // Did we get more data meanwhile? - pn53x_read_register (pnd, REG_CIU_FIFOLEVEL, &sz2); - if (sz2 != 0) { - *pszRxBits += (sz2 & SYMBOL_FIFO_LEVEL) * 8; - for (i=0; i< sz2; i++) { - pn53x_read_register (pnd, REG_CIU_FIFODATA, &(pbtRx[i+sz])); + while (1) { + for (i=0; ibPar) { @@ -949,18 +956,24 @@ pn53x_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx pn53x_write_register (pnd, REG_CIU_BIT_FRAMING, 0xFF, SYMBOL_START_SEND); // Recv data - pn53x_read_register (pnd, REG_CIU_FIFOLEVEL, &sz); - *pszRx = (size_t)(sz); - for (i=0; i 0) + break; } - // Did we get more data meanwhile? - pn53x_read_register (pnd, REG_CIU_FIFOLEVEL, &sz2); - if (sz2 != 0) { - *pszRx += sz2 & SYMBOL_FIFO_LEVEL; - for (i=0; i< sz2; i++) { - pn53x_read_register (pnd, REG_CIU_FIFODATA, &(pbtRx[i+sz])); + while (1) { + for (i=0; i