• Re: timer interrupt

    From A. Dumas@3:770/3 to zeneca on Thu Nov 4 10:18:18 2021
    On 04-11-2021 10:03, zeneca wrote:
    I am looking for information and example on programing a led that's is
    on / off every second, based on a 'timer' interrupt.
    So far I found very few information on timers.

    By the way I am programing in C.

    An actual timer interrupt in Linux using C is not trivial. See for
    example "man timer_create".

    You could simply make a never-ending loop (like Arduino) and sleep 1
    second every loop. That means your program has just that one function,
    though. (Of course you could do other things instead of sleeping.)

    If you want easy and reliable timer interrupts to do simple hardware
    things, use a microcontroller like the Pico, not a full Linux Raspberry Pi.
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From zeneca@3:770/3 to All on Thu Nov 4 10:03:46 2021
    Hello,

    I am looking for information and example on programing a led that's is
    on / off every second, based on a 'timer' interrupt.
    So far I found very few information on timers.

    By the way I am programing in C.
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From nev young@3:770/3 to zeneca on Thu Nov 4 10:11:26 2021
    On 04/11/2021 09:03, zeneca wrote:
    Hello,

    I am looking for information and example on programing a led that's is
    on / off every second, based on a 'timer' interrupt.
    So far I found very few information on timers.

    By the way I am programing in C.

    sleep()

    e.g.
    #include<stdio.h>

    main()
    {
    printf("Sleeping for 1 second.\n");
    sleep(1);
    return 0;
    }

    also see usleep() and nanosleep()
    despite being obsolete they may do what you want if available.

    --
    Nev
    It causes me a great deal of regret and remorse
    that so many people are unable to understand what I write.
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Martin Gregorie@3:770/3 to zeneca on Thu Nov 4 10:00:13 2021
    On Thu, 4 Nov 2021 10:03:46 +0100, zeneca wrote:

    Hello,

    I am looking for information and example on programing a led that's is
    on / off every second, based on a 'timer' interrupt.
    So far I found very few information on timers.

    By the way I am programing in C.

    You could always use the poll() function, though the advisability of this depends on what else the program is doing apart from making an LED blink.

    For instance, if poll() watches stdin and has a timeout value of 1
    second, it would be trivial to use poll() to turn the LED on for a
    second, then off for a second until a key is hit, and then to write
    "Exiting NOW" to stdout and exit.

    IME its useful to understand how to use poll(). This is because writing
    and debugging a program that uses it to handle input from several files
    is a lot simpler and less error-prone than using threads to do the same
    thing.


    --
    --
    Martin | martin at
    Gregorie | gregorie dot org
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Folderol@3:770/3 to nev young on Thu Nov 4 12:14:38 2021
    On Thu, 4 Nov 2021 10:11:26 +0000
    nev young <newsforpasiphae1953@yahoo.co.uk> wrote:

    also see usleep() and nanosleep()
    despite being obsolete they may do what you want if available.

    Not strictly true.
    Windows has never supported nanosleep.
    On Posix, although deprecated, these days the OS actually translates it as
    1000 * nanosleep (which is very much current).

    --
    Basic
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Ahem A Rivet's Shot@3:770/3 to nev young on Thu Nov 4 12:49:29 2021
    On Thu, 4 Nov 2021 10:11:26 +0000
    nev young <newsforpasiphae1953@yahoo.co.uk> wrote:

    On 04/11/2021 09:03, zeneca wrote:
    Hello,

    I am looking for information and example on programing a led that's is
    on / off every second, based on a 'timer' interrupt.
    So far I found very few information on timers.

    By the way I am programing in C.

    sleep()

    Is interruptible and therefore may terminate early (same applies
    to poll and almost anything else you can use in C). If it doesn't terminate early it /will/ be late by whatever delay process switching and system load imposes.

    There is no really correct way to do this because Linux is not a
    real time OS - but you can come close[1] on a reasonably quiet system by
    using nanosleep, checking the time when it returns and either acting or calculating the new interval until you should wake up.

    [1] Almost certainly close enough for visual steadiness.

    --
    Steve O'Hara-Smith
    Odds and Ends at http://www.sohara.org/
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From zeneca@3:770/3 to All on Thu Nov 4 14:12:29 2021
    Le 04/11/2021 à 11:00, Martin Gregorie a écrit :
    On Thu, 4 Nov 2021 10:03:46 +0100, zeneca wrote:

    Hello,

    I am looking for information and example on programing a led that's is
    on / off every second, based on a 'timer' interrupt.
    So far I found very few information on timers.

    By the way I am programing in C.

    You could always use the poll() function, though the advisability of this depends on what else the program is doing apart from making an LED blink.

    For instance, if poll() watches stdin and has a timeout value of 1
    second, it would be trivial to use poll() to turn the LED on for a
    second, then off for a second until a key is hit, and then to write
    "Exiting NOW" to stdout and exit.

    IME its useful to understand how to use poll(). This is because writing
    and debugging a program that uses it to handle input from several files
    is a lot simpler and less error-prone than using threads to do the same thing.


    Many thanks, but for the moment I m only testing /evaluating
    possibilities. I often use timer wile programing microcontrolers (Atmel)
    but here poll doesn't fit because the target is free running procces in backgound supposed to take some action every second?.
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Martin Gregorie@3:770/3 to zeneca on Thu Nov 4 15:18:22 2021
    On Thu, 4 Nov 2021 14:12:29 +0100, zeneca wrote:

    Many thanks, but for the moment I m only testing /evaluating
    possibilities. I often use timer wile programing microcontrolers (Atmel)
    but here poll doesn't fit because the target is free running procces in backgound supposed to take some action every second?.

    That would be sleep(), then, or possibly usleep() if you want to keep
    exact clock time by reducing the wait time my the number of microseconds
    the code in your loop takes to execute. However, don't forget that Linux
    is NOT a real-time OS and so your program may get suspended or slowed
    down while a system process executes.

    The poll() and the earlier select() library functions are designed to
    support asynchronous i/o - the function typically watches several
    channels, which may be character oriented or buffered files. If the
    timeout is unset, poll() waits until data is received on one of the
    channels its watching, when it executes code associated with the channel
    that received the data before going back to waiting for a input on one of
    the channels. It uses no cpu time while waiting for a event.

    If a timeout value is set, if nothing happens until timeout expiry, when
    poll() exits. When that happens you'd normally normally do any required housekeeping and then call poll() again or, if a 'stop' command is
    received on stdin, the program might release any resources it holds and
    exit.

    As others have said, if all you want is to stop execution for a fixed
    amount of time, there are other library functions such as sleep() to do
    that. sleep() delays by whole seconds but there are other functions that
    are finer grained down to intervals measured in microseconds and
    nanoseconds.

    If you haven't found them yet, you really need to get to know the
    'apropos' and 'man' commands, which are used to search for and display
    manual pages for everything in Linux from commands to library functions.

    If you're new to Linux, you may also want to find a copy of "UNIX Systems Programming" by David Curry - it covers all aspects of writing C programs
    to run under any of the *nix operating systems. There's a lot in it, but
    its well organised, well written and contains excellent example code.


    --
    Martin | martin at
    Gregorie | gregorie dot org
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Knute Johnson@3:770/3 to zeneca on Thu Nov 4 09:31:43 2021
    On 11/4/21 04:03, zeneca wrote:
    Hello,

    I am looking for information and example on programing a led that's is
    on / off every second, based on a 'timer' interrupt.
    So far I found very few information on timers.

    By the way I am programing in C.

    I originally wrote these for an Arduino but it works fine on any Linux computer. It is to be used in a processing loop (eg.. Arduino). It is
    written in C++ but easy enough to change them to C. The timer uses very
    little processor and unless you need microsecond accuracy it works fine.

    /*
    * timer.h
    *
    * written by: knute johnson - 13 September 2021
    *
    */

    #undef FOR_ARDUINO // this code is not for arduino

    #ifndef TIMER_H
    #define TIMER_H

    #ifdef FOR_ARDUINO
    #include <Arduino.h>
    #endif

    class timer {
    private:
    // last time triggered
    unsigned long last_triggered;
    // period between triggers
    unsigned long period;
    // method to calculate time difference between two millis()
    readings
    unsigned long diff(unsigned long, unsigned long);
    #ifndef FOR_ARDUINO
    // a millis() function for non-Arduinos
    unsigned long millis();
    #endif
    // flag is inverted during check() if period has been reached
    bool flag = false;

    public:
    // create a new timer
    timer(unsigned long);
    // check to see if it is later than last trigger plus period
    bool check();
    // reset the last trigger to now
    void reset();
    // gets the status of the flag
    bool get_flag();
    };

    #endif /* timer.h */


    -----------------------------------------------------------------------------------------------


    /*
    * timer.cpp
    *
    * written by: Knute Johnson - 13 September 2021
    *
    * This method is NOT thread safe, all calls to the constructor and the
    public
    * methods must be from the same thread.
    *
    */

    #include "timer.h"

    #ifndef FOR_ARDUINO
    #include <sys/time.h>
    #include <cstddef>
    #endif

    // construct new timer
    // per - timer period in milliseconds
    timer::timer(unsigned long per) {
    period = per;
    last_triggered = millis();
    }

    #ifndef FOR_ARDUINO
    // a millis() function for non-arduino
    // returns - unsigned long of milliseconds since program started
    unsigned long timer::millis() {
    struct timeval tp;

    gettimeofday(&tp,NULL);
    return tp.tv_sec * 1000 + tp.tv_usec / 1000;
    }
    #endif

    // calculate elapsed time between two millis() readings
    // returns - unsigned long difference in milliseconds between two millis()
    // readings
    unsigned long timer::diff(unsigned long a, unsigned long b) {
    return a > b ? a - b : b - a;
    }

    // check that the current time has surpassed the last trigger plus period
    // returns - true if it has been period milliseconds since timer was last
    // triggered
    bool timer::check() {
    unsigned long now = millis();

    if (diff(now,last_triggered) >= period) {
    last_triggered = now;
    flag = !flag;
    return true;
    }

    return false;
    }

    // reset the last trigger to now
    void timer::reset() {
    last_triggered = millis();
    }

    // get the state of the flag
    bool timer::get_flag() {
    return flag;
    }

    -------------------------------------------------------------------------------------------

    Simple test program:
    #include <stdio.h>
    #include <unistd.h>

    #include "timer.h"

    int main() {
    timer test(1000); // create a new timer with 1 second timeout
    int n = 0;

    while (1) {
    if (test.check())
    printf("%d:%s\n",n++,test.get_flag() ? "true" : "false");
    usleep(100); // sleep for 100us
    }
    }
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From druck@3:770/3 to Folderol on Thu Nov 4 19:33:48 2021
    On 04/11/2021 12:14, Folderol wrote:
    On Thu, 4 Nov 2021 10:11:26 +0000
    nev young <newsforpasiphae1953@yahoo.co.uk> wrote:

    also see usleep() and nanosleep()
    despite being obsolete they may do what you want if available.

    Not strictly true.
    Windows has never supported nanosleep.
    On Posix, although deprecated, these days the OS actually translates it as 1000 * nanosleep (which is very much current).

    Just make sure you check that whatever sleep function you are using in
    whatever language, is doing what you expect for sub-second values, by
    timing how long it takes do to a few thousand calls.

    For example sleeps of down to 1ms may sleep for the expected time, but
    under that it may either return straight away causing the loop to run
    too fast, or always sleep for a minimum of 1ms, causing the loop to be
    too slow.

    I've seen this change from platform to platform, and language to
    language, including on Windows differences between Python 2 (<1ms
    returned immediately) and Python 3 (minimum sleep of 1ms).

    ---druck
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From The Natural Philosopher@3:770/3 to druck on Thu Nov 4 19:36:15 2021
    On 04/11/2021 19:33, druck wrote:
    On 04/11/2021 12:14, Folderol wrote:
    On Thu, 4 Nov 2021 10:11:26 +0000
    nev young <newsforpasiphae1953@yahoo.co.uk> wrote:

    also see usleep() and nanosleep()
    despite being obsolete they may do what you want if available.

    Not strictly true.
    Windows has never supported nanosleep.
    On Posix, although deprecated, these days the OS actually translates
    it as
    1000 * nanosleep (which is very much current).

    Just make sure you check that whatever sleep function you are using in whatever language, is doing what you expect for sub-second values, by
    timing how long it takes do to a few thousand calls.

    For example sleeps of down to 1ms may sleep for the expected time, but
    under that it may either return straight away causing the loop to run
    too fast, or always sleep for a minimum of 1ms, causing the loop to be
    too slow.

    I've seen this change from platform to platform, and language to
    language, including on Windows differences between Python 2 (<1ms
    returned immediately) and Python 3 (minimum sleep of 1ms).


    IIUIC you can set up a timer alarm to send a signal to process after a predetermined time.

    Interrupts are kernel space, not user space, so cannot be utilised directly.




    --
    "Corbyn talks about equality, justice, opportunity, health care, peace,
    community, compassion, investment, security, housing...."
    "What kind of person is not interested in those things?"

    "Jeremy Corbyn?"
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Brian Gregory@3:770/3 to zeneca on Thu Nov 4 19:43:53 2021
    On 04/11/2021 13:12, zeneca wrote:
    Many thanks, but for the moment I m only testing /evaluating
    possibilities. I often use timer wile programing microcontrolers (Atmel)
    but here poll doesn't fit because the target is free running procces in backgound supposed to take some action every second?.

    Why?
    Linux can do multithreading.

    --
    Brian Gregory (in England).
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From nev young@3:770/3 to Folderol on Thu Nov 4 22:27:41 2021
    On 04/11/2021 12:14, Folderol wrote:
    On Thu, 4 Nov 2021 10:11:26 +0000
    nev young <newsforpasiphae1953@yahoo.co.uk> wrote:

    also see usleep() and nanosleep()
    despite being obsolete they may do what you want if available.

    Not strictly true.
    Windows has never supported nanosleep.

    I wouldn't know what windows does.
    I'm a unix (now linux) user since the 1980s.
    And AFAIK Pi's don't run windows.


    --
    Nev
    It causes me a great deal of regret and remorse
    that so many people are unable to understand what I write.
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Andy Burns@3:770/3 to nev young on Fri Nov 5 08:50:23 2021
    nev young wrote:

    AFAIK Pi's don't run windows.

    <https://docs.microsoft.com/en-us/windows/arm>

    I don't think Microsoft support the Pi, but people seem to find ways

    <https://www.worproject.ml/downloads>
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From R.Wieser@3:770/3 to All on Fri Nov 5 10:29:09 2021
    zeneca,

    A word of warning :

    Most of the people here have adviced to use sleep(). There is something you should be aware of when you chose to use that method :

    The distance between two code executions seperated by sleep()s is whatever
    time you set sleep() to *plus* the time your code takes to do its thing
    (and before you ask : trying to recalculate the sleep time each step to
    account for the code-execution delay comes with its own problems).

    Opposed to that an event from a system timer will /allways/ be the set time apart [1], regardless of how much time the fired-up code takes.

    [1] even though the to-be-fired-up code may be delayed due to another
    processes timeslice still running.

    Ofcourse, this is also a timers drawback : if the fired-up code takes more
    than the set time the next iteration of the fired-up code will start to run when the last one has not yet finished. And that can give .... interresting results. :-)

    Bottom line:
    Using Sleep()s in a loop works good enough when the timing isn't all that important.

    Regards,
    Rudy Wieser
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Ahem A Rivet's Shot@3:770/3 to R.Wieser on Fri Nov 5 10:10:18 2021
    On Fri, 5 Nov 2021 10:29:09 +0100
    "R.Wieser" <address@not.available> wrote:

    The distance between two code executions seperated by sleep()s is
    whatever time you set sleep() to *plus* the time your code takes to do
    its thing

    Unless the process gets a signal and doesn't ignore it, in which
    case the sleep returns as soon as the signal handler is done. Sleep can
    return early or late, it will never return on time.

    --
    Steve O'Hara-Smith
    Odds and Ends at http://www.sohara.org/
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From A. Dumas@3:770/3 to The Natural Philosopher on Fri Nov 5 12:06:45 2021
    On 05-11-2021 11:48, The Natural Philosopher wrote:
    On 05/11/2021 10:26, A. Dumas wrote:
    Sure. I guess that is why they asked about timer interrupts. But can
    you give an example of how to use timer interrupts (hardware or
    otherwise) using C on Raspberry Pi OS? Easy enough on a
    microcontroller, but ...

    As I said already, you need to set a signal and let the kernel return a signal to your process after a timeout. You cannot access interrupts
    directly from user space in Linux...

    Yes, but I wanted to hear Rudy say that. Also, it's still not trivial,
    is it? It has been too long since I did it, or tried it.
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From A. Dumas@3:770/3 to R.Wieser on Fri Nov 5 11:26:01 2021
    On 05-11-2021 10:29, R.Wieser wrote:
    A word of warning :

    Most of the people here have adviced to use sleep(). There is something you should be aware of when you chose to use that method :

    The distance between two code executions seperated by sleep()s is whatever time you set sleep() to *plus* the time your code takes to do its thing
    (and before you ask : trying to recalculate the sleep time each step to account for the code-execution delay comes with its own problems).

    Opposed to that an event from a system timer will /allways/ be the set time apart [1], regardless of how much time the fired-up code takes.

    [1] even though the to-be-fired-up code may be delayed due to another processes timeslice still running.

    Ofcourse, this is also a timers drawback : if the fired-up code takes more than the set time the next iteration of the fired-up code will start to run when the last one has not yet finished. And that can give .... interresting results. :-)

    Bottom line:
    Using Sleep()s in a loop works good enough when the timing isn't all that important.

    Sure. I guess that is why they asked about timer interrupts. But can you
    give an example of how to use timer interrupts (hardware or otherwise)
    using C on Raspberry Pi OS? Easy enough on a microcontroller, but ...
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From The Natural Philosopher@3:770/3 to A. Dumas on Fri Nov 5 10:48:45 2021
    On 05/11/2021 10:26, A. Dumas wrote:
    On 05-11-2021 10:29, R.Wieser wrote:
    A word of warning :

    Most of the people here have adviced to use sleep().  There is
    something you
    should be aware of when you chose to use that method :

    The distance between two code executions seperated by sleep()s is
    whatever
    time you set sleep() to *plus* the time your code takes to do its thing
    (and before you ask : trying to recalculate the sleep time each step to
    account for the code-execution delay comes with its own problems).

    Opposed to that an event from a system timer will /allways/ be the set
    time
    apart [1], regardless of how much time the fired-up code takes.

    [1] even though the to-be-fired-up code may be delayed due to another
    processes timeslice still running.

    Ofcourse, this is also a timers drawback : if the fired-up code takes
    more
    than the set time the next iteration of the fired-up code will start
    to run
    when the last one has not yet finished.  And that can give ....
    interresting
    results. :-)

    Bottom line:
    Using Sleep()s in a loop works good enough when the timing isn't all that
    important.

    Sure. I guess that is why they asked about timer interrupts. But can you
    give an example of how to use timer interrupts (hardware or otherwise)
    using C on Raspberry Pi OS? Easy enough on a microcontroller, but ...

    As I said already, you need to set a signal and let the kernel return a
    signal to your process after a timeout. You cannot access interrupts
    directly from user space in Linux...



    --
    In todays liberal progressive conflict-free education system, everyone
    gets full Marx.
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Martin Gregorie@3:770/3 to A. Dumas on Fri Nov 5 11:53:35 2021
    On Fri, 5 Nov 2021 12:06:45 +0100, A. Dumas wrote:


    Yes, but I wanted to hear Rudy say that. Also, it's still not trivial,
    is it? It has been too long since I did it, or tried it.

    That's sort-of why I mentioned using poll(), which is often convenient in
    cases where you want a program to sleep most of the time between doing
    periodic checks, etc but still to service external events immediately.

    Case in point was a system, written in C, I designed that accepted very
    large quantities of data in the form of device activity logs, transformed
    them into a common format and loaded them into a data warehouse after manipulating and/or compressing the content of some fields. In the
    interest of speed, logs were converted into a common format as they
    arrived. Output from this process was left in memory under the ownership
    of a log manager process, which owned all logs currently in memory and scheduled their processing. Each log stayed in memory until all field-
    level processing was complete - the last process in the series loaded to
    log into the data warehouse and then told the log manager to delete it.

    The aims of this approach were to:
    - minimise time taken by reading, writing or moving activity logs around
    - keep individual processes simple by avoiding multi threading
    - allow reuse of some of the field transforming processes by
    parameterising them
    - allow bottlenecks to be eliminated by running multiple copies of
    any slow process.

    As a result, all these processes were based around poll() with the timeout value set non-zero: on timeout they'd look for more work and sleep if
    there was none: i.e the initial log load&transform process looked for
    files on the input disk space while everything else asked the log manager
    for a new logs to work on. That all worked as intended and, last but not
    least, all processes also included stdin as a file to be monitored by
    poll() so overall system control and monitoring could be handled by
    sending messages to stdin on any or all processes.


    --
    --
    Martin | martin at
    Gregorie | gregorie dot org
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Martin Gregorie@3:770/3 to The Natural Philosopher on Fri Nov 5 12:08:47 2021
    On Fri, 5 Nov 2021 11:20:38 +0000, The Natural Philosopher wrote:

    In essence signals are the asynchronous message from kernel to user
    space I guess. Not hardware interrupts!

    Yes. Leave as much direct hardware interaction as possible where it
    belongs - being handled by the kernel.


    --
    --
    Martin | martin at
    Gregorie | gregorie dot org
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From The Natural Philosopher@3:770/3 to A. Dumas on Fri Nov 5 11:20:38 2021
    On 05/11/2021 11:06, A. Dumas wrote:
    On 05-11-2021 11:48, The Natural Philosopher wrote:
    On 05/11/2021 10:26, A. Dumas wrote:
    Sure. I guess that is why they asked about timer interrupts. But can
    you give an example of how to use timer interrupts (hardware or
    otherwise) using C on Raspberry Pi OS? Easy enough on a
    microcontroller, but ...

    As I said already, you need to set a signal and let the kernel return
    a signal to your process after a timeout. You cannot access interrupts
    directly from user space in Linux...

    Yes, but I wanted to hear Rudy say that. Also, it's still not trivial,
    is it? It has been too long since I did it, or tried it.

    Well handling a timer interrupt in a multitasking environment ain't
    trivial either.

    In essence I think - and I haven't time to try it - its a matter of:

    - setting a timer to return a signal after a certain time. (Not sure the
    exact syntax)
    - suspending the thread to wait for the signal. ( use pause() or
    sigwait() )

    In essence signals are the asynchronous message from kernel to user
    space I guess. Not hardware interrupts!

    https://stackoverflow.com/questions/49860625/send-signal-at-specific-time-in-linux-c

    seems to be a good starting point



    --
    Labour - a bunch of rich people convincing poor people to vote for rich
    people by telling poor people that "other" rich people are the reason
    they are poor.

    Peter Thompson
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Charlie Gibbs@3:770/3 to R.Wieser on Fri Nov 5 17:40:06 2021
    On 2021-11-05, R.Wieser <address@not.available> wrote:

    Ofcourse, this is also a timers drawback : if the fired-up code takes more than the set time the next iteration of the fired-up code will start to run when the last one has not yet finished. And that can give .... interresting results. :-)

    That's why the fired-up code sets a global flag when it begins, and
    resets it when it ends. But before setting the flag it checks whether
    the flag is already set and exits immediately if so (and optionally
    sets a notification that the system is just too damn slow).

    --
    /~\ Charlie Gibbs | Life is perverse.
    \ / <cgibbs@kltpzyxm.invalid> | It can be beautiful -
    X I'm really at ac.dekanfrus | but it won't.
    / \ if you read it the right way. | -- Lily Tomlin
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From R.Wieser@3:770/3 to All on Fri Nov 5 19:55:26 2021
    Alexandre,

    Sure. I guess that is why they asked about timer interrupts.

    Looking at the rather terse question the OP posted I'm not willing to make
    such a guess myself.

    Too many possibilities all culminating into the OPs post. Some of them focussed on just toggeling that pin, others on the actual usage of timer interrupts.

    But can you give an example of how to use timer interrupts (hardware or otherwise) using C on Raspberry Pi OS?

    The real question is : why can't *you* ?

    Besides, how does me being able to give an example or not change anything to the info I provided to the OP ?

    Also, it looks like Knute Johnson already posted an example.

    Regards,
    Rudy Wieser
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From R.Wieser@3:770/3 to All on Fri Nov 5 19:59:52 2021
    Ahem,

    Unless the process gets a signal and doesn't ignore it, in which
    case the sleep returns as soon as the signal handler is done.

    Yeah, I saw you mentioning that. Another reason why sleep() might not be
    the best solution.

    Regards,
    Rudy Wieser
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From R.Wieser@3:770/3 to All on Fri Nov 5 20:12:00 2021
    Charlie,

    That's why the fired-up code sets a global flag when it begins,
    and resets it when it ends.

    :-) Such a basic solution is ofcourse easy enough. But the OP would first
    need to know about the possibility of the problem - which is all I wanted to do.

    Regards,
    Rudy Wieser
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Charlie Gibbs@3:770/3 to R.Wieser on Fri Nov 5 23:51:59 2021
    On 2021-11-05, R.Wieser <address@not.available> wrote:

    Charlie,

    That's why the fired-up code sets a global flag when it begins,
    and resets it when it ends.

    :-) Such a basic solution is ofcourse easy enough. But the OP would first
    need to know about the possibility of the problem - which is all I wanted to do.

    After enough experience you become pessimistic enough to spend your time thinking up ways that things can go wrong, and workarounds for them. :-)

    --
    /~\ Charlie Gibbs | Life is perverse.
    \ / <cgibbs@kltpzyxm.invalid> | It can be beautiful -
    X I'm really at ac.dekanfrus | but it won't.
    / \ if you read it the right way. | -- Lily Tomlin
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From druck@3:770/3 to R.Wieser on Sun Nov 7 19:20:39 2021
    On 05/11/2021 09:29, R.Wieser wrote:
    Most of the people here have adviced to use sleep(). There is something you should be aware of when you chose to use that method :

    The distance between two code executions seperated by sleep()s is whatever time you set sleep() to *plus* the time your code takes to do its thing
    (and before you ask : trying to recalculate the sleep time each step to account for the code-execution delay comes with its own problems).

    Opposed to that an event from a system timer will /allways/ be the set time apart [1], regardless of how much time the fired-up code takes.

    [Snip]

    Bottom line:
    Using Sleep()s in a loop works good enough when the timing isn't all that important.

    Exactly, for a noddy Python routine which reads the CPU once per second,
    and isn't particularly bothered its bang on the second, but would like
    it not to drift as would happen just by using time.sleep(1), I use:-

    While True:
    """Read CPU temp""""
    """Store in circular buffer"""
    # sleep for the remainder of the current second
    time.sleep(1 - (time.time() % 1))

    The circular buffer is used to provide the average CPU temperature over
    periods from 5 to 15 minutes. So even if the worst happened, and sleep
    took more than a second on an occasion, it wouldn't make much difference
    to the average.

    ---druck
    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From zeneca@3:770/3 to All on Thu Nov 11 15:56:01 2021
    Le 04/11/2021 à 10:03, zeneca a écrit :
    Hello,

    I am looking for information and example on programing a led that's is
    on / off every second, based on a 'timer' interrupt.
    So far I found very few information on timers.

    By the way I am programing in C.
    This is how I did it:
    Many thanks to all

    #include <stdio.h>
    #include <stdio.h>
    #include <sys/time.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <bcm2835.h>
    #include <wiringPi.h>
    #include <signal.h>
    #include <time.h>


    // Which GPIO pin we're using
    #define PIN_LED 0
    #define PIN_LED1 2
    #define PIN_BUTTON 3

    #define CLOCKID CLOCK_REALTIME
    #define SIG SIGRTMIN


    // How much time a change must be since the last in order to count as a
    change
    #define IGNORE_CHANGE_BELOW_USEC 10000

    // Current state of the pin
    static volatile int state;
    // Time of last change
    int debug;
    int fd;
    int ret;
    static int led;
    /** timer handler **/
    static void handlerT(int sig, siginfo_t *si, void *uc)
    {
    digitalWrite(PIN_LED1,led);
    led ^= 1;
    }

    ************************************/
    // Handler for interrupt
    void handle(void) {

    usleep(1000);


    state = digitalRead(PIN_BUTTON);

    if (!state) {
    if(debug) printf("Falling\n");
    digitalWrite(PIN_LED, HIGH);
    }
    else {
    if(debug) printf("Rising\n");
    digitalWrite(PIN_LED, LOW);
    }


    }


    int main(void) {

    fd = 0x00;
    debug = isatty(fd);

    wiringPiSetup();

    // Set pin to output in case it's not
    pinMode(PIN_BUTTON, INPUT);
    pinMode(PIN_LED, OUTPUT);
    pinMode(PIN_LED1, OUTPUT);

    digitalWrite(PIN_LED, LOW);
    digitalWrite(PIN_LED1, LOW);


    // Bind to interrupt
    wiringPiISR(PIN_BUTTON, INT_EDGE_BOTH, &handle);

    if(debug)printf("Creating timer\n");
    timer_t timerid;
    struct sigevent sev;
    sev.sigev_notify = SIGEV_SIGNAL;
    sev.sigev_signo = SIG;
    sev.sigev_value.sival_ptr = &timerid;
    ret = timer_create(CLOCKID, &sev, &timerid);

    if(ret) perror ("Timer create\n");

    struct sigaction sa;
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = handlerT;
    sigemptyset(&sa.sa_mask);
    if (sigaction(SIG, &sa, NULL) == -1)
    perror("sigaction");

    struct itimerspec its;
    its.it_value.tv_sec = 1;
    its.it_interval.tv_sec = 1;

    if(debug)printf("settime\n");
    if (timer_settime(timerid, 0, &its, NULL) == -1)
    perror("timer_settime");

    int sleeptime = 5000000;
    // Waste time but not CPU
    for (;;) sleep(sleeptime);
    }

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)