rfal_t1t.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_t1t.c
  27. *
  28. * \author Gustavo Patricio
  29. *
  30. * \brief Provides NFC-A T1T convenience methods and definitions
  31. *
  32. * This module provides an interface to perform as a NFC-A Reader/Writer
  33. * to handle a Type 1 Tag T1T (Topaz)
  34. *
  35. */
  36. /*
  37. ******************************************************************************
  38. * INCLUDES
  39. ******************************************************************************
  40. */
  41. #include "rfal_t1t.h"
  42. #include "utils.h"
  43. /*
  44. ******************************************************************************
  45. * ENABLE SWITCH
  46. ******************************************************************************
  47. */
  48. #ifndef RFAL_FEATURE_T1T
  49. #define RFAL_FEATURE_T1T false /* T1T module configuration missing. Disabled by default */
  50. #endif
  51. #if RFAL_FEATURE_T1T
  52. /*
  53. ******************************************************************************
  54. * GLOBAL DEFINES
  55. ******************************************************************************
  56. */
  57. #define RFAL_T1T_DRD_READ (1236U*2U) /*!< DRD for Reads with n=9 => 1236/fc ~= 91 us T1T 1.2 4.4.2 */
  58. #define RFAL_T1T_DRD_WRITE 36052U /*!< DRD for Write with n=281 => 36052/fc ~= 2659 us T1T 1.2 4.4.2 */
  59. #define RFAL_T1T_DRD_WRITE_E 70996U /*!< DRD for Write/Erase with n=554 => 70996/fc ~= 5236 us T1T 1.2 4.4.2 */
  60. #define RFAL_T1T_RID_RES_HR0_VAL 0x10U /*!< HR0 indicating NDEF support Digital 2.0 (Candidate) 11.6.2.1 */
  61. #define RFAL_T1T_RID_RES_HR0_MASK 0xF0U /*!< HR0 most significant nibble mask */
  62. /*
  63. ******************************************************************************
  64. * GLOBAL TYPES
  65. ******************************************************************************
  66. */
  67. /*! NFC-A T1T (Topaz) RID_REQ Digital 1.1 10.6.1 & Table 49 */
  68. typedef struct
  69. {
  70. uint8_t cmd; /*!< T1T cmd: RID */
  71. uint8_t add; /*!< ADD: undefined value */
  72. uint8_t data; /*!< DATA: undefined value */
  73. uint8_t uid[RFAL_T1T_UID_LEN]; /*!< UID-echo: undefined value */
  74. } rfalT1TRidReq;
  75. /*! NFC-A T1T (Topaz) RALL_REQ T1T 1.2 Table 4 */
  76. typedef struct
  77. {
  78. uint8_t cmd; /*!< T1T cmd: RALL */
  79. uint8_t add1; /*!< ADD: 0x00 */
  80. uint8_t add0; /*!< ADD: 0x00 */
  81. uint8_t uid[RFAL_T1T_UID_LEN]; /*!< UID */
  82. } rfalT1TRallReq;
  83. /*! NFC-A T1T (Topaz) WRITE_REQ T1T 1.2 Table 4 */
  84. typedef struct
  85. {
  86. uint8_t cmd; /*!< T1T cmd: RALL */
  87. uint8_t add; /*!< ADD */
  88. uint8_t data; /*!< DAT */
  89. uint8_t uid[RFAL_T1T_UID_LEN]; /*!< UID */
  90. } rfalT1TWriteReq;
  91. /*! NFC-A T1T (Topaz) WRITE_RES T1T 1.2 Table 4 */
  92. typedef struct
  93. {
  94. uint8_t add; /*!< ADD */
  95. uint8_t data; /*!< DAT */
  96. } rfalT1TWriteRes;
  97. /*
  98. ******************************************************************************
  99. * LOCAL FUNCTION PROTOTYPES
  100. ******************************************************************************
  101. */
  102. /*
  103. ******************************************************************************
  104. * GLOBAL FUNCTIONS
  105. ******************************************************************************
  106. */
  107. ReturnCode rfalT1TPollerInitialize( void )
  108. {
  109. ReturnCode ret;
  110. EXIT_ON_ERR(ret, rfalSetMode( RFAL_MODE_POLL_NFCA_T1T, RFAL_BR_106, RFAL_BR_106 ) );
  111. rfalSetErrorHandling( RFAL_ERRORHANDLING_NFC );
  112. rfalSetGT( RFAL_GT_NONE ); /* T1T should only be initialized after NFC-A mode, therefore the GT has been fulfilled */
  113. rfalSetFDTListen( RFAL_FDT_LISTEN_NFCA_POLLER ); /* T1T uses NFC-A FDT Listen with n=9 Digital 1.1 10.7.2 */
  114. rfalSetFDTPoll( RFAL_FDT_POLL_NFCA_T1T_POLLER );
  115. return ERR_NONE;
  116. }
  117. /*******************************************************************************/
  118. ReturnCode rfalT1TPollerRid( rfalT1TRidRes *ridRes )
  119. {
  120. ReturnCode ret;
  121. rfalT1TRidReq ridReq;
  122. uint16_t rcvdLen;
  123. if( ridRes == NULL )
  124. {
  125. return ERR_PARAM;
  126. }
  127. /* Compute RID command and set Undefined Values to 0x00 Digital 1.1 10.6.1 */
  128. ST_MEMSET( &ridReq, 0x00, sizeof(rfalT1TRidReq) );
  129. ridReq.cmd = (uint8_t)RFAL_T1T_CMD_RID;
  130. EXIT_ON_ERR( ret, rfalTransceiveBlockingTxRx( (uint8_t*)&ridReq, sizeof(rfalT1TRidReq), (uint8_t*)ridRes, sizeof(rfalT1TRidRes), &rcvdLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_T1T_DRD_READ ) );
  131. /* Check expected RID response length and the HR0 Digital 2.0 (Candidate) 11.6.2.1 */
  132. if( (rcvdLen != sizeof(rfalT1TRidRes)) || ((ridRes->hr0 & RFAL_T1T_RID_RES_HR0_MASK) != RFAL_T1T_RID_RES_HR0_VAL) )
  133. {
  134. return ERR_PROTO;
  135. }
  136. return ERR_NONE;
  137. }
  138. /*******************************************************************************/
  139. ReturnCode rfalT1TPollerRall( const uint8_t* uid, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t *rxRcvdLen )
  140. {
  141. rfalT1TRallReq rallReq;
  142. if( (rxBuf == NULL) || (uid == NULL) || (rxRcvdLen == NULL) )
  143. {
  144. return ERR_PARAM;
  145. }
  146. /* Compute RALL command and set Add to 0x00 */
  147. ST_MEMSET( &rallReq, 0x00, sizeof(rfalT1TRallReq) );
  148. rallReq.cmd = (uint8_t)RFAL_T1T_CMD_RALL;
  149. ST_MEMCPY(rallReq.uid, uid, RFAL_T1T_UID_LEN);
  150. return rfalTransceiveBlockingTxRx( (uint8_t*)&rallReq, sizeof(rfalT1TRallReq), (uint8_t*)rxBuf, rxBufLen, rxRcvdLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_T1T_DRD_READ );
  151. }
  152. /*******************************************************************************/
  153. ReturnCode rfalT1TPollerWrite( const uint8_t* uid, uint8_t address, uint8_t data )
  154. {
  155. rfalT1TWriteReq writeReq;
  156. rfalT1TWriteRes writeRes;
  157. uint16_t rxRcvdLen;
  158. ReturnCode err;
  159. if( uid == NULL )
  160. {
  161. return ERR_PARAM;
  162. }
  163. writeReq.cmd = (uint8_t)RFAL_T1T_CMD_WRITE_E;
  164. writeReq.add = address;
  165. writeReq.data = data;
  166. ST_MEMCPY(writeReq.uid, uid, RFAL_T1T_UID_LEN);
  167. err = rfalTransceiveBlockingTxRx( (uint8_t*)&writeReq, sizeof(rfalT1TWriteReq), (uint8_t*)&writeRes, sizeof(rfalT1TWriteRes), &rxRcvdLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_T1T_DRD_WRITE_E );
  168. if( err == ERR_NONE )
  169. {
  170. if( (writeReq.add != writeRes.add) || (writeReq.data != writeRes.data) || (rxRcvdLen != sizeof(rfalT1TWriteRes)) )
  171. {
  172. return ERR_PROTO;
  173. }
  174. }
  175. return err;
  176. }
  177. #endif /* RFAL_FEATURE_T1T */