diff options
author | Amaury Pouly <amaury.pouly@gmail.com> | 2013-09-25 14:17:47 +0200 |
---|---|---|
committer | Amaury Pouly <amaury.pouly@gmail.com> | 2013-09-25 14:31:39 +0200 |
commit | f46ff0f0760febc4d57becf3d3ddb90af300598f (patch) | |
tree | 8cc66f17f7d696ddfc819b9799c20aaadec8078d | |
parent | b181c4963eb73e44f2febb5866ce8741ffefb6ce (diff) | |
download | rockbox-f46ff0f0760febc4d57becf3d3ddb90af300598f.tar.gz rockbox-f46ff0f0760febc4d57becf3d3ddb90af300598f.zip |
hwstub: add stmp i2c code
Change-Id: I6787e682a9e22cb34e6d94d25bf68d7575d784c4
-rw-r--r-- | utils/hwstub/tools/lua/stmp.lua | 1 | ||||
-rw-r--r-- | utils/hwstub/tools/lua/stmp/i2c.lua | 75 |
2 files changed, 76 insertions, 0 deletions
diff --git a/utils/hwstub/tools/lua/stmp.lua b/utils/hwstub/tools/lua/stmp.lua index 18cb59d72d..807c18df8d 100644 --- a/utils/hwstub/tools/lua/stmp.lua +++ b/utils/hwstub/tools/lua/stmp.lua | |||
@@ -76,4 +76,5 @@ if STMP.info.chip ~= nil then | |||
76 | require "stmp/lcdif" | 76 | require "stmp/lcdif" |
77 | require "stmp/pwm" | 77 | require "stmp/pwm" |
78 | require "stmp/clkctrl" | 78 | require "stmp/clkctrl" |
79 | require "stmp/i2c" | ||
79 | end \ No newline at end of file | 80 | 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 new file mode 100644 index 0000000000..54d860e0df --- /dev/null +++ b/utils/hwstub/tools/lua/stmp/i2c.lua | |||
@@ -0,0 +1,75 @@ | |||
1 | --- | ||
2 | --- I2C | ||
3 | --- | ||
4 | STMP.i2c = {} | ||
5 | |||
6 | local h = HELP:get_topic("STMP"):create_topic("i2c") | ||
7 | h:add("The STMP.clkctrl table handles the i2c device for all STMPs.") | ||
8 | |||
9 | function STMP.i2c.init() | ||
10 | HW.I2C.CTRL0.SFTRST.set() | ||
11 | STMP.pinctrl.pin(0, 30).muxsel("MAIN") | ||
12 | STMP.pinctrl.pin(0, 30).pull(true) | ||
13 | STMP.pinctrl.pin(0, 31).muxsel("MAIN") | ||
14 | STMP.pinctrl.pin(0, 31).pull(true) | ||
15 | STMP.i2c.reset() | ||
16 | STMP.i2c.set_speed(true) | ||
17 | end | ||
18 | |||
19 | function STMP.i2c.reset() | ||
20 | HW.I2C.CTRL0.SFTRST.set() | ||
21 | HW.I2C.CTRL0.SFTRST.clr() | ||
22 | HW.I2C.CTRL0.CLKGATE.clr() | ||
23 | -- errata for IMX233 | ||
24 | HW.I2C.CTRL1.ACK_MODE.set(); | ||
25 | end | ||
26 | |||
27 | function STMP.i2c.set_speed(fast) | ||
28 | if fast then | ||
29 | -- Fast-mode @ 400K | ||
30 | HW.I2C.TIMING0.write(0x000F0007) -- tHIGH=0.6us, read at 0.3us | ||
31 | HW.I2C.TIMING1.write(0x001F000F) -- tLOW=1.3us, write at 0.6us | ||
32 | HW.I2C.TIMING2.write(0x0015000D) | ||
33 | else | ||
34 | -- Slow-mode @ 100K | ||
35 | HW.I2C.TIMING0.write(0x00780030) | ||
36 | HW.I2C.TIMING1.write(0x00800030) | ||
37 | HW.I2C.TIMING2.write(0x00300030) | ||
38 | end | ||
39 | end | ||
40 | |||
41 | function STMP.i2c.transmit(slave_addr, buffer, send_stop) | ||
42 | local data = { slave_addr } | ||
43 | for i, v in ipairs(buffer) do | ||
44 | table.insert(data, v) | ||
45 | end | ||
46 | if #data > 4 then | ||
47 | error("PIO mode cannot send more than 4 bytes at once") | ||
48 | end | ||
49 | HW.I2C.CTRL0.MASTER_MODE.set() | ||
50 | HW.I2C.CTRL0.PIO_MODE.set() | ||
51 | HW.I2C.CTRL0.PRE_SEND_START.set() | ||
52 | HW.I2C.CTRL0.POST_SEND_STOP.write(send_stop and 1 or 0) | ||
53 | HW.I2C.CTRL0.DIRECTION.set() | ||
54 | HW.I2C.CTRL0.SEND_NAK_ON_LAST.clr() | ||
55 | HW.I2C.CTRL0.XFER_COUNT.write(#data) | ||
56 | local v = 0 | ||
57 | for i,d in ipairs(data) do | ||
58 | v = v + bit32.lshift(d, (i - 1) * 8) | ||
59 | end | ||
60 | HW.I2C.DATA.write(v) | ||
61 | HW.I2C.CTRL1.clr(0xffff) | ||
62 | HW.I2C.CTRL0.RUN.set() | ||
63 | while HW.I2C.CTRL0.RUN.read() == 1 do | ||
64 | end | ||
65 | if HW.I2C.CTRL1.NO_SLAVE_ACK_IRQ.read() == 1 then | ||
66 | HW.I2C.CTRL1.CLR_GOT_A_NAK.set() | ||
67 | STMP.i2c.reset() | ||
68 | return false | ||
69 | end | ||
70 | if HW.I2C.CTRL1.EARLY_TERM_IRQ.read() == 1 or HW.I2C.CTRL1.MASTER_LOSS_IRQ.read() == 1 then | ||
71 | return false | ||
72 | else | ||
73 | return true | ||
74 | end | ||
75 | end \ No newline at end of file | ||