/************************************************************************** * @file pn532_stm32f1.c * @author Yehui from Waveshare * @license BSD * * This implements the peripheral interfaces. * * Check out the links above for our tutorials and wiring diagrams * These chips use SPI communicate. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documnetation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. **************************************************************************/ // #include "stm32f1xx_hal.h" // #include "main.h" #include "pn532_hi3861.h" #include "cmsis_os2.h" #include "iot_errno.h" #include "iot_gpio.h" #include "iot_gpio_ex.h" #include "iot_i2c.h" #include "iot_i2c_ex.h" #define RESET_GPIO 14 #define WIFI_IOT_I2C_IDX_1 1 #define _I2C_ADDRESS 0x24 #define _I2C_TIMEOUT 10 #define WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA 6 #define WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL 6 #define WIFI_IOT_I2C_IDX_1 1 #define WIFI_IOT_IO_NAME_GPIO_7 7 #define WIFI_IOT_IO_NAME_GPIO_8 8 #define WIFI_IOT_IO_NAME_GPIO_0 0 #define WIFI_IOT_IO_NAME_GPIO_1 1 #define MAX30102_INIT_GPIO 7 /*************************************************************** * 函数名称: GpioInit * 说 明: GPIO初始化 * 参 数: 无 * 返 回 值: 无 ***************************************************************/ void BoardInit(void) { IoTGpioInit(RESET_GPIO); IoTGpioSetFunc(RESET_GPIO, IOT_GPIO_FUNC_GPIO_14_GPIO); IoTGpioSetDir(RESET_GPIO, IOT_GPIO_DIR_OUT); // 设置GPIO_8为输出模式 // IoTGpioInit(MAX30102_INIT_GPIO); // IoTGpioSetFunc(MAX30102_INIT_GPIO, IOT_GPIO_FUNC_GPIO_7_GPIO); // IoTGpioSetDir(MAX30102_INIT_GPIO, IOT_GPIO_DIR_IN); // 设置GPIO_7为输出模式 IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_0); IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA); // GPIO_0复用为I2C1_SDA IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_1); IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL); // GPIO_1复用为I2C1_SCL IoTI2cInit(WIFI_IOT_I2C_IDX_1, 400000); /* baudrate: 400kbps */ } /************************************************************************** * Reset and Log implements **************************************************************************/ int PN532_Reset(void) { IoTGpioSetOutputVal(RESET_GPIO, 1); osDelay(100); IoTGpioSetOutputVal(RESET_GPIO, 0); osDelay(500); IoTGpioSetOutputVal(RESET_GPIO, 1); osDelay(100); return PN532_STATUS_OK; } void PN532_Log(const char* log) { printf("%s\r\n", log); } /************************************************************************** * End: Reset and Log implements **************************************************************************/ /************************************************************************** * I2C **************************************************************************/ void I2cRead(uint8_t* data, uint16_t count) { // HAL_I2C_Master_Receive(&hi2c1, _I2C_ADDRESS, data, count, _I2C_TIMEOUT); IoTI2cRead(WIFI_IOT_I2C_IDX_1, (_I2C_ADDRESS << 1) | 0x01, data, count); } void I2cWrite(uint8_t* data, uint16_t count) { // HAL_I2C_Master_Transmit(&hi2c1, _I2C_ADDRESS, data, count, _I2C_TIMEOUT); IoTI2cWrite(WIFI_IOT_I2C_IDX_1, (_I2C_ADDRESS << 1) | 0x00, data, count); } int PN532_I2C_ReadData(uint8_t* data, uint16_t count) { uint8_t status[] = {0x00}; uint8_t frame[count + 1]; I2cRead(status, sizeof(status)); if (status[0] != PN532_I2C_READY) { return PN532_STATUS_ERROR; } I2cRead(frame, count + 1); for (uint8_t i = 0; i < count; i++) { data[i] = frame[i + 1]; } return PN532_STATUS_OK; } int PN532_I2C_WriteData(uint8_t *data, uint16_t count) { I2cWrite(data, count); return PN532_STATUS_OK; } bool PN532_I2C_WaitReady(uint32_t timeout) { uint8_t status[] = {0x00}; uint32_t tickstart = osKernelGetTickCount(); while (osKernelGetTickCount() - tickstart < timeout) { I2cRead(status, sizeof(status)); if (status[0] == PN532_I2C_READY) { return true; } else { osDelay(5); } } return false; } int PN532_I2C_Wakeup(void) { // TODO // HAL_GPIO_WritePin(PN532_REQ_GPIO_Port, PN532_REQ_Pin, GPIO_PIN_SET); osDelay(100); // HAL_GPIO_WritePin(PN532_REQ_GPIO_Port, PN532_REQ_Pin, GPIO_PIN_RESET); osDelay(100); // HAL_GPIO_WritePin(PN532_REQ_GPIO_Port, PN532_REQ_Pin, GPIO_PIN_SET); osDelay(500); return PN532_STATUS_OK; } void PN532_I2C_Init(PN532* pn532) { BoardInit(); // init the pn532 functions pn532->reset = PN532_Reset; pn532->read_data = PN532_I2C_ReadData; pn532->write_data = PN532_I2C_WriteData; pn532->wait_ready = PN532_I2C_WaitReady; pn532->wakeup = PN532_I2C_Wakeup; pn532->log = PN532_Log; // hardware wakeup pn532->wakeup(); } /************************************************************************** * End: I2C **************************************************************************/