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

1.2     ! misho       1: /*************************************************************************
        !             2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitbg.com>
        !             3:  *  by Michael Pounov <misho@aitbg.com>
        !             4:  *
        !             5:  * $Author: misho $
        !             6:  * $Id: pceng_run.c,v 1.1.2.10 2012/04/05 12:22:44 misho Exp $
        !             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: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
        !            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: 
        !            50: const u_int led_base[2][MAX_GPIO_PINS] = {{0xf410, 0xf400, 0xf400, 0xf400}, 
        !            51:        {0x61b0, 0x6100, 0x6180, 0x6180}};
        !            52: const int led_bit[2][MAX_GPIO_PINS] = {{8, 2, 3, 18}, {8, 6, 9, 11}};
        !            53: const int gpio_off[2] = { 0x4, 0x10 };
        !            54: 
        !            55: 
        !            56: static inline board_t
        !            57: getBoard()
        !            58: {
        !            59:        board_t type;
        !            60:        ait_val_t v;
        !            61: 
        !            62:        FTRACE(3);
        !            63: 
        !            64:        cfg_loadAttribute(&cfg, "pceng", "board", &v, DEFAULT_BOARD);
        !            65:        if (!strcasecmp(AIT_GET_STR(&v), "alix"))
        !            66:                type = alix;
        !            67:        else if (!strcasecmp(AIT_GET_STR(&v), "wrap"))
        !            68:                type = wrap;
        !            69:        else {
        !            70:                type = unknown;
        !            71:                syslog(LOG_ERR, "Error:: unknown board type %s", AIT_GET_STR(&v));
        !            72:        }
        !            73: 
        !            74:        AIT_FREE_VAL(&v);
        !            75:        return type;
        !            76: }
        !            77: 
        !            78: static u_int
        !            79: gpioRead(u_char ledno)
        !            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: 
        !           109: int
        !           110: LED(u_char ledno, u_char state)
        !           111: {
        !           112:        u_int n;
        !           113:        board_t type = getBoard();
        !           114: 
        !           115:        FTRACE(3);
        !           116: 
        !           117:        switch (type) {
        !           118:                case wrap:
        !           119:                        n = inl(led_base[type - 1][ledno]);
        !           120:                        /* read */
        !           121:                        if (state == (u_char) -1)
        !           122:                                return (n &= (1 << led_bit[type - 1][ledno]));
        !           123: 
        !           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:
        !           130:                        /* read */
        !           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: 
        !           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;
        !           141:                default:        /* unknown */
        !           142:                        return -1;
        !           143:        }
        !           144: 
        !           145:        outl(led_base[type - 1][ledno], n);
        !           146:        return (int) state;
        !           147: }
        !           148: 
        !           149: static int
        !           150: RunCmd(u_char pin, u_int state)
        !           151: {
        !           152:        char szArg[STRSIZ] = { 0 };
        !           153:        ait_val_t v;
        !           154: 
        !           155:        FTRACE(3);
        !           156: 
        !           157:        switch (pin) {
        !           158:                case 0:         /* button */
        !           159:                        cfg_loadAttribute(&cfg, "event", "button_exec", &v, NULL);
        !           160:                        break;
        !           161:                case 1:         /* LEDs */
        !           162:                case 2:
        !           163:                case 3:
        !           164:                        cfg_loadAttribute(&cfg, "event", "led_exec", &v, NULL);
        !           165:                        break;
        !           166:        }
        !           167:        if (AIT_ISEMPTY(&v))
        !           168:                return 0;
        !           169:        if (pin)
        !           170:                snprintf(szArg, sizeof szArg, "%d=%d", pin, state);
        !           171:        else
        !           172:                snprintf(szArg, sizeof szArg, "%d", state);
        !           173: 
        !           174:        switch (fork()) {
        !           175:                case -1:
        !           176:                        syslog(LOG_ERR, "Error:: RunCmd #%d - %s\n", errno, strerror(errno));
        !           177:                        AIT_FREE_VAL(&v);
        !           178:                        return -1;
        !           179:                case 0:         /* execute command */
        !           180:                        return execl(AIT_GET_STR(&v), AIT_GET_STR(&v), szArg, NULL);
        !           181:        }
        !           182: 
        !           183:        AIT_FREE_VAL(&v);
        !           184:        return 0;
        !           185: }
        !           186: 
        !           187: int
        !           188: Run()
        !           189: {
        !           190:        register u_char i;
        !           191:        u_int ret, t, slice, states = 0;
        !           192:        ait_val_t v;
        !           193: 
        !           194:        FTRACE(3);
        !           195: 
        !           196:        /* init array */
        !           197:        for (i = 0; i < MAX_GPIO_PINS; i++)
        !           198:                if ((ret = gpioRead(i)) == -1)
        !           199:                        return 126;
        !           200:                else
        !           201:                        states |= ret ? (1 << i) : 0;
        !           202: 
        !           203:        /* state machine */
        !           204:        while (!Kill) {
        !           205:                cfg_loadAttribute(&cfg, "event", "button_slice", &v, DEFAULT_SLICE);
        !           206:                slice = strtol(AIT_GET_STR(&v), NULL, 0);
        !           207:                AIT_FREE_VAL(&v);
        !           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)) {
        !           217:                                if (!i) {       /* button */
        !           218:                                        t = 0;
        !           219:                                        do {
        !           220:                                                t++;
        !           221:                                                usleep(slice);
        !           222:                                                if (gpioRead(i) != ret)
        !           223:                                                        break;
        !           224:                                        } while (!Kill);
        !           225:                                } else {        /* leds */
        !           226:                                        t = ret;
        !           227:                                        if (ret)
        !           228:                                                states |= (1 << i);
        !           229:                                        else
        !           230:                                                states &= ~(1 << i);
        !           231:                                }
        !           232: 
        !           233:                                RunCmd(i, t);
        !           234:                        }
        !           235:                }
        !           236: 
        !           237:                usleep(slice);
        !           238:        }
        !           239: 
        !           240:        return 0;
        !           241: }

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