More functions

This commit is contained in:
Chris 2018-06-18 15:10:00 +02:00
parent 48891d71eb
commit f7c02b1d87
3 changed files with 281 additions and 9 deletions

View File

@ -4,16 +4,78 @@
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
#include <cstring> #include <cstring>
#include <signal.h>
#include "spi.h" #include "spi.h"
#include "mfrc522.h" #include "mfrc522.h"
int reading = 0;
int main(int argc, char** argv) void sig_handler(int signo)
{
if (signo == SIGINT)
{
reading = 0;
}
}
int main(int argc, char **argv)
{ {
SPI spi(0); SPI spi(0);
MFRC522 reader(&spi); MFRC522 MIFAREReader(&spi);
int status;
if (signal(SIGINT, sig_handler) == SIG_ERR)
{
perror("Unable to bind sigint\n");
return EXIT_FAILURE;
}
while (reading)
{
/*
// Scan for cards
status = MIFAREReader.MFRC522_Request(MFRC522::PICC_REQIDL);
// If a card is found
if (status == MFRC522::MI_OK)
{
printf("Card detected\n");
}
unsigned char uid[4];
// This is the default key for authentication
unsigned char key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Get the UID of the card
status = MIFAREReader.MFRC522_Anticoll(&uid);
// If we have the UID, continue
if (status == MFRC522::MI_OK)
{
// Print UID
printf("Card read UID: %02x:%02x:%02x:%02x", uid[0], uid[1], uid[2], uid[3]);
// Select the scanned tag
MIFAREReader.MFRC522_SelectTag(&uid);
// Authenticate
status = MIFAREReader.MFRC522_Auth(MFRC522::PICC_AUTHENT1A, 8, key, &uid);
// Check if authenticated
if (status == MFRC522::MI_OK)
{
MIFAREReader.MFRC522_Read(8);
MIFAREReader.MFRC522_StopCrypto1();
}
else
{
printf("Authentication error");
}
}
*/
}
} }

View File

@ -1,13 +1,111 @@
#include "mfrc522.h" #include "mfrc522.h"
MFRC522::MFRC522(SPI *spi) MFRC522::MFRC522(SPI *spi)
{ {
// Open SPI bus
this->spi = spi; this->spi = spi;
spi->SpiOpenPort(); spi->SpiOpenPort();
// SPI needs configuring for speed as per;
// def __init__(self, dev='/dev/spidev0.0', spd=1000000):
// spi.openSPI(device = dev, speed = spd)
// Setup GPIO for CS
//GPIO.setmode(GPIO.BOARD)
//GPIO.setup(self.NRSTPD, GPIO.OUT)
//GPIO.output(self.NRSTPD, 1)
// call init
MFRC522_Init();
} }
MFRC522::~MFRC522() MFRC522::~MFRC522()
{ {
spi->SpiClosePort(); spi->SpiClosePort();
}
void MFRC522::MFRC522_Init()
{
//GPIO.output(self.NRSTPD, 1);
MFRC522_Reset();
Write_MFRC522(MFRC522RegEnum::TModeReg, 0x8D);
Write_MFRC522(MFRC522RegEnum::TPrescalerReg, 0x3E);
Write_MFRC522(MFRC522RegEnum::TReloadRegL, 30);
Write_MFRC522(MFRC522RegEnum::TReloadRegH, 0);
Write_MFRC522(MFRC522RegEnum::TxAutoReg, 0x40);
Write_MFRC522(MFRC522RegEnum::ModeReg, 0x3D);
AntennaOn();
}
void MFRC522::MFRC522_Reset()
{
// self.Write_MFRC522(self.CommandReg, self.PCD_RESETPHASE)
Write_MFRC522(MFRC522RegEnum::CommandReg, MFRC522::PCD_RESETPHASE);
}
void MFRC522::Write_MFRC522(unsigned char addr, unsigned char val)
{
//def Write_MFRC522(self, addr, val)
// spi.transfer(((addr << 1) & 0x7E, val))
unsigned char buf;
buf = val;
this->spi->SpiWriteAndRead(&buf, (addr << 1) & 0x7E);
}
unsigned char MFRC522::Read_MFRC522(unsigned char addr)
{
//def Read_MFRC522(self, addr)
// val = spi.transfer((((addr << 1) & 0x7E) | 0x80, 0)) return val[1]
unsigned char buf;
this->spi->SpiWriteAndRead(&buf, (addr << 1) & 0x7E);
return buf;
}
void MFRC522::AntennaOn()
{
/*
def AntennaOn(self):
temp = self.Read_MFRC522(self.TxControlReg)
if (~(temp & 0x03)):
self.SetBitMask(self.TxControlReg, 0x03)
*/
uint8_t temp = Read_MFRC522(MFRC522RegEnum::TxControlReg);
if (~(temp & 0x03)) {
SetBitMask(MFRC522RegEnum::TxControlReg, 0x03);
}
}
void MFRC522::AntennaOff()
{
/*
def AntennaOff(self):
self.ClearBitMask(self.TxControlReg, 0x03)
*/
ClearBitMask(MFRC522RegEnum::TxControlReg, 0x03);
}
void MFRC522::SetBitMask(uint8_t addr, uint8_t mask)
{
/*
def SetBitMask(self, reg, mask):
tmp = self.Read_MFRC522(reg)
self.Write_MFRC522(reg, tmp | mask)
*/
uint8_t temp = Read_MFRC522(addr);
Write_MFRC522(addr, temp | mask);
}
void MFRC522::ClearBitMask(uint8_t addr, uint8_t mask)
{
/*
def ClearBitMask(self, reg, mask):
tmp = self.Read_MFRC522(reg);
self.Write_MFRC522(reg, tmp & (~mask))
*/
uint8_t temp = Read_MFRC522(addr);
Write_MFRC522(addr, temp & ~mask);
} }

View File

@ -1,14 +1,126 @@
#pragma once #pragma once
#include "spi.h" #include "spi.h"
#include <stdint.h>
enum MFRC522RegEnum
{
Reserved00 = 0x00,
CommandReg = 0x01,
CommIEnReg = 0x02,
DivlEnReg = 0x03,
CommIrqReg = 0x04,
DivIrqReg = 0x05,
ErrorReg = 0x06,
Status1Reg = 0x07,
Status2Reg = 0x08,
FIFODataReg = 0x09,
FIFOLevelReg = 0x0A,
WaterLevelReg = 0x0B,
ControlReg = 0x0C,
BitFramingReg = 0x0D,
CollReg = 0x0E,
Reserved01 = 0x0F,
Reserved10 = 0x10,
ModeReg = 0x11,
TxModeReg = 0x12,
RxModeReg = 0x13,
TxControlReg = 0x14,
TxAutoReg = 0x15,
TxSelReg = 0x16,
RxSelReg = 0x17,
RxThresholdReg = 0x18,
DemodReg = 0x19,
Reserved11 = 0x1A,
Reserved12 = 0x1B,
MifareReg = 0x1C,
Reserved13 = 0x1D,
Reserved14 = 0x1E,
SerialSpeedReg = 0x1F,
Reserved20 = 0x20,
CRCResultRegM = 0x21,
CRCResultRegL = 0x22,
Reserved21 = 0x23,
ModWidthReg = 0x24,
Reserved22 = 0x25,
RFCfgReg = 0x26,
GsNReg = 0x27,
CWGsPReg = 0x28,
ModGsPReg = 0x29,
TModeReg = 0x2A,
TPrescalerReg = 0x2B,
TReloadRegH = 0x2C,
TReloadRegL = 0x2D,
TCounterValueRegH = 0x2E,
TCounterValueRegL = 0x2F,
Reserved30 = 0x30,
TestSel1Reg = 0x31,
TestSel2Reg = 0x32,
TestPinEnReg = 0x33,
TestPinValueReg = 0x34,
TestBusReg = 0x35,
AutoTestReg = 0x36,
VersionReg = 0x37,
AnalogTestReg = 0x38,
TestDAC1Reg = 0x39,
TestDAC2Reg = 0x3A,
TestADCReg = 0x3B,
Reserved31 = 0x3C,
Reserved32 = 0x3D,
Reserved33 = 0x3E,
Reserved34 = 0x3F
};
class MFRC522 class MFRC522
{ {
public: public:
MFRC522(SPI *spi); MFRC522(SPI *spi);
~MFRC522(); ~MFRC522();
void MFRC522_Reset();
void Write_MFRC522(uint8_t, uint8_t);
uint8_t Read_MFRC522(uint8_t);
void SetBitMask(uint8_t, uint8_t);
void ClearBitMask(uint8_t, uint8_t);
void AntennaOn();
void AntennaOff();
int MFRC522_ToCard(uint8_t, uint8_t);
int MFRC522_Request(uint8_t);
int MFRC522_Anticoll(uint8_t (*)[4]);
int CalulateCRC(uint8_t *);
int MFRC522_SelectTag(uint8_t *);
int MFRC522_Auth(uint8_t, unsigned int, uint8_t *, uint8_t *);
int MFRC522_StopCrypto1();
int MFRC522_Read(uint8_t, uint8_t (*)[4]);
int MFRC522_Write(uint8_t, uint8_t *);
int MFRC522_DumpClassic1K(uint8_t *, uint8_t *);
void MFRC522_Init();
private: static const int NRSTPD = 22;
SPI *spi; static const int MAX_LEN = 16;
static const int PCD_IDLE = 0x00;
static const int PCD_AUTHENT = 0x0E;
static const int PCD_RECEIVE = 0x08;
static const int PCD_TRANSMIT = 0x04;
static const int PCD_TRANSCEIVE = 0x0C;
static const int PCD_RESETPHASE = 0x0F;
static const int PCD_CALCCRC = 0x03;
static const int PICC_REQIDL = 0x26;
static const int PICC_REQALL = 0x52;
static const int PICC_ANTICOLL = 0x93;
static const int PICC_SElECTTAG = 0x93;
static const int PICC_AUTHENT1A = 0x60;
static const int PICC_AUTHENT1B = 0x61;
static const int PICC_READ = 0x30;
static const int PICC_WRITE = 0xA0;
static const int PICC_DECREMENT = 0xC0;
static const int PICC_INCREMENT = 0xC1;
static const int PICC_RESTORE = 0xC2;
static const int PICC_TRANSFER = 0xB0;
static const int PICC_HALT = 0x50;
static const int MI_OK = 0;
static const int MI_NOTAGERR = 1;
static const int MI_ERR = 2;
private:
SPI *spi;
}; };