This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrfid32.lua
353 lines (272 loc) · 7.05 KB
/
rfid32.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
-- Title : RFID32
-- Description : Library for interfacing ESP32 with MFRC522
-- Author : Alija Bobija (https://abobija.com)
-- GitHub : https://github.com/abobija/rfid32
-- Dependencies : > spi
-- > bit
-- > tmr
local M = {}
local _timer = nil
local scan_paused = false
local function hex(arg)
return string.format("0x%02X", arg)
end
local function extend(tbl, with)
if with ~= nil then
for k, v in pairs(with) do
if tbl[k] == nil then
tbl[k] = with[k]
end
end
end
return tbl
end
M.write = function(addr, val)
M._device:transfer(string.char(
bit.band(bit.lshift(addr, 1), 0x7E),
val
))
end
M.read = function(addr)
M._device:transfer(string.char(
bit.bor(bit.band(bit.lshift(addr, 1), 0x7E), 0x80)
))
return M._device:transfer(string.char(0xFF)):byte(1)
end
local function rc522_set_bitmask(address, mask)
M.write(address, bit.bor(M.read(address), mask))
end
local function rc522_clear_bitmask(address, mask)
M.write(address, bit.band(M.read(address), bit.bnot(mask)))
end
local function rc522_antenna_on()
if bit.bnot(bit.band(M.read(0x14), 0x03)) then
rc522_set_bitmask(0x14, 0x03)
end
M.write(0x26, 0x60) -- 43dB gain
end
function rc522_firmware()
return M.read(0x37)
end
M.card_write = function(command, data)
local back_data = {}
local back_length = 0
local err = false
local irq = 0x00
local irq_wait = 0x00
local last_bits = 0
local n = 0
if command == 0x0E then
irq = 0x12
irq_wait = 0x10
end
if command == 0x0C then
irq = 0x77
irq_wait = 0x30
end
M.write(0x02, bit.bor(irq, 0x80))
rc522_clear_bitmask(0x04, 0x80)
rc522_set_bitmask(0x0A, 0x80)
M.write(0x01, 0x00)
for i,v in ipairs(data) do
M.write(0x09, data[i])
end
M.write(0x01, command)
if command == 0x0C then
rc522_set_bitmask(0x0D, 0x80)
end
local i = 1000
while true do
n = M.read(0x04)
i = i - 1
if not ((i ~= 0) and (bit.band(n, 0x01) == 0) and (bit.band(n, irq_wait) == 0)) then
break
end
end
rc522_clear_bitmask(0x0D, 0x80)
if (i ~= 0) then
if bit.band(M.read(0x06), 0x1B) == 0x00 then
err = false
if (command == 0x0C) then
n = M.read(0x0A)
last_bits = bit.band(M.read(0x0C),0x07)
if last_bits ~= 0 then
back_length = (n - 1) * 8 + last_bits
else
back_length = n * 8
end
if (n == 0) then
n = 1
end
if (n > 16) then
n = 16
end
for i=1, n do
back_data[i] = M.read(0x09)
end
end
else
err = true
end
end
return err, back_data, back_length
end
M.request = function()
local req_mode = { 0x26 }
local err = true
local back_bits = 0
local back_data = nil
M.write(0x0D, 0x07)
err, back_data, back_bits = M.card_write(0x0C, req_mode)
if err or (back_bits ~= 0x10) then
return false, nil
end
return true, back_data
end
M.anticoll = function()
local back_data = {}
local serial_number = { 0x93, 0x20 }
local err = nil
local back_bits = nil
local serial_number_check = 0
M.write(0x0D, 0x00)
err, back_data, back_bits = M.card_write(0x0C, serial_number)
if not err then
if table.maxn(back_data) == 5 then
for i, v in ipairs(back_data) do
serial_number_check = bit.bxor(serial_number_check, back_data[i])
end
if serial_number_check ~= back_data[4] then
err = true
end
else
err = true
end
end
return error, back_data
end
M.calculate_crc = function(data)
rc522_clear_bitmask(0x05, 0x04)
rc522_set_bitmask(0x0A, 0x80)
for i,v in ipairs(data) do
M.write(0x09, data[i])
end
M.write(0x01, 0x03)
local i = 255
local n = 0
while true do
n = M.read(0x05)
i = i - 1
if not ((i ~= 0) and not bit.band(n, 0x04)) then
break
end
end
return { M.read(0x22), M.read(0x21) }
end
M.bytes_hex = function(bytes)
local _hex = ''
for i, b in pairs(bytes) do
_hex = _hex .. hex(b)
if i < #bytes then
_hex = _hex .. ' '
end
end
return _hex
end
M.tag = function(bytes)
local self = {
bytes = bytes
}
self.hex = function()
return M.bytes_hex(self.bytes)
end
return self
end
M.get_tag = function()
if M.request() == true then
local err = nil
local serial_no = nil
err, serial_no = M.anticoll()
local buf = { 0x50, 0x00 }
local crc = M.calculate_crc(buf)
table.insert(buf, crc[1])
table.insert(buf, crc[2])
M.card_write(0x0C, buf)
rc522_clear_bitmask(0x08, 0x08)
if #serial_no == 5 then
return M.tag(serial_no)
end
end
return nil
end
M.scan_pause = function()
if _timer ~= nil then
_timer:stop()
scan_paused = true
end
end
M.scan_resume = function()
if _timer ~= nil then
scan_paused = false
_timer:start()
end
end
M.scan = function(opts)
local options = extend({
interval = 125,
pause_interval = 1000,
got_tag = nil
}, opts)
_timer = tmr.create()
_timer:register(options.interval, tmr.ALARM_SEMI, function()
local _tag = M.get_tag()
if _tag == nil then
_timer:interval(options.interval)
else
_timer:interval(options.pause_interval)
options.got_tag(_tag, M)
end
if scan_paused ~= true then
_timer:start()
end
end)
if scan_paused ~= true then
_timer:start()
end
return M
end
local function rc522_spi_init()
M._master = spi.master(spi.VSPI, {
sclk = M.config.pin_clk,
mosi = M.config.pin_mosi,
miso = M.config.pin_miso
}, 0)
M._device = M._master:device({
cs = M.config.pin_sda,
mode = 0,
freq = 5000000,
halfduplex = false
})
end
M.init = function()
rc522_spi_init()
M.write(0x01, 0x0F)
M.write(0x2A, 0x8D)
M.write(0x2B, 0x3E)
M.write(0x2D, 0x1E)
M.write(0x2C, 0x00)
M.write(0x15, 0x40)
M.write(0x11, 0x3D)
rc522_antenna_on()
print('RC522 Firmware:', hex(rc522_firmware()))
return M
end
return function(config)
M.config = extend({
pin_sda = nil,
pin_clk = nil,
pin_miso = nil,
pin_mosi = nil
}, config)
return M
end