Recent

Author Topic: Use RC522 with Lazarus and RPI  (Read 2184 times)

h0mra

  • Newbie
  • Posts: 1
Use RC522 with Lazarus and RPI
« on: January 06, 2022, 12:48:49 pm »
Greetings!

For some time now I was tinkering with the Raspberry Pi und now started a project with the RFID-Reader "RC522" but I can't wrap my mind about how to use it with Lazarus. The Python sample code works as intended but I don't understand how to read out the Reader with Pascal.

I tried working with the pascalio library that was linked on the wiki.

As I understand it so far I need to write a command to the device and in the data-sheet the command for 'Receive' is "1000" so i tried to write that to the device but I get no output.

Code: Pascal  [Select][+][-]
  1. ...
  2.  
  3. var
  4.  spi: TSPILinuxDevice;
  5.  rbuf, wbuf: word;
  6.  
  7. begin
  8.  spi:= TSPILinuxDevice.Create(0,0);
  9.  spi.Mode:=SPI_MODE_0;
  10.  wbuf:=$1000;
  11.  spi.ReadAndWrite(wbuf, sizeof(wbuf), rbuf, sizeof(rbuf));
  12.  writeln(inttostr(rbuf));
  13.  
  14. ...
  15.  

Does somebody have experience with this and can guide me in the right direction?

Thanks in advance!

MarkMLl

  • Hero Member
  • *****
  • Posts: 4183
Re: Use RC522 with Lazarus and RPI
« Reply #1 on: January 24, 2022, 12:08:39 pm »
A very belated response, so that you don't think that everybody's ignoring you.

I don't have one of those but have looked at various other RFID stuff on various platforms. I've got a vague recollection of looking at SPI minimally, but that would have been a few years ago and I went directly to the kernel API.

When you say "don't understand": what's actually happening? Is the problem that you don't know how to use the SPI API, that your attempt at coding won't compile, or that the device is not responding and in that case what hardware faultfinding have you attempted?

I'm not even sure what SPI devices I've got to hand to play with at the moment...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 4183
Re: Use RC522 with Lazarus and RPI
« Reply #2 on: January 29, 2022, 07:59:32 pm »
I've been looking at this, and the Python library hides a great deal of low-level chip access via SPI, plus a bit of GPIO stuff for the reset line.

I'll do a bit more work on it in a few days time as a way of checking out low-level bus drivers (SPI and I2C) on a PC, but to be honest I think that selecting a reader with USB or serial access would be a much better idea.

[A week later] I've been looking at this in depth as a way of familiarising myself with SPI and I2C as provided on e.g. a Raspberry Pi or using a USB-connected CH341A on a PC.

The Pascal components to support SPI or I2C on Linux are basically thin wrappers around the kernel facilities with very little added value.

However the support libraries for the MFRC522 chip amount to around 10k lines of source on top of this. That's based mainly on the ones tailored for the Arduino, but I anticipate that the Python interface is of a similar size.

This is emphatically not a trivial interface.

MarkMLl
« Last Edit: February 08, 2022, 09:01:40 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

avra

  • Hero Member
  • *****
  • Posts: 2331
    • Additional info
Re: Use RC522 with Lazarus and RPI
« Reply #3 on: February 09, 2022, 12:58:57 pm »
As I understand it so far I need to write a command to the device and in the data-sheet the command for 'Receive' is "1000" so i tried to write that to the device but I get no output.
Datasheet https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf indeed says that receive command is 1000, but it looks like a binary number to me - not a hexadecimal number as you've asumed. Anyway, you should look at Arduino code for reference https://forum.lazarus.freepascal.org/index.php/topic,43438.msg304077.html#msg304077 and https://microcontrollerslab.com/wp-content/uploads/2020/01/rfidmifare-commands.pdf. Reading anything beyond serial number seams partially specific to the card type you have. Without a logic analyzer you are making blind shots which I really don't recommend.
« Last Edit: February 09, 2022, 01:13:11 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

MarkMLl

  • Hero Member
  • *****
  • Posts: 4183
Re: Use RC522 with Lazarus and RPI
« Reply #4 on: February 09, 2022, 02:01:46 pm »
Reading anything beyond serial number seams partially specific to the card type you have. Without a logic analyzer you are making blind shots which I really don't recommend.

Even getting the basic UID is hard work: as I've said, a minimal port of the public domain interface library comes to several thousand lines (OK, so that's well-formatted and properly commented but even so).

The best aid for this sort of thing is having byte-level debugging traces, at least until the library port is done. Anything more sophisticated will definitely need a logic analyser with proper decoding facilities.

However I think the most important thing is sufficient experience to be able to read the manufacturer's datasheet, which describes subtleties that you won't appreciate from inspecting code.

-----

If all that is needed is to get the UID of an HF card or the ID of an LF card I suggest using a 7941W module. It's a bit more expensive but is simple to interface via a serial converter (FTDI clone etc.), has the advantage of supporting both frequency bands, and the sensitivity appears a bit better than that of generic RC522 modules (which in common with a lot of other really cheap stuff are notorious for slipshod manufacturing).

MarkMLl
« Last Edit: February 13, 2022, 07:31:39 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018