diff options
Diffstat (limited to 'utils/hwstub/tools/lua/i2c_scan.lua')
-rw-r--r-- | utils/hwstub/tools/lua/i2c_scan.lua | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/utils/hwstub/tools/lua/i2c_scan.lua b/utils/hwstub/tools/lua/i2c_scan.lua index def7b5d84c..55066b0bf2 100644 --- a/utils/hwstub/tools/lua/i2c_scan.lua +++ b/utils/hwstub/tools/lua/i2c_scan.lua | |||
@@ -8,4 +8,41 @@ function I2CSCAN.scan() | |||
8 | print(string.format("%#x OK", i)) | 8 | print(string.format("%#x OK", i)) |
9 | end | 9 | end |
10 | end | 10 | end |
11 | end \ No newline at end of file | 11 | end |
12 | |||
13 | -- if file is nil, return array | ||
14 | -- if size is nil, dump the whole EEPROM | ||
15 | function I2CSCAN.dump_rom(file, size) | ||
16 | STMP.i2c.init() | ||
17 | STMP.i2c.set_speed(true) | ||
18 | if not STMP.i2c.transmit(0xa0, {0, 0}, false) then | ||
19 | error("Cannot send address") | ||
20 | end | ||
21 | local res = {} | ||
22 | if size == nil then | ||
23 | size = 0xffff | ||
24 | end | ||
25 | for i = 0, size do | ||
26 | local l = STMP.i2c.receive(0xa0, 1) | ||
27 | if l == nil then | ||
28 | error("error during transfer") | ||
29 | end | ||
30 | for i = 1, #l do | ||
31 | table.insert(res, l[i]) | ||
32 | end | ||
33 | end | ||
34 | if file == nil then | ||
35 | return res | ||
36 | end | ||
37 | local f = file | ||
38 | if type(file) == "string" then | ||
39 | f = io.open(file, "w") | ||
40 | end | ||
41 | if f == nil then error("Cannot open file or write to nil") end | ||
42 | for i = 1, #res do | ||
43 | f:write(string.char(res[i])) | ||
44 | end | ||
45 | if type(file) == "string" then | ||
46 | io.close(f) | ||
47 | end | ||
48 | end \ No newline at end of file | ||