Compare commits

...

5 Commits

Author SHA1 Message Date
Rotzbua
ba72b92f07 fix typo 2021-08-23 19:45:08 +02:00
Jared Hancock
1ff0b14d2e deadline is ultimately a const value 2021-08-23 19:43:58 +02:00
Jared Hancock
3b760c359a Update documentation about timing in the wait loops 2021-08-23 19:43:58 +02:00
Jared Hancock
c553f4df61 Use yield() in busy wait loops
Also, be more scientific about the timeouts rather than using a set
number of loops, the time of which depends on the value of the
MRFC522_SPICLOCK as well as F_CPU. Instead, use a set number of
milliseconds as a deadline.
2021-08-23 19:43:58 +02:00
Rotzbua
8f9c07f84f add link to more pin layouts
adaption of #562
2021-08-05 11:54:19 +02:00
14 changed files with 55 additions and 13 deletions

View File

@@ -20,6 +20,8 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -31,6 +31,8 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -20,6 +20,8 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -25,6 +25,8 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
* *
*/ */

View File

@@ -23,6 +23,8 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
* *
*/ */

View File

@@ -18,6 +18,8 @@
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
* *
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -25,6 +25,8 @@
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
* *
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -26,6 +26,8 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -25,6 +25,8 @@
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
* *
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -21,6 +21,8 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -23,6 +23,8 @@
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
* *
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -17,6 +17,8 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -12,6 +12,8 @@
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
* *
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*
* Hardware required: * Hardware required:
* Arduino * Arduino
* PCD (Proximity Coupling Device): NXP MFRC522 Contactless Reader IC * PCD (Proximity Coupling Device): NXP MFRC522 Contactless Reader IC

View File

@@ -163,11 +163,13 @@ MFRC522::StatusCode MFRC522::PCD_CalculateCRC( byte *data, ///< In: Pointer to
PCD_WriteRegister(FIFODataReg, length, data); // Write data to the FIFO PCD_WriteRegister(FIFODataReg, length, data); // Write data to the FIFO
PCD_WriteRegister(CommandReg, PCD_CalcCRC); // Start the calculation PCD_WriteRegister(CommandReg, PCD_CalcCRC); // Start the calculation
// Wait for the CRC calculation to complete. Each iteration of the while-loop takes 17.73μs. // Wait for the CRC calculation to complete. Check for the register to
// TODO check/modify for other architectures than Arduino Uno 16bit // indicate that the CRC calculation is complete in a loop. If the
// calculation is not indicated as complete in ~90ms, then time out
// the operation.
const uint32_t deadline = millis() + 89;
// Wait for the CRC calculation to complete. Each iteration of the while-loop takes 17.73us. do {
for (uint16_t i = 5000; i > 0; i--) {
// DivIrqReg[7..0] bits are: Set2 reserved reserved MfinActIRq reserved CRCIRq reserved reserved // DivIrqReg[7..0] bits are: Set2 reserved reserved MfinActIRq reserved CRCIRq reserved reserved
byte n = PCD_ReadRegister(DivIrqReg); byte n = PCD_ReadRegister(DivIrqReg);
if (n & 0x04) { // CRCIRq bit set - calculation done if (n & 0x04) { // CRCIRq bit set - calculation done
@@ -177,8 +179,11 @@ MFRC522::StatusCode MFRC522::PCD_CalculateCRC( byte *data, ///< In: Pointer to
result[1] = PCD_ReadRegister(CRCResultRegH); result[1] = PCD_ReadRegister(CRCResultRegH);
return STATUS_OK; return STATUS_OK;
} }
yield();
} }
// 89ms passed and nothing happend. Communication with the MFRC522 might be down. while (static_cast<uint32_t> (millis()) < deadline);
// 89ms passed and nothing happened. Communication with the MFRC522 might be down.
return STATUS_TIMEOUT; return STATUS_TIMEOUT;
} // End PCD_CalculateCRC() } // End PCD_CalculateCRC()
@@ -422,6 +427,7 @@ void MFRC522::PCD_SoftPowerUp(){
if(!(val & (1<<4))){ // if powerdown bit is 0 if(!(val & (1<<4))){ // if powerdown bit is 0
break;// wake up procedure is finished break;// wake up procedure is finished
} }
yield();
} }
} }
@@ -477,22 +483,32 @@ MFRC522::StatusCode MFRC522::PCD_CommunicateWithPICC( byte command, ///< The co
PCD_SetRegisterBitMask(BitFramingReg, 0x80); // StartSend=1, transmission of data starts PCD_SetRegisterBitMask(BitFramingReg, 0x80); // StartSend=1, transmission of data starts
} }
// Wait for the command to complete. // In PCD_Init() we set the TAuto flag in TModeReg. This means the timer
// In PCD_Init() we set the TAuto flag in TModeReg. This means the timer automatically starts when the PCD stops transmitting. // automatically starts when the PCD stops transmitting.
// Each iteration of the do-while-loop takes 17.86μs. //
// TODO check/modify for other architectures than Arduino Uno 16bit // Wait here for the command to complete. The bits specified in the
uint16_t i; // `waitIRq` parameter define what bits constitute a completed command.
for (i = 2000; i > 0; i--) { // When they are set in the ComIrqReg register, then the command is
// considered complete. If the command is not indicated as complete in
// ~36ms, then consider the command as timed out.
const uint32_t deadline = millis() + 36;
bool completed = false;
do {
byte n = PCD_ReadRegister(ComIrqReg); // ComIrqReg[7..0] bits are: Set1 TxIRq RxIRq IdleIRq HiAlertIRq LoAlertIRq ErrIRq TimerIRq byte n = PCD_ReadRegister(ComIrqReg); // ComIrqReg[7..0] bits are: Set1 TxIRq RxIRq IdleIRq HiAlertIRq LoAlertIRq ErrIRq TimerIRq
if (n & waitIRq) { // One of the interrupts that signal success has been set. if (n & waitIRq) { // One of the interrupts that signal success has been set.
completed = true;
break; break;
} }
if (n & 0x01) { // Timer interrupt - nothing received in 25ms if (n & 0x01) { // Timer interrupt - nothing received in 25ms
return STATUS_TIMEOUT; return STATUS_TIMEOUT;
} }
yield();
} }
// 35.7ms and nothing happend. Communication with the MFRC522 might be down. while (static_cast<uint32_t> (millis()) < deadline);
if (i == 0) {
// 36ms and nothing happened. Communication with the MFRC522 might be down.
if (!completed) {
return STATUS_TIMEOUT; return STATUS_TIMEOUT;
} }