A08_NFC_example.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology 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 "pn532_hi3861.h"
  19. #include "cmsis_os2.h"
  20. #include "ohos_init.h"
  21. #define TASK_STACK_SIZE 1024 * 8
  22. #define TASK_PRIO 25
  23. static void ExampleTask(void)
  24. {
  25. uint8_t buff[255];
  26. uint8_t uid[MIFARE_UID_MAX_LENGTH];
  27. int32_t uid_len = 0;
  28. /* USER CODE BEGIN 2 */
  29. printf("Hello!\r\n");
  30. PN532 pn532;
  31. // PN532_SPI_Init(&pn532);
  32. PN532_I2C_Init(&pn532);
  33. PN532_GetFirmwareVersion(&pn532, buff);
  34. if (PN532_GetFirmwareVersion(&pn532, buff) == PN532_STATUS_OK) {
  35. printf("Found PN532 with firmware version: %d.%d\r\n", buff[1], buff[2]);
  36. } else {
  37. return -1;
  38. }
  39. PN532_SamConfiguration(&pn532);
  40. printf("Waiting for RFID/NFC card...\r\n");
  41. while (1)
  42. {
  43. /* USER CODE END WHILE */
  44. /* USER CODE BEGIN 3 */
  45. // Check if a card is available to read
  46. uid_len = PN532_ReadPassiveTarget(&pn532, uid, PN532_MIFARE_ISO14443A, 1000);
  47. if (uid_len == PN532_STATUS_ERROR) {
  48. printf(".");
  49. } else {
  50. printf("Found card with UID: ");
  51. for (uint8_t i = 0; i < uid_len; i++) {
  52. printf("%02x ", uid[i]);
  53. }
  54. printf("\r\n");
  55. }
  56. }
  57. }
  58. static void ExampleEntry(void)
  59. {
  60. osThreadAttr_t attr;
  61. attr.name = "ExampleTask";
  62. attr.attr_bits = 0U;
  63. attr.cb_mem = NULL;
  64. attr.cb_size = 0U;
  65. attr.stack_mem = NULL;
  66. attr.stack_size = TASK_STACK_SIZE;
  67. attr.priority = TASK_PRIO;
  68. if (osThreadNew((osThreadFunc_t)ExampleTask, NULL, &attr) == NULL) {
  69. printf("Failed to create ExampleTask!\n");
  70. }
  71. }
  72. APP_FEATURE_INIT(ExampleEntry);