add support for mfrc proxyies
This commit is contained in:
278
nfcbord.cpp
278
nfcbord.cpp
@ -4,27 +4,78 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <avr/wdt.h>
|
||||
#include <stdio.h>
|
||||
#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<NfcBoard*>(data);
|
||||
|
||||
printTag(proxy->getId(), uid, instance->serial);
|
||||
}
|
||||
|
||||
void NfcBoard::detectCb(Mfrc522* reader, void* data)
|
||||
{
|
||||
Serial* serial = reinterpret_cast<Serial*>(data);
|
||||
NfcBoard* instance = reinterpret_cast<NfcBoard*>(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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user