summaryrefslogtreecommitdiff
path: root/utils/hwstub/tools/lua/jz/gpio.lua
diff options
context:
space:
mode:
Diffstat (limited to 'utils/hwstub/tools/lua/jz/gpio.lua')
-rw-r--r--utils/hwstub/tools/lua/jz/gpio.lua82
1 files changed, 82 insertions, 0 deletions
diff --git a/utils/hwstub/tools/lua/jz/gpio.lua b/utils/hwstub/tools/lua/jz/gpio.lua
new file mode 100644
index 0000000000..d85529f9bb
--- /dev/null
+++ b/utils/hwstub/tools/lua/jz/gpio.lua
@@ -0,0 +1,82 @@
+---
+--- GPIO
+---
+JZ.gpio = {}
+
+
+function JZ.gpio.pinmask(bank, mask)
+ local t = {}
+ t.read = function()
+ return bit32.band(HW.GPIO.IN[bank].read(), mask)
+ end
+
+ t.write = function(val)
+ if val then t.set() else t.clr() end
+ end
+
+ t.set = function()
+ HW.GPIO.OUT[bank].SET.write(mask)
+ end
+
+ t.clr = function()
+ HW.GPIO.OUT[bank].CLR.write(mask)
+ end
+
+ t.gpio = function()
+ HW.GPIO.FUN[bank].CLR.write(mask)
+ HW.GPIO.SEL[bank].CLR.write(mask)
+ end
+
+ t.dir = function(out)
+ if out then
+ HW.GPIO.DIR[bank].SET.write(mask)
+ else
+ HW.GPIO.DIR[bank].CLR.write(mask)
+ end
+ end
+
+ t.pull = function(val)
+ if val then
+ HW.GPIO.PULL[bank].CLR.write(mask)
+ else
+ HW.GPIO.PULL[bank].SET.write(mask)
+ end
+ end
+
+ t.std_gpio_out = function(data)
+ t.gpio()
+ t.dir(true)
+ t.pull(false)
+ t.write(data)
+ end
+
+ t.gpio_in = function(data)
+ t.gpio()
+ t.dir(false)
+ end
+
+ t.std_function = function(fun_nr)
+ HW.GPIO.FUN[bank].SET.write(mask)
+ if fun_nr >= 2 then
+ HW.GPIO.TRG[bank].SET.write(mask)
+ fun_nr = fun_nr - 2
+ else
+ HW.GPIO.TRG[bank].CLR.write(mask)
+ end
+ if fun_nr >= 2 then
+ HW.GPIO.SEL[bank].SET.write(mask)
+ else
+ HW.GPIO.SEL[bank].CLR.write(mask)
+ end
+ end
+ return t
+end
+
+function JZ.gpio.pin(bank,pin)
+ local mask = bit32.lshift(1, pin)
+ local t = JZ.gpio.pinmask(bank,mask)
+ t.read = function()
+ return bit32.extract(HW.GPIO.IN[bank].read(), pin)
+ end
+ return t
+end \ No newline at end of file