pn532_hi3861.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**************************************************************************
  2. * @file pn532_stm32f1.c
  3. * @author Yehui from Waveshare
  4. * @license BSD
  5. *
  6. * This implements the peripheral interfaces.
  7. *
  8. * Check out the links above for our tutorials and wiring diagrams
  9. * These chips use SPI communicate.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documnetation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. **************************************************************************/
  29. // #include "stm32f1xx_hal.h"
  30. // #include "main.h"
  31. #include "pn532_hi3861.h"
  32. #include "cmsis_os2.h"
  33. #include "iot_errno.h"
  34. #include "iot_gpio.h"
  35. #include "iot_gpio_ex.h"
  36. #include "iot_i2c.h"
  37. #include "iot_i2c_ex.h"
  38. #define RESET_GPIO 14
  39. #define WIFI_IOT_I2C_IDX_1 1
  40. #define _I2C_ADDRESS 0x24
  41. #define _I2C_TIMEOUT 10
  42. #define WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA 6
  43. #define WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL 6
  44. #define WIFI_IOT_I2C_IDX_1 1
  45. #define WIFI_IOT_IO_NAME_GPIO_7 7
  46. #define WIFI_IOT_IO_NAME_GPIO_8 8
  47. #define WIFI_IOT_IO_NAME_GPIO_0 0
  48. #define WIFI_IOT_IO_NAME_GPIO_1 1
  49. #define MAX30102_INIT_GPIO 7
  50. /***************************************************************
  51. * 函数名称: GpioInit
  52. * 说 明: GPIO初始化
  53. * 参 数: 无
  54. * 返 回 值: 无
  55. ***************************************************************/
  56. void BoardInit(void)
  57. {
  58. IoTGpioInit(RESET_GPIO);
  59. IoTGpioSetFunc(RESET_GPIO, IOT_GPIO_FUNC_GPIO_14_GPIO);
  60. IoTGpioSetDir(RESET_GPIO, IOT_GPIO_DIR_OUT); // 设置GPIO_8为输出模式
  61. // IoTGpioInit(MAX30102_INIT_GPIO);
  62. // IoTGpioSetFunc(MAX30102_INIT_GPIO, IOT_GPIO_FUNC_GPIO_7_GPIO);
  63. // IoTGpioSetDir(MAX30102_INIT_GPIO, IOT_GPIO_DIR_IN); // 设置GPIO_7为输出模式
  64. IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_0);
  65. IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA); // GPIO_0复用为I2C1_SDA
  66. IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_1);
  67. IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL); // GPIO_1复用为I2C1_SCL
  68. IoTI2cInit(WIFI_IOT_I2C_IDX_1, 400000); /* baudrate: 400kbps */
  69. }
  70. /**************************************************************************
  71. * Reset and Log implements
  72. **************************************************************************/
  73. int PN532_Reset(void) {
  74. IoTGpioSetOutputVal(RESET_GPIO, 1);
  75. osDelay(100);
  76. IoTGpioSetOutputVal(RESET_GPIO, 0);
  77. osDelay(500);
  78. IoTGpioSetOutputVal(RESET_GPIO, 1);
  79. osDelay(100);
  80. return PN532_STATUS_OK;
  81. }
  82. void PN532_Log(const char* log) {
  83. printf("%s\r\n", log);
  84. }
  85. /**************************************************************************
  86. * End: Reset and Log implements
  87. **************************************************************************/
  88. /**************************************************************************
  89. * I2C
  90. **************************************************************************/
  91. void I2cRead(uint8_t* data, uint16_t count) {
  92. // HAL_I2C_Master_Receive(&hi2c1, _I2C_ADDRESS, data, count, _I2C_TIMEOUT);
  93. IoTI2cRead(WIFI_IOT_I2C_IDX_1, (_I2C_ADDRESS << 1) | 0x01, data, count);
  94. }
  95. void I2cWrite(uint8_t* data, uint16_t count) {
  96. // HAL_I2C_Master_Transmit(&hi2c1, _I2C_ADDRESS, data, count, _I2C_TIMEOUT);
  97. IoTI2cWrite(WIFI_IOT_I2C_IDX_1, (_I2C_ADDRESS << 1) | 0x00, data, count);
  98. }
  99. int PN532_I2C_ReadData(uint8_t* data, uint16_t count) {
  100. uint8_t status[] = {0x00};
  101. uint8_t frame[count + 1];
  102. I2cRead(status, sizeof(status));
  103. if (status[0] != PN532_I2C_READY) {
  104. return PN532_STATUS_ERROR;
  105. }
  106. I2cRead(frame, count + 1);
  107. for (uint8_t i = 0; i < count; i++) {
  108. data[i] = frame[i + 1];
  109. }
  110. return PN532_STATUS_OK;
  111. }
  112. int PN532_I2C_WriteData(uint8_t *data, uint16_t count) {
  113. I2cWrite(data, count);
  114. return PN532_STATUS_OK;
  115. }
  116. bool PN532_I2C_WaitReady(uint32_t timeout) {
  117. uint8_t status[] = {0x00};
  118. uint32_t tickstart = osKernelGetTickCount();
  119. while (osKernelGetTickCount() - tickstart < timeout) {
  120. I2cRead(status, sizeof(status));
  121. if (status[0] == PN532_I2C_READY) {
  122. return true;
  123. } else {
  124. osDelay(5);
  125. }
  126. }
  127. return false;
  128. }
  129. int PN532_I2C_Wakeup(void) {
  130. // TODO
  131. // HAL_GPIO_WritePin(PN532_REQ_GPIO_Port, PN532_REQ_Pin, GPIO_PIN_SET);
  132. osDelay(100);
  133. // HAL_GPIO_WritePin(PN532_REQ_GPIO_Port, PN532_REQ_Pin, GPIO_PIN_RESET);
  134. osDelay(100);
  135. // HAL_GPIO_WritePin(PN532_REQ_GPIO_Port, PN532_REQ_Pin, GPIO_PIN_SET);
  136. osDelay(500);
  137. return PN532_STATUS_OK;
  138. }
  139. void PN532_I2C_Init(PN532* pn532) {
  140. BoardInit();
  141. // init the pn532 functions
  142. pn532->reset = PN532_Reset;
  143. pn532->read_data = PN532_I2C_ReadData;
  144. pn532->write_data = PN532_I2C_WriteData;
  145. pn532->wait_ready = PN532_I2C_WaitReady;
  146. pn532->wakeup = PN532_I2C_Wakeup;
  147. pn532->log = PN532_Log;
  148. // hardware wakeup
  149. pn532->wakeup();
  150. }
  151. /**************************************************************************
  152. * End: I2C
  153. **************************************************************************/