version 1.1, 2011/07/22 14:20:24
|
version 1.2, 2012/07/22 22:46:47
|
Line 0
|
Line 1
|
|
/************************************************************************* |
|
* (C) 2011 AITNET - Sofia/Bulgaria - <office@aitbg.com> |
|
* by Michael Pounov <misho@aitbg.com> |
|
* |
|
* $Author$ |
|
* $Id$ |
|
* |
|
************************************************************************* |
|
The ELWIX and AITNET software is distributed under the following |
|
terms: |
|
|
|
All of the documentation and software included in the ELWIX and AITNET |
|
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
|
|
|
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 |
|
by Michael Pounov <misho@elwix.org>. All rights reserved. |
|
|
|
Redistribution and use in source and binary forms, with or without |
|
modification, are permitted provided that the following conditions |
|
are met: |
|
1. Redistributions of source code must retain the above copyright |
|
notice, this list of conditions and the following disclaimer. |
|
2. Redistributions in binary form must reproduce the above copyright |
|
notice, this list of conditions and the following disclaimer in the |
|
documentation and/or other materials provided with the distribution. |
|
3. All advertising materials mentioning features or use of this software |
|
must display the following acknowledgement: |
|
This product includes software developed by Michael Pounov <misho@elwix.org> |
|
ELWIX - Embedded LightWeight unIX and its contributors. |
|
4. Neither the name of AITNET nor the names of its contributors |
|
may be used to endorse or promote products derived from this software |
|
without specific prior written permission. |
|
|
|
THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND |
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
SUCH DAMAGE. |
|
*/ |
|
#include "global.h" |
|
#include "pceng.h" |
|
|
|
|
|
const u_int led_base[2][MAX_GPIO_PINS] = {{0xf410, 0xf400, 0xf400, 0xf400}, |
|
{0x61b0, 0x6100, 0x6180, 0x6180}}; |
|
const int led_bit[2][MAX_GPIO_PINS] = {{8, 2, 3, 18}, {8, 6, 9, 11}}; |
|
const int gpio_off[2] = { 0x4, 0x10 }; |
|
|
|
|
|
static inline board_t |
|
getBoard() |
|
{ |
|
board_t type; |
|
ait_val_t v; |
|
|
|
FTRACE(3); |
|
|
|
cfg_loadAttribute(&cfg, "pceng", "board", &v, DEFAULT_BOARD); |
|
if (!strcasecmp(AIT_GET_STR(&v), "alix")) |
|
type = alix; |
|
else if (!strcasecmp(AIT_GET_STR(&v), "wrap")) |
|
type = wrap; |
|
else { |
|
type = unknown; |
|
syslog(LOG_ERR, "Error:: unknown board type %s", AIT_GET_STR(&v)); |
|
} |
|
|
|
AIT_FREE_VAL(&v); |
|
return type; |
|
} |
|
|
|
static u_int |
|
gpioRead(u_char ledno) |
|
{ |
|
u_int n; |
|
board_t type = getBoard(); |
|
|
|
FTRACE(3); |
|
|
|
switch (type) { |
|
case wrap: |
|
n = inl(led_base[type - 1][ledno] + gpio_off[type - 1]); |
|
if (n & (1 << led_bit[type - 1][ledno])) |
|
n = 1; |
|
else |
|
n = 0; |
|
break; |
|
case alix: |
|
n = inl(led_base[type - 1][ledno]); |
|
if (n & (1 << led_bit[type - 1][ledno])) |
|
n = 0; |
|
else |
|
n = 1; |
|
break; |
|
default: |
|
return -1; |
|
} |
|
|
|
return n; |
|
} |
|
|
|
|
|
int |
|
LED(u_char ledno, u_char state) |
|
{ |
|
u_int n; |
|
board_t type = getBoard(); |
|
|
|
FTRACE(3); |
|
|
|
switch (type) { |
|
case wrap: |
|
n = inl(led_base[type - 1][ledno]); |
|
/* read */ |
|
if (state == (u_char) -1) |
|
return (n &= (1 << led_bit[type - 1][ledno])); |
|
|
|
if (state) |
|
n |= (1 << led_bit[type - 1][ledno]); |
|
else |
|
n &= ~(1 << led_bit[type - 1][ledno]); |
|
break; |
|
case alix: |
|
/* read */ |
|
if (state == (u_char) -1) { |
|
n = inl(led_base[type - 1][ledno]); |
|
return !(n &= (1 << led_bit[type - 1][ledno])); |
|
} |
|
|
|
if (state) |
|
n = (1 << (led_bit[type - 1][ledno] + gpio_off[type - 1])); |
|
else |
|
n = (1 << led_bit[type - 1][ledno]); |
|
break; |
|
default: /* unknown */ |
|
return -1; |
|
} |
|
|
|
outl(led_base[type - 1][ledno], n); |
|
return (int) state; |
|
} |
|
|
|
static int |
|
RunCmd(u_char pin, u_int state) |
|
{ |
|
char szArg[STRSIZ] = { 0 }; |
|
ait_val_t v; |
|
|
|
FTRACE(3); |
|
|
|
switch (pin) { |
|
case 0: /* button */ |
|
cfg_loadAttribute(&cfg, "event", "button_exec", &v, NULL); |
|
break; |
|
case 1: /* LEDs */ |
|
case 2: |
|
case 3: |
|
cfg_loadAttribute(&cfg, "event", "led_exec", &v, NULL); |
|
break; |
|
} |
|
if (AIT_ISEMPTY(&v)) |
|
return 0; |
|
if (pin) |
|
snprintf(szArg, sizeof szArg, "%d=%d", pin, state); |
|
else |
|
snprintf(szArg, sizeof szArg, "%d", state); |
|
|
|
switch (fork()) { |
|
case -1: |
|
syslog(LOG_ERR, "Error:: RunCmd #%d - %s\n", errno, strerror(errno)); |
|
AIT_FREE_VAL(&v); |
|
return -1; |
|
case 0: /* execute command */ |
|
return execl(AIT_GET_STR(&v), AIT_GET_STR(&v), szArg, NULL); |
|
} |
|
|
|
AIT_FREE_VAL(&v); |
|
return 0; |
|
} |
|
|
|
int |
|
Run() |
|
{ |
|
register u_char i; |
|
u_int ret, t, slice, states = 0; |
|
ait_val_t v; |
|
|
|
FTRACE(3); |
|
|
|
/* init array */ |
|
for (i = 0; i < MAX_GPIO_PINS; i++) |
|
if ((ret = gpioRead(i)) == -1) |
|
return 126; |
|
else |
|
states |= ret ? (1 << i) : 0; |
|
|
|
/* state machine */ |
|
while (!Kill) { |
|
cfg_loadAttribute(&cfg, "event", "button_slice", &v, DEFAULT_SLICE); |
|
slice = strtol(AIT_GET_STR(&v), NULL, 0); |
|
AIT_FREE_VAL(&v); |
|
if (!slice) |
|
slice = strtol(DEFAULT_SLICE, NULL, 0); |
|
slice *= 1000; |
|
|
|
for (i = t = 0; i < MAX_GPIO_PINS; t = 0, i++) { |
|
if ((ret = gpioRead(i)) == -1) |
|
return 127; |
|
|
|
if (ret != ((states >> i) & 0x1)) { |
|
if (!i) { /* button */ |
|
t = 0; |
|
do { |
|
t++; |
|
usleep(slice); |
|
if (gpioRead(i) != ret) |
|
break; |
|
} while (!Kill); |
|
} else { /* leds */ |
|
t = ret; |
|
if (ret) |
|
states |= (1 << i); |
|
else |
|
states &= ~(1 << i); |
|
} |
|
|
|
RunCmd(i, t); |
|
} |
|
} |
|
|
|
usleep(slice); |
|
} |
|
|
|
return 0; |
|
} |