gpio.c revision 1.1 1 1.1 mbalmer /*
2 1.1 mbalmer * Copyright (c) 2011 Marc Balmer <marc (at) msys.ch>
3 1.1 mbalmer * All rights reserved.
4 1.1 mbalmer *
5 1.1 mbalmer * Redistribution and use in source and binary forms, with or without
6 1.1 mbalmer * modification, are permitted provided that the following conditions
7 1.1 mbalmer * are met:
8 1.1 mbalmer * 1. Redistributions of source code must retain the above copyright
9 1.1 mbalmer * notice, this list of conditions and the following disclaimer.
10 1.1 mbalmer * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 mbalmer * notice, this list of conditions and the following disclaimer in the
12 1.1 mbalmer * documentation and/or other materials provided with the distribution.
13 1.1 mbalmer *
14 1.1 mbalmer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 1.1 mbalmer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 1.1 mbalmer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 1.1 mbalmer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 1.1 mbalmer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 1.1 mbalmer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 1.1 mbalmer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 1.1 mbalmer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 1.1 mbalmer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 1.1 mbalmer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 1.1 mbalmer */
25 1.1 mbalmer
26 1.1 mbalmer /* GPIO interface for Lua */
27 1.1 mbalmer
28 1.1 mbalmer #include <errno.h>
29 1.1 mbalmer #include <fcntl.h>
30 1.1 mbalmer #include <stdarg.h>
31 1.1 mbalmer #include <stdio.h>
32 1.1 mbalmer #include <string.h>
33 1.1 mbalmer #include <ctype.h>
34 1.1 mbalmer #include <stdlib.h>
35 1.1 mbalmer #include <unistd.h>
36 1.1 mbalmer
37 1.1 mbalmer #include <lua.h>
38 1.1 mbalmer #include <lauxlib.h>
39 1.1 mbalmer #include <lualib.h>
40 1.1 mbalmer
41 1.1 mbalmer #include <sys/gpio.h>
42 1.1 mbalmer #include <sys/ioctl.h>
43 1.1 mbalmer
44 1.1 mbalmer #define GPIO_METATABLE "GPIO object methods"
45 1.1 mbalmer
46 1.1 mbalmer static void
47 1.1 mbalmer gpio_error(lua_State *L, const char *fmt, ...)
48 1.1 mbalmer {
49 1.1 mbalmer va_list ap;
50 1.1 mbalmer int len;
51 1.1 mbalmer char *msg;
52 1.1 mbalmer
53 1.1 mbalmer va_start(ap, fmt);
54 1.1 mbalmer len = vasprintf(&msg, fmt, ap);
55 1.1 mbalmer va_end(ap);
56 1.1 mbalmer
57 1.1 mbalmer if (len != -1) {
58 1.1 mbalmer lua_pushstring(L, msg);
59 1.1 mbalmer free(msg);
60 1.1 mbalmer } else
61 1.1 mbalmer lua_pushstring(L, "vasprintf failed");
62 1.1 mbalmer lua_error(L);
63 1.1 mbalmer }
64 1.1 mbalmer
65 1.1 mbalmer static int
66 1.1 mbalmer gpio_open(lua_State *L)
67 1.1 mbalmer {
68 1.1 mbalmer int *fd;
69 1.1 mbalmer
70 1.1 mbalmer fd = lua_newuserdata(L, sizeof(int));
71 1.1 mbalmer *fd = open(luaL_checkstring(L, -2), O_RDWR);
72 1.1 mbalmer if (*fd == -1) {
73 1.1 mbalmer gpio_error(L, "%s", strerror(errno));
74 1.1 mbalmer /* NOTREACHED */
75 1.1 mbalmer return 0;
76 1.1 mbalmer }
77 1.1 mbalmer luaL_getmetatable(L, GPIO_METATABLE);
78 1.1 mbalmer lua_setmetatable(L, -2);
79 1.1 mbalmer return 1;
80 1.1 mbalmer }
81 1.1 mbalmer
82 1.1 mbalmer static int
83 1.1 mbalmer gpio_close(lua_State *L)
84 1.1 mbalmer {
85 1.1 mbalmer int *fd;
86 1.1 mbalmer
87 1.1 mbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
88 1.1 mbalmer if (*fd != -1) {
89 1.1 mbalmer close(*fd);
90 1.1 mbalmer *fd = -1;
91 1.1 mbalmer }
92 1.1 mbalmer return 0;
93 1.1 mbalmer }
94 1.1 mbalmer
95 1.1 mbalmer static int
96 1.1 mbalmer gpio_info(lua_State *L)
97 1.1 mbalmer {
98 1.1 mbalmer struct gpio_info info;
99 1.1 mbalmer int *fd;
100 1.1 mbalmer
101 1.1 mbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
102 1.1 mbalmer if (ioctl(*fd, GPIOINFO, &info) == -1)
103 1.1 mbalmer gpio_error(L, "GPIOINFO");
104 1.1 mbalmer lua_pushinteger(L, info.gpio_npins);
105 1.1 mbalmer return 1;
106 1.1 mbalmer }
107 1.1 mbalmer
108 1.1 mbalmer static void
109 1.1 mbalmer gpio_get_pin(lua_State *L, int n, struct gpio_req *req)
110 1.1 mbalmer {
111 1.1 mbalmer switch (lua_type(L, n)) {
112 1.1 mbalmer case LUA_TNUMBER:
113 1.1 mbalmer req->gp_pin = lua_tointeger(L, n) - 1; /* 1 based! */
114 1.1 mbalmer break;
115 1.1 mbalmer case LUA_TSTRING:
116 1.1 mbalmer strlcpy(req->gp_name, lua_tostring(L, n), sizeof(req->gp_name));
117 1.1 mbalmer break;
118 1.1 mbalmer default:
119 1.1 mbalmer luaL_argerror(L, n, "expected string or integer");
120 1.1 mbalmer /* NOTREACHED */
121 1.1 mbalmer }
122 1.1 mbalmer }
123 1.1 mbalmer
124 1.1 mbalmer static int
125 1.1 mbalmer gpio_set(lua_State *L)
126 1.1 mbalmer {
127 1.1 mbalmer struct gpio_set set;
128 1.1 mbalmer int *fd;
129 1.1 mbalmer
130 1.1 mbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
131 1.1 mbalmer memset(&set, 0, sizeof(set));
132 1.1 mbalmer gpio_get_pin(L, 2, (struct gpio_req *)&set);
133 1.1 mbalmer set.gp_flags = luaL_checkinteger(L, 3);
134 1.1 mbalmer if (ioctl(*fd, GPIOSET, &set) == -1)
135 1.1 mbalmer gpio_error(L, "GPIOSET");
136 1.1 mbalmer return 0;
137 1.1 mbalmer }
138 1.1 mbalmer
139 1.1 mbalmer static int
140 1.1 mbalmer gpio_unset(lua_State *L)
141 1.1 mbalmer {
142 1.1 mbalmer struct gpio_set set;
143 1.1 mbalmer int *fd;
144 1.1 mbalmer
145 1.1 mbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
146 1.1 mbalmer memset(&set, 0, sizeof(set));
147 1.1 mbalmer gpio_get_pin(L, 2, (struct gpio_req *)&set);
148 1.1 mbalmer if (ioctl(*fd, GPIOUNSET, &set) == -1)
149 1.1 mbalmer gpio_error(L, "GPIOUNSET");
150 1.1 mbalmer return 0;
151 1.1 mbalmer }
152 1.1 mbalmer
153 1.1 mbalmer static int
154 1.1 mbalmer gpio_read(lua_State *L)
155 1.1 mbalmer {
156 1.1 mbalmer struct gpio_req req;
157 1.1 mbalmer int *fd;
158 1.1 mbalmer
159 1.1 mbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
160 1.1 mbalmer memset(&req, 0, sizeof(req));
161 1.1 mbalmer gpio_get_pin(L, 2, &req);
162 1.1 mbalmer if (ioctl(*fd, GPIOREAD, &req) == -1)
163 1.1 mbalmer gpio_error(L, "GPIOREAD");
164 1.1 mbalmer lua_pushinteger(L, req.gp_value);
165 1.1 mbalmer return 1;
166 1.1 mbalmer }
167 1.1 mbalmer
168 1.1 mbalmer
169 1.1 mbalmer static int
170 1.1 mbalmer gpio_write(lua_State *L)
171 1.1 mbalmer {
172 1.1 mbalmer struct gpio_req req;
173 1.1 mbalmer int *fd, val;
174 1.1 mbalmer
175 1.1 mbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
176 1.1 mbalmer val = luaL_checkinteger(L, 3);
177 1.1 mbalmer if (val != GPIO_PIN_HIGH && val != GPIO_PIN_LOW)
178 1.1 mbalmer gpio_error(L, "%d: invalid value", val);
179 1.1 mbalmer memset(&req, 0, sizeof(req));
180 1.1 mbalmer gpio_get_pin(L, 2, &req);
181 1.1 mbalmer req.gp_value = val;
182 1.1 mbalmer if (ioctl(*fd, GPIOWRITE, &req) == -1)
183 1.1 mbalmer gpio_error(L, "GPIOWRITE");
184 1.1 mbalmer lua_pushinteger(L, req.gp_value);
185 1.1 mbalmer return 1;
186 1.1 mbalmer }
187 1.1 mbalmer
188 1.1 mbalmer
189 1.1 mbalmer static int
190 1.1 mbalmer gpio_toggle(lua_State *L)
191 1.1 mbalmer {
192 1.1 mbalmer struct gpio_req req;
193 1.1 mbalmer int *fd, val;
194 1.1 mbalmer
195 1.1 mbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
196 1.1 mbalmer memset(&req, 0, sizeof(req));
197 1.1 mbalmer gpio_get_pin(L, 2, &req);
198 1.1 mbalmer if (ioctl(*fd, GPIOTOGGLE, &req) == -1)
199 1.1 mbalmer gpio_error(L, "GPIOTOGGLE");
200 1.1 mbalmer lua_pushinteger(L, req.gp_value);
201 1.1 mbalmer return 1;
202 1.1 mbalmer }
203 1.1 mbalmer
204 1.1 mbalmer
205 1.1 mbalmer static int
206 1.1 mbalmer gpio_attach(lua_State *L)
207 1.1 mbalmer {
208 1.1 mbalmer struct gpio_attach attach;
209 1.1 mbalmer int *fd;
210 1.1 mbalmer
211 1.1 mbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
212 1.1 mbalmer memset(&attach, 0, sizeof(attach));
213 1.1 mbalmer strlcpy(attach.ga_dvname, luaL_checkstring(L, 2),
214 1.1 mbalmer sizeof(attach.ga_dvname));
215 1.1 mbalmer attach.ga_offset = luaL_checkinteger(L, 3);
216 1.1 mbalmer attach.ga_mask = luaL_checkinteger(L, 4);
217 1.1 mbalmer if (lua_gettop(L) > 4)
218 1.1 mbalmer attach.ga_flags = luaL_checkinteger(L, 5);
219 1.1 mbalmer else
220 1.1 mbalmer attach.ga_flags = 0;
221 1.1 mbalmer
222 1.1 mbalmer if (ioctl(*fd, GPIOATTACH, &attach) == -1)
223 1.1 mbalmer gpio_error(L, "GPIOATTACH");
224 1.1 mbalmer return 0;
225 1.1 mbalmer }
226 1.1 mbalmer
227 1.1 mbalmer
228 1.1 mbalmer static int
229 1.1 mbalmer gpio_pulse(lua_State *L)
230 1.1 mbalmer {
231 1.1 mbalmer struct gpio_pulse pulse;
232 1.1 mbalmer suseconds_t period, on, off;
233 1.1 mbalmer double freq, dc;
234 1.1 mbalmer int *fd;
235 1.1 mbalmer
236 1.1 mbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
237 1.1 mbalmer freq = luaL_checknumber(L, 3);
238 1.1 mbalmer dc = luaL_checknumber(L, 4);
239 1.1 mbalmer
240 1.1 mbalmer if (freq < 0.0 || (dc < 0.0 || dc >= 100.0))
241 1.1 mbalmer gpio_error(L, "%.f Hz, %.f%% duty cycle: invalid value",
242 1.1 mbalmer freq, dc);
243 1.1 mbalmer
244 1.1 mbalmer memset(&pulse, 0, sizeof(pulse));
245 1.1 mbalmer gpio_get_pin(L, 2, (struct gpio_req *)&pulse);
246 1.1 mbalmer
247 1.1 mbalmer if (freq > 0.0 && dc > 0.0) {
248 1.1 mbalmer period = 1000000 / freq;
249 1.1 mbalmer on = period * dc / 100;
250 1.1 mbalmer off = period - on;
251 1.1 mbalmer
252 1.1 mbalmer if (on >= 1000000) {
253 1.1 mbalmer pulse.gp_pulse_on.tv_sec = on / 1000000;
254 1.1 mbalmer on -= pulse.gp_pulse_on.tv_sec * 1000000;
255 1.1 mbalmer pulse.gp_pulse_on.tv_usec = on;
256 1.1 mbalmer } else {
257 1.1 mbalmer pulse.gp_pulse_on.tv_sec = 0;
258 1.1 mbalmer pulse.gp_pulse_on.tv_usec = on;
259 1.1 mbalmer }
260 1.1 mbalmer if (off >= 1000000) {
261 1.1 mbalmer pulse.gp_pulse_off.tv_sec = off / 1000000;
262 1.1 mbalmer off -= pulse.gp_pulse_off.tv_sec * 1000000;
263 1.1 mbalmer pulse.gp_pulse_off.tv_usec = off;
264 1.1 mbalmer } else {
265 1.1 mbalmer pulse.gp_pulse_off.tv_sec = 0;
266 1.1 mbalmer pulse.gp_pulse_off.tv_usec = off;
267 1.1 mbalmer }
268 1.1 mbalmer } else { /* gpio(4) defaults */
269 1.1 mbalmer freq = 1.0;
270 1.1 mbalmer dc = 50.0;
271 1.1 mbalmer }
272 1.1 mbalmer
273 1.1 mbalmer if (ioctl(*fd, GPIOPULSE, &pulse) == -1)
274 1.1 mbalmer gpio_error(L, "GPIOPULSE");
275 1.1 mbalmer }
276 1.1 mbalmer
277 1.1 mbalmer struct constant {
278 1.1 mbalmer char *name;
279 1.1 mbalmer int value;
280 1.1 mbalmer };
281 1.1 mbalmer
282 1.1 mbalmer static struct constant gpio_constant[] = {
283 1.1 mbalmer /* GPIO pin states */
284 1.1 mbalmer { "PIN_LOW", GPIO_PIN_LOW },
285 1.1 mbalmer { "PIN_HIGH", GPIO_PIN_HIGH },
286 1.1 mbalmer { "PIN_PULSE", GPIO_PIN_PULSE },
287 1.1 mbalmer
288 1.1 mbalmer /* GPIO pin configuration flags */
289 1.1 mbalmer { "PIN_INPUT", GPIO_PIN_INPUT },
290 1.1 mbalmer { "PIN_OUTPUT", GPIO_PIN_OUTPUT },
291 1.1 mbalmer { "PIN_INOUT", GPIO_PIN_INOUT },
292 1.1 mbalmer { "PIN_OPENDRAIN", GPIO_PIN_OPENDRAIN },
293 1.1 mbalmer { "PIN_PUSHPULL", GPIO_PIN_PUSHPULL },
294 1.1 mbalmer { "PIN_TRISTATE", GPIO_PIN_TRISTATE },
295 1.1 mbalmer { "PIN_PULLUP", GPIO_PIN_PULLUP },
296 1.1 mbalmer { "PIN_PULLDOWN", GPIO_PIN_PULLDOWN },
297 1.1 mbalmer { "PIN_INVIN", GPIO_PIN_INVIN },
298 1.1 mbalmer { "PIN_INVOUT", GPIO_PIN_INVOUT },
299 1.1 mbalmer { "PIN_USER", GPIO_PIN_USER },
300 1.1 mbalmer { "PIN_PULSATE", GPIO_PIN_PULSATE },
301 1.1 mbalmer { "PIN_INTR", GPIO_PIN_INTR },
302 1.1 mbalmer { "PIN_INTR_HIGH", GPIO_PIN_INTR_HIGH },
303 1.1 mbalmer { "PIN_SET", GPIO_PIN_SET },
304 1.1 mbalmer { NULL, 0 }
305 1.1 mbalmer };
306 1.1 mbalmer
307 1.1 mbalmer static void
308 1.1 mbalmer gpio_set_info(lua_State *L)
309 1.1 mbalmer {
310 1.1 mbalmer lua_pushliteral(L, "_COPYRIGHT");
311 1.1 mbalmer lua_pushliteral(L, "Copyright (C) 2011 Marc Balmer <marc (at) msys.ch>");
312 1.1 mbalmer lua_settable(L, -3);
313 1.1 mbalmer lua_pushliteral(L, "_DESCRIPTION");
314 1.1 mbalmer lua_pushliteral(L, "GPIO interface for Lua");
315 1.1 mbalmer lua_settable(L, -3);
316 1.1 mbalmer lua_pushliteral(L, "_VERSION");
317 1.1 mbalmer lua_pushliteral(L, "gpio 1.0.0");
318 1.1 mbalmer lua_settable(L, -3);
319 1.1 mbalmer }
320 1.1 mbalmer
321 1.1 mbalmer int
322 1.1 mbalmer luaopen_gpio(lua_State* L)
323 1.1 mbalmer {
324 1.1 mbalmer static const struct luaL_Reg methods[] = {
325 1.1 mbalmer { "open", gpio_open },
326 1.1 mbalmer { NULL, NULL }
327 1.1 mbalmer };
328 1.1 mbalmer static const struct luaL_Reg gpio_methods[] = {
329 1.1 mbalmer { "info", gpio_info },
330 1.1 mbalmer { "close", gpio_close },
331 1.1 mbalmer { "set", gpio_set },
332 1.1 mbalmer { "unset", gpio_unset },
333 1.1 mbalmer { "read", gpio_read },
334 1.1 mbalmer { "write", gpio_write },
335 1.1 mbalmer { "toggle", gpio_toggle },
336 1.1 mbalmer { "attach", gpio_attach },
337 1.1 mbalmer { "pulse", gpio_pulse },
338 1.1 mbalmer { NULL, NULL }
339 1.1 mbalmer };
340 1.1 mbalmer int n;
341 1.1 mbalmer
342 1.1 mbalmer luaL_register(L, "gpio", methods);
343 1.1 mbalmer gpio_set_info(L);
344 1.1 mbalmer
345 1.1 mbalmer /* The gpio metatable */
346 1.1 mbalmer if (luaL_newmetatable(L, GPIO_METATABLE)) {
347 1.1 mbalmer luaL_register(L, NULL, gpio_methods);
348 1.1 mbalmer
349 1.1 mbalmer lua_pushliteral(L, "__gc");
350 1.1 mbalmer lua_pushcfunction(L, gpio_close);
351 1.1 mbalmer lua_settable(L, -3);
352 1.1 mbalmer
353 1.1 mbalmer lua_pushliteral(L, "__index");
354 1.1 mbalmer lua_pushvalue(L, -2);
355 1.1 mbalmer lua_settable(L, -3);
356 1.1 mbalmer
357 1.1 mbalmer lua_pushliteral(L, "__metatable");
358 1.1 mbalmer lua_pushliteral(L, "must not access this metatable");
359 1.1 mbalmer lua_settable(L, -3);
360 1.1 mbalmer }
361 1.1 mbalmer lua_pop(L, 1);
362 1.1 mbalmer
363 1.1 mbalmer for (n = 0; gpio_constant[n].name != NULL; n++) {
364 1.1 mbalmer lua_pushinteger(L, gpio_constant[n].value);
365 1.1 mbalmer lua_setfield(L, -2, gpio_constant[n].name);
366 1.1 mbalmer };
367 1.1 mbalmer return 1;
368 1.1 mbalmer }
369