From eb0d631ea2dbf9df9c1dfa846003630c666818fe Mon Sep 17 00:00:00 2001 From: Stefan Ostermann Date: Sat, 24 Jun 2023 14:16:30 +0200 Subject: [PATCH] wip rfid --- README.md | 32 ++++++++++------ src/main.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3b4beb1..0550f54 100644 --- a/README.md +++ b/README.md @@ -9,23 +9,31 @@ This library only works on multi-core ESP32 chips like the ESP32-S3. It does not ## Pins +### Amplifier ``` -Amplifier: -25 -> DIN -D8 -> BCLK -D4 -> LRC +26 -> DIN +27 -> BCLK +25 -> LRC 5V -> Vin GND -> GND - -DAC Channel ESP32 = PIN 25 - -SD Card: -CS -> D1 -MOSI -> D7 -CLK -> D5 -MISO -> D6 ``` +### SD Card + +``` +CS -> 22 (D1) +MOSI -> 23 (D7) +CLK -> 18 (D5) +MISO -> 19 (D6) +``` + +## Buttons + +``` +Start / Stop -> 17 and GND +``` + + ## TODOs * Card Reader with SPI. Configure hspi, second SPI channel? Or does SD Card + RFID run at the same channel? diff --git a/src/main.cpp b/src/main.cpp index 22de49a..bc4096f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ // define pins for RFID #define CS_RFID 32 #define RST_RFID 33 +#define IRQ_RFID 34 // Audio DAC #define I2S_DOUT 26 // connect to DAC pin DIN @@ -27,6 +28,8 @@ #define BTN_START_STOP 17 // Button on D3 and GND +#define CS_SDCARD 22 + #include "globals.h" #include "WebContent.h" @@ -50,10 +53,28 @@ DirectoryNode *currentNode = NULL; boolean blockState = false; +volatile bool newRfidInt = false; +MFRC522 rfid(CS_RFID, RST_RFID); // instatiate a MFRC522 reader object. +void activateSD() { + digitalWrite(CS_SDCARD, LOW); +} + +void deactivateSD() { + digitalWrite(CS_SDCARD, HIGH); +} + +void activateRFID() { + digitalWrite(CS_RFID, LOW); +} + +void deactivateRFID() { + digitalWrite(CS_RFID, HIGH); +} + void playNextMp3() { blockState = true; @@ -168,6 +189,22 @@ void audio_eof_mp3(const char *info) { playNextMp3(); } +void readRFID() { + rfid.PICC_ReadCardSerial(); + Serial.print("Tag UID: "); + String uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + + String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]); + Serial.println(uidString); +} + +void clearInt(MFRC522 mfrc522) { + mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x7F); +} + +void IRAM_ATTR rfid_interrupt() { + newRfidInt = true; +} + void setup() { // put your setup code here, to run once: @@ -175,11 +212,50 @@ void setup() pinMode(BTN_START_STOP, INPUT_PULLUP); +/* setup the IRQ pin*/ + pinMode(IRQ_RFID, INPUT_PULLUP); + + + + // Init MFRC522 + // Init SPI bus + SPI.begin(); + rfid.PCD_Init(); + /* + * Allow the ... irq to be propagated to the IRQ pin + * For test purposes propagate the IdleIrq and loAlert + */ + rfid.PCD_WriteRegister(rfid.ComIEnReg, 0xA0); + + newRfidInt = false; //interrupt flag + + attachInterrupt(IRQ_RFID, rfid_interrupt, FALLING); + + do { //clear a spourious interrupt at start + ; + } while (!newRfidInt); + newRfidInt = false; + + + + + + //switch off RFID Reader: + //pinMode(RST_RFID, OUTPUT); + //digitalWrite(RST_RFID, LOW); + + + + // first parameter is name of access point, second is the password AsyncWiFiManager wifiManager(&server, &dns); wifiManager.autoConnect("HannaBox"); + + + deactivateRFID(); + Serial.print("Initializing SD card..."); if (!SD.begin(D1)) @@ -192,6 +268,16 @@ void setup() rootNode.buildDirectoryTree("/"); rootNode.printDirectoryTree(); + + Serial.println("RFID"); + + // clear RFID Reset + //digitalWrite(RST_RFID, HIGH); + + activateRFID(); + + + // printDirectoryTree(&rootNode, 0); // printDirectory(root, 0); @@ -227,9 +313,28 @@ void setup() server.begin(); } + + void loop() { + if (newRfidInt) { + + deactivateSD(); + activateRFID(); + + if(rfid.PICC_IsNewCardPresent()) { + readRFID(); + } + + deactivateRFID(); + activateSD(); + + clearInt(rfid); + newRfidInt = false; + stop(); + } + audio.loop(); if (buttonPressed(BTN_START_STOP)) { @@ -240,6 +345,7 @@ void loop() } } + } boolean buttonPressed(const uint8_t pin) {