.. currentmodule:: rfid .. _rfid.MFRC522: class MFRC522 --- read/write RFID tags via mfrc522 chip ========================================================== The MFRC522 class provides an interface to the mfrc522 IC, i.e. methods to query, read and write RFID tags (Mifare Classic/Plus, Mifare Ultralight/NTAGs). Usage example:: import rfid #create a mfrc522 object (not configured - pass a gain value to the constructor to init in place) mfrc = rfid.mfrc522(1, cs=pyb.Pin.cpu.C13, rst=pyb.Pin.cpu.C14) #init the IC and power it on with 43dB gain value mfrc.init(gain=rfid.G_43dB) #just as an example - called by `init` internally mfrc.poweron(rfid.G_48dB) #list all cards cards=[uid for uid in mfrc] Constructors ------------ .. class:: rfid.mfrc522(id='default', cs='cs pin', rst='rst pin', gain='off', hw='default', key=None, ul=None, plus=None) Construct an MFRC522 object. The parameters are: * *id* is an SPI bus number or an UART bus number. Default for the board is used if not provided. * *cs* is the mfrc522 chip select pin * *rst* is the mfrc522 chip reset pin * *gain* is the antenna gain value. If provided - the IC is initialized and powered on immediately * *hw* (rfid.SPI or rfid.UART). Default for the board is used if not provided. * *key* - set the Mifare Classic key (6 bytes) [default: ``b'\xFF\xFF\xFF\xFF\xFF\xFF'``] * *typ* - set the Mifare Classic key type (rfid.KEY_A/rfid.KEY_B or b'A'/b'B') [default: A] * *ul* - set the current Ultralight password (integer (32le) or 4 bytes) [default: 0xFFFFFFFF] * *plus* - set the current Mifare Plus key (16 bytes) - not all boards support this [default: all FF-s] All parameters are optional. At this point the class object is created, but the IC is not functional (unless the ``gain`` parameter has been passed) - call ``init()`` to init and power on the IC. Setup commands -------------- .. method:: mfrc522.init(gain=G_33dB) Chip is initialized and `poweron()` with the provided gain (or the default 33dB) is called (see `poweron()` description for the valid values). .. method:: mfrc522.reinit() Re-initialize the IC with the currently used gain value. (includes hard-resetting the IC and switching the RF field off/on) .. method:: mfrc522.poweron([gain]) Power on the mfrc522 chip. Set the antenna gain to ``gain``, which must be one of: * `rfid.G_18dB` - 18dBm * `rfid.G_23dB` - 23dBm * `rfid.G_33dB` - 33dBm (\* the default one) * `rfid.G_38dB` - 38dBm * `rfid.G_43dB` - 43dBm * `rfid.G_48dB` - 48dBm .. method:: mfrc522.shutdown() Power off the mfrc522 IC. .. method:: mfrc522.on() Switch the RFID antenna on (i.e the RF field). .. method:: mfrc522.off() Switch the RFID antenna off (i.e the RF field). .. method:: mfrc522.version() Get the IC hardware revision. .. method:: mfrc522.key([key, type]) Request (without args) or set the Mifare Classic key and type. Returns a tuple (key, type) for the getter function, ``none`` for the setter function. .. method:: mfrc522.pwd([value]) Request (without args) or set the Mifare Ultralight/NTAG password. Setter function accepts an integer or a 4-byte long buffer. Returs the current password as a 32LE integer for the getter function, ``none`` for the setter function. Example (getting the current password as a bytes object):: from ustruct import pack pack('0) or decrement (if delta<0) the stored value. Returns a tuple (value, address) for the getter function, ``Đ¢one`` for the setter function. Raises an exception is the block is not a value block. .. method:: mfrc522.pack([pwd]) Authenticates an Ultralight EV1 / NTAG tag with the previously set password (if ``pwd`` is not passed), or sets the password and then authenticates the tag. Returns the PACK value as an 16LE integer. .. method:: mfrc522.sprintf(fmt[, key=None, typ=b'A']) Returns ASCII string with the tag data using the format specified in *fmt*: - a - switch to ascii mode: output data as ascii symbols. Skips data outside the range 8(backspace), 9(tab), 10(return) and 32(space)-126(~). - d - switch to decimal mode: output data as a decimal (0-255). No zero padding. - h - switch to hexadecimal mode (lower-case). - H - switch to hexadecimal mode (upper-case). - U - output the tag's UID. The *U* switch must be followed by one of the modifiers: - ``*`` - output the whole UID - ``~`` - output the whole UID in reversed order (the last byte first) - ``n:m`` - output bytes n, n+1, ... m (incl.) of the UID. If m