rfal_t2t.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /******************************************************************************
  2. * \attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2020 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R391x firmware
  23. * Revision:
  24. * LANGUAGE: ISO C99
  25. */
  26. /*! \file rfal_t2t.c
  27. *
  28. * \author
  29. *
  30. * \brief Provides NFC-A T2T convenience methods and definitions
  31. *
  32. * This module provides an interface to perform as a NFC-A Reader/Writer
  33. * to handle a Type 2 Tag T2T
  34. *
  35. */
  36. /*
  37. ******************************************************************************
  38. * INCLUDES
  39. ******************************************************************************
  40. */
  41. #include "rfal_t2t.h"
  42. #include "utils.h"
  43. /*
  44. ******************************************************************************
  45. * ENABLE SWITCH
  46. ******************************************************************************
  47. */
  48. #ifndef RFAL_FEATURE_T2T
  49. #define RFAL_FEATURE_T2T false /* T2T module configuration missing. Disabled by default */
  50. #endif
  51. #if RFAL_FEATURE_T2T
  52. /*
  53. ******************************************************************************
  54. * GLOBAL DEFINES
  55. ******************************************************************************
  56. */
  57. #define RFAL_FDT_POLL_READ_MAX rfalConvMsTo1fc(5U) /*!< Maximum Wait time for Read command as defined in TS T2T 1.0 table 18 */
  58. #define RFAL_FDT_POLL_WRITE_MAX rfalConvMsTo1fc(10U) /*!< Maximum Wait time for Write command as defined in TS T2T 1.0 table 18 */
  59. #define RFAL_FDT_POLL_SL_MAX rfalConvMsTo1fc(1U) /*!< Maximum Wait time for Sector Select as defined in TS T2T 1.0 table 18 */
  60. #define RFAL_T2T_ACK_NACK_LEN 1U /*!< Len of NACK in bytes (4 bits) */
  61. #define RFAL_T2T_ACK 0x0AU /*!< ACK value */
  62. #define RFAL_T2T_ACK_MASK 0x0FU /*!< ACK value */
  63. #define RFAL_T2T_SECTOR_SELECT_P1_BYTE2 0xFFU /*!< Sector Select Packet 1 byte 2 */
  64. #define RFAL_T2T_SECTOR_SELECT_P2_RFU_LEN 3U /*!< Sector Select RFU length */
  65. /*
  66. ******************************************************************************
  67. * GLOBAL TYPES
  68. ******************************************************************************
  69. */
  70. /*! NFC-A T2T command set T2T 1.0 5.1 */
  71. typedef enum
  72. {
  73. RFAL_T2T_CMD_READ = 0x30, /*!< T2T Read */
  74. RFAL_T2T_CMD_WRITE = 0xA2, /*!< T2T Write */
  75. RFAL_T2T_CMD_SECTOR_SELECT = 0xC2 /*!< T2T Sector Select */
  76. } rfalT2Tcmds;
  77. /*! NFC-A T2T READ T2T 1.0 5.2 and table 11 */
  78. typedef struct
  79. {
  80. uint8_t code; /*!< Command code */
  81. uint8_t blNo; /*!< Block number */
  82. } rfalT2TReadReq;
  83. /*! NFC-A T2T WRITE T2T 1.0 5.3 and table 12 */
  84. typedef struct
  85. {
  86. uint8_t code; /*!< Command code */
  87. uint8_t blNo; /*!< Block number */
  88. uint8_t data[RFAL_T2T_WRITE_DATA_LEN]; /*!< Data */
  89. } rfalT2TWriteReq;
  90. /*! NFC-A T2T SECTOR SELECT Packet 1 T2T 1.0 5.4 and table 13 */
  91. typedef struct
  92. {
  93. uint8_t code; /*!< Command code */
  94. uint8_t byte2; /*!< Sector Select Packet 1 byte 2 */
  95. } rfalT2TSectorSelectP1Req;
  96. /*! NFC-A T2T SECTOR SELECT Packet 2 T2T 1.0 5.4 and table 13 */
  97. typedef struct
  98. {
  99. uint8_t secNo; /*!< Block number */
  100. uint8_t rfu[RFAL_T2T_SECTOR_SELECT_P2_RFU_LEN]; /*!< Sector Select Packet RFU */
  101. } rfalT2TSectorSelectP2Req;
  102. /*
  103. ******************************************************************************
  104. * GLOBAL FUNCTIONS
  105. ******************************************************************************
  106. */
  107. ReturnCode rfalT2TPollerRead( uint8_t blockNum, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t *rcvLen )
  108. {
  109. ReturnCode ret;
  110. rfalT2TReadReq req;
  111. if( (rxBuf == NULL) || (rcvLen == NULL) )
  112. {
  113. return ERR_PARAM;
  114. }
  115. req.code = (uint8_t)RFAL_T2T_CMD_READ;
  116. req.blNo = blockNum;
  117. /* Transceive Command */
  118. ret = rfalTransceiveBlockingTxRx( (uint8_t*)&req, sizeof(rfalT2TReadReq), rxBuf, rxBufLen, rcvLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FDT_POLL_READ_MAX );
  119. /* T2T 1.0 5.2.1.7 The Reader/Writer SHALL treat a NACK in response to a READ Command as a Protocol Error */
  120. if( (ret == ERR_INCOMPLETE_BYTE) && (*rcvLen == RFAL_T2T_ACK_NACK_LEN) && ((*rxBuf & RFAL_T2T_ACK_MASK) != RFAL_T2T_ACK) )
  121. {
  122. return ERR_PROTO;
  123. }
  124. return ret;
  125. }
  126. /*******************************************************************************/
  127. ReturnCode rfalT2TPollerWrite( uint8_t blockNum, const uint8_t* wrData )
  128. {
  129. ReturnCode ret;
  130. rfalT2TWriteReq req;
  131. uint8_t res;
  132. uint16_t rxLen;
  133. req.code = (uint8_t)RFAL_T2T_CMD_WRITE;
  134. req.blNo = blockNum;
  135. ST_MEMCPY(req.data, wrData, RFAL_T2T_WRITE_DATA_LEN);
  136. /* Transceive WRITE Command */
  137. ret = rfalTransceiveBlockingTxRx( (uint8_t*)&req, sizeof(rfalT2TWriteReq), &res, sizeof(uint8_t), &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FDT_POLL_READ_MAX );
  138. /* Check for a valid ACK */
  139. if( (ret == ERR_INCOMPLETE_BYTE) || (ret == ERR_NONE) )
  140. {
  141. ret = ERR_PROTO;
  142. if( (rxLen == RFAL_T2T_ACK_NACK_LEN) && ((res & RFAL_T2T_ACK_MASK) == RFAL_T2T_ACK) )
  143. {
  144. ret = ERR_NONE;
  145. }
  146. }
  147. return ret;
  148. }
  149. /*******************************************************************************/
  150. ReturnCode rfalT2TPollerSectorSelect( uint8_t sectorNum )
  151. {
  152. rfalT2TSectorSelectP1Req p1Req;
  153. rfalT2TSectorSelectP2Req p2Req;
  154. ReturnCode ret;
  155. uint8_t res;
  156. uint16_t rxLen;
  157. /* Compute SECTOR SELECT Packet 1 */
  158. p1Req.code = (uint8_t)RFAL_T2T_CMD_SECTOR_SELECT;
  159. p1Req.byte2 = RFAL_T2T_SECTOR_SELECT_P1_BYTE2;
  160. /* Transceive SECTOR SELECT Packet 1 */
  161. ret = rfalTransceiveBlockingTxRx( (uint8_t*)&p1Req, sizeof(rfalT2TSectorSelectP1Req), &res, sizeof(uint8_t), &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FDT_POLL_SL_MAX );
  162. /* Check and report any transmission error */
  163. if( (ret != ERR_INCOMPLETE_BYTE) && (ret != ERR_NONE) )
  164. {
  165. return ret;
  166. }
  167. /* Ensure that an ACK was received */
  168. if( (ret != ERR_INCOMPLETE_BYTE) || (rxLen != RFAL_T2T_ACK_NACK_LEN) || ((res & RFAL_T2T_ACK_MASK) != RFAL_T2T_ACK) )
  169. {
  170. return ERR_PROTO;
  171. }
  172. /* Compute SECTOR SELECT Packet 2 */
  173. p2Req.secNo = sectorNum;
  174. ST_MEMSET( &p2Req.rfu, 0x00, RFAL_T2T_SECTOR_SELECT_P2_RFU_LEN );
  175. /* Transceive SECTOR SELECT Packet 2 */
  176. ret = rfalTransceiveBlockingTxRx( (uint8_t*)&p2Req, sizeof(rfalT2TSectorSelectP2Req), &res, sizeof(uint8_t), &rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FDT_POLL_SL_MAX );
  177. /* T2T 1.0 5.4.1.14 The Reader/Writer SHALL treat any response received before the end of PATT2T,SL,MAX as a Protocol Error */
  178. if( (ret == ERR_NONE) || (ret == ERR_INCOMPLETE_BYTE) )
  179. {
  180. return ERR_PROTO;
  181. }
  182. /* T2T 1.0 5.4.1.13 The Reader/Writer SHALL treat the transmission of the SECTOR SELECT Command Packet 2 as being successful when it receives no response until PATT2T,SL,MAX. */
  183. if( ret == ERR_TIMEOUT )
  184. {
  185. return ERR_NONE;
  186. }
  187. return ret;
  188. }
  189. #endif /* RFAL_FEATURE_T2T */