Changed to Class renamed files

This commit is contained in:
Jimmy 2020-12-26 00:56:38 +13:00
parent a82a2e2b19
commit cf2c0d4f24
3 changed files with 153 additions and 98 deletions

View File

@ -1,92 +1,109 @@
#include "spi.h"
#include "rc522.h"
///////////////////////////////////////////////////////////
void Write_MFRC522(uchar addr, uchar val)
MFRC522::MFRC522(volatile uchar cs_pin, volatile uchar *cs_port, volatile uchar reset_pin, volatile uchar *reset_port) {
this->cs_pin = cs_pin;
this->cs_port = cs_port;
this->reset_pin = reset_pin;
this->reset_port = reset_port;
*(cs_port-1) |= (1<<cs_pin);
*(cs_port) |= (1<<cs_pin);
*(reset_port-1) |= (1<<reset_pin);
}
void MFRC522::put(uchar addr, uchar val)
{
spi::cs_low();
*cs_port &= ~(1<<cs_pin);
spi::transfer((addr<<1)&0x7E);
spi::transfer(val);
spi::cs_high();
*cs_port |= (1<<cs_pin);
}
uchar Read_MFRC522(uchar addr)
uchar MFRC522::get(uchar addr)
{
uchar val;
spi::cs_low();
//spi::cs_low();
*cs_port &= ~(1<<cs_pin);
//address format: 1XXXXXX0
spi::transfer(((addr<<1)&0x7E) | 0x80);
val =spi::transfer(0x00);
spi::cs_high();
//spi::cs_high();
*cs_port |= (1<<cs_pin);
return val;
}
void SetBitMask(uchar reg, uchar mask)
void MFRC522::SetBitMask(uchar reg, uchar mask)
{
uchar tmp;
tmp = Read_MFRC522(reg);
Write_MFRC522(reg, tmp | mask); // set bit mask
tmp = get(reg);
put(reg, tmp | mask); // set bit mask
}
void ClearBitMask(uchar reg, uchar mask)
void MFRC522::ClearBitMask(uchar reg, uchar mask)
{
uchar tmp;
tmp = Read_MFRC522(reg);
Write_MFRC522(reg, tmp & (~mask)); // clear bit mask
tmp = get(reg);
put(reg, tmp & (~mask)); // clear bit mask
}
void AntennaOn(void)
void MFRC522::AntennaOn(void)
{
uchar temp;
temp = Read_MFRC522(TxControlReg);
temp = get(TxControlReg);
if (!(temp & 0x03))
{
SetBitMask(TxControlReg, 0x03);
}
}
void AntennaOff(void)
void MFRC522::AntennaOff(void)
{
ClearBitMask(TxControlReg, 0x03);
}
void MFRC522_Reset(void)
void MFRC522::Reset(void)
{
Write_MFRC522(CommandReg, PCD_RESETPHASE);
put(CommandReg, PCD_RESETPHASE);
}
void MFRC522_Init(void)
void MFRC522::Init(void)
{
spi::init();
NRSTPD_DDR |= (1<<NRSTPD_PIN);
NRSTPD_PORT |= (1<<NRSTPD_PIN);
// NRSTPD_DDR |= (1<<NRSTPD_PIN);
// NRSTPD_PORT |= (1<<NRSTPD_PIN);
*reset_port &= ~(1<<reset_pin);
*reset_port |= (1<<reset_pin);
MFRC522_Reset();
Reset();
//Timer: TPrescaler*TreloadVal/6.78MHz = 24ms
Write_MFRC522(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler
Write_MFRC522(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg
Write_MFRC522(TReloadRegL, 30);
Write_MFRC522(TReloadRegH, 0);
Write_MFRC522(TxAutoReg, 0x40); //100%ASK
Write_MFRC522(ModeReg, 0x3D); //CRC original value 0x6363 ???
put(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler
put(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg
put(TReloadRegL, 30);
put(TReloadRegH, 0);
put(TxAutoReg, 0x40); //100%ASK
put(ModeReg, 0x3D); //CRC original value 0x6363 ???
AntennaOn(); // open antenna
}
uchar MFRC522_Request(uchar reqMode, uchar *TagType)
uchar MFRC522::Request(uchar reqMode, uchar *TagType)
{
uchar status;
uint backBits; // bits of data received
Write_MFRC522(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ???
put(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ???
TagType[0] = reqMode;
status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);
status = ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);
if ((status != MI_OK) || (backBits != 0x10))
{
@ -96,7 +113,7 @@ uchar MFRC522_Request(uchar reqMode, uchar *TagType)
return status;
}
uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen)
uchar MFRC522::ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen)
{
uchar status = MI_ERR;
uchar irqEn = 0x00;
@ -124,20 +141,20 @@ uchar irqEn = 0x00;
break;
}
Write_MFRC522(CommIEnReg, irqEn|0x80); // permission for interrupt request
put(CommIEnReg, irqEn|0x80); // permission for interrupt request
ClearBitMask(CommIrqReg, 0x80); // clear all bits of the interrupt request
SetBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO initialize
Write_MFRC522(CommandReg, PCD_IDLE); //NO action; clear current command ???
put(CommandReg, PCD_IDLE); //NO action; clear current command ???
// write data into FIFO
for (i=0; i<sendLen; i++)
{
Write_MFRC522(FIFODataReg, sendData[i]);
put(FIFODataReg, sendData[i]);
}
// execute command
Write_MFRC522(CommandReg, command);
put(CommandReg, command);
if (command == PCD_TRANSCEIVE)
{
SetBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts
@ -150,7 +167,7 @@ uchar irqEn = 0x00;
{
//CommIrqReg[7..0]
//Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
n = Read_MFRC522(CommIrqReg);
n = get(CommIrqReg);
i--;
}
while ((i!=0) && !(n&0x01) && !(n&waitIRq));
@ -159,7 +176,7 @@ uchar irqEn = 0x00;
if (i != 0)
{
if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
if(!(get(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
{
status = MI_OK;
if (n & irqEn & 0x01)
@ -170,8 +187,8 @@ uchar irqEn = 0x00;
if (command == PCD_TRANSCEIVE)
{
n = Read_MFRC522(FIFOLevelReg);
lastBits = Read_MFRC522(ControlReg) & 0x07;
n = get(FIFOLevelReg);
lastBits = get(ControlReg) & 0x07;
if (lastBits)
{
*backLen = (n-1)*8 + lastBits;
@ -194,7 +211,7 @@ uchar irqEn = 0x00;
// read the data received in FIFO
for (i=0; i<n; i++)
{
backData[i] = Read_MFRC522(FIFODataReg);
backData[i] = get(FIFODataReg);
}
}
}
@ -212,18 +229,18 @@ uchar irqEn = 0x00;
return status;
}
uchar MFRC522_Anticoll(uchar *serNum)
uchar MFRC522::Anticoll(uchar *serNum)
{
uchar status;
uchar i;
uchar serNumCheck=0;
uint unLen;
Write_MFRC522(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0]
put(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0]
serNum[0] = PICC_ANTICOLL;
serNum[1] = 0x20;
status = MFRC522_ToCard(PCD_TRANSCEIVE, serNum, 2, serNum, &unLen);
status = ToCard(PCD_TRANSCEIVE, serNum, 2, serNum, &unLen);
if (status == MI_OK)
{
@ -244,7 +261,7 @@ uchar MFRC522_Anticoll(uchar *serNum)
return status;
}
void CalulateCRC(uchar *pIndata, uchar len, uchar *pOutData)
void MFRC522::CalulateCRC(uchar *pIndata, uchar len, uchar *pOutData)
{
uchar i, n;
@ -255,26 +272,26 @@ void CalulateCRC(uchar *pIndata, uchar len, uchar *pOutData)
// write data into FIFO
for (i=0; i<len; i++)
{
Write_MFRC522(FIFODataReg, *(pIndata+i));
put(FIFODataReg, *(pIndata+i));
}
Write_MFRC522(CommandReg, PCD_CALCCRC);
put(CommandReg, PCD_CALCCRC);
// wait for completion of CRC calculation
i = 0xFF;
do
{
n = Read_MFRC522(DivIrqReg);
n = get(DivIrqReg);
i--;
}
while ((i!=0) && !(n&0x04)); //CRCIrq = 1
// read result from CRC calculation
pOutData[0] = Read_MFRC522(CRCResultRegL);
pOutData[1] = Read_MFRC522(CRCResultRegM);
pOutData[0] = get(CRCResultRegL);
pOutData[1] = get(CRCResultRegM);
}
uchar MFRC522_SelectTag(uchar *serNum)
uchar MFRC522::SelectTag(uchar *serNum)
{
uchar i;
uchar status;
@ -291,7 +308,7 @@ uchar MFRC522_SelectTag(uchar *serNum)
buffer[i+2] = *(serNum+i);
}
CalulateCRC(buffer, 7, &buffer[7]); //??
status = MFRC522_ToCard(PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits);
status = ToCard(PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits);
if ((status == MI_OK) && (recvBits == 0x18))
@ -306,7 +323,7 @@ if ((status == MI_OK) && (recvBits == 0x18))
return size;
}
uchar MFRC522_Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *serNum)
uchar MFRC522::Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *serNum)
{
uchar status;
uint recvBits;
@ -324,9 +341,9 @@ uchar MFRC522_Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *ser
{
buff[i+8] = *(serNum+i);
}
status = MFRC522_ToCard(PCD_AUTHENT, buff, 12, buff, &recvBits);
status = ToCard(PCD_AUTHENT, buff, 12, buff, &recvBits);
if ((status != MI_OK) || (!(Read_MFRC522(Status2Reg) & 0x08)))
if ((status != MI_OK) || (!(get(Status2Reg) & 0x08)))
{
status = MI_ERR;
}
@ -334,7 +351,7 @@ uchar MFRC522_Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *ser
return status;
}
uchar MFRC522_Read(uchar blockAddr, uchar *recvData)
uchar MFRC522::Read(uchar blockAddr, uchar *recvData)
{
uchar status;
@ -343,7 +360,7 @@ uchar MFRC522_Read(uchar blockAddr, uchar *recvData)
recvData[0] = PICC_READ;
recvData[1] = blockAddr;
CalulateCRC(recvData,2, &recvData[2]);
status = MFRC522_ToCard(PCD_TRANSCEIVE, recvData, 4, recvData, &unLen);
status = ToCard(PCD_TRANSCEIVE, recvData, 4, recvData, &unLen);
if ((status != MI_OK) || (unLen != 0x90))
{
@ -353,7 +370,7 @@ uchar MFRC522_Read(uchar blockAddr, uchar *recvData)
return status;
}
uchar MFRC522_Write(uchar blockAddr, uchar *writeData)
uchar MFRC522::Write(uchar blockAddr, uchar *writeData)
{
uchar status;
uint recvBits;
@ -364,7 +381,7 @@ uchar MFRC522_Write(uchar blockAddr, uchar *writeData)
buff[0] = PICC_WRITE;
buff[1] = blockAddr;
CalulateCRC(buff, 2, &buff[2]);
status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff, &recvBits);
status = ToCard(PCD_TRANSCEIVE, buff, 4, buff, &recvBits);
if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A))
{
@ -378,7 +395,7 @@ uchar MFRC522_Write(uchar blockAddr, uchar *writeData)
buff[i] = *(writeData+i);
}
CalulateCRC(buff, 16, &buff[16]);
status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 18, buff, &recvBits);
status = ToCard(PCD_TRANSCEIVE, buff, 18, buff, &recvBits);
if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A))
{
@ -389,9 +406,8 @@ uchar MFRC522_Write(uchar blockAddr, uchar *writeData)
return status;
}
void MFRC522_Halt(void)
char MFRC522::Halt(void)
{
uchar status;
uint unLen;
uchar buff[4];
@ -399,5 +415,5 @@ void MFRC522_Halt(void)
buff[1] = 0;
CalulateCRC(buff, 2, &buff[2]);
status = MFRC522_ToCard(PCD_TRANSCEIVE, buff, 4, buff,&unLen);
return ToCard(PCD_TRANSCEIVE, buff, 4, buff,&unLen);
}

View File

@ -1,5 +1,4 @@
#ifndef RC522_H
#define RC522_H
#pragma once
#include <avr/io.h>
@ -115,21 +114,36 @@
#ifndef NRSTPD_PORT
#define NRSTPD_PORT PORTB
#define NRSTPD_DDR DDRB
#define NRSTPD_PIN PB1
#define NRSTPD_PIN PB2
#endif
#define MAX_LEN 16
void MFRC522_Init(void);
void MFRC522_Halt(void);
uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen);
void Write_MFRC522(uchar addr, uchar val);
uchar Read_MFRC522(uchar addr);
uchar MFRC522_Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *serNum);
uchar MFRC522_Write(uchar blockAddr, uchar *writeData);
uchar MFRC522_Read(uchar blockAddr, uchar *recvData);
uchar MFRC522_Request(uchar reqMode, uchar *TagType);
uchar MFRC522_Anticoll(uchar *serNum);
uchar MFRC522_SelectTag(uchar *serNum);
class MFRC522
{
private:
/* data */
volatile uchar *reset_port, *cs_port;
volatile uchar reset_pin, cs_pin;
void SetBitMask(uchar reg, uchar mask);
void ClearBitMask(uchar reg, uchar mask);
void AntennaOn(void);
void AntennaOff(void);
void Reset(void);
void CalulateCRC(uchar *pIndata, uchar len, uchar *pOutData);
public:
MFRC522(volatile uchar cs_pin, volatile uchar *cs_port, volatile uchar reset_pin, volatile uchar *reset_port);
~MFRC522();
void Init(void);
char Halt(void);
uchar ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen);
void put(uchar addr, uchar val);
uchar get(uchar addr);
uchar Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *serNum);
uchar Write(uchar blockAddr, uchar *writeData);
uchar Read(uchar blockAddr, uchar *recvData);
uchar Request(uchar reqMode, uchar *TagType);
uchar Anticoll(uchar *serNum);
uchar SelectTag(uchar *serNum);
};
#endif

View File

@ -2,35 +2,58 @@
#include <avr/io.h>
#include "../lib/usart/src/usart.h"
#include "string.h"
#include <util/delay.h>
#define uchar unsigned char
#define uint8_t unsigned char
#define uint unsigned int
#define MAX_LEN 16
const int chipSelectPin = 10;
const int NRSTPD = 5;
uchar serNum[5];
uchar writeDate[16] ={'T', 'e', 'n', 'g', ' ', 'B', 'o', 0, 0, 0, 0, 0, 0, 0, 0,0};
uint8_t serNum[5];
uint8_t writeDate[16] ={'T', 'e', 'n', 'g', ' ', 'B', 'o', 0, 0, 0, 0, 0, 0, 0, 0,0};
uchar sectorKeyA[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t sectorKeyA[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
int main() {
usart::init(1000000);
printf("Hello");
PORTC |= (1<<PC6)|(1<<PC7);
MFRC522 *mfrc522;
MFRC522 mfrc522_1(PB4, &PORTB, PB2, &PORTB);
MFRC522 mfrc522_2(PB3, &PORTB, PB2, &PORTB);
printf("%x %x ", &PORTB, &DDRB);
printf("%x %x ", &PORTC, &DDRC);
int reader;
while(1) {
MFRC522_Init();
uchar i;
uchar status;
uchar str[MAX_LEN];
uchar RC_size;
uchar blockAddr; //Select the address of the operation 063
uint8_t i;
uint8_t status;
uint8_t str[MAX_LEN];
uint8_t RC_size;
uint8_t blockAddr; //Select the address of the operation 063
// searching card, return card type
while(MFRC522_Request(PICC_REQIDL, str) != MI_OK);
while(1) {
mfrc522_1.Init();
if(mfrc522_1.Request(PICC_REQIDL, str) == MI_OK) {
mfrc522 = &mfrc522_1;
printf("Reader 1\n\r");
reader = 1;
break;
}
mfrc522_2.Init();
if(mfrc522_2.Request(PICC_REQIDL, str) == MI_OK) {
mfrc522 = &mfrc522_2;
printf("Reader 2\n\r");
reader = 0;
break;
}
}
status = MFRC522_Anticoll(str);
status = mfrc522->Anticoll(str);
memcpy(serNum, str, 5);
if (status == MI_OK)
{
@ -43,20 +66,21 @@ int main() {
}
// select card, return card capacity
RC_size = MFRC522_SelectTag(serNum);
RC_size = mfrc522->SelectTag(serNum);
if (RC_size != 0)
{}
if(PINC & (1<<PC6)) {
if(reader) {
// write data card
blockAddr = 7; // data block 7
status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA, serNum); // authentication
status = mfrc522->Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA, serNum); // authentication
if (status == MI_OK)
{
// write data
blockAddr = blockAddr - 3 ;
status = MFRC522_Write(blockAddr, writeDate);
writeDate[15] = writeDate[15] + 1;
status = mfrc522->Write(blockAddr, writeDate);
if(status == MI_OK)
{
printf("OK!\n\r");
@ -65,26 +89,27 @@ int main() {
} else {
// read card
blockAddr = 7; // data block 7
status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA, serNum); // authentication
status = mfrc522->Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA, serNum); // authentication
if (status == MI_OK)
{
// read data
blockAddr = blockAddr - 3 ;
status = MFRC522_Read(blockAddr, str);
status = mfrc522->Read(blockAddr, str);
if (status == MI_OK)
{
printf("Read from the card ,the data is : \n\r");
for (i=0; i<16; i++)
{
printf("%c", str[i]);
printf("%x", str[i]);
if(writeDate[i] != str[i]) printf("Not equal");
}
printf("\n\r");
}
}
}
printf("\n\r");
MFRC522_Halt(); // command card into sleeping mode
printf("\n\r\n\n\n");
mfrc522->Halt(); // command card into sleeping mode
_delay_ms(1000);
}