I have the following arduino code:
uint32_t hold_time=600000;
uint32_t curr_time;
uint32_t last_event;
bool on_hold=false;
beginning of main loop
curr_time = millis();
if (on_hold && curr_time - last_event >= hold_time) {
on_hold = false;
}
...
if (!on_hold)
{
run the function();
on_hold = true; // Ignore this function for 1 minute
}
This basically will execute the main loop many times but the run_the_function(); only when it is unlocked so in this example once in every minute. I would like to accomplish the same in standard POSIX C which works on BSDs as well.
Since you asked for POSIX, I will give you POSIX. This is a sample code that is able to run a timer without using pthreads, but only through OS provided timers. It runs a specific function every 2 seconds. You can configure it to make it run every 60 seconds, if you prefer. I have comment thoroughly the code, and I hope it is easy enough to understand:
#include <stdlib.h> // For declaration of exit
#include <stdio.h> // For printf function
#include <signal.h> // Will be used for the signal callbacks
#include <time.h> // Timer and current time stuff
#define TIMER_SECONDS 2 // To test more rapidly I will wait
// only for 2 seconds instead of a minute...
int counter = 0; // Whe want to call the timer a limited number
// of time for this example
// BEFORE READING THIS, READ THE MAIN:
// This function is your "run_the_function" callback. As for now,
// it has no arguments and returns nothing. It asks to the system the current
// time and prints it, just to check if the timer works. It uses **printf**
// and this should be avoided in signal handlers!
static void run_the_function() {
time_t rawtime; // This is the current time variable
struct tm * timeinfo; // This is a strut that contains the time
// broken down to its components
time( &rawtime ); // To get from the system the current time
timeinfo = localtime ( &rawtime ); // For splitting the time in its components
printf("Timer CALLED %d times :: %s", ++counter, asctime(timeinfo));
}
// BEFORE READING THIS, READ THE MAIN
// This is the signal handler, a function that is called when the timer
// signals that has finished to count
static void timer_callback(int sig, siginfo_t *si, void *uc) {
run_the_function();
}
int main() {
timer_t timer_id; // An unique identifier for the timer that you are creating
struct itimerspec intervals; // Specify the intervals for the timer that we are creating
struct sigevent timer_event; // The structure that handles the event generated by the timer
struct sigaction timer_action; // The action for the timer event
// First you need to implement the action to do when the timer reaches zero,
// then you need to say that you want an event for a timer that reaches zero,
// and only at the end you set the timer.
// The function "sigaction" connects your timer event to the timer signal SIGRTMIN.
// The timer_event.sigev_signo instructs to create an EVENT for the signal SIGRTMIN, and
// for that event you prepared a custom action.
// The timer sends the SIGRTMIN signal every time it reaches zero, and when you
// create it, you connect it to the timer_event.
// Now we define what is the action to perform
timer_action.sa_flags = SA_SIGINFO; // The action to perform is to run a callback
timer_action.sa_sigaction = timer_callback; // The callback is "timer_callback"
sigemptyset(&timer_action.sa_mask); // And we are initializing the event structure
if (sigaction(SIGRTMIN, &timer_action, NULL) < 0) // We are binding this action
exit(1); // to a timer event (SIGRTMIN)
timer_event.sigev_notify = SIGEV_SIGNAL; // Instruct the event that it is related to
// a signal.
timer_event.sigev_signo = SIGRTMIN; // Instruct the event that the signal to track is SIGRTMIN
// At this point we are ready to create the timer, that uses the REAL TIME CLOCK of your
// system. When it reaches zero it raise a timer_event, and it also sets the id of the
// created timer.
if (timer_create(CLOCK_REALTIME, &timer_event, &timer_id) < 0)
exit(1);
// We now need to define the times for the timer. Intervals is composed by
// two structure: it_value, that contains the current time (or the starting time
// for the first run of your timer) and it_intervals, the time at which it will be
// reset after completing one lap. If you set it_interval to zero, the timer runs only
// one time. If you set it_value to zero, the timer does not run.
intervals.it_value.tv_sec = TIMER_SECONDS;
intervals.it_value.tv_nsec = 0;
intervals.it_interval.tv_sec = TIMER_SECONDS;
intervals.it_interval.tv_nsec = 0;
// Let's setup the time and the interval of the timer, so that it starts...
if (timer_settime(timer_id, 0, &intervals, NULL) < 0)
exit(1);
// And now we have only to wait for the timer... Easy, isn't it?
printf("Let's go!\n");
while(counter < 5) { /* Do your stuff here*/ };
return 0;
}
You need to compile it with:
gcc test.c -lrt -o test
and run it with:
./test
Let's go!
Timer CALLED 1 times :: Thu May 3 15:48:29 2018
Timer CALLED 2 times :: Thu May 3 15:48:31 2018
Timer CALLED 3 times :: Thu May 3 15:48:33 2018
Timer CALLED 4 times :: Thu May 3 15:48:35 2018
Timer CALLED 5 times :: Thu May 3 15:48:37 2018
Related
Currently I'm polling the register to get the expected value and now I want reduce the CPU usage and increase the performance.
So, I think, if we do polling for particular time (Say for 10ms) and if we didn't get expected value then wait for some time (like udelay(10*1000) or usleep(10*1000) delay/sleep in ms) then continue to do polling for more more extra time (Say 100ms) and still if you didn't get the expected value then do sleep/delay for 100ms.....vice versa... need to do till it reach to maximum timeout value.
Please let me know if anything.
This is the old code:
#include <sys/time.h> /* for setitimer */
#include <unistd.h> /* for pause */
#include <signal.h> /* for signal */
#define INTERVAL 500 //timeout in ms
static int timedout = 0;
struct itimerval it_val; /* for setting itimer */
char temp_reg[2];
int main(void)
{
/* Upon SIGALRM, call DoStuff().
* Set interval timer. We want frequency in ms,
* but the setitimer call needs seconds and useconds. */
if (signal(SIGALRM, (void (*)(int)) DoStuff) == SIG_ERR)
{
perror("Unable to catch SIGALRM");
exit(1);
}
it_val.it_value.tv_sec = INTERVAL/1000;
it_val.it_value.tv_usec = (INTERVAL*1000) % 1000000;
it_val.it_interval = it_val.it_value;
if (setitimer(ITIMER_REAL, &it_val, NULL) == -1)
{
perror("error calling setitimer()");
exit(1);
}
do
{
temp_reg[0] = read_reg();
//Read the register here and copy the value into char array (temp_reg
if (timedout == 1 )
return -1;//Timedout
} while (temp_reg[0] != 0 );//Check the value and if not try to read the register again (poll)
}
/*
* DoStuff
*/
void DoStuff(void)
{
timedout = 1;
printf("Timer went off.\n");
}
Now I want to optimize and reduce the CPU usage and want to improve the performance.
Can any one help me on this issue ?
Thanks for your help on this.
Currently I'm polling the register to get the expected value [...]
wow wow wow, hold on a moment here, there is a huge story hidden behind this sentence; what is "the register"? what is "the expected value"? What does read_reg() do? are you polling some external hardware? Well then, it all depends on how your hardware behaves.
There are two possibilities:
Your hardware buffers the values that it produces. This means that the hardware will keep each value available until you read it; it will detect when you have read the value, and then it will provide the next value.
Your hardware does not buffer values. This means that values are being made available in real time, for an unknown length of time each, and they are replaced by new values at a rate that only your hardware knows.
If your hardware is buffering, then you do not need to be afraid that some values might be lost, so there is no need to poll at all: just try reading the next value once and only once, and if it is not what you expect, sleep for a while. Each value will be there when you get around to reading it.
If your hardware is not buffering, then there is no strategy of polling and sleeping that will work for you. Your hardware must provide an interrupt, and you must write an interrupt-handling routine that will read every single new value as quickly as possible from the moment that it has been made available.
Here are some pseudo code that might help:
do
{
// Pseudo code
start_time = get_current_time();
do
{
temp_reg[0] = read_reg();
//Read the register here and copy the value into char array (temp_reg
if (timedout == 1 )
return -1;//Timedout
// Pseudo code
stop_time = get_current_time();
if (stop_time - start_time > some_limit) break;
} while (temp_reg[0] != 0 );
if (temp_reg[0] != 0)
{
usleep(some_time);
start_time = get_current_time();
}
} while (temp_reg[0] != 0 );
To turn the pseudo code into real code, see https://stackoverflow.com/a/2150334/4386427
I'm producing a game in C on a microprocessor. The score is controlled by how long you can survive; the score increases by 1 every 3 seconds. The score is an integer which is declared globally, but displayed from a function.
int score = 0;//globally declared
void draw_score(int score_d)
{
char score_draw[99];
sprintf(score_draw,"%d", score_d);
draw_string(score_draw, 9, 0);
}
I was thinking of a function which just increases the score by one with a delay on it, however that has not worked.
void score_increaser(int score)
{
score++;
_delay_ms( 3000 );
}
Does it need to be in a while loop? the function itself would go into a while loop in the main anyway.
C is pass by value.
score_increaser() as shown in your question increases just a copy of what is passed in.
To fix this there are (mainly) two options:
As score is defined globally, do not pass in anything:
void score_increaser(void) {
score++;
_delay_ms( 3000 );
}
This modifes the globale score directly.
Pass in the address of score and de-reference it inside the function
void score_increaser(int * pscore) {
(*pscore)++;
_delay_ms( 3000 );
}
Call it like this
...
score_increaser(&score);
...
A 3rd, a bit more complex, approach (which assumes signals are supported on the target platform) would
setup a signal and a referring handler, then
setup a timer to fire a signal every N seconds.
This signal then is handled by the handler, which in turn
increases the global score and
starts the timer again.
This might look like:
#include <signal.h> /* for signal() and sig_atomic_t */
#include <unistd.h> /* for alarm() */
#define DURATION (3) /* Increase score every 3 seconds. */
sig_atomic_t score = 0;
void set_alarm(unsigned);
void handler_alarm(int sig)
{
++score;
set_alarm(DURATION);
}
void set_alarm(unsigned duration)
{
signal(SIGALRM, handler_alarm);
alarm(duration);
}
int main(void)
{
set_alarm(DURATION);
... /* The game's codes here. */
}
This latter approach has the advantage that your game's code does not need to take care about increasing score. score is just increased every 3 seconds as long as the program runs.
I'd recommend using a timer interrupt. Configure the timer to 3 seconds.
volatile int score = 0; //global
void Intr_Init(peripheral_t per)
{
//Initialize the timer interrupt
}
void draw_score(int score_d)
{
char score_draw[99];
sprintf(score_draw,"%d", score_d);
draw_string(score_draw, 9, 0);
}
int main(void)
{
Intr_Init(TIMER);
while(1)
{
//Code that makes your game run
draw_score(score);
}
}
ISR (TIMER1_COMPA_vect)
{
//clear disable interrupt
score++;
//enable interrupt
}
In embedded, you should rely on Timers for better time critical tasks and accuracy. The way Delay routines are implemented is usually a loop or a up/down counter. Whereas a timer is usually based on counting SysTicks.
Another major advantage of Interrupts is that you let processor do its tasks all the while instead of making it block in a delay loop.
score is global value then do not need to pass it in function if that function has access to that global space
void score_increaser() {
score++;
_delay_ms( 3000 );
}
here is a good method for handling the score.
in the 'start game' function,
clear 'score' to 0
setup a timer:
--to expire once each 3 seconds
--enable the automatic reload feature,
--enable the timer interrupt
--enable the timer counter
in the timer interrupt handler function
--increment 'score'
--clear the timer interrupt pending flag
in the 'end game' function
disable the timer counter
disable the timer interrupt
display the 'score' value
You dont need parameter for the score since it's declared globally..
//global
int score = 0;
void score_increaser()
{
_delay_ms(3000);
score++;
}
calling is like: score_increaser(); should do the work..
i suggest you check for score in any other line/function.. maybe you have redeclared it or accidentally changed the value..
hope this helped..
I need to be able to start multiple timers simultaneously and know specifically if a timer has stopped or is still going.
#define RESEND_TIMEOUT 5
void timerCreate();
void timer_start(timer_t * timer, uint32 timeout);
bool timer_complete(timer_t * timer);
int main() {
timer_t resend_timer = timerCreate();
timer_start(&resend_timer, RESEND_TIMEOUT);
while(1) {
if (timer_complete(&resend_timer))
break;
}
}
void timer_start(timer_t * timer, uint32_t timeout)
{
printf("timer starting\n");
struct itimerspec it_val;
it_val.it_value.tv_sec = timeout;
it_val.it_value.tv_nsec = 0;
// timer expires once
it_val.it_interval.tv_sec = 0;
it_val.it_interval.tv_nsec = 0;
if (timer_settime(*timer, 0, &it_val, NULL) == -1) {
errExit("Could not set timeout");
}
}
// return true if timer ended
bool timer_complete(timer_t * timer)
{
if(timer_getoverrun(*timer) == 0)
return false;
else
return true;
}
I never break out of the loop. Why can't I get the overrun of the timer (it always returns 0, which means the timer has not passed its expiration)? Yet when I add a signal handler, I know that the timer expires.
I want to try timer_gettime(timer_t timerid, struct itimerspec *curr_value) inside of my timer_complete function to see if the remaining time is 0, but how can I pass the curr_value argument without having a global variable?
Last but not least, I have tried with the TIMER_ABSTIME flag when arming the timer with timer_settime. From the manpage of int timer_settime(timer_t timerid, int flags,
const struct itimerspec *new_value,
struct itimerspec * old_value):
By default, the initial expiration time specified in
new_value->it_value is interpreted relative to the current time on
the timer's clock at the time of the call. This can be modified by
specifying TIMER_ABSTIME in flags, in which case new_value->it_value
is interpreted as an absolute value as measured on the timer's clock;
that is, the timer will expire when the clock value reaches the value
specified by new_value->it_value. If the specified absolute time has
already passed, then the timer expires immediately, and the overrun
count (see timer_getoverrun(2)) will be set correctly.
I never break out of the loop. Why can't I get the overrun of the timer (it always returns 0, which means the timer has not passed its expiration)?
No, it means you had no overruns.
The OS is not going to queue timer signals even if you specify realtime signals. Overun tells you how many signals would have been queued if the wood chuck didn't chuck signals.
So consider you set a timer to go off once every second. But for some reason you didn't handle the signal. Say you had it blocked for 5 seconds. The overrun count is going to be 4 - the signal you will/are processing and the 4 you missed.
In your case you set a one-time timer to go off after "timeout" seconds. The signal was delivered. There will be no more signals hence overrun is always going to be 0, as it should be.
I was using a periodic timer and taking times between when two SIGALRM signals are received. what I observed was that itimer might expires a little before or little after the time I set. e.g. if I set it for 1m sec , it might expires at 0.9998msec or 1.0023msec.
Shouldn't the timer expiration would always be greater than what is set? less time taken is what I dont understand.
here's my code:
enter code here
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include <time.h>
#define INTERVAL 1000
struct timespec ti[100];
int s=0;
void ex(int i)
{int d=0;
struct timespec t[100],s1,s2;
for(d=0;d<99;d++)
{
s1= ti[d];
s2= ti[d+1];
printf("%u:%u\t%u:%u\t", s1.tv_sec, s1.tv_nsec, s2.tv_sec, s2.tv_nsec);
if ((s2.tv_nsec- s1.tv_nsec)<0) {
t[d].tv_sec = s2.tv_sec-s1.tv_sec-1;
t[d].tv_nsec = 1000000000 +s2.tv_nsec -s1.tv_nsec;
} else {
t[d].tv_sec = s2.tv_sec-s1.tv_sec;
t[d].tv_nsec = s2.tv_nsec-s1.tv_nsec;
}
printf("%u:%u\n",t[d].tv_sec,t[d].tv_nsec);
}
exit(0);
}
void alarm_wakeup (int i)
{
clock_gettime(CLOCK_MONOTONIC, &ti[s]);
s++;
if(s==100)
{ ex(0);
}
}
void main ()
{
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = INTERVAL;
tout_val.it_value.tv_sec = 0;
tout_val.it_value.tv_usec = INTERVAL;
setitimer(ITIMER_REAL, &tout_val,0);
signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */
signal(SIGINT,ex);
while (1)
{
}
}
When the timer expires, the signal is raised and the timer is rescheduled.
However, there can be a delay between the signal being raised and the signal being handled - if the process isn't running already, it has to be rescheduled. This means that there is a potentially variable delay between the actual expiration of the timer and when the clock_gettime() call in your signal handler runs.
If this delay before the clock_gettime() call is higher one iteration than the next, then the time between the clock_gettime() calls will be slightly less than 1ms even though there was a 1ms gap between the subsequent timer expiries.
In diagrammatic form:
time: 0ms...............1ms...............2ms...............3ms
timer expiry: X X X X
signal handler runs: S S S S
You can see that the longer delay before the second signal handler ran made the third signal appear to be "early", even though the underlying timer was not.
gcc (GCC) 4.6.0 20110419 (Red Hat 4.6.0-5)
I am trying to get the time of start and end time. And get the difference between them.
The function I have is for creating a API for our existing hardware.
The API wait_events take one argument that is time in milli-seconds. So what I am trying to get the start before the while loop. And using time to get the number of seconds. Then after 1 iteration of the loop get the time difference and then compare that difference with the time out.
Many thanks for any suggestions,
/* Wait for an event up to a specified time out.
* If an event occurs before the time out return 0
* If an event timeouts out before an event return -1 */
int wait_events(int timeout_ms)
{
time_t start = 0;
time_t end = 0;
double time_diff = 0;
/* convert to seconds */
int timeout = timeout_ms / 100;
/* Get the initial time */
start = time(NULL);
while(TRUE) {
if(open_device_flag == TRUE) {
device_evt.event_id = EVENT_DEV_OPEN;
return TRUE;
}
/* Get the end time after each iteration */
end = time(NULL);
/* Get the difference between times */
time_diff = difftime(start, end);
if(time_diff > timeout) {
/* timed out before getting an event */
return FALSE;
}
}
}
The function that will call will be like this.
int main(void)
{
#define TIMEOUT 500 /* 1/2 sec */
while(TRUE) {
if(wait_events(TIMEOUT) != 0) {
/* Process incoming event */
printf("Event fired\n");
}
else {
printf("Event timed out\n");
}
}
return 0;
}
=============== EDIT with updated results ==================
1) With no sleep -> 99.7% - 100% CPU
2) Setting usleep(10) -> 25% CPU
3) Setting usleep(100) -> 13% CPU
3) Setting usleep(1000) -> 2.6% CPU
4) Setting usleep(10000) -> 0.3 - 0.7% CPU
You're overcomplicating it - simplified:
time_t start = time();
for (;;) {
// try something
if (time() > start + 5) {
printf("5s timeout!\n");
break;
}
}
time_t should in general just be an int or long int depending on your platform counting the number of seconds since January 1st 1970.
Side note:
int timeout = timeout_ms / 1000;
One second consists of 1000 milliseconds.
Edit - another note:
You'll most likely have to ensure that the other thread(s) and/or event handling can happen, so include some kind of thread inactivity (using sleep(), nanosleep() or whatever).
Without calling a Sleep() function this a really bad design : your loop will use 100% of the CPU. Even if you are using threads, your other threads won't have much time to run as this thread will use many CPU cycles.
You should design something like that:
while(true) {
Sleep(100); // lets say you want a precision of 100 ms
// Do the compare time stuff here
}
If you need precision of the timing and are using different threads/processes, use Mutexes (semaphores with a increment/decrement of 1) or Critical Sections to make sure the time compare of your function is not interrupted by another process/thread of your own.
I believe your Red Hat is a System V so you can sync using IPC