summaryrefslogtreecommitdiffstats
path: root/utils/hwstub/tools
diff options
context:
space:
mode:
Diffstat (limited to 'utils/hwstub/tools')
-rw-r--r--utils/hwstub/tools/lua/stmp.lua1
-rw-r--r--utils/hwstub/tools/lua/stmp/i2c.lua75
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
require "stmp/lcdif"
require "stmp/pwm"
require "stmp/clkctrl"
+ require "stmp/i2c"
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 @@
+---
+--- I2C
+---
+STMP.i2c = {}
+
+local h = HELP:get_topic("STMP"):create_topic("i2c")
+h:add("The STMP.clkctrl table handles the i2c device for all STMPs.")
+
+function STMP.i2c.init()
+ HW.I2C.CTRL0.SFTRST.set()
+ STMP.pinctrl.pin(0, 30).muxsel("MAIN")
+ STMP.pinctrl.pin(0, 30).pull(true)
+ STMP.pinctrl.pin(0, 31).muxsel("MAIN")
+ STMP.pinctrl.pin(0, 31).pull(true)
+ STMP.i2c.reset()
+ STMP.i2c.set_speed(true)
+end
+
+function STMP.i2c.reset()
+ HW.I2C.CTRL0.SFTRST.set()
+ HW.I2C.CTRL0.SFTRST.clr()
+ HW.I2C.CTRL0.CLKGATE.clr()
+ -- errata for IMX233
+ HW.I2C.CTRL1.ACK_MODE.set();
+end
+
+function STMP.i2c.set_speed(fast)
+ if fast then
+ -- Fast-mode @ 400K
+ HW.I2C.TIMING0.write(0x000F0007) -- tHIGH=0.6us, read at 0.3us
+ HW.I2C.TIMING1.write(0x001F000F) -- tLOW=1.3us, write at 0.6us
+ HW.I2C.TIMING2.write(0x0015000D)
+ else
+ -- Slow-mode @ 100K
+ HW.I2C.TIMING0.write(0x00780030)
+ HW.I2C.TIMING1.write(0x00800030)
+ HW.I2C.TIMING2.write(0x00300030)
+ end
+end
+
+function STMP.i2c.transmit(slave_addr, buffer, send_stop)
+ local data = { slave_addr }
+ for i, v in ipairs(buffer) do
+ table.insert(data, v)
+ end
+ if #data > 4 then
+ error("PIO mode cannot send more than 4 bytes at once")
+ end
+ HW.I2C.CTRL0.MASTER_MODE.set()
+ HW.I2C.CTRL0.PIO_MODE.set()
+ HW.I2C.CTRL0.PRE_SEND_START.set()
+ HW.I2C.CTRL0.POST_SEND_STOP.write(send_stop and 1 or 0)
+ HW.I2C.CTRL0.DIRECTION.set()
+ HW.I2C.CTRL0.SEND_NAK_ON_LAST.clr()
+ HW.I2C.CTRL0.XFER_COUNT.write(#data)
+ local v = 0
+ for i,d in ipairs(data) do
+ v = v + bit32.lshift(d, (i - 1) * 8)
+ end
+ HW.I2C.DATA.write(v)
+ HW.I2C.CTRL1.clr(0xffff)
+ HW.I2C.CTRL0.RUN.set()
+ while HW.I2C.CTRL0.RUN.read() == 1 do
+ end
+ if HW.I2C.CTRL1.NO_SLAVE_ACK_IRQ.read() == 1 then
+ HW.I2C.CTRL1.CLR_GOT_A_NAK.set()
+ STMP.i2c.reset()
+ return false
+ end
+ if HW.I2C.CTRL1.EARLY_TERM_IRQ.read() == 1 or HW.I2C.CTRL1.MASTER_LOSS_IRQ.read() == 1 then
+ return false
+ else
+ return true
+ end
+end \ No newline at end of file