RetroBSD
http://retrobsd.org/

Halt - based on a hw signal
http://retrobsd.org/viewtopic.php?f=5&t=37300
Page 1 of 1

Author:  Pito [ Mon Sep 28, 2015 8:44 am ]
Post subject:  Halt - based on a hw signal

Would it be possible to add following functionality into the kernel?
1. a signal from a powerdown detector at PinX (configurable active low or high)
2. when the signal triggers, it starts "halt"

So when the detector sees the power is dropping down, it does shutdown of the system cleanly.
There will be a backup battery to power system during power down, as the "halt" takes about 2-3secs.
P.

Author:  majenko [ Mon Sep 28, 2015 9:37 am ]
Post subject:  Re: Halt - based on a hw signal

Why do you need such functionality in the kernel? That's the kind of thing you run as a daemon in userland. Just open the GPIO and watch it. When it changes to your desired state you run the halt command.

There is no need for anything in the kernel...

Author:  Pito [ Mon Sep 28, 2015 3:58 pm ]
Post subject:  Re: Halt - based on a hw signal

So you want watch the power down from userland? How? With app in cron ?? :) :)
It must be in kernel, sure.. It is time critical thing.
Writing daemons is difficult..

Author:  vak [ Mon Sep 28, 2015 6:19 pm ]
Post subject:  Re: Halt - based on a hw signal

Pito wrote:
So you want watch the power down from userland? How? With app in cron ?? :)
Well, it's definitely possible from userland with a daemon. Just poll a signal every second and invoke reboot() when needed. Not a good solution though, as it will result in excessive swapping. Every second this daemon will be swapped in, just to check the signal, and swap out again.

An optimal way would be to have a daemon suspended on a blocking system call like read(), and activated by a change of the hardware signal. We can enhance the gpio driver to do this. An alternative approach is to translate the hardware signal event into a software signal, and handle it much like SIGALRM in the daemon.

Pito wrote:
Writing daemons is difficult..
Writing "hello world" is also difficult, when you do it for the first time. :)
Here is the source of /sbin/update daemon, as an example:
Code:
int main()
{
    daemon(0, 0);
    for (;;) {
        sync();
        sleep(30);
    }
}

Author:  majenko [ Mon Sep 28, 2015 9:03 pm ]
Post subject:  Re: Halt - based on a hw signal

The model used by the Linux GPIO is quite a good one.

You have a file per IO (which you have to "export" first) and you can enable interrupt mode. When the value of the input changes the value in its file is changed.

Reading it (blocking) is nice and easy, and here's some code I use on an ARM system to activate a function on a button press:

Code:
    while (1) {
        memset((void *)fdset, 0, sizeof(fdset));
        fdset[0].fd = fd;
        fdset[0].events = POLLPRI;
        rc = poll(fdset, nfds, timeout);
        if (rc < 0) {
            error("Error polling input");
        }

        if (fdset[0].revents & POLLPRI) {
            memset(temp, 0, 100);
            lseek(fd, 0, SEEK_SET);
            read(fd, temp, 100);
            if (isFirstExec == 0) {
                executeCommand();
            } else {
                isFirstExec = 0;
            }
        }
    }

Author:  Pito [ Tue Sep 29, 2015 5:43 am ]
Post subject:  Re: Halt - based on a hw signal

So experts may help :).
I think that functionality is important as switching off the system via console would be nogo with standalone system (I do plan).
Imagine a box with an on/off switch. No console attached. Without that functionality you get a corrupt sdcard filesystem most of the time..

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/