1 1.9 lneto /* $NetBSD: gpio.c,v 1.9 2014/07/19 18:38:34 lneto 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.7 joerg static __printflike(2, 3) 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 struct constant { 228 1.3 christos const char *name; 229 1.1 mbalmer int value; 230 1.1 mbalmer }; 231 1.1 mbalmer 232 1.3 christos static const struct constant gpio_constant[] = { 233 1.1 mbalmer /* GPIO pin states */ 234 1.1 mbalmer { "PIN_LOW", GPIO_PIN_LOW }, 235 1.1 mbalmer { "PIN_HIGH", GPIO_PIN_HIGH }, 236 1.1 mbalmer 237 1.1 mbalmer /* GPIO pin configuration flags */ 238 1.1 mbalmer { "PIN_INPUT", GPIO_PIN_INPUT }, 239 1.1 mbalmer { "PIN_OUTPUT", GPIO_PIN_OUTPUT }, 240 1.1 mbalmer { "PIN_INOUT", GPIO_PIN_INOUT }, 241 1.1 mbalmer { "PIN_OPENDRAIN", GPIO_PIN_OPENDRAIN }, 242 1.1 mbalmer { "PIN_PUSHPULL", GPIO_PIN_PUSHPULL }, 243 1.1 mbalmer { "PIN_TRISTATE", GPIO_PIN_TRISTATE }, 244 1.1 mbalmer { "PIN_PULLUP", GPIO_PIN_PULLUP }, 245 1.1 mbalmer { "PIN_PULLDOWN", GPIO_PIN_PULLDOWN }, 246 1.1 mbalmer { "PIN_INVIN", GPIO_PIN_INVIN }, 247 1.1 mbalmer { "PIN_INVOUT", GPIO_PIN_INVOUT }, 248 1.1 mbalmer { "PIN_USER", GPIO_PIN_USER }, 249 1.1 mbalmer { "PIN_PULSATE", GPIO_PIN_PULSATE }, 250 1.1 mbalmer { "PIN_SET", GPIO_PIN_SET }, 251 1.1 mbalmer { NULL, 0 } 252 1.1 mbalmer }; 253 1.1 mbalmer 254 1.1 mbalmer static void 255 1.1 mbalmer gpio_set_info(lua_State *L) 256 1.1 mbalmer { 257 1.1 mbalmer lua_pushliteral(L, "_COPYRIGHT"); 258 1.8 mbalmer lua_pushliteral(L, "Copyright (C) 2011, 2013 Marc Balmer " 259 1.8 mbalmer "<marc (at) msys.ch>"); 260 1.1 mbalmer lua_settable(L, -3); 261 1.1 mbalmer lua_pushliteral(L, "_DESCRIPTION"); 262 1.1 mbalmer lua_pushliteral(L, "GPIO interface for Lua"); 263 1.1 mbalmer lua_settable(L, -3); 264 1.1 mbalmer lua_pushliteral(L, "_VERSION"); 265 1.8 mbalmer lua_pushliteral(L, "gpio 1.0.2"); 266 1.1 mbalmer lua_settable(L, -3); 267 1.1 mbalmer } 268 1.1 mbalmer 269 1.3 christos int luaopen_gpio(lua_State*); 270 1.3 christos 271 1.1 mbalmer int 272 1.1 mbalmer luaopen_gpio(lua_State* L) 273 1.1 mbalmer { 274 1.1 mbalmer static const struct luaL_Reg methods[] = { 275 1.1 mbalmer { "open", gpio_open }, 276 1.1 mbalmer { NULL, NULL } 277 1.1 mbalmer }; 278 1.1 mbalmer static const struct luaL_Reg gpio_methods[] = { 279 1.1 mbalmer { "info", gpio_info }, 280 1.1 mbalmer { "close", gpio_close }, 281 1.1 mbalmer { "set", gpio_set }, 282 1.1 mbalmer { "unset", gpio_unset }, 283 1.1 mbalmer { "read", gpio_read }, 284 1.1 mbalmer { "write", gpio_write }, 285 1.1 mbalmer { "toggle", gpio_toggle }, 286 1.1 mbalmer { "attach", gpio_attach }, 287 1.1 mbalmer { NULL, NULL } 288 1.1 mbalmer }; 289 1.1 mbalmer int n; 290 1.1 mbalmer 291 1.9 lneto luaL_newlib(L, methods); 292 1.9 lneto luaL_setfuncs(L, gpio_methods, 0); 293 1.1 mbalmer gpio_set_info(L); 294 1.1 mbalmer 295 1.1 mbalmer /* The gpio metatable */ 296 1.1 mbalmer if (luaL_newmetatable(L, GPIO_METATABLE)) { 297 1.9 lneto luaL_setfuncs(L, gpio_methods, 0); 298 1.1 mbalmer 299 1.1 mbalmer lua_pushliteral(L, "__gc"); 300 1.1 mbalmer lua_pushcfunction(L, gpio_close); 301 1.1 mbalmer lua_settable(L, -3); 302 1.1 mbalmer 303 1.1 mbalmer lua_pushliteral(L, "__index"); 304 1.1 mbalmer lua_pushvalue(L, -2); 305 1.1 mbalmer lua_settable(L, -3); 306 1.1 mbalmer 307 1.1 mbalmer lua_pushliteral(L, "__metatable"); 308 1.1 mbalmer lua_pushliteral(L, "must not access this metatable"); 309 1.1 mbalmer lua_settable(L, -3); 310 1.1 mbalmer } 311 1.1 mbalmer lua_pop(L, 1); 312 1.1 mbalmer 313 1.1 mbalmer for (n = 0; gpio_constant[n].name != NULL; n++) { 314 1.1 mbalmer lua_pushinteger(L, gpio_constant[n].value); 315 1.1 mbalmer lua_setfield(L, -2, gpio_constant[n].name); 316 1.1 mbalmer }; 317 1.1 mbalmer return 1; 318 1.1 mbalmer } 319