-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.lua
More file actions
30 lines (27 loc) · 1.22 KB
/
scan.lua
File metadata and controls
30 lines (27 loc) · 1.22 KB
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
-- Based on work by sancho and zeroday among many other open source authors
-- This code is public domain, attribution to gareth@l0l.org.uk appreciated.
id=0 -- need this to identify (software) IC2 bus?
gpio_pin= {4,3,0,1,2} -- this array maps internal IO references to GPIO numbers
-- user defined function: see if device responds with ACK to i2c start
function find_dev(i2c_id, dev_addr)
i2c.start(i2c_id)
c=i2c.address(i2c_id, dev_addr ,i2c.TRANSMITTER)
i2c.stop(i2c_id)
return c
end
print("Scanning all pins for I2C Bus device")
for scl=1,7 do
for sda=1,7 do
tmr.wdclr() -- call this to pat the (watch)dog!
if sda~=scl then -- if the pins are the same then skip this round
i2c.setup(id,sda,scl,i2c.SLOW) -- initialize i2c with our id and current pins in slow mode :-)
for i=0,127 do -- TODO - skip invalid addresses
if find_dev(id, i)==true then
print("Device is wired: SDA to GPIO - IO index "..sda)
print("Device is wired: SCL to GPIO - IO index "..scl)
print("Device found at address 0x"..string.format("%02X",i))
end
end
end
end
end