From 58fe96f8b7c8244cc55c7e56e424bc83a2675ae9 Mon Sep 17 00:00:00 2001 From: uvos Date: Mon, 21 Mar 2022 21:21:50 +0100 Subject: [PATCH] add support for mfrc proxyies --- CMakeLists.txt | 13 ++- defines.h | 10 ++ main.cpp | 10 +- mfrc522.cpp | 85 +++++++-------- mfrc522.h | 57 +++++----- mfrcproxy.cpp | 52 +++++++++ mfrcproxy.h | 35 ++++++ nfcbord.cpp | 278 ++++++++++++++++++++++++++++++++++++----------- nfcbord.h | 70 ++++++++++-- placementnew.cpp | 11 ++ placementnew.h | 5 + shiftreg.h | 2 +- uid.h | 24 ++++ watchdog.h | 18 +++ 14 files changed, 518 insertions(+), 152 deletions(-) create mode 100644 defines.h create mode 100644 mfrcproxy.cpp create mode 100644 mfrcproxy.h create mode 100644 placementnew.cpp create mode 100644 placementnew.h create mode 100644 uid.h create mode 100644 watchdog.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 517218d..1f1167f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,18 @@ set(PORT_SPEED "57600" CACHE STRING "Serial Port Speed") set(PROGRAMMER "stk500v1" CACHE STRING "Programmer Type") set(COMPILE_FLAGS "" CACHE STRING "Additional Compiler Flags") -set(SRC_FILES main.cpp serial.cpp train.cpp item.cpp turnout.cpp signal.cpp softspim.cpp nfcbord.cpp mfrc522.cpp) +set(SRC_FILES + main.cpp + serial.cpp + train.cpp + item.cpp + turnout.cpp + signal.cpp + softspim.cpp + nfcbord.cpp + mfrc522.cpp + placementnew.cpp + mfrcproxy.cpp) # Compiler suite specification set(CMAKE_C_COMPILER /usr/bin/avr-gcc) diff --git a/defines.h b/defines.h new file mode 100644 index 0000000..60717ca --- /dev/null +++ b/defines.h @@ -0,0 +1,10 @@ +#pragma once + +static constexpr int COMMAND_BUFFER_SIZE = 64; +static constexpr int SNPRINTF_BUFFER_SIZE = 128; +static constexpr int EPPROM_SIZE = 1024; + +static constexpr int EEPROM_RESERVE = 32; +static constexpr int BLOCK = 4; + +static constexpr int NFC_PORTS = 8; diff --git a/main.cpp b/main.cpp index 65da1e7..cd86948 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "serial.h" #include "writepin.h" #include "train.h" @@ -144,8 +145,13 @@ inline static void printHelp(Serial* serial) turnout [nn] delete : Delete Turnout\n\ signal add [address] [subaddress] [type] : Add a signal\n\ signal list : List signal\n\ - signal [nn] set [status] : Set signal direction\n\ - signal [nn] delete : Delete signal\n\ + signal [nn] set [status] : Set signal status\n\ + nfc probe : Detect newly added nfc reader\n\ + nfc list : List nfc readers\n\ + nfc debug : Enable nfc debug output\n\ + nfc quiet : Disable nfc debug output\n\ + nfc enable : Enable tag detection\n\ + nfc disable : Disable tag detection\n\ stop : stop all trains\n\ power off : power off the rail\n\ power on : power on the rail\n\ diff --git a/mfrc522.cpp b/mfrc522.cpp index e26a4a6..fb80011 100644 --- a/mfrc522.cpp +++ b/mfrc522.cpp @@ -61,14 +61,22 @@ void* _userData; Mfrc522::Mfrc522(SpiMaster* spi, ShiftReg* csReg, uint8_t csPin): _csReg(csReg), _spi(spi), _csPin(csPin) +{ + reset(); +} + +void Mfrc522::reset() { write(CommandReg, SOFTRESET); _delay_ms(100); + mode = MODE_IDLE; + irqDetect = false; + write(TModeReg, 0x80); - write(TPrescalerReg, 0xA9); - write(TReloadRegH, 0x0B); - write(TReloadRegL, 0xB8); + write(TPrescalerReg, PRESCALLER); + write(TReloadRegH, POLL_TIMER >> 8); + write(TReloadRegL, POLL_TIMER & 0x0F); write(ModWidthReg, 0x26); write(RFCfgReg, 0b111 << 4); //set gain to 48dB @@ -100,15 +108,6 @@ uint8_t Mfrc522::calculateCrc(uint8_t *data, uint8_t length, uint16_t *result) return ERR; write(CommandReg, IDLE); - if(serial) - { - serial->write_p(PSTR("Calculated CRC ")); - serial->write((int)read(CRCResultRegL)); - serial->putChar(' '); - serial->write((int)read(CRCResultRegH)); - serial->putChar('\n'); - } - *result = read(CRCResultRegL); *result |= read(CRCResultRegH) << 8; return 0; @@ -165,6 +164,13 @@ void Mfrc522::transceiveAsyncFinish(uint8_t irq) { write(ComIEnReg, 0); // disable irqs + if(irq & 1) + { + mode = MODE_IDLE; + _transceiveCb(TIMEOUT, this, nullptr, 0, _transceiveUserData); + return; + } + if(!_transceiveCb) { mode = MODE_IDLE; @@ -179,13 +185,6 @@ void Mfrc522::transceiveAsyncFinish(uint8_t irq) _transceiveCb(ERR, this, nullptr, 0, _transceiveUserData); return; } - - if(irq & 1) - { - mode = MODE_IDLE; - _transceiveCb(TIMEOUT, this, nullptr, 0, _transceiveUserData); - return; - } uint8_t fifoBites = read(FIFOLevelReg); @@ -408,17 +407,6 @@ uint8_t Mfrc522::selectTag(Uid *uid) uint8_t rxAlign = txLastBits; // RxAlign = BitFramingReg[6..4]. TxLastBits = BitFramingReg[2..0] write(BitFramingReg, (rxAlign << 4) + txLastBits); - - if(serial) - { - serial->write_p(PSTR("entering transceive ")); - serial->write((int)responseLength); - serial->putChar(' '); - serial->write((int)txLastBits); - serial->putChar(' '); - serial->write((int)currentLevelKnownBits); - serial->putChar('\n'); - } // Transmit the buffer and receive the response. result = transceive(reponseBuffer, bufferUsed, responseBufferPtr, &responseLength, txLastBits, rxAlign, &txLastBits); @@ -437,8 +425,6 @@ uint8_t Mfrc522::selectTag(Uid *uid) } if (collisionPos <= currentLevelKnownBits) // No progress - should not happen { - if(serial) - serial->write_p(PSTR("err No progress\n")); return ERR; } // Choose the PICC with the bit set. @@ -530,7 +516,7 @@ uint8_t Mfrc522::getUid(Uid *uid) for(uint8_t i = 0; i < uid->size; ++i) { - uid->uidByte[i] = reponseBuffer[i]; + uid->uidByte[i] = reponseBuffer[i+1]; } return 0; @@ -554,8 +540,20 @@ void Mfrc522::irq() transceiveAsyncFinish(irqs); } } + else if(mode == MODE_TIMEOUT) + { + write(ComIrqReg, 0b01111111); // clear irqs + write(TReloadRegH, POLL_TIMER >> 8); + write(TReloadRegL, POLL_TIMER & 0x0F); + mode = MODE_IDLE; + if(_transceiveCb) + _transceiveCb(TIMEOUT, this, nullptr, 0, _transceiveUserData); + } else if(serial) + { + write(ComIrqReg, 0b01111111); serial->write("IRQ wrong mode\n"); + } } void Mfrc522::setRf(bool on) @@ -589,30 +587,30 @@ bool Mfrc522::probe(SpiMaster* spi, ShiftReg* csReg, uint8_t csPin) return version == 0x91 || version == 0x92; } -bool Mfrc522::testFifo() +bool Mfrc522::testFifo(Serial* serial) { uint8_t buffer[8] = {42, 43, 44, 45, 46, 47, 48, 49}; write(FIFOLevelReg, 1 << 7); // Flush fifo Buffer; write(FIFODataReg, buffer, sizeof(buffer)); // Fill fifo - if(serial) - serial->write_p(PSTR("Fifo buffer contains: ")); + serial->write_p(PSTR("Fifo buffer contains: ")); uint8_t len = read(FIFOLevelReg); + if(len > 8) + { + serial->write_p(PSTR("FIFOLevelReg to long\n")); + return false; + } read(FIFODataReg, reponseBuffer, len); bool ret = true; for(uint8_t i = 0; i < len; ++i) { if(buffer[i] != reponseBuffer[i]) ret = false; - if(serial) - { - serial->write((int)reponseBuffer[i]); - serial->putChar(' '); - } + serial->write((int)reponseBuffer[i]); + serial->putChar(' '); } - if(serial) - serial->putChar('\n'); + serial->putChar('\n'); return ret; } @@ -639,6 +637,7 @@ void Mfrc522::stopAsync() write(FIFOLevelReg, 1 << 7); mode = MODE_IDLE; irqDetect = false; + write(ComIrqReg, 0b01111111); } void Mfrc522::detectAsyncCb(uint8_t ret, Mfrc522* reader, uint8_t* response, uint8_t responseLen, void* userData) diff --git a/mfrc522.h b/mfrc522.h index a4bdc70..61eb457 100644 --- a/mfrc522.h +++ b/mfrc522.h @@ -3,10 +3,19 @@ #include "softspim.h" #include "defines.h" #include "serial.h" +#include "uid.h" class Mfrc522 { public: + + static constexpr uint32_t POLL_OUT_US = 15000; + static constexpr uint8_t PRESCALLER = 169; + static constexpr uint16_t POLL_TIMER = (POLL_OUT_US*1.0E-6)/((2*PRESCALLER+1)/13.56E6); + + static constexpr uint32_t TIMEOUT_US = 1000000; + static constexpr uint16_t TIMEOUT_TIMER = (TIMEOUT_US*1.0E-6)/((2*169.0+1)/13.56E6); + // Error codes. static constexpr uint8_t OK = 0; // Everything A-OK. static constexpr uint8_t NOTAGERR = 1; // No tag error @@ -133,32 +142,10 @@ public: // The PICC_CMD_MF_READ and PICC_CMD_MF_WRITE can also be used for MIFARE Ultralight. static constexpr uint8_t PICC_CMD_UL_WRITE = 0xA2; // Writes one 4 byte page to the PICC - class Uid - { - public: - uint8_t size; // Number of bytes in the UID. 4, 7 or 10. - uint8_t uidByte[10]; - uint8_t sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection. - bool operator==(Uid& in) - { - if(size != in.size) - return false; - for(uint8_t i = 0; i < size; ++i) - { - if(uidByte[i] != in.uidByte[i]) - return false; - } - return true; - } - bool operator!=(Uid& in) - { - return !operator==(in); - } - }; - // Modes static constexpr uint8_t MODE_IDLE = 0; static constexpr uint8_t MODE_TRANSCEIVE = 1; + static constexpr uint8_t MODE_TIMEOUT = 2; private: ShiftReg* _csReg; @@ -207,6 +194,8 @@ public: uint8_t selectTag(Uid *uid); uint8_t getUid(Uid *uid); + + void reset(); void irq(); @@ -218,17 +207,21 @@ public: bool cardPresent(); - bool testFifo(); + bool testFifo(Serial* serial); - bool checkIrq(uint8_t *irq) + bool startTimeout(void (transceiveCb)(uint8_t, Mfrc522*, uint8_t*, uint8_t, void*), void* userData) { - write(ComIrqReg, 0b01111111); - if(read(Status1Reg) & (1 << 4)) - { - *irq = read(ComIrqReg); - return true; - } - return false; + _transceiveCb = transceiveCb; + _transceiveUserData = userData; + if(mode != MODE_IDLE) + return false; + mode = MODE_TIMEOUT; + write(TReloadRegH, TIMEOUT_TIMER >> 8); + write(TReloadRegL, TIMEOUT_TIMER & 0x0F); + write(ComIrqReg, 0b01111111); // clear irqs + write(ComIEnReg, (1 << 0)); + updateBit(ControlReg, 6, true); + return true; } static bool probe(SpiMaster* spi, ShiftReg* csReg, uint8_t csPin); diff --git a/mfrcproxy.cpp b/mfrcproxy.cpp new file mode 100644 index 0000000..f1fb502 --- /dev/null +++ b/mfrcproxy.cpp @@ -0,0 +1,52 @@ +#include "mfrcproxy.h" + +MfrcProxy::MfrcProxy(SpiMaster* spi, ShiftReg* csReg, uint8_t csPin, void (detectCb)(MfrcProxy* proxy, Uid uid, void* data), void* userData): +_csReg(csReg), _spi(spi), _detectCb(detectCb), _userData(userData), _csPin(csPin) +{ + +} + +uint8_t MfrcProxy::read(uint8_t addr) +{ + _csReg->setBit(_csPin, false); + _delay_us(5); + _spi->readWrite(addr); + _delay_us(5); + uint8_t res = _spi->readWrite(); + _csReg->setBit(_csPin, true); + return res; +} + +void MfrcProxy::irq() +{ + if(read(REG_IRQ_BITS)) + { + uint8_t len = read(REG_UID_LEN); + if(len > 0 && len < 11) + { + Uid uid; + for(uint8_t i = 0; i < len; ++i) + { + uid.uidByte[i] = read(REG_UID_START_REG+i); + } + uid.size = len; + _detectCb(this, uid, _userData); + } + } +} + +bool MfrcProxy::probe(SpiMaster* spi, ShiftReg* csReg, uint8_t csPin) +{ + csReg->setBit(csPin, false); + _delay_us(5); + spi->readWrite(REG_CHIP_ID); + _delay_us(5); + uint8_t version = spi->readWrite(); + csReg->setBit(csPin, true); + return version == 0x87; +} + +uint8_t MfrcProxy::getId() +{ + return read(REG_ID); +} diff --git a/mfrcproxy.h b/mfrcproxy.h new file mode 100644 index 0000000..1cfd1b5 --- /dev/null +++ b/mfrcproxy.h @@ -0,0 +1,35 @@ +#pragma once +#include + +#include "shiftreg.h" +#include "softspim.h" +#include "defines.h" +#include "serial.h" +#include "uid.h" + +class MfrcProxy +{ +public: + static constexpr uint8_t REG_CHIP_ID = 0xEE; + static constexpr uint8_t REG_ID = 0x01; + static constexpr uint8_t REG_IRQ_BITS = 0x02; + static constexpr uint8_t REG_UID_LEN = 0x04; + static constexpr uint8_t REG_UID_START_REG = 0x10; + +private: + ShiftReg* _csReg; + SpiMaster* _spi; + + void (*_detectCb)(MfrcProxy* proxy, Uid uid, void* data); + void* _userData; + + uint8_t _csPin; + + uint8_t read(uint8_t addr); + +public: + MfrcProxy(SpiMaster* spi, ShiftReg* csReg, uint8_t csPin, void (detectCb)(MfrcProxy* proxy, Uid uid, void* data), void* userData = nullptr); + void irq(); + static bool probe(SpiMaster* spi, ShiftReg* csReg, uint8_t csPin); + uint8_t getId(); +}; diff --git a/nfcbord.cpp b/nfcbord.cpp index 3b2a871..8b08d6b 100644 --- a/nfcbord.cpp +++ b/nfcbord.cpp @@ -4,27 +4,78 @@ #include #include #include +#include #include #include "writepin.h" +#include "watchdog.h" -NfcBoard nfcBoard; +NfcBoard nfcBoard(Serial::getInstance()); extern char buffer[SNPRINTF_BUFFER_SIZE]; -ISR(PCINT1_vect, ISR_NOBLOCK) +ISR(WDT_vect) { + for(uint8_t i = 0; i < nfcBoard.watchDogBits.count(); ++i) + { + if(nfcBoard.watchDogBits[i] == 0) + nfcBoard.watchDogBits[i] = 2; + else + nfcBoard.watchDogBits[i] = 0; + } +} + +NfcBoard::NfcBoard(Serial* serialIn): +serial(serialIn), +csReg(&PORTC, PC1, PC2, PC0), +irqReg(&PORTC, &PINB, PC5, PB1, PC4) +{ + DDRC |= (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC4) | (1 << PC5); + DDRB |= (1 << PB4) | (1 << PB3); + csReg.clear(true); + irqReg.read(); + probe(); + setEnabled(true); +} + +void NfcBoard::poll(Serial* serial) +{ + for(uint8_t i = 0; i < watchDogBits.count(); ++i) + { + if(watchDogBits[i] == 2 && readers[i].type == TYPE_MFRC522) + { + if(serial) + { + serial->write_p(PSTR("Warning reader watchdog timeout for reader ")); + serial->write((int)i); + serial->putChar('\n'); + } + readers[i].device.mfrc522.reset(); + if(enabled_) + readers[i].device.mfrc522.detectAsync(detectCb, this); + watchDogBits[i] = 1; + } + } + if(readPin(&PINC, PC3)) { - uint8_t* data = nfcBoard.irqReg.read(); + uint8_t* data = irqReg.read(); for(uint8_t i = 0; i < NFC_PORTS; ++i) { if(*data & (1 << i)) { - for(uint8_t j = 0; j < nfcBoard.readers.count(); ++j) + for(uint8_t j = 0; j < readers.count(); ++j) { - if(nfcBoard.irqPins[j] == i) + if(irqPins[j] == i) { - nfcBoard.readers[j].irq(); + if(readers[j].type == TYPE_MFRC522) + { + readers[j].device.mfrc522.irq(); + watchDogBits[j] = 1; + } + else + { + readers[j].device.proxy.irq(); + } } } } @@ -32,17 +83,27 @@ ISR(PCINT1_vect, ISR_NOBLOCK) } } -NfcBoard::NfcBoard(): -csReg(&PORTC, PC1, PC2, PC0), -irqReg(&PORTC, &PINB, PC5, PB1, PC4) +void NfcBoard::setEnabled(bool enabled) { - DDRC |= (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC4) | (1 << PC5); - DDRB |= (1 << PB4) | (1 << PB3); - PCMSK1 |= 1 << PCINT11; - PCICR |= 1 << PCIE1; - csReg.clear(true); - irqReg.read(); - probe(); + if(enabled != enabled_) + { + enabled_ = enabled; + for(uint8_t i = 0; i < readers.count(); ++i) + { + if(readers[i].type == TYPE_MFRC522) + { + if(enabled) + readers[i].device.mfrc522.detectAsync(detectCb, this); + else + readers[i].device.mfrc522.stopAsync(); + watchDogBits[i] = 0; + } + } + if(enabled) + wdt_set(WDTO_4S); + else + wdt_disable(); + } } uint8_t NfcBoard::csToIrq(uint8_t cs) @@ -74,28 +135,48 @@ void NfcBoard::probe() { readers.clear(); irqPins.clear(); + watchDogBits.clear(); for(uint8_t i = 0; i < NFC_PORTS; ++i) { if(Mfrc522::probe(&spim, &csReg, i)) { - readers.push_back(Mfrc522(&spim, &csReg, i)); + NfcPort port(Mfrc522(&spim, &csReg, i)); + readers.push_back(port); irqPins.push_back(csToIrq(i)); + watchDogBits.push_back(0); + } + else if(MfrcProxy::probe(&spim, &csReg, i)) + { + NfcPort port(MfrcProxy(&spim, &csReg, i, proxyDetectCb, this)); + readers.push_back(port); + irqPins.push_back(csToIrq(i)); + watchDogBits.push_back(0); } } } -void NfcBoard::printNfcDevices(Serial* serial) +void NfcBoard::printNfcDevices() { serial->write_p(PSTR("NFC DEVICES:\n")); for(uint8_t i = 0; i < readers.count(); ++i) { - snprintf_P(buffer, SNPRINTF_BUFFER_SIZE, PSTR("NFC NUMBER: %u IRQ: %x\n"), i, irqPins[i]); + snprintf_P(buffer, SNPRINTF_BUFFER_SIZE, PSTR("NFC NUMBER: %u IRQ: %x TYPE: %d\n"), + readers[i].type == TYPE_MFRC522 ? i+0x80 : readers[i].device.proxy.getId(), + irqPins[i], readers[i].type); serial->write(buffer, SNPRINTF_BUFFER_SIZE); } } -int NfcBoard::dispatch(char* inBuffer, Serial* serial) +int NfcBoard::dispatch(char* inBuffer) { + Mfrc522* mainReader = nullptr; + for(uint8_t i = 0; i < readers.count(); ++i) + { + if(readers[i].type == TYPE_MFRC522) + { + mainReader = &readers[i].device.mfrc522; + } + } if(strcmp(inBuffer, "debug") == 0) { Mfrc522::serial = serial; @@ -129,16 +210,23 @@ int NfcBoard::dispatch(char* inBuffer, Serial* serial) PCICR |= 1 << PCIE1; return 0; } - else if(strcmp(inBuffer, "detect") == 0 ) + else if(strcmp(inBuffer, "select") == 0 ) { + bool enabled = enabled_; + setEnabled(false); + if(!mainReader) + { + serial->write_p(PSTR("No nfc reader present\n")); + return -1; + } serial->write_p(PSTR("Runing tag detection test\n")); while(!serial->dataIsWaiting()) { - bool present = readers[0].cardPresent(); + bool present = mainReader->cardPresent(); if(present) { - Mfrc522::Uid uid; - uint8_t res = readers[0].selectTag(&uid); + Uid uid; + uint8_t res = mainReader->selectTag(&uid); if(res != 0) continue; @@ -153,19 +241,27 @@ int NfcBoard::dispatch(char* inBuffer, Serial* serial) } _delay_ms(100); } + setEnabled(enabled); serial->write_p(PSTR("Finished\n")); return 0; } - else if(strcmp(inBuffer, "fastdetect") == 0 ) + else if(strcmp(inBuffer, "detect") == 0 ) { + bool enabled = enabled_; + setEnabled(false); + if(!mainReader) + { + serial->write_p(PSTR("No nfc reader present\n")); + return -1; + } serial->write_p(PSTR("Runing fast tag detection test\n")); while(!serial->dataIsWaiting()) { - bool present = readers[0].cardPresent(); + bool present = mainReader->cardPresent(); if(present) { - Mfrc522::Uid uid; - uint8_t res = readers[0].getUid(&uid); + Uid uid; + uint8_t res = mainReader->getUid(&uid); if(res != 0) continue; @@ -179,72 +275,124 @@ int NfcBoard::dispatch(char* inBuffer, Serial* serial) serial->putChar('\n'); } } - serial->write_p(PSTR("Finished\n")); - return 0; - } - else if(strcmp(inBuffer, "fifotst") == 0 ) - { - serial->write_p(PSTR("Runing fifo test\n")); - Mfrc522::serial = serial; - readers[0].testFifo(); - Mfrc522::serial = nullptr; + setEnabled(enabled); serial->write_p(PSTR("Finished\n")); return 0; } else if(strcmp(inBuffer, "enable") == 0 ) { - for(uint8_t i = 0; i < readers.count(); ++i) + if(enabled_) { - readers[i].detectAsync(detectCb, serial); + serial->write_p(PSTR("Nfc tag listening allready enabled\n")); + return -1; } + setEnabled(true); serial->write_p(PSTR("Nfc tag listening enabled\n")); return 0; } - else if(strcmp(inBuffer, "status") == 0 ) + else if(strcmp(inBuffer, "disable") == 0 ) { - printNfcDevices(serial); + if(!enabled_) + { + serial->write_p(PSTR("Nfc tag listening already disabled\n")); + return -1; + } + setEnabled(false); + serial->write_p(PSTR("Nfc tag listening disabled\n")); return 0; } - else if(strcmp(inBuffer, "wake") == 0 ) + else if(strcmp(inBuffer, "list") == 0 ) { - uint8_t bufferATQA[2]; - uint8_t len = sizeof(bufferATQA); - - uint8_t res = readers[0].wakeupTag(bufferATQA, &len); - snprintf_P(buffer, SNPRINTF_BUFFER_SIZE, PSTR("wakeupTag returned: %u Buffer: 0x%x 0x%x len %u\n"), - res, bufferATQA[0], bufferATQA[1], len); - serial->write(buffer, SNPRINTF_BUFFER_SIZE); + printNfcDevices(); return 0; } else if(strcmp(inBuffer, "probe") == 0 ) { + bool enabled = enabled_; + setEnabled(false); probe(); - printNfcDevices(serial); + printNfcDevices(); + setEnabled(enabled); + return 0; + } + else if(strcmp(inBuffer, "read") == 0 ) + { + char* token = strtok(NULL, " "); + int16_t addr = -1; + int16_t csPin = -1; + if(token) + addr = strtol(token, nullptr, 16); + if(addr < 0 || addr > 0xFF) + { + serial->write_p(PSTR("A address must be specified\n")); + return -1; + } + token = strtok(NULL, " "); + if(token) + csPin = strtol(token, nullptr, 16); + if(csPin < 0 || csPin > 7) + { + serial->write_p(PSTR("A valid cs pin must be specified\n")); + return -1; + } + csReg.setBit(csPin, false); + _delay_us(5); + spim.readWrite(addr); + _delay_us(5); + uint8_t result = spim.readWrite(); + csReg.setBit(csPin, true); + serial->write_p(PSTR("Got: ")); + serial->write((int)result); + serial->write_p(PSTR(" in return\n")); return 0; } return -3; } +void NfcBoard::printTag(const uint8_t id, const Uid& uid, Serial* serial) +{ + serial->write("NFC "); + serial->write((int)id); + serial->write(" TAG "); + for(uint8_t i = 0; i < uid.size; ++i) + { + serial->write((int)uid.uidByte[i]); + if(i < uid.size-1) + serial->putChar(':'); + } + serial->putChar('\n'); +} + +void NfcBoard::proxyDetectCb(MfrcProxy* proxy, const Uid uid, void* data) +{ + NfcBoard* instance = reinterpret_cast(data); + + printTag(proxy->getId(), uid, instance->serial); +} + void NfcBoard::detectCb(Mfrc522* reader, void* data) { - Serial* serial = reinterpret_cast(data); + NfcBoard* instance = reinterpret_cast(data); - static Mfrc522* oldReader = nullptr; - static Mfrc522::Uid oldUid = {0}; - - Mfrc522::Uid uid; - if(reader->getUid(&uid) == 0 && (uid != oldUid || reader != oldReader)) + Uid uid; + if(reader->getUid(&uid) == 0) { - serial->write("TAG: "); - for(uint8_t i = 0; i < uid.size; ++i) + uint8_t i; + for(i = 0; i < instance->readers.count(); ++i) { - serial->write((int)uid.uidByte[i]); - if(i < uid.size-1) - serial->putChar(':'); + if(&instance->readers[i].device.mfrc522 == reader) + break; } - serial->putChar('\n'); - oldUid = uid; - oldReader = reader; + printTag(i+0x80, uid, instance->serial); + reader->startTimeout(&timeoutCb, data); + } + else + { + reader->detectAsync(detectCb, data); } - reader->detectAsync(detectCb, serial); +} + +void NfcBoard::timeoutCb(uint8_t ret, Mfrc522* reader, uint8_t* response, uint8_t responseLen, void* userData) +{ + reader->detectAsync(&detectCb, userData); } diff --git a/nfcbord.h b/nfcbord.h index 6a010ca..ed204ea 100644 --- a/nfcbord.h +++ b/nfcbord.h @@ -1,4 +1,5 @@ #pragma once +#include "placementnew.h" #include "serial.h" #include "shiftreg.h" #include "inputshiftreg.h" @@ -6,25 +7,78 @@ #include "softspim.h" #include "staticvector.h" #include "defines.h" +#include "mfrcproxy.h" class NfcBoard { -public: - ShiftReg csReg; - InputShiftReg irqReg; +private: + + static constexpr int TYPE_MFRC522 = 0; + static constexpr int TYPE_PROXY = 1; + + union PortDevice + { + Mfrc522 mfrc522; + MfrcProxy proxy; + PortDevice(const Mfrc522& mfrc522In) + { + new (&mfrc522) Mfrc522(mfrc522In); + } + PortDevice(const MfrcProxy& proxyIn) + { + new (&proxy) MfrcProxy(proxyIn); + } + }; + + struct NfcPort + { + PortDevice device; + uint8_t type; + NfcPort(const Mfrc522& mfrc522): device(mfrc522) + { + type = TYPE_MFRC522; + } + NfcPort(const MfrcProxy& proxy): device(proxy) + { + type = TYPE_PROXY; + } + ~NfcPort() + { + if(type == TYPE_MFRC522) + device.mfrc522.~Mfrc522(); + else + device.proxy.~MfrcProxy(); + } + }; + + Serial* serial; SpiMaster spim; - SVector readers; + ShiftReg csReg; + bool enabled_ = false; + + static void printTag(const uint8_t id, const Uid& uid, Serial* serial); + +public: + InputShiftReg irqReg; SVector irqPins; - + SVector readers; + SVector watchDogBits; + static void detectCb(Mfrc522* reader, void* data); + static void timeoutCb(uint8_t ret, Mfrc522* reader, uint8_t* response, uint8_t responseLen, void* userData); + static void proxyDetectCb(MfrcProxy* proxy, const Uid uid, void* data); - NfcBoard(); + NfcBoard(Serial* serialIn); void probe(); - void printNfcDevices(Serial* serial); + void setEnabled(bool enabled); - int dispatch(char* inBuffer, Serial* serial); + void poll(Serial* serial = nullptr); + + void printNfcDevices(); + + int dispatch(char* inBuffer); uint8_t csToIrq(uint8_t cs); }; diff --git a/placementnew.cpp b/placementnew.cpp new file mode 100644 index 0000000..bdadaf8 --- /dev/null +++ b/placementnew.cpp @@ -0,0 +1,11 @@ +#include "placementnew.h" + +void* operator new(uint16_t, void* const buf) +{ + return buf; +} + +void* operator new[](uint16_t, void* const buf) +{ + return buf; +} diff --git a/placementnew.h b/placementnew.h new file mode 100644 index 0000000..6a5a332 --- /dev/null +++ b/placementnew.h @@ -0,0 +1,5 @@ +#pragma once +#include "stdint.h" + +void* operator new(uint16_t, void* const buf); +void* operator new[](uint16_t, void* const buf); diff --git a/shiftreg.h b/shiftreg.h index 2e31b99..9a38af5 100644 --- a/shiftreg.h +++ b/shiftreg.h @@ -53,8 +53,8 @@ public: for(unsigned char i = 0; i < BITS; ++i) { - *_port |= (1 << _pinSerClk); in[i/8] & (1<<(i%8)) ? (*_port &= ~(1 << _pinSer)) : (*_port |= (1 << _pinSer)); + *_port |= (1 << _pinSerClk); *_port &= ~(1 << _pinSerClk); } *_port &= ~(1<<_pinRClk); diff --git a/uid.h b/uid.h new file mode 100644 index 0000000..21589c3 --- /dev/null +++ b/uid.h @@ -0,0 +1,24 @@ +#pragma once + +class Uid +{ +public: + uint8_t size; // Number of bytes in the UID. 4, 7 or 10. + uint8_t uidByte[10]; + uint8_t sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection. + bool operator==(Uid& in) + { + if(size != in.size) + return false; + for(uint8_t i = 0; i < size; ++i) + { + if(uidByte[i] != in.uidByte[i]) + return false; + } + return true; + } + bool operator!=(Uid& in) + { + return !operator==(in); + } +}; diff --git a/watchdog.h b/watchdog.h new file mode 100644 index 0000000..ac5b0e3 --- /dev/null +++ b/watchdog.h @@ -0,0 +1,18 @@ +#pragma once +#include + +#define wdt_set(value) \ + __asm__ __volatile__ ( \ + "in __tmp_reg__,__SREG__" "\n\t" \ + "cli" "\n\t" \ + "wdr" "\n\t" \ + "sts %0,%1" "\n\t" \ + "out __SREG__,__tmp_reg__" "\n\t" \ + "sts %0,%2" \ + : \ + : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ + "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ + "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ + _BV(WDIE) | (value & 0x07)) ) \ + : "r0" \ + )