| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- /******************************************************************************
- * \attention
- *
- * <h2><center>© COPYRIGHT 2020 STMicroelectronics</center></h2>
- *
- * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * www.st.com/myliberty
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
- * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************/
- /*
- * PROJECT: ST25R391x firmware
- * Revision:
- * LANGUAGE: ISO C99
- */
- /*! \file rfal_st25tb.c
- *
- * \author Gustavo Patricio
- *
- * \brief Implementation of ST25TB interface
- *
- */
- /*
- ******************************************************************************
- * INCLUDES
- ******************************************************************************
- */
- #include "rfal_st25tb.h"
- #include "utils.h"
- /*
- ******************************************************************************
- * ENABLE SWITCH
- ******************************************************************************
- */
- #ifndef RFAL_FEATURE_ST25TB
- #define RFAL_FEATURE_ST25TB false /* ST25TB module configuration missing. Disabled by default */
- #endif
- #if RFAL_FEATURE_ST25TB
- /*
- ******************************************************************************
- * GLOBAL DEFINES
- ******************************************************************************
- */
- #define RFAL_ST25TB_CMD_LEN 1U /*!< ST25TB length of a command */
- #define RFAL_ST25TB_SLOTS 16U /*!< ST25TB number of slots */
- #define RFAL_ST25TB_SLOTNUM_MASK 0x0FU /*!< ST25TB Slot Number bit mask on SlotMarker */
- #define RFAL_ST25TB_SLOTNUM_SHIFT 4U /*!< ST25TB Slot Number shift on SlotMarker */
- #define RFAL_ST25TB_INITIATE_CMD1 0x06U /*!< ST25TB Initiate command byte1 */
- #define RFAL_ST25TB_INITIATE_CMD2 0x00U /*!< ST25TB Initiate command byte2 */
- #define RFAL_ST25TB_PCALL_CMD1 0x06U /*!< ST25TB Pcall16 command byte1 */
- #define RFAL_ST25TB_PCALL_CMD2 0x04U /*!< ST25TB Pcall16 command byte2 */
- #define RFAL_ST25TB_SELECT_CMD 0x0EU /*!< ST25TB Select command */
- #define RFAL_ST25TB_GET_UID_CMD 0x0BU /*!< ST25TB Get UID command */
- #define RFAL_ST25TB_COMPLETION_CMD 0x0FU /*!< ST25TB Completion command */
- #define RFAL_ST25TB_RESET_INV_CMD 0x0CU /*!< ST25TB Reset to Inventory command */
- #define RFAL_ST25TB_READ_BLOCK_CMD 0x08U /*!< ST25TB Read Block command */
- #define RFAL_ST25TB_WRITE_BLOCK_CMD 0x09U /*!< ST25TB Write Block command */
- #define RFAL_ST25TB_T0 2157U /*!< ST25TB t0 159 us ST25TB RF characteristics */
- #define RFAL_ST25TB_T1 2048U /*!< ST25TB t1 151 us ST25TB RF characteristics */
- #define RFAL_ST25TB_FWT (RFAL_ST25TB_T0 + RFAL_ST25TB_T1) /*!< ST25TB FWT = T0 + T1 */
- #define RFAL_ST25TB_TW rfalConvMsTo1fc(7U) /*!< ST25TB TW : Programming time for write max 7ms */
- /*
- ******************************************************************************
- * GLOBAL MACROS
- ******************************************************************************
- */
- /*
- ******************************************************************************
- * GLOBAL TYPES
- ******************************************************************************
- */
- /*! Initiate Request */
- typedef struct
- {
- uint8_t cmd1; /*!< Initiate Request cmd1: 0x06 */
- uint8_t cmd2; /*!< Initiate Request cmd2: 0x00 */
- } rfalSt25tbInitiateReq;
- /*! Pcall16 Request */
- typedef struct
- {
- uint8_t cmd1; /*!< Pcal16 Request cmd1: 0x06 */
- uint8_t cmd2; /*!< Pcal16 Request cmd2: 0x04 */
- } rfalSt25tbPcallReq;
- /*! Select Request */
- typedef struct
- {
- uint8_t cmd; /*!< Select Request cmd: 0x0E */
- uint8_t chipId; /*!< Chip ID */
- } rfalSt25tbSelectReq;
- /*! Read Block Request */
- typedef struct
- {
- uint8_t cmd; /*!< Select Request cmd: 0x08 */
- uint8_t address; /*!< Block address */
- } rfalSt25tbReadBlockReq;
- /*! Write Block Request */
- typedef struct
- {
- uint8_t cmd; /*!< Select Request cmd: 0x09 */
- uint8_t address; /*!< Block address */
- rfalSt25tbBlock data; /*!< Block Data */
- } rfalSt25tbWriteBlockReq;
- /*
- ******************************************************************************
- * LOCAL FUNCTION PROTOTYPES
- ******************************************************************************
- */
- /*!
- *****************************************************************************
- * \brief ST25TB Poller Do Collision Resolution
- *
- * This method performs ST25TB Collision resolution loop for each slot
- *
- * \param[in] devLimit : device limit value, and size st25tbDevList
- * \param[out] st25tbDevList : ST35TB listener device info
- * \param[out] devCnt : Devices found counter
- *
- * \return colPending : true if a collision was detected
- *****************************************************************************
- */
- static bool rfalSt25tbPollerDoCollisionResolution( uint8_t devLimit, rfalSt25tbListenDevice *st25tbDevList, uint8_t *devCnt );
- /*
- ******************************************************************************
- * LOCAL FUNCTION PROTOTYPES
- ******************************************************************************
- */
- static bool rfalSt25tbPollerDoCollisionResolution( uint8_t devLimit, rfalSt25tbListenDevice *st25tbDevList, uint8_t *devCnt )
- {
- uint8_t i;
- uint8_t chipId;
- ReturnCode ret;
- bool col;
- col = false;
-
- for(i = 0; i < RFAL_ST25TB_SLOTS; i++)
- {
- platformDelay(1); /* Wait t2: Answer to new request delay */
-
- if( i==0U )
- {
- /* Step 2: Send Pcall16 */
- ret = rfalSt25tbPollerPcall( &chipId );
- }
- else
- {
- /* Step 3-17: Send Pcall16 */
- ret = rfalSt25tbPollerSlotMarker( i, &chipId );
- }
-
- if( ret == ERR_NONE )
- {
- /* Found another device */
- st25tbDevList[*devCnt].chipID = chipId;
- st25tbDevList[*devCnt].isDeselected = false;
-
- /* Select Device, retrieve its UID */
- ret = rfalSt25tbPollerSelect( chipId );
- /* By Selecting this device, the previous gets Deselected */
- if( (*devCnt) > 0U )
- {
- st25tbDevList[(*devCnt)-1U].isDeselected = true;
- }
- if( ERR_NONE == ret )
- {
- rfalSt25tbPollerGetUID( &st25tbDevList[*devCnt].UID );
- }
- if( ERR_NONE == ret )
- {
- (*devCnt)++;
- }
- }
- else if( (ret == ERR_CRC) || (ret == ERR_FRAMING) )
- {
- col = true;
- }
- else
- {
- /* MISRA 15.7 - Empty else */
- }
-
- if( *devCnt >= devLimit )
- {
- break;
- }
- }
- return col;
- }
- /*
- ******************************************************************************
- * LOCAL VARIABLES
- ******************************************************************************
- */
- /*
- ******************************************************************************
- * GLOBAL FUNCTIONS
- ******************************************************************************
- */
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerInitialize( void )
- {
- return rfalNfcbPollerInitialize();
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerCheckPresence( uint8_t *chipId )
- {
- ReturnCode ret;
- uint8_t chipIdRes;
- chipIdRes = 0x00;
-
- /* Send Initiate Request */
- ret = rfalSt25tbPollerInitiate( &chipIdRes );
-
- /* Check if a transmission error was detected */
- if( (ret == ERR_CRC) || (ret == ERR_FRAMING) )
- {
- return ERR_NONE;
- }
-
- /* Copy chip ID if requested */
- if( chipId != NULL )
- {
- *chipId = chipIdRes;
- }
-
- return ret;
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerInitiate( uint8_t *chipId )
- {
- ReturnCode ret;
- uint16_t rxLen;
- rfalSt25tbInitiateReq initiateReq;
- uint8_t rxBuf[RFAL_ST25TB_CHIP_ID_LEN + RFAL_ST25TB_CRC_LEN]; /* In case we receive less data that CRC, RF layer will not remove the CRC from buffer */
-
- /* Compute Initiate Request */
- initiateReq.cmd1 = RFAL_ST25TB_INITIATE_CMD1;
- initiateReq.cmd2 = RFAL_ST25TB_INITIATE_CMD2;
-
- /* Send Initiate Request */
- ret = rfalTransceiveBlockingTxRx( (uint8_t*)&initiateReq, sizeof(rfalSt25tbInitiateReq), (uint8_t*)rxBuf, sizeof(rxBuf), &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT );
-
- /* Check for valid Select Response */
- if( (ret == ERR_NONE) && (rxLen != RFAL_ST25TB_CHIP_ID_LEN) )
- {
- return ERR_PROTO;
- }
-
- /* Copy chip ID if requested */
- if( chipId != NULL )
- {
- *chipId = *rxBuf;
- }
-
- return ret;
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerPcall( uint8_t *chipId )
- {
- ReturnCode ret;
- uint16_t rxLen;
- rfalSt25tbPcallReq pcallReq;
- /* Compute Pcal16 Request */
- pcallReq.cmd1 = RFAL_ST25TB_PCALL_CMD1;
- pcallReq.cmd2 = RFAL_ST25TB_PCALL_CMD2;
-
- /* Send Pcal16 Request */
- ret = rfalTransceiveBlockingTxRx( (uint8_t*)&pcallReq, sizeof(rfalSt25tbPcallReq), (uint8_t*)chipId, RFAL_ST25TB_CHIP_ID_LEN, &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT );
-
- /* Check for valid Select Response */
- if( (ret == ERR_NONE) && (rxLen != RFAL_ST25TB_CHIP_ID_LEN) )
- {
- return ERR_PROTO;
- }
-
- return ret;
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerSlotMarker( uint8_t slotNum, uint8_t *chipIdRes )
- {
- ReturnCode ret;
- uint16_t rxLen;
- uint8_t slotMarker;
- if( (slotNum == 0U) || (slotNum > 15U) )
- {
- return ERR_PARAM;
- }
-
- /* Compute SlotMarker */
- slotMarker = ( ((slotNum & RFAL_ST25TB_SLOTNUM_MASK) << RFAL_ST25TB_SLOTNUM_SHIFT) | RFAL_ST25TB_PCALL_CMD1 );
-
-
- /* Send SlotMarker */
- ret = rfalTransceiveBlockingTxRx( (uint8_t*)&slotMarker, RFAL_ST25TB_CMD_LEN, (uint8_t*)chipIdRes, RFAL_ST25TB_CHIP_ID_LEN, &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT );
-
- /* Check for valid ChipID Response */
- if( (ret == ERR_NONE) && (rxLen != RFAL_ST25TB_CHIP_ID_LEN) )
- {
- return ERR_PROTO;
- }
-
- return ret;
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerSelect( uint8_t chipId )
- {
- ReturnCode ret;
- uint16_t rxLen;
- rfalSt25tbSelectReq selectReq;
- uint8_t chipIdRes;
- /* Compute Select Request */
- selectReq.cmd = RFAL_ST25TB_SELECT_CMD;
- selectReq.chipId = chipId;
-
- /* Send Select Request */
- ret = rfalTransceiveBlockingTxRx( (uint8_t*)&selectReq, sizeof(rfalSt25tbSelectReq), (uint8_t*)&chipIdRes, RFAL_ST25TB_CHIP_ID_LEN, &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT );
-
- /* Check for valid Select Response */
- if( (ret == ERR_NONE) && ((rxLen != RFAL_ST25TB_CHIP_ID_LEN) || (chipIdRes != chipId)) )
- {
- return ERR_PROTO;
- }
-
- return ret;
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerGetUID( rfalSt25tbUID *UID )
- {
- ReturnCode ret;
- uint16_t rxLen;
- uint8_t getUidReq;
-
- /* Compute Get UID Request */
- getUidReq = RFAL_ST25TB_GET_UID_CMD;
-
- /* Send Select Request */
- ret = rfalTransceiveBlockingTxRx( (uint8_t*)&getUidReq, RFAL_ST25TB_CMD_LEN, (uint8_t*)UID, sizeof(rfalSt25tbUID), &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT );
-
- /* Check for valid UID Response */
- if( (ret == ERR_NONE) && (rxLen != RFAL_ST25TB_UID_LEN) )
- {
- return ERR_PROTO;
- }
-
- return ret;
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerCollisionResolution( uint8_t devLimit, rfalSt25tbListenDevice *st25tbDevList, uint8_t *devCnt )
- {
-
- uint8_t chipId;
- ReturnCode ret;
- bool detected; /* collision or device was detected */
-
- if( (st25tbDevList == NULL) || (devCnt == NULL) || (devLimit == 0U) )
- {
- return ERR_PARAM;
- }
-
- *devCnt = 0;
-
- /* Step 1: Send Initiate */
- ret = rfalSt25tbPollerInitiate( &chipId );
- if( ret == ERR_NONE )
- {
- /* If only 1 answer is detected */
- st25tbDevList[*devCnt].chipID = chipId;
- st25tbDevList[*devCnt].isDeselected = false;
-
- /* Retrieve its UID and keep it Selected*/
- ret = rfalSt25tbPollerSelect( chipId );
-
- if( ERR_NONE == ret )
- {
- ret = rfalSt25tbPollerGetUID( &st25tbDevList[*devCnt].UID );
- }
-
- if( ERR_NONE == ret )
- {
- (*devCnt)++;
- }
- }
- /* Always proceed to Pcall16 anticollision as phase differences of tags can lead to no tag recognized, even if there is one */
- if( *devCnt < devLimit )
- {
- /* Multiple device responses */
- do
- {
- detected = rfalSt25tbPollerDoCollisionResolution( devLimit, st25tbDevList, devCnt );
- }
- while( (detected == true) && (*devCnt < devLimit) );
- }
- return ERR_NONE;
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerReadBlock( uint8_t blockAddress, rfalSt25tbBlock *blockData )
- {
- ReturnCode ret;
- uint16_t rxLen;
- rfalSt25tbReadBlockReq readBlockReq;
-
- /* Compute Read Block Request */
- readBlockReq.cmd = RFAL_ST25TB_READ_BLOCK_CMD;
- readBlockReq.address = blockAddress;
-
- /* Send Read Block Request */
- ret = rfalTransceiveBlockingTxRx( (uint8_t*)&readBlockReq, sizeof(rfalSt25tbReadBlockReq), (uint8_t*)blockData, sizeof(rfalSt25tbBlock), &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT );
-
- /* Check for valid UID Response */
- if( (ret == ERR_NONE) && (rxLen != RFAL_ST25TB_BLOCK_LEN) )
- {
- return ERR_PROTO;
- }
-
- return ret;
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerWriteBlock( uint8_t blockAddress, const rfalSt25tbBlock *blockData )
- {
- ReturnCode ret;
- uint16_t rxLen;
- rfalSt25tbWriteBlockReq writeBlockReq;
- rfalSt25tbBlock tmpBlockData;
-
- /* Compute Write Block Request */
- writeBlockReq.cmd = RFAL_ST25TB_WRITE_BLOCK_CMD;
- writeBlockReq.address = blockAddress;
- ST_MEMCPY( &writeBlockReq.data, blockData, RFAL_ST25TB_BLOCK_LEN );
-
- /* Send Write Block Request */
- ret = rfalTransceiveBlockingTxRx( (uint8_t*)&writeBlockReq, sizeof(rfalSt25tbWriteBlockReq), tmpBlockData, RFAL_ST25TB_BLOCK_LEN, &rxLen, RFAL_TXRX_FLAGS_DEFAULT, (RFAL_ST25TB_FWT + RFAL_ST25TB_TW) );
-
-
- /* Check if there was any error besides timeout */
- if( ret != ERR_TIMEOUT )
- {
- /* Check if an unexpected answer was received */
- if( ret == ERR_NONE )
- {
- return ERR_PROTO;
- }
-
- /* Check whether a transmission error occurred */
- if( (ret != ERR_CRC) && (ret != ERR_FRAMING) && (ret != ERR_NOMEM) && (ret != ERR_RF_COLLISION) )
- {
- return ret;
- }
-
- /* If a transmission error occurred (maybe noise while commiting data) wait maximum programming time and verify data afterwards */
- rfalSetGT( (RFAL_ST25TB_FWT + RFAL_ST25TB_TW) );
- rfalFieldOnAndStartGT();
- }
-
- ret = rfalSt25tbPollerReadBlock(blockAddress, &tmpBlockData);
- if( ret == ERR_NONE )
- {
- if( ST_BYTECMP( &tmpBlockData, blockData, RFAL_ST25TB_BLOCK_LEN ) == 0 )
- {
- return ERR_NONE;
- }
- return ERR_PROTO;
- }
- return ret;
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerCompletion( void )
- {
- uint8_t completionReq;
- /* Compute Completion Request */
- completionReq = RFAL_ST25TB_COMPLETION_CMD;
-
- /* Send Completion Request, no response is expected */
- return rfalTransceiveBlockingTxRx( (uint8_t*)&completionReq, RFAL_ST25TB_CMD_LEN, NULL, 0, NULL, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT );
- }
- /*******************************************************************************/
- ReturnCode rfalSt25tbPollerResetToInventory( void )
- {
- uint8_t resetInvReq;
- /* Compute Completion Request */
- resetInvReq = RFAL_ST25TB_RESET_INV_CMD;
-
- /* Send Completion Request, no response is expected */
- return rfalTransceiveBlockingTxRx( (uint8_t*)&resetInvReq, RFAL_ST25TB_CMD_LEN, NULL, 0, NULL, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ST25TB_FWT );
- }
- #endif /* RFAL_FEATURE_ST25TB */
|