ESP8266 上的多个从属 SPI - PN532 和 ILI9341

Multiple Slave SPI on ESP8266 - PN532 and ILI9341

我正在尝试在同一 SPI 总线上将 ESP12-E 模块与 ILI9341 显示器 320*240 和 PN532 RFID reader 连接起来。我在不同的 GPIO 上分配了 SS 引脚。

我无法与两者通信。显示器在任何条件下都能完美工作。但是一旦我与 ILI9341 通信,PN532 停止工作,即使我重新初始化它也不会响应,直到我重新启动设备。

任何帮助将不胜感激 我的代码:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <UTFT.h>
UTFT lcd(ILI9341_S5P,15,D1,D3);

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SS   (D4)
#define PN532_SCK  (D5)
#define PN532_MOSI (D7)
#define PN532_MISO (D6)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a SPI connection:
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
extern uint8_t BigFont[];

void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");
  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }

  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  lcd.InitLCD();
    lcd.setColor ( 0, 0, 0 );
    lcd.fillRect(1,1,319,239);
    lcd.setColor ( 255, 255, 255 );
    lcd.fillRect(100,100,220,140);
  lcd.setFont ( BigFont );
  lcd.print(String("Scanning"),0,0);
  // Set the max number of retry attempts to read from a card
  // This prevents us from waiting forever for a card, which is
  // the default behaviour of the PN532.
  nfc.setPassiveActivationRetries(0xFF);

  // configure board to read RFID tags
  nfc.SAMConfig();

  Serial.println("Waiting for an ISO14443A card");
}

void loop(void) {
  boolean success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength,25);

  if (success) {
    Serial.println("Found a card!");
    Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("UID Value: ");
    for (uint8_t i=0; i < uidLength; i++) 
    {
      Serial.print(" 0x");Serial.print(uid[i], HEX); 
    }
    Serial.println("");
    // Wait 1 second before continuing
    delay(1000);
  }
  else
  {
    // PN532 probably timed out waiting for a card
    //Serial.println("Timed out waiting for a card");
  }
}

我进行了大量测试并发现 - SPI 位顺序不同,因此您不能在同一个 SPI 上驱动这两个设备。

但是你可以对NFC模块使用SW-SPI (bitbanging),因为不需要快速驱动它。相反,TFT 必须很快才能具有良好的更新率。

我的设置中仍有一个问题未解决: 显示器没有显示内容,如果你有一个打开的串行终端。但是当它关​​闭时,TFT 和 NFC 都可以很好地协同工作。

这是我的代码:

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <UTFT.h>

// NFC module
#define PN532_SCK   D1
#define PN532_MOSI  D2
#define PN532_MISO  D3
#define PN532_SS    D0
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// TFT display
// HSPI defines
#define TFT_SCK     D5
#define TFT_MOSI    D7
//#define TFT_MISO    D6 (not connected)
#define TFT_CS      D8
#define TFT_DC      D4
UTFT myGLCD(ILI9341_S5P, TFT_CS, -1, TFT_DC);

// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];

// forward declaration of helper function to get UID as HEX-String
void byteToHexString(String &dataString, byte *uidBuffer, byte bufferSize, String strSeperator);

void setup() {
  Serial.begin(9600); 

  Serial.println("Initial nfc module");
  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }

  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

  // Set the max number of retry attempts to read from a card
  // This prevents us from waiting forever for a card, which is
  // the default behaviour of the PN532.
  nfc.setPassiveActivationRetries(0xFF);

  // configure board to read RFID tags
  nfc.SAMConfig();

  Serial.println("Waiting for an ISO14443A card");
  Serial.println("Initial tft display");

  myGLCD.InitLCD();
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect(1,1,319,239);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(BigFont);
  myGLCD.print(String("Scanning"),0,0);
}

void loop(void) {
  boolean success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  //success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength,25);

  if (success) {
    Serial.println("Found a card!");
    Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("UID Value: ");
    String strUID;
    // store UID as HEX-String to strUID and print it to display
    byteToHexString(strUID, uid, uidLength, "-");
    Serial.println("");
    Serial.println(strUID);
    myGLCD.print(strUID, CENTER, 50);
    myGLCD.setColor ( 255, 0, 0 );
    myGLCD.setFont ( BigFont );
    myGLCD.print(String("Scanning"),0,0);
    // Wait 1 second before continuing
    delay(1000);
  }
  else
  {
    Serial.println("Timed out or waiting for a card");
  }
}

// helper function to get UID as HEX-String
void byteToHexString(String &dataString, byte *uidBuffer, byte bufferSize, String strSeperator=":") {
  dataString = "";
  for (byte i = 0; i < bufferSize; i++) {
    if (i>0) {
      dataString += strSeperator;
      if (uidBuffer[i] < 0x10)
        dataString += String("0");
    }
    dataString += String(uidBuffer[i], HEX);
  }
  dataString.toUpperCase();
}

感谢您的帮助。 我不是 bitbanging SPI 的忠实粉丝。 我尝试了几种方法并尝试重新初始化等。 终于解决了

不知道为什么,当我读卡后重新初始化spi硬件时,两个模块都可以正常工作。

我读卡后刚加的代码:

    lcd._hw_special_init();

赞:

    success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength,0);
    if (success) {
    lcd._hw_special_init();
    //Do the rest
    }