NT3H.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2021 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "iot_i2c.h"
  19. #include "iot_i2c_ex.h"
  20. #include "NT3H.h"
  21. #include "ndef.h"
  22. #include "nfc.h"
  23. #include "nfcForum.h"
  24. /**
  25. * @brief Defines I2C data transmission attributes.
  26. */
  27. typedef struct {
  28. /** Pointer to the buffer storing data to send */
  29. unsigned char *sendBuf;
  30. /** Length of data to send */
  31. unsigned int sendLen;
  32. /** Pointer to the buffer for storing data to receive */
  33. unsigned char *receiveBuf;
  34. /** Length of data received */
  35. unsigned int receiveLen;
  36. } WifiIotI2cData;
  37. uint8_t nfcPageBuffer[NFC_PAGE_SIZE];
  38. NT3HerrNo errNo;
  39. // due to the nature of the NT3H a timeout is required to
  40. // protectd 2 consecutive I2C access
  41. inline const uint8_t* get_last_ncf_page(void) {
  42. return nfcPageBuffer;
  43. }
  44. static bool writeTimeout( uint8_t *data, uint8_t dataSend) {
  45. uint32_t status = 0;
  46. status = IoTI2cWrite(1, (NT3H1X_SLAVE_ADDRESS<<1)|0x00, data,dataSend);
  47. if (status != 0) {
  48. printf("===== Error: I2C write status1 = 0x%x! =====\r\n", status);
  49. return 0;
  50. }
  51. usleep(300000);
  52. return 1;
  53. }
  54. static bool readTimeout(uint8_t address, uint8_t *block_data) {
  55. uint32_t status = 0;
  56. IotI2cData nt3h1101_i2c_data = {0};
  57. uint8_t buffer[1] = {address};
  58. nt3h1101_i2c_data.sendBuf = buffer;
  59. nt3h1101_i2c_data.sendLen = 1;
  60. nt3h1101_i2c_data.receiveBuf = block_data;
  61. nt3h1101_i2c_data.receiveLen = NFC_PAGE_SIZE;
  62. status = IoTI2cWriteread(1, (NT3H1X_SLAVE_ADDRESS<<1)|0x00, &nt3h1101_i2c_data);
  63. if (status != 0) {
  64. printf("===== Error: I2C write status = 0x%x! =====\r\n", status);
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. bool NT3HReadHeaderNfc(uint8_t *endRecordsPtr, uint8_t *ndefHeader) {
  70. *endRecordsPtr=0;
  71. bool ret = NT3HReadUserData(0);
  72. // read the first page to see where is the end of the Records.
  73. if (ret == true) {
  74. // if the first byte is equals to NDEF_START_BYTE there are some records
  75. // store theend of that
  76. if ((NDEF_START_BYTE == nfcPageBuffer[0]) && (NTAG_ERASED != nfcPageBuffer[2])) {
  77. *endRecordsPtr = nfcPageBuffer[1];
  78. *ndefHeader = nfcPageBuffer[2];
  79. }
  80. return true;
  81. } else {
  82. errNo = NT3HERROR_READ_HEADER;
  83. }
  84. return ret;
  85. }
  86. bool NT3HWriteHeaderNfc(uint8_t endRecordsPtr, uint8_t ndefHeader) {
  87. // read the first page to see where is the end of the Records.
  88. bool ret = NT3HReadUserData(0);
  89. if (ret == true) {
  90. nfcPageBuffer[1] = endRecordsPtr;
  91. nfcPageBuffer[2] = ndefHeader;
  92. ret = NT3HWriteUserData(0, nfcPageBuffer);
  93. if (ret == false) {
  94. errNo = NT3HERROR_WRITE_HEADER;
  95. }
  96. } else {
  97. errNo = NT3HERROR_READ_HEADER;
  98. }
  99. return ret;
  100. }
  101. bool NT3HEraseAllTag(void) {
  102. bool ret = true;
  103. uint8_t erase[NFC_PAGE_SIZE+1] = {USER_START_REG, 0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE};
  104. ret = writeTimeout(erase, sizeof(erase));
  105. if (ret == false) {
  106. errNo = NT3HERROR_ERASE_USER_MEMORY_PAGE;
  107. }
  108. return ret;
  109. }
  110. bool NT3HReaddManufactoringData(uint8_t *manuf) {
  111. return readTimeout(MANUFACTORING_DATA_REG, manuf);
  112. }
  113. bool NT3HReadConfiguration(uint8_t *configuration){
  114. return readTimeout(CONFIG_REG, configuration);
  115. }
  116. bool getSessionReg(void) {
  117. return readTimeout(SESSION_REG, nfcPageBuffer);
  118. }
  119. bool NT3HReadUserData(uint8_t page) {
  120. uint8_t reg = USER_START_REG+page;
  121. // if the requested page is out of the register exit with error
  122. if (reg > USER_END_REG) {
  123. errNo = NT3HERROR_INVALID_USER_MEMORY_PAGE;
  124. return false;
  125. }
  126. bool ret = readTimeout(reg, nfcPageBuffer);
  127. if (ret == false) {
  128. errNo = NT3HERROR_READ_USER_MEMORY_PAGE;
  129. }
  130. return ret;
  131. }
  132. void NT3H1101_Read_Userpages(uint8_t pagenumber,uint8_t *outbuf)
  133. {
  134. for (int i = 0; i < pagenumber; i++)
  135. {
  136. NT3HReadUserData(i);
  137. for (int j = 0; j < 16; j++)
  138. {
  139. outbuf[16 * i + j] = nfcPageBuffer[j];
  140. }
  141. }
  142. }
  143. bool NT3HWriteUserData(uint8_t page, const uint8_t* data) {
  144. bool ret = true;
  145. uint8_t dataSend[NFC_PAGE_SIZE +1]; // data plus register
  146. uint8_t reg = USER_START_REG+page;
  147. // if the requested page is out of the register exit with error
  148. if (reg > USER_END_REG) {
  149. errNo = NT3HERROR_INVALID_USER_MEMORY_PAGE;
  150. ret = false;
  151. goto end;
  152. }
  153. dataSend[0] = reg; // store the register
  154. memcpy(&dataSend[1], data, NFC_PAGE_SIZE);
  155. ret = writeTimeout(dataSend, sizeof(dataSend));
  156. if (ret == false) {
  157. errNo = NT3HERROR_WRITE_USER_MEMORY_PAGE;
  158. goto end;
  159. }
  160. end:
  161. return ret;
  162. }
  163. bool NT3HReadSram(void){
  164. bool ret=false;
  165. for (int i = SRAM_START_REG, j=0; i<=SRAM_END_REG; i++,j++) {
  166. ret = readTimeout(i, nfcPageBuffer);
  167. if (ret==false) {
  168. return ret;
  169. }
  170. //memcpy(&userData[offset], pageBuffer, sizeof(pageBuffer));
  171. }
  172. return ret;
  173. }
  174. void NT3HGetNxpSerialNumber(uint8_t *buffer) {
  175. uint8_t manuf[16];
  176. if (NT3HReaddManufactoringData(manuf)) {
  177. for(int i=0; i<16; i++) {
  178. buffer[i] = manuf[i];
  179. }
  180. }
  181. }