合宙Air103|射频读写器|RC522|RDIF|Mifare1 S50|非接触式IC卡|AB密钥|社区库接口|官方demo|学习(9):RC522-射频读写器
基礎(chǔ)資料
基于Air103開發(fā)板:🚗 Air103 - LuatOS 文檔
上手:開發(fā)上手 - LuatOS 文檔
探討重點(diǎn)
對(duì)官方社區(qū)庫(kù)接口RC522模塊庫(kù)調(diào)用及示例進(jìn)行復(fù)現(xiàn)及分析,了解RDIF及非接觸式IC卡的原理及操作方法。
實(shí)現(xiàn)功能
利用已知的A,B密鑰,進(jìn)行標(biāo)準(zhǔn)和異型IC卡的數(shù)據(jù)處理及讀寫;
硬件準(zhǔn)備
Air103開發(fā)板1塊,面包板1塊,RC522讀寫器模塊,標(biāo)準(zhǔn)、異型IC卡各1張,導(dǎo)線若干。
接線
標(biāo)準(zhǔn)SPI接口,可參考博文:
合宙Air105|攝像頭|capture|SPI|Serial 串口|TFTLCD|Micro SD卡|GC032A|USB轉(zhuǎn)TTL|官方demo|學(xué)習(xí)(2-1):攝像頭camera-capture_Medlar_CN的博客
除絲印為IRQ的端子無作用(應(yīng)為NC)外,其他參照開發(fā)板功能定義正常接入方式(使用)。
SPI接線設(shè)置:
? ? spi_rc522 = spi.setup(0,nil,0,0,8,10*1000*1000,spi.MSB,1,1)result=rc522.init(0,pin.PB04,pin.PB01) --spi_id, cs, rst軟件版本
AIR103:LuatOS@AIR103 base 22.10 bsp V0013
軟件使用
接口文檔可參考:rc522 非接觸式讀寫卡驅(qū)動(dòng)
RFID與IC卡
RFID一般指射頻識(shí)別技術(shù)。 射頻識(shí)別(RFID)是 Radio Frequency Identification 的縮寫。其原理為閱讀器與標(biāo)簽之間進(jìn)行非接觸式的數(shù)據(jù)通信,達(dá)到識(shí)別目標(biāo)的目的。
而IC卡 (Integrated Circuit Card,集成電路卡),也稱智能卡(Smart card)、智慧卡(Intelligent card)、微電路卡(Microcircuit card)或微芯片卡等。
IC卡由于其固有的信息安全、便于攜帶、比較完善的標(biāo)準(zhǔn)化等優(yōu)點(diǎn)。IC卡屬于使用RFID技術(shù)的一個(gè)卡片種類。
本文主要介紹Mifare1 S50IC卡,有標(biāo)準(zhǔn)和異型兩種類別。
S50IC卡的編碼格式
產(chǎn)品簡(jiǎn)介
S50非接觸式IC卡性能簡(jiǎn)介(M1)
一、?? 主要指標(biāo)
?? 容量為8K位EEPROM
?? 分為16個(gè)扇區(qū),每個(gè)扇區(qū)為4塊,每塊16個(gè)字節(jié),以塊為存取單位
?? 每個(gè)扇區(qū)有獨(dú)立的一組密碼及訪問控制
?? 每張卡有唯一序列號(hào),為32位
?? 具有防沖突機(jī)制,支持多卡操作
?? 無電源,自帶天線,內(nèi)含加密控制邏輯和通訊邏輯電路
?? 數(shù)據(jù)保存期為10年,可改寫10萬次,讀無限次
?? 工作溫度:-20℃~50℃(濕度為90%)
?? 工作頻率:13.56MHZ
?? 通信速率:106 KBPS
?? 讀寫距離:10 cm以內(nèi)(與讀寫器有關(guān))
二、存儲(chǔ)結(jié)構(gòu)
M1卡分為16個(gè)扇區(qū),每個(gè)扇區(qū)由4塊(塊0、塊1、塊2、塊3)組成(16個(gè)扇區(qū)的64個(gè)塊按絕對(duì)地址編號(hào)為0~63。
PHILIPS原始芯片的卡片,控制字為:FF078069。
?A、B密鑰是重要的訪問參數(shù),即為操作庫(kù)代碼中的Key_A、Key_B。
local Key_A = string.char(0x00,0x00,0x00,0x00,0x00,0x0) local Key_B = string.char(0xff,0xff,0xff,0xff,0xff,0xff)三、特別提醒
測(cè)試時(shí)最好使用空白卡或者無重要作用的測(cè)試卡。
讀寫操作如果密鑰錯(cuò)誤或不知道密鑰,有加密區(qū)塊被鎖定,而導(dǎo)致該區(qū)塊整體無法訪問和使用的風(fēng)險(xiǎn)。
RC522讀寫驅(qū)動(dòng)使用及分析
Main.lua
(官方代碼庫(kù)只有RC22庫(kù)和基礎(chǔ)用法。為測(cè)試方便,增加了主文件:main.lua):
操作:
1、正常初始化
2、寫扇區(qū)2-數(shù)據(jù)塊8為:0100000000000000
3、順序讀扇區(qū)數(shù)據(jù):(0-63)
PROJECT = "rc522-air103" VERSION = "1.0.0" sys = require("sys")local rc522 = require "rc522"sys.taskInit(function()spi_rc522 = spi.setup(0,nil,0,0,8,10*1000*1000,spi.MSB,1,1)--spi_id, cs, rstrc522.init(0,pin.PB04,pin.PB01)wdata = {0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}local n=8while 1 doresult=rc522.write_datablock(n,wdata)if result thenlog.info("rc522_write:"..tostring(n), "OK!", result)elselog.info("rc522_write:"..tostring(n), "Error!", result)endfor i=0,63 dolocal a,b = rc522.read_datablock(i)if a thenlog.info("read",i,b:toHex()) --字節(jié)流轉(zhuǎn)換為16進(jìn)制顯示endendsys.wait(500)end end)sys.run()rc522.lua(驅(qū)動(dòng)程序,主要代碼分析,完整代碼見官方gitee)
操作庫(kù)定義:
local rc522 = {} local sys = require "sys" local rc522_spi,rc522_rst,rc522_irq,rc522_csA,B密鑰:
local Key_A = string.char(0x00,0x00,0x00,0x00,0xff,0x0) local Key_B = string.char(0xff,0xff,0xff,0xff,0xff,0xff)讀寫器初始化:
--[[ rc522 初始化 @api rc522.init(spi_id, cs, rst) @number spi_id spi端口號(hào) @number cs cs引腳 @number rst rst引腳 @return bool 初始化結(jié)果 @usage spi_rc522 = spi.setup(0,nil,0,0,8,10*1000*1000,spi.MSB,1,1) rc522.init(0,pin.PB04,pin.PB01) ]] function rc522.init(spi_id,cs,rst)rc522_spi = spi_idrc522_cs = gpio.setup(cs, 0, gpio.PULLUP)rc522_cs(1)rc522_rst = gpio.setup(rst, 0, gpio.PULLUP)rc522_rst(1)rc522.reset()rc522_config_isotype()print("rc522.version",rc522.version())return true end讀塊內(nèi)容:
--[[ 按照rc522操作流程讀取塊 @api rc522.read_datablock(addr) @number addr 任意塊地址.M1卡總共有16個(gè)扇區(qū)(每個(gè)扇區(qū)有:3個(gè)數(shù)據(jù)塊+1個(gè)控制塊),共64個(gè)塊 @return bool string 結(jié)果 數(shù)據(jù) @usagefor i=0,63 dolocal a,b = rc522.read_datablock(i)if a thenprint("read",i,b:toHex())endend ]] function rc522.read_datablock(addr)local status,array_id = rc522.request(rc522.reqall)if status ~= true thenstatus,array_id = rc522.request(rc522.reqall)endif status == true thenlocal status,uid = rc522.anticoll(array_id)if status == true thenrc522.select(uid)status = authstate( rc522_authent1b, addr,Key_B,uid )if status == true thenlocal status,data = read(addr)if status == true thenreturn true, dataendrc522.halt()endendendreturn false end寫塊內(nèi)容:
--[[ 按照rc522操作流程寫入16字節(jié)數(shù)據(jù)到塊 @api rc522.write_datablock(addr,data) @number addr 任意塊地址.M1卡總共有16個(gè)扇區(qū)(每個(gè)扇區(qū)有:3個(gè)數(shù)據(jù)塊+1個(gè)控制塊),共64個(gè)塊 @table data 指向要寫入的數(shù)據(jù),必須為16個(gè)字符 @return bool 結(jié)果 @usage rc522.write_datablock(addr,data) ]] function rc522.write_datablock(addr,data)if #data ~= 16 thenreturn falseendlocal status,array_id = rc522.request(rc522.reqall)if status ~= true thenstatus,array_id = rc522.request(rc522.reqall)endif status == true thenlocal status,uid = rc522.anticoll(array_id)if status == true thenrc522.select(uid)status = authstate( rc522_authent1b, addr,Key_B,uid )if status == true thenstatus = rc522.write(addr,data)rc522.halt()return statusendendendreturn false end日志log文件示例:
總結(jié)
以上是生活随笔為你收集整理的合宙Air103|射频读写器|RC522|RDIF|Mifare1 S50|非接触式IC卡|AB密钥|社区库接口|官方demo|学习(9):RC522-射频读写器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python | Intel RealS
- 下一篇: 软件工程中的耦合类型