Skip to content

Commit

Permalink
More logical method ordering.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljos committed Sep 24, 2013
1 parent 0545ce1 commit c4eb64b
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 131 deletions.
234 changes: 111 additions & 123 deletions MFRC522.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,103 +38,6 @@ MFRC522::MFRC522(int sad, int reset) {

}


/**************************************************************************/
/*!
@brief Checks the firmware version of the chip.
@returns The firmware version of the MFRC522 chip.
*/
/**************************************************************************/
uint8_t MFRC522::getFirmwareVersion() {
uint8_t response;
response = readFromRegister(VersionReg);
return response;
}

/*
* Method name: selectTag
* Description:
* Selects the tag.
* Input parameters:
* serial - Incoming tag serial number
* Return value:
* Returns SAK response.
*/

/**************************************************************************/
/*!
@brief Selects a tag for processing.
@param serial The serial number of the tag that is to be selected.
@returns The SAK response from the tag.
*/
/**************************************************************************/
uint8_t MFRC522::selectTag(uint8_t *serial) {
uint8_t i;
uint8_t status;
uint8_t sak;
uint8_t result;
uint8_t buffer[9];

buffer[0] = MF1_SELECTTAG;
buffer[1] = 0x70;
for (i=0; i<5; i++) {
buffer[i+2] = *(serial+i);
}
calculateCRC(buffer, 7, &buffer[7]);
status = commandTag(MFRC522_TRANSCEIVE, buffer, 9, buffer, &result);

if ((status == MI_OK) && (result == 0x18)) {
sak = buffer[0];
}
else {
sak = 0;
}

return sak;
}

/**************************************************************************/
/*!
@brief Does the setup for the MFRC522.
*/
/**************************************************************************/
void MFRC522::begin() {
digitalWrite(_sad, HIGH);

reset();

//Timer: TPrescaler*TreloadVal/6.78MHz = 24ms
writeToRegister(TModeReg, 0x8D); // Tauto=1; f(Timer) = 6.78MHz/TPreScaler
writeToRegister(TPrescalerReg, 0x3E); // TModeReg[3..0] + TPrescalerReg
writeToRegister(TReloadRegL, 30);
writeToRegister(TReloadRegH, 0);

writeToRegister(TxAutoReg, 0x40); // 100%ASK
writeToRegister(ModeReg, 0x3D); // CRC initial value 0x6363

setBitMask(TxControlReg, 0x03); // Turn antenna on.
}

/**************************************************************************/
/*!
@brief Sends a SOFTRESET command to the MFRC522 chip.
*/
/**************************************************************************/
void MFRC522::reset() {
writeToRegister(CommandReg, MFRC522_SOFTRESET);
}

/**************************************************************************/
/*!
Expand Down Expand Up @@ -175,7 +78,6 @@ uint8_t MFRC522::readFromRegister(uint8_t addr) {
return val;
}


/**************************************************************************/
/*!
Expand Down Expand Up @@ -211,37 +113,51 @@ void MFRC522::clearBitMask(uint8_t reg, uint8_t mask) {
/**************************************************************************/
/*!
@brief Calculates the CRC value for some data that should be sent to
a tag.
@brief Does the setup for the MFRC522.
@param data The data to calculate the value for.
@param len The length of the data.
@param result The result of the CRC calculation.
*/
/**************************************************************************/
void MFRC522::begin() {
digitalWrite(_sad, HIGH);

reset();

//Timer: TPrescaler*TreloadVal/6.78MHz = 24ms
writeToRegister(TModeReg, 0x8D); // Tauto=1; f(Timer) = 6.78MHz/TPreScaler
writeToRegister(TPrescalerReg, 0x3E); // TModeReg[3..0] + TPrescalerReg
writeToRegister(TReloadRegL, 30);
writeToRegister(TReloadRegH, 0);

writeToRegister(TxAutoReg, 0x40); // 100%ASK
writeToRegister(ModeReg, 0x3D); // CRC initial value 0x6363

setBitMask(TxControlReg, 0x03); // Turn antenna on.
}

/**************************************************************************/
/*!
@brief Sends a SOFTRESET command to the MFRC522 chip.
*/
/**************************************************************************/
void MFRC522::calculateCRC(uint8_t *data, uint8_t len, uint8_t *result) {
uint8_t i, n;
void MFRC522::reset() {
writeToRegister(CommandReg, MFRC522_SOFTRESET);
}

clearBitMask(DivIrqReg, 0x04); // CRCIrq = 0
setBitMask(FIFOLevelReg, 0x80); // Clear the FIFO pointer
/**************************************************************************/
/*!
//Writing data to the FIFO.
for (i=0; i<len; i++) {
writeToRegister(FIFODataReg, *(data+i));
}
writeToRegister(CommandReg, MFRC522_CALCCRC);
@brief Checks the firmware version of the chip.
// Wait for the CRC calculation to complete.
i = 0xFF;
do {
n = readFromRegister(DivIrqReg);
i--;
} while ((i!=0) && !(n&0x04)); //CRCIrq = 1
@returns The firmware version of the MFRC522 chip.
// Read the result from the CRC calculation.
result[0] = readFromRegister(CRCResultRegL);
result[1] = readFromRegister(CRCResultRegM);
*/
/**************************************************************************/
uint8_t MFRC522::getFirmwareVersion() {
uint8_t response;
response = readFromRegister(VersionReg);
return response;
}

/**************************************************************************/
Expand Down Expand Up @@ -393,7 +309,7 @@ uint8_t MFRC522::requestTag(uint8_t mode, uint8_t *type) {
@brief Handles collisions that might occur if there are multiple
tags available.
@param serial The
@param serial The serial nb of the tag.
@returns Returns the status of the collision detection.
MI_ERR if something went wrong,
Expand All @@ -402,7 +318,7 @@ uint8_t MFRC522::requestTag(uint8_t mode, uint8_t *type) {
*/
/**************************************************************************/
uint8_t MFRC522::anticollision(uint8_t *serial) {
uint8_t MFRC522::antiCollision(uint8_t *serial) {
uint8_t status;
uint8_t i;
uint8_t check = 0;
Expand All @@ -429,6 +345,78 @@ uint8_t MFRC522::anticollision(uint8_t *serial) {
return status;
}

/**************************************************************************/
/*!
@brief Calculates the CRC value for some data that should be sent to
a tag.
@param data The data to calculate the value for.
@param len The length of the data.
@param result The result of the CRC calculation.
*/
/**************************************************************************/
void MFRC522::calculateCRC(uint8_t *data, uint8_t len, uint8_t *result) {
uint8_t i, n;

clearBitMask(DivIrqReg, 0x04); // CRCIrq = 0
setBitMask(FIFOLevelReg, 0x80); // Clear the FIFO pointer

//Writing data to the FIFO.
for (i=0; i<len; i++) {
writeToRegister(FIFODataReg, *(data+i));
}
writeToRegister(CommandReg, MFRC522_CALCCRC);

// Wait for the CRC calculation to complete.
i = 0xFF;
do {
n = readFromRegister(DivIrqReg);
i--;
} while ((i!=0) && !(n&0x04)); //CRCIrq = 1

// Read the result from the CRC calculation.
result[0] = readFromRegister(CRCResultRegL);
result[1] = readFromRegister(CRCResultRegM);
}

/**************************************************************************/
/*!
@brief Selects a tag for processing.
@param serial The serial number of the tag that is to be selected.
@returns The SAK response from the tag.
*/
/**************************************************************************/
uint8_t MFRC522::selectTag(uint8_t *serial) {
uint8_t i;
uint8_t status;
uint8_t sak;
uint8_t result;
uint8_t buffer[9];

buffer[0] = MF1_SELECTTAG;
buffer[1] = 0x70;
for (i=0; i<5; i++) {
buffer[i+2] = *(serial+i);
}
calculateCRC(buffer, 7, &buffer[7]);
status = commandTag(MFRC522_TRANSCEIVE, buffer, 9, buffer, &result);

if ((status == MI_OK) && (result == 0x18)) {
sak = buffer[0];
}
else {
sak = 0;
}

return sak;
}

/**************************************************************************/
/*!
Expand Down
13 changes: 6 additions & 7 deletions MFRC522.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,17 @@
class MFRC522 {
public:
MFRC522(int sad, int reset);
uint8_t getFirmwareVersion();
void begin();
void reset();
void writeToRegister(uint8_t addr, uint8_t val);
void antennaOn(void);
uint8_t readFromRegister(uint8_t addr);
void setBitMask(uint8_t reg, uint8_t mask);
void clearBitMask(uint8_t reg, uint8_t mask);
void calculateCRC(uint8_t *pIndata, uint8_t len, uint8_t *pOutData);
uint8_t requestTag(uint8_t mode, uint8_t *type);
void begin();
void reset();
uint8_t getFirmwareVersion();
uint8_t commandTag(uint8_t command, uint8_t *sendData, uint8_t sendLen, uint8_t *backData, uint8_t *backLen);
uint8_t anticollision(uint8_t *serial);
uint8_t requestTag(uint8_t mode, uint8_t *type);
uint8_t antiCollision(uint8_t *serial);
void calculateCRC(uint8_t *pIndata, uint8_t len, uint8_t *pOutData);
uint8_t selectTag(uint8_t *serial);
uint8_t authenticate(uint8_t mode, uint8_t block, uint8_t *key, uint8_t *serial);
uint8_t readFromTag(uint8_t blockAddr, uint8_t *recvData);
Expand Down
2 changes: 1 addition & 1 deletion examples/authenticate/authenticate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void loop() {
Serial.print(data[0], HEX);
Serial.println(data[1], HEX);

status = nfc.anticollision(data);
status = nfc.antiCollision(data);
memcpy(serial, data, 5);

Serial.println("The serial nb of the tag is:");
Expand Down

0 comments on commit c4eb64b

Please sign in to comment.