summaryrefslogtreecommitdiff
path: root/utils/hwstub/tools/lua/stmp/pinctrl.lua
diff options
context:
space:
mode:
Diffstat (limited to 'utils/hwstub/tools/lua/stmp/pinctrl.lua')
-rw-r--r--utils/hwstub/tools/lua/stmp/pinctrl.lua253
1 files changed, 253 insertions, 0 deletions
diff --git a/utils/hwstub/tools/lua/stmp/pinctrl.lua b/utils/hwstub/tools/lua/stmp/pinctrl.lua
new file mode 100644
index 0000000000..2676f6476b
--- /dev/null
+++ b/utils/hwstub/tools/lua/stmp/pinctrl.lua
@@ -0,0 +1,253 @@
1---
2--- PINCTRL
3---
4STMP.pinctrl = {}
5
6local h = HELP:get_topic("STMP"):create_topic("pinctrl")
7h:add("The STMP.pinctrl table handles the pinctrl device for all STMPs.")
8h:add("It provides a simple abstraction to set individual pins.")
9
10local hh = h:create_topic("pin")
11hh:add("The STMP.pinctrl.pin(x,yy) function returns a table for the pin BxPyy.")
12hh:add("Depending on the STMP family, the following function might be available.")
13hh:add("* read() returns the value of the pin (the pin does not need to be configured as GPIO)")
14hh:add("* write(x) sets the value of the pin (provided it is a GPIO with output enabled)")
15hh:add("* set() is equivalent to write(1)")
16hh:add("* clr() is equivalent to write(0)")
17hh:add("* enable() enables the gpio output (provided it is configured as GPIO).")
18hh:add("* disable() disables the gpio output (provided it is configured as GPIO)")
19hh:add("* muxsel(x) set pin function to x, which can be an integer or one of: MAIN, ALT1, ALT2, GPIO")
20
21function STMP.pinctrl.pin(bank,pin)
22 local t = {
23 read = function()
24 return bit32.extract(HW.PINCTRL.DINn[bank].read(), pin)
25 end,
26
27 write = function(val)
28 if val then t.set() else t.clr() end
29 end,
30
31 set = function()
32 HW.PINCTRL.DOUTn[bank].set(bit32.lshift(1, pin))
33 end,
34
35 clr = function()
36 HW.PINCTRL.DOUTn[bank].clr(bit32.lshift(1, pin))
37 end,
38
39 enable = function()
40 HW.PINCTRL.DOEn[bank].set(bit32.lshift(1, pin))
41 end,
42
43 disable = function()
44 HW.PINCTRL.DOEn[bank].clr(bit32.lshift(1, pin))
45 end,
46
47 muxsel = function(x)
48 if type(x) == "string" then
49 if x == "MAIN" then x = 0
50 elseif x == "ALT1" then x = 1
51 elseif x == "ALT2" then x = 2
52 elseif x == "GPIO" then x = 3
53 else error("Invalid muxsel string " .. x) end
54 end
55 local v = nil
56 if STMP.is_stmp3600() then
57 if pin < 16 then
58 v = HW.PINCTRL.MUXSELLn[bank]
59 else
60 v = HW.PINCTRL.MUXSELHn[bank]
61 end
62 else
63 v = HW.PINCTRL.MUXSELn[2 * bank + math.floor(pin / 16)]
64 end
65 v.write(bit32.replace(v.read(), x, (pin % 16) * 2, 2))
66 end
67 }
68 return t
69end
70
71hh = h:create_topic("configure")
72hh:add("The STMP.pinctrl.configure(tbl) function configures pins according to a table.")
73hh:add("The table must contain a list of subtable, each corresponding to a pin.")
74hh:add("Each subtable can configure one or more aspects of a specific pin.")
75hh:add("The following characteristics are understood:")
76hh:add("* bank: pin bank (mandatory)")
77hh:add("* pin: pin number (mandatory) ")
78hh:add("* muxsel: same values as STMP.pinctrl.pin().muxsel (optional)")
79hh:add("* enable: enable/disable output (optional)")
80hh:add("* output: set/clear output (optional)")
81hh:add("All non-subtable entries are ignored")
82hh:add("All unknown parameters in subtablkes are ignored")
83hh:add("")
84hh:add("Example:")
85hh:add("STMP.pinctrl.configure({{bank=0,pin=1,muxsel=\"GPIO\",enable=false},{bank=0,pin=2,muxsel=\"MAIN\"}})")
86
87function STMP.pinctrl.configure(tbl)
88 if type(tbl) ~= "table" then error("Parameter must be a table") end
89 for i,v in pairs(tbl) do
90 if type(v) == "table" then
91 if v.bank ~= nil and v.pin ~= nil then
92 local pin = STMP.pinctrl.pin(v.bank,v.pin)
93 if v.muxsel ~= nil then
94 STMP.debug(string.format("cfg B%dP%02d muxsel %s", v.bank, v.pin, v.muxsel))
95 pin.muxsel(v.muxsel)
96 end
97 if v.enable ~= nil then
98 STMP.debug(string.format("cfg B%dP%02d enable %s", v.bank, v.pin, v.enable))
99 if v.enable then pin.enable()
100 else pin.disable() end
101 end
102 if v.output ~= nil then
103 STMP.debug(string.format("cfg B%dP%02d output %s", v.bank, v.pin, v.output))
104 if v.output then pin.set()
105 else pin.clr() end
106 end
107 end
108 end
109 end
110end
111
112hh = h:create_topic("configure_ex")
113hh:add("The STMP.pinctrl.configure_ex(tbl) function configures pins according to a table.")
114hh:add("It is an enhanced version of configure which handles the different families and packages.")
115hh:add("The argument must be a table with one entry per STMP family.")
116hh:add("Each entry must a subtable with one subentry per package.")
117hh:add("Each subentry must be a valid argument to STMP.pinctrl.configure().")
118hh:add("The family names are the one returned by STMP.family.")
119hh:add("The table can also contain an entry \"all\" which apply to all families")
120hh:add("The package names are the one returned by STMP.digctl.package().")
121hh:add("The table can also contain an entry \"all\" which apply to all packages.")
122
123function STMP.pinctrl.configure_ex(tbl)
124 if type(tbl) ~= "table" then error("Parameter must be a table") end
125 local pack = STMP.digctl.package()
126 if tbl[STMP.family] ~= nil then
127 if tbl[STMP.family][pack] ~= nil then STMP.pinctrl.configure(tbl[STMP.family][pack]) end
128 if tbl[STMP.family]["all"] ~= nil then STMP.pinctrl.configure(tbl[STMP.family]["all"]) end
129 end
130 if tbl["all"] ~= nil then
131 if tbl["all"][pack] ~= nil then STMP.pinctrl.configure(tbl["all"][pack]) end
132 if tbl["all"]["all"] ~= nil then STMP.pinctrl.configure(tbl["all"]["all"]) end
133 end
134end
135
136hh = h:create_topic("lcdif")
137hh:add("The STMP.pinctrl.lcdif tables provides some high level routines to configure the pins.")
138hh:add("It is specialised to configure the LCDIF pins correctly.")
139hh:add("Some of the modes may not be available on all STMP families.")
140
141STMP.pinctrl.lcdif = {}
142
143local hhh = hh:create_topic("setup_system")
144hhh:add("The STMP.pinctrl.lcdif.setup_system(bus_width,busy) functions configure the LCDIF pins.")
145hhh:add("It only take cares of the pins used in system mode and for a specified bus width.")
146hhh:add("The handled bus widths are 8, 16 and 18")
147hhh:add("The busy pin is configured only if busy is true")
148
149function STMP.pinctrl.lcdif.setup_system(bus_width, busy)
150 local common =
151 {
152 stmp3600 =
153 {
154 all =
155 {
156 lcd_reset = { bank = 1, pin = 16, muxsel = "MAIN"},
157 lcd_rs = { bank = 1, pin = 17, muxsel = "MAIN"},
158 lcd_wr = { bank = 1, pin = 18, muxsel = "MAIN"},
159 lcd_cs = { bank = 1, pin = 19, muxsel = "MAIN"},
160 lcd_d0 = {bank = 1, pin = 0, muxsel = "MAIN"},
161 lcd_d1 = {bank = 1, pin = 1, muxsel = "MAIN"},
162 lcd_d2 = {bank = 1, pin = 2, muxsel = "MAIN"},
163 lcd_d3 = {bank = 1, pin = 3, muxsel = "MAIN"},
164 lcd_d4 = {bank = 1, pin = 4, muxsel = "MAIN"},
165 lcd_d5 = {bank = 1, pin = 5, muxsel = "MAIN"},
166 lcd_d6 = {bank = 1, pin = 6, muxsel = "MAIN"},
167 lcd_d7 = {bank = 1, pin = 7, muxsel = "MAIN"}
168 }
169 },
170 stmp3700 =
171 {
172 all =
173 {
174 lcd_reset = { bank = 1, pin = 16, muxsel = "MAIN"},
175 lcd_rs = { bank = 1, pin = 17, muxsel = "MAIN"},
176 lcd_wr = { bank = 1, pin = 18, muxsel = "MAIN"},
177 lcd_rd = { bank = 1, pin = 19, muxsel = "MAIN"},
178 lcd_cs = { bank = 1, pin = 20, muxsel = "MAIN"},
179 lcd_d0 = {bank = 1, pin = 0, muxsel = "MAIN"},
180 lcd_d1 = {bank = 1, pin = 1, muxsel = "MAIN"},
181 lcd_d2 = {bank = 1, pin = 2, muxsel = "MAIN"},
182 lcd_d3 = {bank = 1, pin = 3, muxsel = "MAIN"},
183 lcd_d4 = {bank = 1, pin = 4, muxsel = "MAIN"},
184 lcd_d5 = {bank = 1, pin = 5, muxsel = "MAIN"},
185 lcd_d6 = {bank = 1, pin = 6, muxsel = "MAIN"},
186 lcd_d7 = {bank = 1, pin = 7, muxsel = "MAIN"}
187 }
188 }
189 }
190 local bus8_15 =
191 {
192 stmp3600 =
193 {
194 all =
195 {
196 lcd_d8 = {bank = 1, pin = 8, muxsel = "MAIN"},
197 lcd_d9 = {bank = 1, pin = 9, muxsel = "MAIN"},
198 lcd_d10 = {bank = 1, pin = 10, muxsel = "MAIN"},
199 lcd_d11 = {bank = 1, pin = 11, muxsel = "MAIN"},
200 lcd_d12 = {bank = 1, pin = 12, muxsel = "MAIN"},
201 lcd_d13 = {bank = 1, pin = 13, muxsel = "MAIN"},
202 lcd_d14 = {bank = 1, pin = 14, muxsel = "MAIN"},
203 lcd_d15 = {bank = 1, pin = 15, muxsel = "MAIN"}
204 }
205 },
206 stmp3700 =
207 {
208 all =
209 {
210 lcd_d8 = {bank = 1, pin = 8, muxsel = "MAIN"},
211 lcd_d9 = {bank = 1, pin = 9, muxsel = "MAIN"},
212 lcd_d10 = {bank = 1, pin = 10, muxsel = "MAIN"},
213 lcd_d11 = {bank = 1, pin = 11, muxsel = "MAIN"},
214 lcd_d12 = {bank = 1, pin = 12, muxsel = "MAIN"},
215 lcd_d13 = {bank = 1, pin = 13, muxsel = "MAIN"},
216 lcd_d14 = {bank = 1, pin = 14, muxsel = "MAIN"},
217 lcd_d15 = {bank = 1, pin = 15, muxsel = "MAIN"}
218 }
219 }
220 }
221 local bus16_17 =
222 {
223
224 }
225 local busy_pin =
226 {
227 stmp3600 =
228 {
229 all =
230 {
231 lcd_busy = { bank = 1, pin = 21, muxsel = "MAIN"},
232 }
233 },
234 stmp3700 =
235 {
236 all =
237 {
238 lcd_busy = { bank = 1, pin = 21, muxsel = "MAIN"},
239 }
240 },
241 }
242
243 STMP.pinctrl.configure_ex(common)
244 if bus_width > 8 then
245 STMP.pinctrl.configure_ex(bus8_15)
246 end
247 if bus_width > 16 then
248 STMP.pinctrl.configure_ex(bus16_17)
249 end
250 if busy then
251 STMP.pinctrl.configure_ex(busy_pin)
252 end
253end