IRQ based tag polling
This commit is contained in:
263
mfrc522.cpp
263
mfrc522.cpp
@ -1,6 +1,7 @@
|
|||||||
#include "mfrc522.h"
|
#include "mfrc522.h"
|
||||||
|
|
||||||
|
Serial *Mfrc522::serial = nullptr;
|
||||||
|
uint8_t Mfrc522::reponseBuffer[Mfrc522::reponseBufferLen];
|
||||||
|
|
||||||
uint8_t Mfrc522::read(uint8_t addr)
|
uint8_t Mfrc522::read(uint8_t addr)
|
||||||
{
|
{
|
||||||
@ -58,25 +59,26 @@ void Mfrc522::updateBit(uint8_t addr, uint8_t bit, bool value)
|
|||||||
void (*_tagEnterdCb)(Mfrc522*, void*);
|
void (*_tagEnterdCb)(Mfrc522*, void*);
|
||||||
void* _userData;
|
void* _userData;
|
||||||
|
|
||||||
Mfrc522::Mfrc522(SpiMaster* spi, ShiftReg<NFC_PORTS>* csReg, uint8_t csPin,
|
Mfrc522::Mfrc522(SpiMaster* spi, ShiftReg<NFC_PORTS>* csReg, uint8_t csPin):
|
||||||
void (*tagEnterdCb)(Mfrc522*, void*), void* userData):
|
_csReg(csReg), _spi(spi), _csPin(csPin)
|
||||||
_csReg(csReg), _spi(spi), _csPin(csPin), _tagEnterdCb(tagEnterdCb), _userData(userData)
|
|
||||||
{
|
{
|
||||||
write(CommandReg, SOFTRESET);
|
write(CommandReg, SOFTRESET);
|
||||||
_delay_ms(100);
|
_delay_ms(100);
|
||||||
|
|
||||||
write(TModeReg, 0x80);
|
write(TModeReg, 0x80);
|
||||||
write(TPrescalerReg, 0xA9);
|
write(TPrescalerReg, 0xA9);
|
||||||
write(TReloadRegH, 0x03);
|
write(TReloadRegH, 0x0B);
|
||||||
write(TReloadRegL, 0xE8);
|
write(TReloadRegL, 0xB8);
|
||||||
write(ModWidthReg, 0x26);
|
write(ModWidthReg, 0x26);
|
||||||
|
|
||||||
write(RFCfgReg, 0b111 << 4); //set gain to 48dB
|
write(RFCfgReg, 0b111 << 4); //set gain to 48dB
|
||||||
|
|
||||||
write(TxAutoReg, 0x40); // Default 0x00. Force a 100 % ASK modulation independent of the ModGsPReg register setting
|
write(TxAutoReg, 0x40); // Default 0x00. Force a 100 % ASK modulation independent of the ModGsPReg register setting
|
||||||
|
updateBit(RxModeReg, 3, true);
|
||||||
write(ModeReg, 0x3D); // Default 0x3F. Set the preset value for the CRC coprocessor for the CalcCRC command to 0x6363 (ISO 14443-3 part 6.2.4)
|
write(ModeReg, 0x3D); // Default 0x3F. Set the preset value for the CRC coprocessor for the CalcCRC command to 0x6363 (ISO 14443-3 part 6.2.4)
|
||||||
//write(DivIEnReg, 0b10010000); // enable MfinActIrq as push-pull
|
write(ComIrqReg, 0b01111111);
|
||||||
//write(ComIEnReg, 0b00100000); // enable Rx irq
|
write(ComIEnReg, 0); // invert irq pin (high is active)
|
||||||
|
write(DivIEnReg, 1 << 7); // enable MfinActIrq as push-pull
|
||||||
|
|
||||||
setRf(true);
|
setRf(true);
|
||||||
}
|
}
|
||||||
@ -112,18 +114,12 @@ uint8_t Mfrc522::calculateCrc(uint8_t *data, uint8_t length, uint16_t *result)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Mfrc522::commuicateWithTag(uint8_t command, uint8_t waitIrq,
|
void Mfrc522::setupTransceive(uint8_t *sendData, uint8_t sendLen, uint8_t validBits, uint8_t rxAlign)
|
||||||
uint8_t *sendData, uint8_t sendLen,
|
|
||||||
uint8_t *recvData, uint8_t *recvLen,
|
|
||||||
uint8_t validBits, uint8_t rxAlign,
|
|
||||||
uint8_t *rxValidBits)
|
|
||||||
{
|
{
|
||||||
write(CommandReg, IDLE);
|
|
||||||
write(ComIrqReg, 0b01111111); // clear irqs
|
|
||||||
write(FIFOLevelReg, 1 << 7); // Flush fifo Buffer;
|
write(FIFOLevelReg, 1 << 7); // Flush fifo Buffer;
|
||||||
write(FIFODataReg, sendData, sendLen); // Fill fifo
|
write(FIFODataReg, sendData, sendLen); // Fill fifo
|
||||||
write(BitFramingReg, (rxAlign << 4) + validBits);
|
write(BitFramingReg, (rxAlign << 4) + validBits);
|
||||||
write(CommandReg, command); // Execute the command
|
write(CommandReg, TRANSCEIVE); // Execute the command
|
||||||
|
|
||||||
if(serial)
|
if(serial)
|
||||||
{
|
{
|
||||||
@ -140,14 +136,82 @@ uint8_t Mfrc522::commuicateWithTag(uint8_t command, uint8_t waitIrq,
|
|||||||
serial->write((int)read(BitFramingReg));
|
serial->write((int)read(BitFramingReg));
|
||||||
serial->putChar('\n');
|
serial->putChar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateBit(BitFramingReg, 7, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Mfrc522::transceiveAsync(void (*transceiveCb)(uint8_t, Mfrc522*, uint8_t*, uint8_t, void*), void* userData,
|
||||||
|
uint8_t *sendData, uint8_t sendLen, uint8_t validBits, uint8_t rxAlign)
|
||||||
|
{
|
||||||
|
if(mode != MODE_IDLE)
|
||||||
|
return BUSY;
|
||||||
|
|
||||||
|
mode = MODE_TRANSCEIVE;
|
||||||
|
|
||||||
if (command == TRANSCEIVE)
|
_transceiveCb = transceiveCb;
|
||||||
updateBit(BitFramingReg, 7, true);
|
_transceiveUserData = userData;
|
||||||
|
|
||||||
|
write(CommandReg, IDLE);
|
||||||
|
|
||||||
|
write(ComIrqReg, 0b01111111); // clear irqs
|
||||||
|
write(ComIEnReg, (1 << 5) | (1 << 0));
|
||||||
|
|
||||||
|
setupTransceive(sendData, sendLen, validBits, rxAlign);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mfrc522::transceiveAsyncFinish(uint8_t irq)
|
||||||
|
{
|
||||||
|
write(ComIEnReg, 0); // disable irqs
|
||||||
|
|
||||||
|
if(!_transceiveCb)
|
||||||
|
{
|
||||||
|
mode = MODE_IDLE;
|
||||||
|
updateBit(FIFOLevelReg, 7, true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t errorRegValue = read(ErrorReg);
|
||||||
|
if (errorRegValue & 0b00010011) // BufferOvfl ParityErr ProtocolErr
|
||||||
|
{
|
||||||
|
mode = MODE_IDLE;
|
||||||
|
_transceiveCb(ERR, this, nullptr, 0, _transceiveUserData);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(irq & 1)
|
||||||
|
{
|
||||||
|
mode = MODE_IDLE;
|
||||||
|
_transceiveCb(TIMEOUT, this, nullptr, 0, _transceiveUserData);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t fifoBites = read(FIFOLevelReg);
|
||||||
|
|
||||||
|
if(fifoBites > reponseBufferLen)
|
||||||
|
_transceiveCb(LEN, this, nullptr, 0, _transceiveUserData);
|
||||||
|
read(FIFODataReg, reponseBuffer, fifoBites);
|
||||||
|
mode = MODE_IDLE;
|
||||||
|
if(errorRegValue & 0x08)
|
||||||
|
_transceiveCb(COLLISION, this, reponseBuffer, fifoBites, _transceiveUserData);
|
||||||
|
else
|
||||||
|
_transceiveCb(0, this, reponseBuffer, fifoBites, _transceiveUserData);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Mfrc522::transceive(uint8_t *sendData, uint8_t sendLen,
|
||||||
|
uint8_t *recvData, uint8_t *recvLen,
|
||||||
|
uint8_t validBits, uint8_t rxAlign,
|
||||||
|
uint8_t *rxValidBits)
|
||||||
|
{
|
||||||
|
write(CommandReg, IDLE);
|
||||||
|
write(ComIrqReg, 0b01111111); // clear irqs
|
||||||
|
setupTransceive(sendData, sendLen, validBits, rxAlign);
|
||||||
|
|
||||||
uint16_t i = 2000;
|
uint16_t i = 2000;
|
||||||
uint8_t irq = read(ComIrqReg);
|
uint8_t irq = read(ComIrqReg);
|
||||||
while(irq & waitIrq)
|
while(!(irq & 0x30)) // RxIRq and IdleIRq
|
||||||
{
|
{
|
||||||
irq = read(ComIrqReg);
|
irq = read(ComIrqReg);
|
||||||
if(irq & 0x01 || --i == 0)
|
if(irq & 0x01 || --i == 0)
|
||||||
@ -200,13 +264,6 @@ uint8_t Mfrc522::commuicateWithTag(uint8_t command, uint8_t waitIrq,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Mfrc522::transceive(uint8_t *sendData, uint8_t sendLen, uint8_t *recvData, uint8_t *recvLen,
|
|
||||||
uint8_t validBits, uint8_t rxAlign, uint8_t *rxValidBits)
|
|
||||||
{
|
|
||||||
uint8_t waitIRq = 0x30; // RxIRq and IdleIRq
|
|
||||||
return commuicateWithTag(TRANSCEIVE, waitIRq, sendData, sendLen, recvData,
|
|
||||||
recvLen, validBits, rxAlign, rxValidBits);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t Mfrc522::wakeupTag(uint8_t* bufferATQA, uint8_t *bufferLen)
|
uint8_t Mfrc522::wakeupTag(uint8_t* bufferATQA, uint8_t *bufferLen)
|
||||||
{
|
{
|
||||||
@ -234,14 +291,13 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
uint8_t index;
|
uint8_t index;
|
||||||
uint8_t uidIndex; // The first index in uid->uidByte[] that is used in the current Cascade Level.
|
uint8_t uidIndex; // The first index in uid->uidByte[] that is used in the current Cascade Level.
|
||||||
int8_t currentLevelKnownBits; // The number of known UID bits in the current Cascade Level.
|
int8_t currentLevelKnownBits; // The number of known UID bits in the current Cascade Level.
|
||||||
uint8_t buffer[9]; // The SELECT/ANTICOLLISION commands uses a 7 uint8_t standard frame + 2 uint8_ts CRC_A
|
|
||||||
uint8_t bufferUsed; // The number of uint8_ts used in the buffer, ie the number of uint8_ts to transfer to the FIFO
|
uint8_t bufferUsed; // The number of uint8_ts used in the buffer, ie the number of uint8_ts to transfer to the FIFO
|
||||||
uint8_t txLastBits; // Used in BitFramingReg. The number of valid bits in the last transmitted uint8_t.
|
uint8_t txLastBits; // Used in BitFramingReg. The number of valid bits in the last transmitted uint8_t.
|
||||||
uint8_t *responseBuffer;
|
uint8_t *responseBufferPtr;
|
||||||
uint8_t responseLength;
|
uint8_t responseLength;
|
||||||
|
|
||||||
if(serial)
|
if(serial)
|
||||||
serial->write_p(PSTR("Select\n"));
|
serial->write_p(PSTR("Select\n"));
|
||||||
|
|
||||||
// Description of buffer structure:
|
// Description of buffer structure:
|
||||||
// Byte 0: SEL Indicates the Cascade Level: PICC_CMD_SEL_CL1, PICC_CMD_SEL_CL2 or PICC_CMD_SEL_CL3
|
// Byte 0: SEL Indicates the Cascade Level: PICC_CMD_SEL_CL1, PICC_CMD_SEL_CL2 or PICC_CMD_SEL_CL3
|
||||||
@ -277,17 +333,17 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
switch(cascadeLevel)
|
switch(cascadeLevel)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
buffer[0] = PICC_CMD_SEL_CL1;
|
reponseBuffer[0] = PICC_CMD_SEL_CL1;
|
||||||
uidIndex = 0;
|
uidIndex = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
buffer[0] = PICC_CMD_SEL_CL2;
|
reponseBuffer[0] = PICC_CMD_SEL_CL2;
|
||||||
uidIndex = 3;
|
uidIndex = 3;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
buffer[0] = PICC_CMD_SEL_CL3;
|
reponseBuffer[0] = PICC_CMD_SEL_CL3;
|
||||||
uidIndex = 6;
|
uidIndex = 6;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -309,7 +365,7 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
if (bytesToCopy > maxBytes)
|
if (bytesToCopy > maxBytes)
|
||||||
bytesToCopy = maxBytes;
|
bytesToCopy = maxBytes;
|
||||||
for (count = 0; count < bytesToCopy; count++)
|
for (count = 0; count < bytesToCopy; count++)
|
||||||
buffer[index++] = uid->uidByte[uidIndex + count];
|
reponseBuffer[index++] = uid->uidByte[uidIndex + count];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Repeat anti collision loop until we can transmit all UID bits + BCC and receive a SAK - max 32 iterations.
|
// Repeat anti collision loop until we can transmit all UID bits + BCC and receive a SAK - max 32 iterations.
|
||||||
@ -319,11 +375,11 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
// Find out how many bits and bytes to send and receive.
|
// Find out how many bits and bytes to send and receive.
|
||||||
if (currentLevelKnownBits >= 32) // All UID bits in this Cascade Level are known. This is a SELECT.
|
if (currentLevelKnownBits >= 32) // All UID bits in this Cascade Level are known. This is a SELECT.
|
||||||
{
|
{
|
||||||
buffer[1] = 0x70; // NVB - Number of Valid Bits: Seven whole bytes
|
reponseBuffer[1] = 0x70; // NVB - Number of Valid Bits: Seven whole bytes
|
||||||
// Calculate BCC - Block Check Character
|
// Calculate BCC - Block Check Character
|
||||||
buffer[6] = buffer[2] ^ buffer[3] ^ buffer[4] ^ buffer[5];
|
reponseBuffer[6] = reponseBuffer[2] ^ reponseBuffer[3] ^ reponseBuffer[4] ^ reponseBuffer[5];
|
||||||
// Calculate CRC_A
|
// Calculate CRC_A
|
||||||
result = calculateCrc(buffer, 7, reinterpret_cast<uint16_t*>(&buffer[7]));
|
result = calculateCrc(reponseBuffer, 7, reinterpret_cast<uint16_t*>(&reponseBuffer[7]));
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
if(serial)
|
if(serial)
|
||||||
@ -333,7 +389,7 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
txLastBits = 0; // 0 => All 8 bits are valid.
|
txLastBits = 0; // 0 => All 8 bits are valid.
|
||||||
bufferUsed = 9;
|
bufferUsed = 9;
|
||||||
// Store response in the last 3 bytes of buffer (BCC and CRC_A - not needed after tx)
|
// Store response in the last 3 bytes of buffer (BCC and CRC_A - not needed after tx)
|
||||||
responseBuffer = &buffer[6];
|
responseBufferPtr = &reponseBuffer[6];
|
||||||
responseLength = 3;
|
responseLength = 3;
|
||||||
}
|
}
|
||||||
else // This is an ANTICOLLISION.
|
else // This is an ANTICOLLISION.
|
||||||
@ -341,11 +397,11 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
txLastBits = currentLevelKnownBits % 8;
|
txLastBits = currentLevelKnownBits % 8;
|
||||||
count = currentLevelKnownBits / 8; // Number of whole bytes in the UID part.
|
count = currentLevelKnownBits / 8; // Number of whole bytes in the UID part.
|
||||||
index = 2 + count; // Number of whole bytes: SEL + NVB + UIDs
|
index = 2 + count; // Number of whole bytes: SEL + NVB + UIDs
|
||||||
buffer[1] = (index << 4) + txLastBits; // NVB - Number of Valid Bits
|
reponseBuffer[1] = (index << 4) + txLastBits; // NVB - Number of Valid Bits
|
||||||
bufferUsed = index + (txLastBits ? 1 : 0);
|
bufferUsed = index + (txLastBits ? 1 : 0);
|
||||||
// Store response in the unused part of buffer
|
// Store response in the unused part of buffer
|
||||||
responseBuffer = &buffer[index];
|
responseBufferPtr = &reponseBuffer[index];
|
||||||
responseLength = sizeof(buffer) - index;
|
responseLength = sizeof(reponseBuffer) - index;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set bit adjustments
|
// Set bit adjustments
|
||||||
@ -365,7 +421,7 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Transmit the buffer and receive the response.
|
// Transmit the buffer and receive the response.
|
||||||
result = transceive(buffer, bufferUsed, responseBuffer, &responseLength, txLastBits, rxAlign, &txLastBits);
|
result = transceive(reponseBuffer, bufferUsed, responseBufferPtr, &responseLength, txLastBits, rxAlign, &txLastBits);
|
||||||
if (result == COLLISION) // More than one PICC in the field => collision.
|
if (result == COLLISION) // More than one PICC in the field => collision.
|
||||||
{
|
{
|
||||||
result = read(CollReg); // CollReg[7..0] bits are: ValuesAfterColl reserved CollPosNotValid CollPos[4:0]
|
result = read(CollReg); // CollReg[7..0] bits are: ValuesAfterColl reserved CollPosNotValid CollPos[4:0]
|
||||||
@ -390,7 +446,7 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
count = currentLevelKnownBits % 8; // The bit to modify
|
count = currentLevelKnownBits % 8; // The bit to modify
|
||||||
checkBit = (currentLevelKnownBits - 1) % 8;
|
checkBit = (currentLevelKnownBits - 1) % 8;
|
||||||
index = 1 + (currentLevelKnownBits / 8) + (count ? 1 : 0); // First byte is index 0.
|
index = 1 + (currentLevelKnownBits / 8) + (count ? 1 : 0); // First byte is index 0.
|
||||||
buffer[index] |= (1 << checkBit);
|
reponseBuffer[index] |= (1 << checkBit);
|
||||||
}
|
}
|
||||||
else if (result != 0)
|
else if (result != 0)
|
||||||
{
|
{
|
||||||
@ -410,11 +466,11 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
// We do not check the CBB - it was constructed by us above.
|
// We do not check the CBB - it was constructed by us above.
|
||||||
|
|
||||||
// Copy the found UID bytes from buffer[] to uid->uidByte[]
|
// Copy the found UID bytes from buffer[] to uid->uidByte[]
|
||||||
index = (buffer[2] == PICC_CMD_CT) ? 3 : 2; // source index in buffer[]
|
index = (reponseBuffer[2] == PICC_CMD_CT) ? 3 : 2; // source index in buffer[]
|
||||||
bytesToCopy = (buffer[2] == PICC_CMD_CT) ? 3 : 4;
|
bytesToCopy = (reponseBuffer[2] == PICC_CMD_CT) ? 3 : 4;
|
||||||
for (count = 0; count < bytesToCopy; count++)
|
for (count = 0; count < bytesToCopy; count++)
|
||||||
{
|
{
|
||||||
uid->uidByte[uidIndex + count] = buffer[index++];
|
uid->uidByte[uidIndex + count] = reponseBuffer[index++];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check response SAK (Select Acknowledge)
|
// Check response SAK (Select Acknowledge)
|
||||||
@ -431,19 +487,19 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
return ERR;
|
return ERR;
|
||||||
}
|
}
|
||||||
// Verify CRC_A - do our own calculation and store the control in buffer[2..3] - those bytes are not needed anymore.
|
// Verify CRC_A - do our own calculation and store the control in buffer[2..3] - those bytes are not needed anymore.
|
||||||
result = calculateCrc(responseBuffer, 1, reinterpret_cast<uint16_t*>(&buffer[2]));
|
result = calculateCrc(responseBufferPtr, 1, reinterpret_cast<uint16_t*>(&reponseBuffer[2]));
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
return result;
|
return result;
|
||||||
if ((buffer[2] != responseBuffer[1]) || (buffer[3] != responseBuffer[2]))
|
if ((reponseBuffer[2] != responseBufferPtr[1]) || (reponseBuffer[3] != responseBufferPtr[2]))
|
||||||
return CRC;
|
return CRC;
|
||||||
if (responseBuffer[0] & 0x04) // Cascade bit set - UID not complete yes
|
if (responseBufferPtr[0] & 0x04) // Cascade bit set - UID not complete yes
|
||||||
{
|
{
|
||||||
cascadeLevel++;
|
cascadeLevel++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
uidComplete = true;
|
uidComplete = true;
|
||||||
uid->sak = responseBuffer[0];
|
uid->sak = responseBufferPtr[0];
|
||||||
}
|
}
|
||||||
} // End of while ( ! uidComplete)
|
} // End of while ( ! uidComplete)
|
||||||
|
|
||||||
@ -453,9 +509,53 @@ uint8_t Mfrc522::selectTag(Uid *uid)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t Mfrc522::getUid(Uid *uid)
|
||||||
|
{
|
||||||
|
uint8_t bufferOut[2];
|
||||||
|
uint8_t reponseBuffer[5];
|
||||||
|
|
||||||
|
bufferOut[0] = PICC_CMD_SEL_CL1;
|
||||||
|
bufferOut[1] = 2 << 4;
|
||||||
|
|
||||||
|
uint8_t rxLen = sizeof(reponseBuffer);
|
||||||
|
|
||||||
|
uint8_t ret = transceive(bufferOut, 2, reponseBuffer, &rxLen);
|
||||||
|
if(ret != 0)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
if(rxLen != 5)
|
||||||
|
return ERR;
|
||||||
|
|
||||||
|
uid->size = 4;
|
||||||
|
|
||||||
|
for(uint8_t i = 0; i < uid->size; ++i)
|
||||||
|
{
|
||||||
|
uid->uidByte[i] = reponseBuffer[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void Mfrc522::irq()
|
void Mfrc522::irq()
|
||||||
{
|
{
|
||||||
|
if(mode == MODE_TRANSCEIVE)
|
||||||
|
{
|
||||||
|
uint8_t irqs = read(ComIrqReg);
|
||||||
|
|
||||||
|
if(irqs)
|
||||||
|
{
|
||||||
|
if(serial)
|
||||||
|
{
|
||||||
|
serial->write("IRQS: ");
|
||||||
|
serial->write((int)irqs);
|
||||||
|
serial->putChar('\n');
|
||||||
|
}
|
||||||
|
write(ComIrqReg, 0b01111111); // clear irqs
|
||||||
|
transceiveAsyncFinish(irqs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(serial)
|
||||||
|
serial->write("IRQ wrong mode\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mfrc522::setRf(bool on)
|
void Mfrc522::setRf(bool on)
|
||||||
@ -469,9 +569,8 @@ void Mfrc522::setRf(bool on)
|
|||||||
|
|
||||||
bool Mfrc522::cardPresent()
|
bool Mfrc522::cardPresent()
|
||||||
{
|
{
|
||||||
uint8_t bufferATQA[2];
|
uint8_t bufferLen = sizeof(reponseBuffer);
|
||||||
uint8_t bufferLen = sizeof(bufferATQA);
|
uint8_t ret = wakeupTag(reponseBuffer, &bufferLen);
|
||||||
uint8_t ret = wakeupTag(bufferATQA, &bufferLen);
|
|
||||||
return ret == 0 || ret == COLLISION;
|
return ret == 0 || ret == COLLISION;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,6 +579,12 @@ bool Mfrc522::probe(SpiMaster* spi, ShiftReg<NFC_PORTS>* csReg, uint8_t csPin)
|
|||||||
csReg->setBit(csPin, false);
|
csReg->setBit(csPin, false);
|
||||||
spi->readWrite((VersionReg << 1) | (1 << 7));
|
spi->readWrite((VersionReg << 1) | (1 << 7));
|
||||||
uint8_t version = spi->readWrite();
|
uint8_t version = spi->readWrite();
|
||||||
|
if(serial)
|
||||||
|
{
|
||||||
|
serial->write_p(PSTR("Got version register: "));
|
||||||
|
serial->write((int)version);
|
||||||
|
serial->putChar('\n');
|
||||||
|
}
|
||||||
csReg->setBit(csPin, true);
|
csReg->setBit(csPin, true);
|
||||||
return version == 0x91 || version == 0x92;
|
return version == 0x91 || version == 0x92;
|
||||||
}
|
}
|
||||||
@ -487,7 +592,6 @@ bool Mfrc522::probe(SpiMaster* spi, ShiftReg<NFC_PORTS>* csReg, uint8_t csPin)
|
|||||||
bool Mfrc522::testFifo()
|
bool Mfrc522::testFifo()
|
||||||
{
|
{
|
||||||
uint8_t buffer[8] = {42, 43, 44, 45, 46, 47, 48, 49};
|
uint8_t buffer[8] = {42, 43, 44, 45, 46, 47, 48, 49};
|
||||||
uint8_t buffer2[8] = {};
|
|
||||||
write(FIFOLevelReg, 1 << 7); // Flush fifo Buffer;
|
write(FIFOLevelReg, 1 << 7); // Flush fifo Buffer;
|
||||||
write(FIFODataReg, buffer, sizeof(buffer)); // Fill fifo
|
write(FIFODataReg, buffer, sizeof(buffer)); // Fill fifo
|
||||||
|
|
||||||
@ -495,15 +599,58 @@ bool Mfrc522::testFifo()
|
|||||||
serial->write_p(PSTR("Fifo buffer contains: "));
|
serial->write_p(PSTR("Fifo buffer contains: "));
|
||||||
|
|
||||||
uint8_t len = read(FIFOLevelReg);
|
uint8_t len = read(FIFOLevelReg);
|
||||||
read(FIFODataReg, buffer2, len);
|
read(FIFODataReg, reponseBuffer, len);
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
for(uint8_t i = 0; i < len; ++i)
|
for(uint8_t i = 0; i < len; ++i)
|
||||||
{
|
{
|
||||||
if(buffer[i] != buffer2[i])
|
if(buffer[i] != reponseBuffer[i])
|
||||||
ret = false;
|
ret = false;
|
||||||
serial->write((int)buffer2[i]);
|
if(serial)
|
||||||
serial->putChar(' ');
|
{
|
||||||
|
serial->write((int)reponseBuffer[i]);
|
||||||
|
serial->putChar(' ');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
serial->putChar('\n');
|
if(serial)
|
||||||
|
serial->putChar('\n');
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Mfrc522::detectAsync(void (*tagEnterdCb)(Mfrc522*, void*), void* userData)
|
||||||
|
{
|
||||||
|
if(irqDetect)
|
||||||
|
return false;
|
||||||
|
_tagEnterdCb = tagEnterdCb;
|
||||||
|
_userData = userData;
|
||||||
|
irqDetect = true;
|
||||||
|
updateBit(CollReg, 7, false);
|
||||||
|
uint8_t data = PICC_CMD_WUPA;
|
||||||
|
if(transceiveAsync(&detectAsyncCb, nullptr, &data, 1, 7) != 0)
|
||||||
|
{
|
||||||
|
irqDetect = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mfrc522::stopAsync()
|
||||||
|
{
|
||||||
|
write(CommandReg, IDLE);
|
||||||
|
write(FIFOLevelReg, 1 << 7);
|
||||||
|
mode = MODE_IDLE;
|
||||||
|
irqDetect = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mfrc522::detectAsyncCb(uint8_t ret, Mfrc522* reader, uint8_t* response, uint8_t responseLen, void* userData)
|
||||||
|
{
|
||||||
|
if((ret == 0 || ret == COLLISION) && reader->_tagEnterdCb)
|
||||||
|
{
|
||||||
|
reader->stopAsync();
|
||||||
|
reader->_tagEnterdCb(reader, reader->_userData);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reader->irqDetect = false;
|
||||||
|
reader->detectAsync(reader->_tagEnterdCb, reader->_userData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
90
mfrc522.h
90
mfrc522.h
@ -14,7 +14,8 @@ public:
|
|||||||
static constexpr uint8_t LEN = 3; // Buffer length error
|
static constexpr uint8_t LEN = 3; // Buffer length error
|
||||||
static constexpr uint8_t COLLISION = 4; // Chip collision
|
static constexpr uint8_t COLLISION = 4; // Chip collision
|
||||||
static constexpr uint8_t CRC = 5; // CRC incorrect collision
|
static constexpr uint8_t CRC = 5; // CRC incorrect collision
|
||||||
static constexpr uint8_t ERR = 6; // General error
|
static constexpr uint8_t BUSY = 6; // System is busy
|
||||||
|
static constexpr uint8_t ERR = 7; // General error
|
||||||
|
|
||||||
// Command words
|
// Command words
|
||||||
static constexpr uint8_t IDLE = 0x00; // NO action; Cancel the current command
|
static constexpr uint8_t IDLE = 0x00; // NO action; Cancel the current command
|
||||||
@ -132,13 +133,32 @@ public:
|
|||||||
// The PICC_CMD_MF_READ and PICC_CMD_MF_WRITE can also be used for MIFARE Ultralight.
|
// The PICC_CMD_MF_READ and PICC_CMD_MF_WRITE can also be used for MIFARE Ultralight.
|
||||||
static constexpr uint8_t PICC_CMD_UL_WRITE = 0xA2; // Writes one 4 byte page to the PICC
|
static constexpr uint8_t PICC_CMD_UL_WRITE = 0xA2; // Writes one 4 byte page to the PICC
|
||||||
|
|
||||||
typedef struct
|
class Uid
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
uint8_t size; // Number of bytes in the UID. 4, 7 or 10.
|
uint8_t size; // Number of bytes in the UID. 4, 7 or 10.
|
||||||
uint8_t uidByte[10];
|
uint8_t uidByte[10];
|
||||||
uint8_t sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection.
|
uint8_t sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection.
|
||||||
} Uid;
|
bool operator==(Uid& in)
|
||||||
|
{
|
||||||
|
if(size != in.size)
|
||||||
|
return false;
|
||||||
|
for(uint8_t i = 0; i < size; ++i)
|
||||||
|
{
|
||||||
|
if(uidByte[i] != in.uidByte[i])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool operator!=(Uid& in)
|
||||||
|
{
|
||||||
|
return !operator==(in);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Modes
|
||||||
|
static constexpr uint8_t MODE_IDLE = 0;
|
||||||
|
static constexpr uint8_t MODE_TRANSCEIVE = 1;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ShiftReg<NFC_PORTS>* _csReg;
|
ShiftReg<NFC_PORTS>* _csReg;
|
||||||
@ -146,44 +166,70 @@ private:
|
|||||||
|
|
||||||
uint8_t _csPin;
|
uint8_t _csPin;
|
||||||
|
|
||||||
|
void (*_tagEnterdCb)(Mfrc522*, void*);
|
||||||
|
void* _userData;
|
||||||
|
|
||||||
|
void (*_transceiveCb)(uint8_t ret, Mfrc522* reader, uint8_t* response, uint8_t responseLen, void* userData);
|
||||||
|
void* _transceiveUserData;
|
||||||
|
|
||||||
|
static constexpr uint8_t reponseBufferLen = 16;
|
||||||
|
static uint8_t reponseBuffer[reponseBufferLen];
|
||||||
|
|
||||||
|
volatile uint8_t mode = MODE_IDLE;
|
||||||
|
|
||||||
|
volatile bool irqDetect = false;
|
||||||
|
|
||||||
|
void setupTransceive(uint8_t *sendData, uint8_t sendLen, uint8_t validBits = 0, uint8_t rxAlign = 0);
|
||||||
|
|
||||||
|
uint8_t transceiveAsync(void (*transceiveCb)(uint8_t, Mfrc522*, uint8_t*, uint8_t, void*), void* userData, uint8_t *sendData,
|
||||||
|
uint8_t sendLen, uint8_t validBits = 0, uint8_t rxAlign = 0);
|
||||||
|
void transceiveAsyncFinish(uint8_t irq);
|
||||||
|
|
||||||
|
uint8_t transceive(uint8_t *sendData, uint8_t sendLen, uint8_t *recvData, uint8_t *recvLen,
|
||||||
|
uint8_t validBits= 0, uint8_t rxAlign = 0, uint8_t *rxValidBits = nullptr);
|
||||||
|
|
||||||
|
static void detectAsyncCb(uint8_t ret, Mfrc522* reader, uint8_t* response, uint8_t responseLen, void* userData);
|
||||||
|
|
||||||
|
public:
|
||||||
uint8_t read(uint8_t addr);
|
uint8_t read(uint8_t addr);
|
||||||
void read(uint8_t addr, uint8_t* data, uint8_t datalen, uint8_t rxAlign = 0);
|
void read(uint8_t addr, uint8_t* data, uint8_t datalen, uint8_t rxAlign = 0);
|
||||||
void write(uint8_t addr, uint8_t data);
|
void write(uint8_t addr, uint8_t data);
|
||||||
void write(uint8_t addr, uint8_t* data, uint8_t datalen);
|
void write(uint8_t addr, uint8_t* data, uint8_t datalen);
|
||||||
void updateBit(uint8_t addr, uint8_t bit, bool value);
|
void updateBit(uint8_t addr, uint8_t bit, bool value);
|
||||||
|
static Serial* serial;
|
||||||
void (*_tagEnterdCb)(Mfrc522*, void*);
|
|
||||||
void* _userData;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
inline static Serial* serial = nullptr;
|
Mfrc522(SpiMaster* spi, ShiftReg<NFC_PORTS>* csReg, uint8_t csPin);
|
||||||
|
|
||||||
Mfrc522(SpiMaster* spi, ShiftReg<NFC_PORTS>* csReg, uint8_t csPin,
|
|
||||||
void (*tagEnterdCb)(Mfrc522*, void*) = nullptr, void* userData = nullptr);
|
|
||||||
|
|
||||||
uint8_t calculateCrc(uint8_t *data, uint8_t length, uint16_t *result);
|
uint8_t calculateCrc(uint8_t *data, uint8_t length, uint16_t *result);
|
||||||
|
|
||||||
uint8_t commuicateWithTag(uint8_t command, uint8_t waitIrq,
|
|
||||||
uint8_t *sendData, uint8_t sendLen,
|
|
||||||
uint8_t *recvData, uint8_t *recvLen,
|
|
||||||
uint8_t validBits = 0, uint8_t rxAlign = 0,
|
|
||||||
uint8_t *rxValidBits = nullptr);
|
|
||||||
|
|
||||||
uint8_t transceive(uint8_t *sendData, uint8_t sendLen, uint8_t *recvData, uint8_t *recvLen,
|
|
||||||
uint8_t validBits = 0, uint8_t rxAlign = 0, uint8_t *rxValidBits = nullptr);
|
|
||||||
|
|
||||||
uint8_t wakeupTag(uint8_t* bufferATQA, uint8_t *bufferLen);
|
uint8_t wakeupTag(uint8_t* bufferATQA, uint8_t *bufferLen);
|
||||||
|
|
||||||
uint8_t selectTag(Uid *uid);
|
uint8_t selectTag(Uid *uid);
|
||||||
|
|
||||||
|
uint8_t getUid(Uid *uid);
|
||||||
|
|
||||||
void irq();
|
void irq();
|
||||||
|
|
||||||
void setRf(bool on);
|
void setRf(bool on);
|
||||||
|
|
||||||
|
bool detectAsync(void (*tagEnterdCb)(Mfrc522*, void*) = nullptr, void* userData = nullptr);
|
||||||
|
|
||||||
|
void stopAsync();
|
||||||
|
|
||||||
bool cardPresent();
|
bool cardPresent();
|
||||||
|
|
||||||
bool testFifo();
|
bool testFifo();
|
||||||
|
|
||||||
|
bool checkIrq(uint8_t *irq)
|
||||||
|
{
|
||||||
|
write(ComIrqReg, 0b01111111);
|
||||||
|
if(read(Status1Reg) & (1 << 4))
|
||||||
|
{
|
||||||
|
*irq = read(ComIrqReg);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static bool probe(SpiMaster* spi, ShiftReg<NFC_PORTS>* csReg, uint8_t csPin);
|
static bool probe(SpiMaster* spi, ShiftReg<NFC_PORTS>* csReg, uint8_t csPin);
|
||||||
};
|
};
|
||||||
|
186
nfcbord.cpp
186
nfcbord.cpp
@ -7,18 +7,69 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "writepin.h"
|
#include "writepin.h"
|
||||||
|
|
||||||
|
NfcBoard nfcBoard;
|
||||||
|
|
||||||
extern char buffer[SNPRINTF_BUFFER_SIZE];
|
extern char buffer[SNPRINTF_BUFFER_SIZE];
|
||||||
|
|
||||||
|
ISR(PCINT1_vect, ISR_NOBLOCK)
|
||||||
|
{
|
||||||
|
if(readPin(&PINC, PC3))
|
||||||
|
{
|
||||||
|
uint8_t* data = nfcBoard.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)
|
||||||
|
{
|
||||||
|
if(nfcBoard.irqPins[j] == i)
|
||||||
|
{
|
||||||
|
nfcBoard.readers[j].irq();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NfcBoard::NfcBoard():
|
NfcBoard::NfcBoard():
|
||||||
csReg(&PORTC, PC1, PC2, PC0),
|
csReg(&PORTC, PC1, PC2, PC0),
|
||||||
irqReg(&PORTC, &PINB, PC5, PB1, PC4)
|
irqReg(&PORTC, &PINB, PC5, PB1, PC4)
|
||||||
{
|
{
|
||||||
DDRC = (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC4) | (1 << PC5);
|
DDRC |= (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC4) | (1 << PC5);
|
||||||
DDRB = (1 << PB4) | (1 << PB3);
|
DDRB |= (1 << PB4) | (1 << PB3);
|
||||||
|
PCMSK1 |= 1 << PCINT11;
|
||||||
|
PCICR |= 1 << PCIE1;
|
||||||
csReg.clear(true);
|
csReg.clear(true);
|
||||||
|
irqReg.read();
|
||||||
probe();
|
probe();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t NfcBoard::csToIrq(uint8_t cs)
|
||||||
|
{
|
||||||
|
switch(cs)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
return 3;
|
||||||
|
case 1:
|
||||||
|
return 2;
|
||||||
|
case 2:
|
||||||
|
return 1;
|
||||||
|
case 3:
|
||||||
|
return 7;
|
||||||
|
case 4:
|
||||||
|
return 7;
|
||||||
|
case 5:
|
||||||
|
return 6;
|
||||||
|
case 6:
|
||||||
|
return 5;
|
||||||
|
case 7:
|
||||||
|
return 3;
|
||||||
|
default:
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void NfcBoard::probe()
|
void NfcBoard::probe()
|
||||||
{
|
{
|
||||||
readers.clear();
|
readers.clear();
|
||||||
@ -27,13 +78,10 @@ void NfcBoard::probe()
|
|||||||
{
|
{
|
||||||
if(Mfrc522::probe(&spim, &csReg, i))
|
if(Mfrc522::probe(&spim, &csReg, i))
|
||||||
{
|
{
|
||||||
irqPins.push_back(i);
|
readers.push_back(Mfrc522(&spim, &csReg, i));
|
||||||
|
irqPins.push_back(csToIrq(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(uint8_t i = 0; i < irqPins.count(); ++i)
|
|
||||||
{
|
|
||||||
readers.push_back(Mfrc522(&spim, &csReg, irqPins[i]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NfcBoard::printNfcDevices(Serial* serial)
|
void NfcBoard::printNfcDevices(Serial* serial)
|
||||||
@ -41,55 +89,100 @@ void NfcBoard::printNfcDevices(Serial* serial)
|
|||||||
serial->write_p(PSTR("NFC DEVICES:\n"));
|
serial->write_p(PSTR("NFC DEVICES:\n"));
|
||||||
for(uint8_t i = 0; i < readers.count(); ++i)
|
for(uint8_t i = 0; i < readers.count(); ++i)
|
||||||
{
|
{
|
||||||
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "NFC NUMBER: %u IRQ: %x\n", i, irqPins[i]);
|
snprintf_P(buffer, SNPRINTF_BUFFER_SIZE, PSTR("NFC NUMBER: %u IRQ: %x\n"), i, irqPins[i]);
|
||||||
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
|
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int NfcBoard::dispatch(char* inBuffer, Serial* serial)
|
int NfcBoard::dispatch(char* inBuffer, Serial* serial)
|
||||||
{
|
{
|
||||||
|
if(strcmp(inBuffer, "debug") == 0)
|
||||||
|
{
|
||||||
|
Mfrc522::serial = serial;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else if(strcmp(inBuffer, "quiet") == 0 )
|
||||||
|
{
|
||||||
|
Mfrc522::serial = nullptr;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else if(strcmp(inBuffer, "irqs") == 0 )
|
||||||
|
{
|
||||||
|
PCMSK1 &= ~(1 << PCINT11);
|
||||||
|
PCICR &= ~(1 << PCIE1);
|
||||||
|
serial->write_p(PSTR("Irq pin detection test\n"));
|
||||||
|
while(!serial->dataIsWaiting())
|
||||||
|
{
|
||||||
|
uint8_t* data = irqReg.read();
|
||||||
|
for(uint8_t i = 0; i < NFC_PORTS; ++i)
|
||||||
|
{
|
||||||
|
if(*data & (1 << i))
|
||||||
|
{
|
||||||
|
serial->write("IRQ: ");
|
||||||
|
serial->write((int)i);
|
||||||
|
serial->putChar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
serial->write_p(PSTR("Finished\n"));
|
||||||
|
PCMSK1 |= 1 << PCINT11;
|
||||||
|
PCICR |= 1 << PCIE1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
else if(strcmp(inBuffer, "detect") == 0 )
|
else if(strcmp(inBuffer, "detect") == 0 )
|
||||||
{
|
{
|
||||||
serial->write_p(PSTR("Runing tag detection test\n"));
|
serial->write_p(PSTR("Runing tag detection test\n"));
|
||||||
bool oldPresent = false;
|
|
||||||
while(!serial->dataIsWaiting())
|
while(!serial->dataIsWaiting())
|
||||||
{
|
{
|
||||||
bool present = readers[0].cardPresent();
|
bool present = readers[0].cardPresent();
|
||||||
if(present && !oldPresent)
|
if(present)
|
||||||
{
|
{
|
||||||
oldPresent = present;
|
|
||||||
|
|
||||||
Mfrc522::Uid uid;
|
Mfrc522::Uid uid;
|
||||||
//Mfrc522::serial = serial;
|
|
||||||
uint8_t res = readers[0].selectTag(&uid);
|
uint8_t res = readers[0].selectTag(&uid);
|
||||||
Mfrc522::serial = nullptr;
|
|
||||||
if(res != 0)
|
if(res != 0)
|
||||||
{
|
|
||||||
serial->write_p(PSTR("Select Failed with "));
|
|
||||||
serial->write((int)res);
|
|
||||||
serial->putChar('\n');
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
serial->write_p(PSTR("Uid: "));
|
serial->write_p(PSTR("Uid: "));
|
||||||
for(uint8_t i = 0; i < uid.size; ++i)
|
for(uint8_t i = 0; i < uid.size; ++i)
|
||||||
{
|
{
|
||||||
serial->write((int)uid.uidByte[i]);
|
serial->write((int)uid.uidByte[i]);
|
||||||
serial->putChar(':');
|
if(i < uid.size-1)
|
||||||
|
serial->putChar(':');
|
||||||
}
|
}
|
||||||
serial->putChar('\n');
|
serial->putChar('\n');
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if(!present && oldPresent)
|
|
||||||
{
|
|
||||||
serial->write_p(PSTR("Tag lost\n"));
|
|
||||||
oldPresent = present;
|
|
||||||
}
|
}
|
||||||
_delay_ms(100);
|
_delay_ms(100);
|
||||||
}
|
}
|
||||||
serial->write_p(PSTR("Finished\n"));
|
serial->write_p(PSTR("Finished\n"));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else if(strcmp(inBuffer, "tste") == 0 )
|
else if(strcmp(inBuffer, "fastdetect") == 0 )
|
||||||
|
{
|
||||||
|
serial->write_p(PSTR("Runing fast tag detection test\n"));
|
||||||
|
while(!serial->dataIsWaiting())
|
||||||
|
{
|
||||||
|
bool present = readers[0].cardPresent();
|
||||||
|
if(present)
|
||||||
|
{
|
||||||
|
Mfrc522::Uid uid;
|
||||||
|
uint8_t res = readers[0].getUid(&uid);
|
||||||
|
if(res != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
serial->write_p(PSTR("Uid: "));
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
serial->write_p(PSTR("Finished\n"));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else if(strcmp(inBuffer, "fifotst") == 0 )
|
||||||
{
|
{
|
||||||
serial->write_p(PSTR("Runing fifo test\n"));
|
serial->write_p(PSTR("Runing fifo test\n"));
|
||||||
Mfrc522::serial = serial;
|
Mfrc522::serial = serial;
|
||||||
@ -98,6 +191,15 @@ int NfcBoard::dispatch(char* inBuffer, Serial* serial)
|
|||||||
serial->write_p(PSTR("Finished\n"));
|
serial->write_p(PSTR("Finished\n"));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
else if(strcmp(inBuffer, "enable") == 0 )
|
||||||
|
{
|
||||||
|
for(uint8_t i = 0; i < readers.count(); ++i)
|
||||||
|
{
|
||||||
|
readers[i].detectAsync(detectCb, serial);
|
||||||
|
}
|
||||||
|
serial->write_p(PSTR("Nfc tag listening enabled\n"));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
else if(strcmp(inBuffer, "status") == 0 )
|
else if(strcmp(inBuffer, "status") == 0 )
|
||||||
{
|
{
|
||||||
printNfcDevices(serial);
|
printNfcDevices(serial);
|
||||||
@ -109,7 +211,7 @@ int NfcBoard::dispatch(char* inBuffer, Serial* serial)
|
|||||||
uint8_t len = sizeof(bufferATQA);
|
uint8_t len = sizeof(bufferATQA);
|
||||||
|
|
||||||
uint8_t res = readers[0].wakeupTag(bufferATQA, &len);
|
uint8_t res = readers[0].wakeupTag(bufferATQA, &len);
|
||||||
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "wakeupTag returned: %u Buffer: 0x%x 0x%x len %u\n",
|
snprintf_P(buffer, SNPRINTF_BUFFER_SIZE, PSTR("wakeupTag returned: %u Buffer: 0x%x 0x%x len %u\n"),
|
||||||
res, bufferATQA[0], bufferATQA[1], len);
|
res, bufferATQA[0], bufferATQA[1], len);
|
||||||
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
|
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
|
||||||
return 0;
|
return 0;
|
||||||
@ -122,3 +224,27 @@ int NfcBoard::dispatch(char* inBuffer, Serial* serial)
|
|||||||
}
|
}
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NfcBoard::detectCb(Mfrc522* reader, void* data)
|
||||||
|
{
|
||||||
|
Serial* serial = reinterpret_cast<Serial*>(data);
|
||||||
|
|
||||||
|
static Mfrc522* oldReader = nullptr;
|
||||||
|
static Mfrc522::Uid oldUid = {0};
|
||||||
|
|
||||||
|
Mfrc522::Uid uid;
|
||||||
|
if(reader->getUid(&uid) == 0 && (uid != oldUid || reader != oldReader))
|
||||||
|
{
|
||||||
|
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');
|
||||||
|
oldUid = uid;
|
||||||
|
oldReader = reader;
|
||||||
|
}
|
||||||
|
reader->detectAsync(detectCb, serial);
|
||||||
|
}
|
||||||
|
@ -16,6 +16,8 @@ public:
|
|||||||
SVector<Mfrc522, NFC_PORTS> readers;
|
SVector<Mfrc522, NFC_PORTS> readers;
|
||||||
SVector<uint8_t, NFC_PORTS> irqPins;
|
SVector<uint8_t, NFC_PORTS> irqPins;
|
||||||
|
|
||||||
|
static void detectCb(Mfrc522* reader, void* data);
|
||||||
|
|
||||||
NfcBoard();
|
NfcBoard();
|
||||||
|
|
||||||
void probe();
|
void probe();
|
||||||
@ -23,4 +25,8 @@ public:
|
|||||||
void printNfcDevices(Serial* serial);
|
void printNfcDevices(Serial* serial);
|
||||||
|
|
||||||
int dispatch(char* inBuffer, Serial* serial);
|
int dispatch(char* inBuffer, Serial* serial);
|
||||||
|
|
||||||
|
uint8_t csToIrq(uint8_t cs);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern NfcBoard nfcBoard;
|
||||||
|
15
serial.h
15
serial.h
@ -2,7 +2,8 @@
|
|||||||
#define SERIAL_H
|
#define SERIAL_H
|
||||||
|
|
||||||
#define BAUD 38400
|
#define BAUD 38400
|
||||||
#define SERIAL_BUFFER_SIZE 384
|
#define SERIAL_RX_BUFFER_SIZE 256
|
||||||
|
#define SERIAL_TX_BUFFER_SIZE 128
|
||||||
|
|
||||||
#include <util/setbaud.h>
|
#include <util/setbaud.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
@ -11,15 +12,25 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <avr/pgmspace.h>
|
#include <avr/pgmspace.h>
|
||||||
|
|
||||||
|
#include "ringbuffer.h"
|
||||||
|
|
||||||
const bool serialFlowControl = false;
|
const bool serialFlowControl = false;
|
||||||
|
|
||||||
class Serial
|
class Serial
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
char _terminator = '\n';
|
char _terminator = '\n';
|
||||||
|
Serial();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Serial();
|
|
||||||
|
volatile RingBuffer<SERIAL_RX_BUFFER_SIZE, volatile uint8_t> rxBuffer;
|
||||||
|
volatile RingBuffer<SERIAL_TX_BUFFER_SIZE, volatile uint8_t> txBuffer;
|
||||||
|
|
||||||
|
bool stopped = false;
|
||||||
|
volatile bool transmitting = false;
|
||||||
|
|
||||||
|
static Serial* getInstance();
|
||||||
void putChar(const char c);
|
void putChar(const char c);
|
||||||
void write(const char* in, const unsigned int length);
|
void write(const char* in, const unsigned int length);
|
||||||
void write(const char in[]);
|
void write(const char in[]);
|
||||||
|
Reference in New Issue
Block a user