inital nfc code
This commit is contained in:
150
nfcbord.cpp
Normal file
150
nfcbord.cpp
Normal file
@ -0,0 +1,150 @@
|
||||
#include "nfcbord.h"
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "writepin.h"
|
||||
|
||||
extern char buffer[SNPRINTF_BUFFER_SIZE];
|
||||
|
||||
NfcBoard::NfcBoard():
|
||||
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);
|
||||
probe();
|
||||
}
|
||||
|
||||
void NfcBoard::probe()
|
||||
{
|
||||
readers.clear();
|
||||
irqPins.clear();
|
||||
for(uint8_t i = 0; i < NFC_PORTS; ++i)
|
||||
{
|
||||
if(Mfrc522::probe(&spim, &csReg, i))
|
||||
{
|
||||
irqPins.push_back(i);
|
||||
}
|
||||
}
|
||||
for(uint8_t i = 0; i < irqPins.count(); ++i)
|
||||
{
|
||||
readers.push_back(Mfrc522(&spim, &csReg, irqPins[i]));
|
||||
}
|
||||
}
|
||||
|
||||
void NfcBoard::printNfcDevices(Serial* serial)
|
||||
{
|
||||
serial->write_p(PSTR("NFC DEVICES:\n"));
|
||||
for(uint8_t i = 0; i < readers.count(); ++i)
|
||||
{
|
||||
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "NFC NUMBER: %u IRQ: %x\n", i, irqPins[i]);
|
||||
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
int NfcBoard::dispatch(char* inBuffer, Serial* serial)
|
||||
{
|
||||
if(strcmp(inBuffer, "tsta") == 0 )
|
||||
{
|
||||
serial->write_p(PSTR("Runing shift test\n"));
|
||||
while(!serial->dataIsWaiting())
|
||||
{
|
||||
csReg.clear(false);
|
||||
_delay_us(100);
|
||||
}
|
||||
serial->write_p(PSTR("Finished\n"));
|
||||
csReg.clear(true);
|
||||
return 0;
|
||||
}
|
||||
else if(strcmp(inBuffer, "tstb") == 0 )
|
||||
{
|
||||
serial->write_p(PSTR("Runing input shift test b\n"));
|
||||
while(!serial->dataIsWaiting())
|
||||
{
|
||||
serial->write((int)(*irqReg.read()));
|
||||
serial->putChar('\n');
|
||||
_delay_us(100);
|
||||
}
|
||||
serial->write_p(PSTR("Finished\n"));
|
||||
return 0;
|
||||
}
|
||||
else if(strcmp(inBuffer, "tstc") == 0 )
|
||||
{
|
||||
serial->write_p(PSTR("Runing spi test\n"));
|
||||
while(!serial->dataIsWaiting())
|
||||
{
|
||||
csReg.setBit(1, false);
|
||||
spim.readWrite((0x37 << 1) | (1 << 7));
|
||||
serial->write((int)spim.readWrite());
|
||||
serial->putChar('\n');
|
||||
csReg.setBit(1, true);
|
||||
_delay_us(100);
|
||||
}
|
||||
serial->write_p(PSTR("Finished\n"));
|
||||
return 0;
|
||||
}
|
||||
else if(strcmp(inBuffer, "tstd") == 0 )
|
||||
{
|
||||
serial->write_p(PSTR("Runing tag detection test\n"));
|
||||
bool oldPresent = false;
|
||||
Mfrc522::serial = serial;
|
||||
while(!serial->dataIsWaiting())
|
||||
{
|
||||
bool present = readers[0].cardPresent();
|
||||
if(present && !oldPresent)
|
||||
{
|
||||
oldPresent = present;
|
||||
|
||||
serial->write_p(PSTR("Tag found, selecting\n"));
|
||||
|
||||
Mfrc522::Uid uid;
|
||||
uint8_t res = readers[0].selectTag(&uid);
|
||||
if(res != 0)
|
||||
{
|
||||
serial->write_p(PSTR("Select Failed with "));
|
||||
serial->write((int)res);
|
||||
serial->putChar('\n');
|
||||
continue;
|
||||
}
|
||||
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "Uid: %x %x %x %x %x %x %x %x %x %x\n",
|
||||
uid.uidByte[0], uid.uidByte[1], uid.uidByte[2], uid.uidByte[3], uid.uidByte[4],
|
||||
uid.uidByte[5], uid.uidByte[6], uid.uidByte[7], uid.uidByte[8], uid.uidByte[9]);
|
||||
}
|
||||
else if(!present && oldPresent)
|
||||
{
|
||||
serial->write_p(PSTR("Tag lost\n"));
|
||||
oldPresent = present;
|
||||
}
|
||||
_delay_ms(100);
|
||||
}
|
||||
serial->write_p(PSTR("Finished\n"));
|
||||
return 0;
|
||||
}
|
||||
else if(strcmp(inBuffer, "status") == 0 )
|
||||
{
|
||||
printNfcDevices(serial);
|
||||
return 0;
|
||||
}
|
||||
else if(strcmp(inBuffer, "wake") == 0 )
|
||||
{
|
||||
uint8_t bufferATQA[2];
|
||||
uint8_t len = sizeof(bufferATQA);
|
||||
|
||||
uint8_t res = readers[0].wakeupTag(bufferATQA, &len);
|
||||
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "wakeupTag returned: %u Buffer: 0x%x 0x%x len %u\n",
|
||||
res, bufferATQA[0], bufferATQA[1], len);
|
||||
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
|
||||
return 0;
|
||||
}
|
||||
else if(strcmp(inBuffer, "probe") == 0 )
|
||||
{
|
||||
probe();
|
||||
printNfcDevices(serial);
|
||||
return 0;
|
||||
}
|
||||
return -3;
|
||||
}
|
Reference in New Issue
Block a user