Annotation of embedtools/src/pceng_run.c, revision 1.1.2.10

1.1.2.1   misho       1: /*************************************************************************
                      2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitbg.com>
                      3:  *  by Michael Pounov <misho@aitbg.com>
                      4:  *
                      5:  * $Author: misho $
1.1.2.10! misho       6:  * $Id: pceng_run.c,v 1.1.2.9 2011/10/24 23:14:36 misho Exp $
1.1.2.1   misho       7:  *
                      8:  *************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.1.2.10! misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.1.2.1   misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
                     46: #include "global.h"
                     47: #include "pceng.h"
                     48: 
                     49: 
1.1.2.5   misho      50: const u_int led_base[2][MAX_GPIO_PINS] = {{0xf410, 0xf400, 0xf400, 0xf400}, 
1.1.2.3   misho      51:        {0x61b0, 0x6100, 0x6180, 0x6180}};
1.1.2.5   misho      52: const int led_bit[2][MAX_GPIO_PINS] = {{8, 2, 3, 18}, {8, 6, 9, 11}};
1.1.2.3   misho      53: const int gpio_off[2] = { 0x4, 0x10 };
                     54: 
                     55: 
                     56: static inline board_t
                     57: getBoard()
                     58: {
                     59:        board_t type;
1.1.2.10! misho      60:        ait_val_t v;
1.1.2.3   misho      61: 
                     62:        FTRACE(3);
                     63: 
1.1.2.10! misho      64:        cfg_loadAttribute(&cfg, "pceng", "board", &v, DEFAULT_BOARD);
        !            65:        if (!strcasecmp(AIT_GET_STR(&v), "alix"))
1.1.2.3   misho      66:                type = alix;
1.1.2.10! misho      67:        else if (!strcasecmp(AIT_GET_STR(&v), "wrap"))
1.1.2.3   misho      68:                type = wrap;
                     69:        else {
                     70:                type = unknown;
1.1.2.10! misho      71:                syslog(LOG_ERR, "Error:: unknown board type %s", AIT_GET_STR(&v));
1.1.2.3   misho      72:        }
                     73: 
1.1.2.10! misho      74:        AIT_FREE_VAL(&v);
1.1.2.3   misho      75:        return type;
                     76: }
                     77: 
                     78: static u_int
1.1.2.5   misho      79: gpioRead(u_char ledno)
1.1.2.3   misho      80: {
                     81:        u_int n;
                     82:        board_t type = getBoard();
                     83: 
                     84:        FTRACE(3);
                     85: 
                     86:        switch (type) {
                     87:                case wrap:
                     88:                        n = inl(led_base[type - 1][ledno] + gpio_off[type - 1]);
                     89:                        if (n & (1 << led_bit[type - 1][ledno]))
                     90:                                n = 1;
                     91:                        else
                     92:                                n = 0;
                     93:                        break;
                     94:                case alix:
                     95:                        n = inl(led_base[type - 1][ledno]);
                     96:                        if (n & (1 << led_bit[type - 1][ledno]))
                     97:                                n = 0;
                     98:                        else
                     99:                                n = 1;
                    100:                        break;
                    101:                default:
                    102:                        return -1;
                    103:        }
                    104: 
                    105:        return n;
                    106: }
                    107: 
                    108: 
1.1.2.1   misho     109: int
1.1.2.3   misho     110: LED(u_char ledno, u_char state)
1.1.2.2   misho     111: {
1.1.2.3   misho     112:        u_int n;
                    113:        board_t type = getBoard();
                    114: 
1.1.2.2   misho     115:        FTRACE(3);
                    116: 
1.1.2.3   misho     117:        switch (type) {
                    118:                case wrap:
                    119:                        n = inl(led_base[type - 1][ledno]);
1.1.2.6   misho     120:                        /* read */
1.1.2.4   misho     121:                        if (state == (u_char) -1)
                    122:                                return (n &= (1 << led_bit[type - 1][ledno]));
                    123: 
1.1.2.3   misho     124:                        if (state)
                    125:                                n |= (1 << led_bit[type - 1][ledno]);
                    126:                        else
                    127:                                n &= ~(1 << led_bit[type - 1][ledno]);
                    128:                        break;
                    129:                case alix:
1.1.2.6   misho     130:                        /* read */
1.1.2.4   misho     131:                        if (state == (u_char) -1) {
                    132:                                n = inl(led_base[type - 1][ledno]);
                    133:                                return !(n &= (1 << led_bit[type - 1][ledno]));
                    134:                        }
                    135: 
1.1.2.3   misho     136:                        if (state)
                    137:                                n = (1 << (led_bit[type - 1][ledno] + gpio_off[type - 1]));
                    138:                        else
                    139:                                n = (1 << led_bit[type - 1][ledno]);
                    140:                        break;
1.1.2.6   misho     141:                default:        /* unknown */
1.1.2.3   misho     142:                        return -1;
                    143:        }
                    144: 
                    145:        outl(led_base[type - 1][ledno], n);
1.1.2.7   misho     146:        return (int) state;
1.1.2.2   misho     147: }
                    148: 
1.1.2.5   misho     149: static int
                    150: RunCmd(u_char pin, u_int state)
                    151: {
1.1.2.10! misho     152:        char szArg[STRSIZ] = { 0 };
        !           153:        ait_val_t v;
1.1.2.5   misho     154: 
                    155:        FTRACE(3);
                    156: 
                    157:        switch (pin) {
                    158:                case 0:         /* button */
1.1.2.10! misho     159:                        cfg_loadAttribute(&cfg, "event", "button_exec", &v, NULL);
1.1.2.5   misho     160:                        break;
                    161:                case 1:         /* LEDs */
                    162:                case 2:
                    163:                case 3:
1.1.2.10! misho     164:                        cfg_loadAttribute(&cfg, "event", "led_exec", &v, NULL);
1.1.2.5   misho     165:                        break;
                    166:        }
1.1.2.10! misho     167:        if (AIT_ISEMPTY(&v))
1.1.2.5   misho     168:                return 0;
1.1.2.9   misho     169:        if (pin)
                    170:                snprintf(szArg, sizeof szArg, "%d=%d", pin, state);
1.1.2.5   misho     171:        else
                    172:                snprintf(szArg, sizeof szArg, "%d", state);
                    173: 
1.1.2.6   misho     174:        switch (fork()) {
1.1.2.5   misho     175:                case -1:
                    176:                        syslog(LOG_ERR, "Error:: RunCmd #%d - %s\n", errno, strerror(errno));
1.1.2.10! misho     177:                        AIT_FREE_VAL(&v);
1.1.2.5   misho     178:                        return -1;
                    179:                case 0:         /* execute command */
1.1.2.10! misho     180:                        return execl(AIT_GET_STR(&v), AIT_GET_STR(&v), szArg, NULL);
1.1.2.5   misho     181:        }
                    182: 
1.1.2.10! misho     183:        AIT_FREE_VAL(&v);
1.1.2.5   misho     184:        return 0;
                    185: }
                    186: 
1.1.2.2   misho     187: int
1.1.2.3   misho     188: Run()
1.1.2.1   misho     189: {
1.1.2.5   misho     190:        register u_char i;
                    191:        u_int ret, t, slice, states = 0;
1.1.2.10! misho     192:        ait_val_t v;
1.1.2.5   misho     193: 
1.1.2.1   misho     194:        FTRACE(3);
                    195: 
1.1.2.5   misho     196:        /* init array */
                    197:        for (i = 0; i < MAX_GPIO_PINS; i++)
                    198:                if ((ret = gpioRead(i)) == -1)
                    199:                        return 126;
                    200:                else
1.1.2.7   misho     201:                        states |= ret ? (1 << i) : 0;
1.1.2.5   misho     202: 
                    203:        /* state machine */
                    204:        while (!Kill) {
1.1.2.10! misho     205:                cfg_loadAttribute(&cfg, "event", "button_slice", &v, DEFAULT_SLICE);
        !           206:                slice = strtol(AIT_GET_STR(&v), NULL, 0);
        !           207:                AIT_FREE_VAL(&v);
1.1.2.5   misho     208:                if (!slice)
                    209:                        slice = strtol(DEFAULT_SLICE, NULL, 0);
                    210:                slice *= 1000;
                    211: 
                    212:                for (i = t = 0; i < MAX_GPIO_PINS; t = 0, i++) {
                    213:                        if ((ret = gpioRead(i)) == -1)
                    214:                                return 127;
                    215: 
                    216:                        if (ret != ((states >> i) & 0x1)) {
1.1.2.7   misho     217:                                if (!i) {       /* button */
1.1.2.5   misho     218:                                        t = 0;
                    219:                                        do {
                    220:                                                t++;
                    221:                                                usleep(slice);
                    222:                                                if (gpioRead(i) != ret)
                    223:                                                        break;
                    224:                                        } while (!Kill);
1.1.2.7   misho     225:                                } else {        /* leds */
1.1.2.5   misho     226:                                        t = ret;
                    227:                                        if (ret)
                    228:                                                states |= (1 << i);
                    229:                                        else
                    230:                                                states &= ~(1 << i);
                    231:                                }
                    232: 
                    233:                                RunCmd(i, t);
                    234:                        }
                    235:                }
1.1.2.8   misho     236: 
                    237:                usleep(slice);
1.1.2.5   misho     238:        }
                    239: 
1.1.2.1   misho     240:        return 0;
                    241: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>