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