RFID2 Unit and Core2 Processor



  • I've bought an RFID2 reader/writer unit which I connected to the M5 CORE2 processor.

    Using the web based UIFlow, I do not see a specific unit type RFID2 to add to the unit list. For this reason I've added the default RFID and the basic functions are OK and support functions like "card near", tag ID, read and write from address x, like 1. With few UIflow commands and a logic loop I was able to build a basic RFID Tag reader & writer and also to get the UID information.

    Please clarify in some more depth the use of read from "addr" and write to “addr”.

    My guess is that "addr" means sector number, where we find 16 sectors each constructed with 4 blocks, as shown in the memory reader of the "Android NFC Tools – function Memory Hex". There are 3 blocks of data, each 16 characters long, followed by one block of keys.
    Sector 0 is used for UID.

    Sector 0 → UID
    Sector 1 → Data1
    Sector 2 → Data2
    Sector 3 → Key
    Sector 4 → Data3, etc

    My doubt: By writing into "addr" 3, does it overwrite the key in sector 3, or does it write into sector 4 data field, by this preserving the key address? Overwriting a key field possibly would destroy the default RFID tag format. Hope there is code example available for this topic. Below see a hardcopy of my code: 0_1665152588676_rfid_code_sample1.png



  • @falbriard 0_1665413893147_rfid-mirafecard_layout.png Might be useful to illustrate the topic, attached you find the RC522 Mifare tag memory layout.



  • Using the UIflow code, I wrote data into the RFID addr 1, 2 and 4. Checking the result with Android "NFC Tools", function "Read Hex Memory" I see all the data written into the corresponding sectors in a hex format.

    "addr 1" is "sector 0, Block 1"
    "addr 2" is "sector 0, Block 2"
    "addr 4" is "sector 1, Block 0"
    ...

    My guess is that the following write sequence will preserve the formatted key addresses
    and make good use of the available Mifare tag memory space, organized in 16 bytes blocks.
    The resulting addressing table for writing data is:

    Data addr: 1,2,4,5,6,8,9,10,12,13,14,16,17,18,20,21,22 .. etc
    reserved
    UID addr: 0
    Key addr: 3,7,11,15,19,23 .. etc

    0_1665581580009_write_into_block_4_small.png



  • @falbriard An attempt to write a logic with Blockly & MicroPython code (via UIFlow), able to store up to 1000 characters on a RFID tag:

    # by Claude Falbriard - QcLab Brazil
    # RFID tag with RC522 layout    
    from m5stack import *
    from m5stack_ui import *
    from uiflow import *
    import time
    import unit
            
    screen = M5Screen()
    screen.clean_screen()
    screen.set_screen_bg_color(0x000000)
    rfid_0 = unit.get(unit.RFID, unit.PORTA)
        
    inputsrt2 = None
    elecounter = None
    alldata = None
    inputstr1 = None
    inputdata = None
    inputstr4 = None
    cardready = None
       
    
    label0 = M5Label('RFID Reader', x=10, y=10, color=0xf5f2f2, font=FONT_MONT_14, 
    parent=None)
    text_2 = M5Label('text_2', x=10, y=113, color=0xb1ed94, font=FONT_MONT_14, 
    parent=None)
    text_4 = M5Label('text_4', x=10, y=138, color=0xccf1a6, font=FONT_MONT_14, 
    parent=None)
    debug1 = M5Label('Debug: ', x=10, y=226, color=0xf09a9a, font=FONT_MONT_14, 
    parent=None)
    alldata_label = M5Label('alldata', x=10, y=170, color=0xdff2d7, font=FONT_MONT_14, 
    parent=None)
    inputd1 = M5Label('input data', x=10, y=48, color=0xe8f081, font=FONT_MONT_14, 
    parent=None)
    text_1 = M5Label('text_1', x=10, y=88, color=0xcbeeb9, font=FONT_MONT_14, 
    parent=None)
       
    
    elecounter = 0
    # constructing an RFID RC522 compliant data writer
    # https://www.blindtextgenerator.com/lorem-ipsum
    # generate a string with max length of 1000 characters
    ipsumstr = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo 
    ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient 
    montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium 
    quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, 
    vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam 
    dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum 
    semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat 
    vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. 
    Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam 
    ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. 
    Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet 
    adipiscing sem neque sed ipsum. N"""
    # chop test data string to 256 bytes
    ipsumstr = ipsumstr[:256]
    
    # split a text into 16 bytes elements, max size = 1000
    def split_by_n(seq, n):
        '''A generator to divide a sequence into chunks of n units.'''
        while seq:
            yield seq[:n]
            seq = seq[n:]
    
    rc522list = list(split_by_n(ipsumstr, 16))
    blkcount = len(rc522list)
    # process list by RC522 element max 1000 char
    validptr = [1,2,4,5,6,8,9,10,12,13,14,16,17,18,20,21,22,24,25,26,28,29,30,\
                32,33,34,36,37,38,40,41,42,44,45,46,48,49,50,52,53,54,56,57,58,\
                60,61,62,64,65,66,68,69,70,72,73,74,76,77,78,80,81,82,84,85,86]
    keyptr =  [3,7,11,15,19,23,27,31,35,39,43,47,51,55,59,63,67,71,75,79,83]
    
    while True:
      alldata = ''
      inputstr1 = ''
      inputsrt2 = ''
      inputdata = ''
      inputstr4 = ''
      cardready = False
      wait(1)
      debug1.set_text('start reading ...')
      if rfid_0.isCardOn():
        debug1.set_text('card found')
        inputdata = rfid_0.readUid()
        speaker.playTone(740, 1/2)
      inputd1.set_text(str(inputdata))
      elecounter = 0
      for ele in rc522list:
         blkdata = ele
         blkaddr = validptr[elecounter]
         rfid_0.writeBlock(blkaddr,blkdata)
         elecounter += 1
      wait_ms(400)
      # read data only for debug   
      inputstr1 = rfid_0.readBlockStr(1)
      inputsrt2 = rfid_0.readBlockStr(2)
      inputstr4 = rfid_0.readBlockStr(4)
      # all data stored on tag 
      for blkcount in range (0,elecounter):
          blkaddr = validptr[blkcount]
          record = rfid_0.readBlockStr(blkaddr)
          alldata += record
      if inputdata == '1e1cb67cc8':
        power.setVibrationIntensity(80)
        power.setVibrationEnable(True)
        wait(0.5)
        power.setVibrationEnable(False)
      # show data for debug and validation   
      text_1.set_text(str(inputstr1))
      text_2.set_text(str(inputsrt2))
      text_4.set_text(str(inputstr4))
      alldata_label.set_text(str(alldata))
      wait(2)