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 @@
1---
2--- GPIO
3---
4JZ.gpio = {}
5
6
7function JZ.gpio.pinmask(bank, mask)
8 local t = {}
9 t.read = function()
10 return bit32.band(HW.GPIO.IN[bank].read(), mask)
11 end
12
13 t.write = function(val)
14 if val then t.set() else t.clr() end
15 end
16
17 t.set = function()
18 HW.GPIO.OUT[bank].SET.write(mask)
19 end
20
21 t.clr = function()
22 HW.GPIO.OUT[bank].CLR.write(mask)
23 end
24
25 t.gpio = function()
26 HW.GPIO.FUN[bank].CLR.write(mask)
27 HW.GPIO.SEL[bank].CLR.write(mask)
28 end
29
30 t.dir = function(out)
31 if out then
32 HW.GPIO.DIR[bank].SET.write(mask)
33 else
34 HW.GPIO.DIR[bank].CLR.write(mask)
35 end
36 end
37
38 t.pull = function(val)
39 if val then
40 HW.GPIO.PULL[bank].CLR.write(mask)
41 else
42 HW.GPIO.PULL[bank].SET.write(mask)
43 end
44 end
45
46 t.std_gpio_out = function(data)
47 t.gpio()
48 t.dir(true)
49 t.pull(false)
50 t.write(data)
51 end
52
53 t.gpio_in = function(data)
54 t.gpio()
55 t.dir(false)
56 end
57
58 t.std_function = function(fun_nr)
59 HW.GPIO.FUN[bank].SET.write(mask)
60 if fun_nr >= 2 then
61 HW.GPIO.TRG[bank].SET.write(mask)
62 fun_nr = fun_nr - 2
63 else
64 HW.GPIO.TRG[bank].CLR.write(mask)
65 end
66 if fun_nr >= 2 then
67 HW.GPIO.SEL[bank].SET.write(mask)
68 else
69 HW.GPIO.SEL[bank].CLR.write(mask)
70 end
71 end
72 return t
73end
74
75function JZ.gpio.pin(bank,pin)
76 local mask = bit32.lshift(1, pin)
77 local t = JZ.gpio.pinmask(bank,mask)
78 t.read = function()
79 return bit32.extract(HW.GPIO.IN[bank].read(), pin)
80 end
81 return t
82end \ No newline at end of file