miguelbalboa / rfid

Arduino RFID Library for MFRC522
The Unlicense
2.74k stars 1.43k forks source link

RFID reader is delaying millis() timer less when running more logic and peripherals #555

Closed martinius96 closed 3 years ago

martinius96 commented 3 years ago

Step 1: Describe your environment

Step 2: Describe the problem

Hello there. I have some problem with my sketch that is using RFID reader RC522. I have two RFID readers RC522, one have address 0x12 (counterfeit chip) and other is 0x92 (Firmware v2.0 with original NXP chip i think), problem is same at both readers (using only one at project).

Whole project is like STOPWATCH that is couting time each second (RFID card is used for user entry for start, stop or eraser time). In program implementation I am using simple timer... if millis - my_saved_millis() is more or equal to 1000, run logic. I am using also I2C LCD display there. So, fist time is stopped and it is not overwriten on display. In that moment I have running of logic each +- 1030ms (so 30 ms over 1000 ms that should trigger that timer). When I attach card that will start running logic of stopwatch, it is also sending characters to display, but delay is now only 4ms over 1000 ms (1004 ms).

So it is so weird, because delay is less when I am using more peripherals... I am using more peripherals (RFID and display) and delay is 4ms over 1000ms and if there I am using at the beginning RFID only (before display is overwriten - waiting for first attach of card) I have 30 ms over 1000ms. I have look into .cpp files of MFRC522 library, but I havent found there anything that can cause that problem. Also I tried to set SCK from 4MHz (used in lib) to 8MHz and delay is 2ms over 1000. Delays over 1000ms are same at COUNTERFEIT or at ORIGINAL chip of NXP RC522 reader Printing on LCD display is almost 1ms for 2 characters, there is 16 characters, so it is 8ms per its print on display. I am using standard I2C speed 100KHz.

Have somebody idea what can cause this? Why I have bigger delays when I am nothing printing on display and when counter starts with running full logic, delay is less? That doesn't make sense to me.

My goal is to get millis() timer running each 1000 ms without some ms delays over it, becuase it is braking stopwatch too. Thanks, Martin

Schematics

Schematics RFID

UART output

RFID RC522 delay

Minimal reproductive example

MRE available at: https://pastebin.com/9y6ZX5YT

martinius96 commented 3 years ago

so, it looks like that statement in loop.... if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) { ... } is running 24 up to 26 ms if card is attached or not... It's so long... And in moment when card is attached it is running 30ms. Is there some option how to speed it up a bit? :)

Edit: it is running same ms duration if using 4 MHz or 10 MHz SPI CLOCK.

Rotzbua commented 3 years ago

[..] running 24 up to 26 ms if card is attached or not... It's so long...

Long? No.

Is there some option how to speed it up a bit? :)

The library is simple straight forward. The access to the spi device is blocking. I do not think it is possible to speed it up without changing the fundamental method of the library, e.g. add interrupt usage see #554.

One option without changing the library I see is to use a multicore system like the esp32. The other to use different order and timings.

In general the rfid chip is not designed for timing stuff. It is designed to read and write information to the token.

For timing use light barriers or buttons.