/* Nucleo-L031K6 - PN532 Custom Chip Test (Simulation 10.1) */
#include <Wire.h>
#include "Adafruit_PN532.h" // Your manually added library files
// Define pins for IRQ and RST for Adafruit_PN532 constructor
// Use Arduino Dx numbers corresponding to Nucleo pins from diagram.json
#define PN532_IRQ (D9) // Example, matches diagram.json
#define PN532_RST (D8) // Example, matches diagram.json
Adafruit_PN532 nfc(PN532_IRQ, PN532_RST);
// Mapping Table (same as before)
struct Mapping { uint8_t uid[4]; const char* file; };
Mapping table[] = {
{{0xDE,0xAD,0xBE,0xEF}, "audio1.wav"},
{{0xCA,0xFE,0xBA,0xBE}, "audio2.wav"}
};
const int tableSize = sizeof(table) / sizeof(table[0]);
int currentTableIndex = 0; // For cycling through simulated UIDs
void setup() {
Serial.begin(115200);
Wire.begin();
Serial.println("Scanning I2C bus...");
byte error, address;
int nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) Serial.print("0");
Serial.println(address,HEX);
nDevices++;
}
}
if (nDevices == 0) Serial.println("No I2C devices found by scanner.");
else Serial.println("Scanner finished.");
// Now proceed with your PN532 specific code
Serial.println(F("\nNucleo PN532 Custom Chip Test - Sim 10.1"));
Serial.println(F("Initializing PN532 over I2C..."));
Wire.begin(); // Initialize default I2C (e.g., on D14/D15 for Nucleo)
nfc.begin(); // Initialize PN532 library
uint32_t versiondata = nfc.getFirmwareVersion();
Serial.print(F("PN532 Firmware Version data raw: 0x")); Serial.println(versiondata, HEX);
if (!versiondata) {
Serial.println(F(" ERROR: Didn't find PN532 board (Custom Chip)."));
Serial.println(F(" Check I2C wiring in diagram.json (Nucleo D14/SDA, D15/SCL)."));
Serial.println(F(" Check custom chip's .chip.c I2C slave logic & printf in browser console."));
while (1);
}
Serial.print(F(" Found chip PN5")); Serial.println((versiondata >> 24) & 0xFF, HEX);
Serial.print(F(" Firmware ver. ")); Serial.print((versiondata >> 16) & 0xFF, DEC);
Serial.print(F('.')); Serial.println((versiondata >> 8) & 0xFF, DEC);
nfc.SAMConfig();
Serial.println(F("PN532 Configured. Loop will simulate UID lookup."));
}
void loop() {
Serial.println(F("\n--- Simulating Tag Read & Lookup ---"));
// --- SIMULATED UID INJECTION ---
// In a real scenario with working custom chip I2C read for tags,
// you would call nfc.readPassiveTargetID() here.
// For now, we directly use a UID from the table.
uint8_t simulated_uid[4];
memcpy(simulated_uid, table[currentTableIndex].uid, 4);
uint8_t simulated_uid_length = 4;
Serial.print(F("Injected Simulated UID: "));
for (int i = 0; i < simulated_uid_length; i++) { Serial.printf("%02X ", simulated_uid[i]); }
Serial.println();
// --- LOOK UP filename in table ---
const char* fname = "UNKNOWN";
for (int i = 0; i < tableSize; ++i) {
if (simulated_uid_length == 4 && memcmp(table[i].uid, simulated_uid, 4) == 0) {
fname = table[i].file;
break;
}
}
Serial.print(F(" Mapped to: "));
Serial.println(fname);
currentTableIndex = (currentTableIndex + 1) % tableSize; // Cycle to next UID
delay(3000);
}