diff options
-rw-r--r-- | utils/hwstub/tools/lua/i2c_scan.lua | 39 | ||||
-rw-r--r-- | utils/hwstub/tools/lua/stmp/i2c.lua | 34 |
2 files changed, 72 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 | ||
diff --git a/utils/hwstub/tools/lua/stmp/i2c.lua b/utils/hwstub/tools/lua/stmp/i2c.lua index 209f1df4e2..dee5aaedb6 100644 --- a/utils/hwstub/tools/lua/stmp/i2c.lua +++ b/utils/hwstub/tools/lua/stmp/i2c.lua | |||
@@ -73,4 +73,38 @@ function STMP.i2c.transmit(slave_addr, buffer, send_stop) | |||
73 | else | 73 | else |
74 | return true | 74 | return true |
75 | end | 75 | end |
76 | end | ||
77 | |||
78 | function STMP.i2c.receive(slave_addr, length) | ||
79 | if length > 4 then | ||
80 | error("PIO mode cannot receive mode than 4 bytes at once") | ||
81 | end | ||
82 | -- send address | ||
83 | HW.I2C.CTRL0.RETAIN_CLOCK.set() | ||
84 | STMP.i2c.transmit(bit32.bor(slave_addr, 1), {}, false, true) | ||
85 | HW.I2C.CTRL0.DIRECTION.clr() | ||
86 | HW.I2C.CTRL0.XFER_COUNT.write(length) | ||
87 | HW.I2C.CTRL0.SEND_NAK_ON_LAST.set() | ||
88 | HW.I2C.CTRL0.POST_SEND_STOP.set() | ||
89 | HW.I2C.CTRL0.RETAIN_CLOCK.clr() | ||
90 | HW.I2C.CTRL0.RUN.set() | ||
91 | while HW.I2C.CTRL0.RUN.read() == 1 do | ||
92 | end | ||
93 | if HW.I2C.CTRL1.NO_SLAVE_ACK_IRQ.read() == 1 then | ||
94 | if STMP.is_imx233() then | ||
95 | HW.I2C.CTRL1.CLR_GOT_A_NAK.set() | ||
96 | end | ||
97 | STMP.i2c.reset() | ||
98 | return nil | ||
99 | end | ||
100 | if HW.I2C.CTRL1.EARLY_TERM_IRQ.read() == 1 or HW.I2C.CTRL1.MASTER_LOSS_IRQ.read() == 1 then | ||
101 | return nil | ||
102 | else | ||
103 | local data = HW.I2C.DATA.read() | ||
104 | local res = {} | ||
105 | for i = 0, length - 1 do | ||
106 | table.insert(res, bit32.band(0xff, bit32.rshift(data, 8 * i))) | ||
107 | end | ||
108 | return res | ||
109 | end | ||
76 | end \ No newline at end of file | 110 | end \ No newline at end of file |