rfal_t1t.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "../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 \
  58. (1236U * 2U) /*!< DRD for Reads with n=9 => 1236/fc ~= 91 us T1T 1.2 4.4.2 */
  59. #define RFAL_T1T_DRD_WRITE \
  60. 36052U /*!< DRD for Write with n=281 => 36052/fc ~= 2659 us T1T 1.2 4.4.2 */
  61. #define RFAL_T1T_DRD_WRITE_E \
  62. 70996U /*!< DRD for Write/Erase with n=554 => 70996/fc ~= 5236 us T1T 1.2 4.4.2 */
  63. #define RFAL_T1T_RID_RES_HR0_VAL \
  64. 0x10U /*!< HR0 indicating NDEF support Digital 2.0 (Candidate) 11.6.2.1 */
  65. #define RFAL_T1T_RID_RES_HR0_MASK \
  66. 0xF0U /*!< HR0 most significant nibble mask */
  67. /*
  68. ******************************************************************************
  69. * GLOBAL TYPES
  70. ******************************************************************************
  71. */
  72. /*! NFC-A T1T (Topaz) RID_REQ Digital 1.1 10.6.1 & Table 49 */
  73. typedef struct {
  74. uint8_t cmd; /*!< T1T cmd: RID */
  75. uint8_t add; /*!< ADD: undefined value */
  76. uint8_t data; /*!< DATA: undefined value */
  77. uint8_t uid[RFAL_T1T_UID_LEN]; /*!< UID-echo: undefined value */
  78. } rfalT1TRidReq;
  79. /*! NFC-A T1T (Topaz) RALL_REQ T1T 1.2 Table 4 */
  80. typedef struct {
  81. uint8_t cmd; /*!< T1T cmd: RALL */
  82. uint8_t add1; /*!< ADD: 0x00 */
  83. uint8_t add0; /*!< ADD: 0x00 */
  84. uint8_t uid[RFAL_T1T_UID_LEN]; /*!< UID */
  85. } rfalT1TRallReq;
  86. /*! NFC-A T1T (Topaz) WRITE_REQ T1T 1.2 Table 4 */
  87. typedef struct {
  88. uint8_t cmd; /*!< T1T cmd: RALL */
  89. uint8_t add; /*!< ADD */
  90. uint8_t data; /*!< DAT */
  91. uint8_t uid[RFAL_T1T_UID_LEN]; /*!< UID */
  92. } rfalT1TWriteReq;
  93. /*! NFC-A T1T (Topaz) WRITE_RES T1T 1.2 Table 4 */
  94. typedef struct {
  95. uint8_t add; /*!< ADD */
  96. uint8_t data; /*!< DAT */
  97. } rfalT1TWriteRes;
  98. /*
  99. ******************************************************************************
  100. * LOCAL FUNCTION PROTOTYPES
  101. ******************************************************************************
  102. */
  103. /*
  104. ******************************************************************************
  105. * GLOBAL FUNCTIONS
  106. ******************************************************************************
  107. */
  108. ReturnCode rfalT1TPollerInitialize(void) {
  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(
  113. RFAL_GT_NONE); /* T1T should only be initialized after NFC-A mode, therefore the GT has been fulfilled */
  114. rfalSetFDTListen(
  115. RFAL_FDT_LISTEN_NFCA_POLLER); /* T1T uses NFC-A FDT Listen with n=9 Digital 1.1 10.7.2 */
  116. rfalSetFDTPoll(RFAL_FDT_POLL_NFCA_T1T_POLLER);
  117. return ERR_NONE;
  118. }
  119. /*******************************************************************************/
  120. ReturnCode rfalT1TPollerRid(rfalT1TRidRes* ridRes) {
  121. ReturnCode ret;
  122. rfalT1TRidReq ridReq;
  123. uint16_t rcvdLen;
  124. if(ridRes == NULL) {
  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(
  131. ret,
  132. rfalTransceiveBlockingTxRx(
  133. (uint8_t*)&ridReq,
  134. sizeof(rfalT1TRidReq),
  135. (uint8_t*)ridRes,
  136. sizeof(rfalT1TRidRes),
  137. &rcvdLen,
  138. RFAL_TXRX_FLAGS_DEFAULT,
  139. RFAL_T1T_DRD_READ));
  140. /* Check expected RID response length and the HR0 Digital 2.0 (Candidate) 11.6.2.1 */
  141. if((rcvdLen != sizeof(rfalT1TRidRes)) ||
  142. ((ridRes->hr0 & RFAL_T1T_RID_RES_HR0_MASK) != RFAL_T1T_RID_RES_HR0_VAL)) {
  143. return ERR_PROTO;
  144. }
  145. return ERR_NONE;
  146. }
  147. /*******************************************************************************/
  148. ReturnCode
  149. rfalT1TPollerRall(const uint8_t* uid, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* rxRcvdLen) {
  150. rfalT1TRallReq rallReq;
  151. if((rxBuf == NULL) || (uid == NULL) || (rxRcvdLen == NULL)) {
  152. return ERR_PARAM;
  153. }
  154. /* Compute RALL command and set Add to 0x00 */
  155. ST_MEMSET(&rallReq, 0x00, sizeof(rfalT1TRallReq));
  156. rallReq.cmd = (uint8_t)RFAL_T1T_CMD_RALL;
  157. ST_MEMCPY(rallReq.uid, uid, RFAL_T1T_UID_LEN);
  158. return rfalTransceiveBlockingTxRx(
  159. (uint8_t*)&rallReq,
  160. sizeof(rfalT1TRallReq),
  161. (uint8_t*)rxBuf,
  162. rxBufLen,
  163. rxRcvdLen,
  164. RFAL_TXRX_FLAGS_DEFAULT,
  165. RFAL_T1T_DRD_READ);
  166. }
  167. /*******************************************************************************/
  168. ReturnCode rfalT1TPollerWrite(const uint8_t* uid, uint8_t address, uint8_t data) {
  169. rfalT1TWriteReq writeReq;
  170. rfalT1TWriteRes writeRes;
  171. uint16_t rxRcvdLen;
  172. ReturnCode err;
  173. if(uid == NULL) {
  174. return ERR_PARAM;
  175. }
  176. writeReq.cmd = (uint8_t)RFAL_T1T_CMD_WRITE_E;
  177. writeReq.add = address;
  178. writeReq.data = data;
  179. ST_MEMCPY(writeReq.uid, uid, RFAL_T1T_UID_LEN);
  180. err = rfalTransceiveBlockingTxRx(
  181. (uint8_t*)&writeReq,
  182. sizeof(rfalT1TWriteReq),
  183. (uint8_t*)&writeRes,
  184. sizeof(rfalT1TWriteRes),
  185. &rxRcvdLen,
  186. RFAL_TXRX_FLAGS_DEFAULT,
  187. RFAL_T1T_DRD_WRITE_E);
  188. if(err == ERR_NONE) {
  189. if((writeReq.add != writeRes.add) || (writeReq.data != writeRes.data) ||
  190. (rxRcvdLen != sizeof(rfalT1TWriteRes))) {
  191. return ERR_PROTO;
  192. }
  193. }
  194. return err;
  195. }
  196. #endif /* RFAL_FEATURE_T1T */