src/timer.c
author dejoigny
Wed, 17 May 2006 20:40:17 +0200
changeset 12 1d3b9ad366c8
parent 0 4472ee7c6c3e
child 38 9b5bb1dcb4f5
permissions -rwxr-xr-x
CANOPEN_BIG_ENDIAN :
fonctionne pour SDO et PDO.
SDO segment?s test?s en upload et download. Semble ok.
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     1
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     7
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    12
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    21
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    22
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    23
//#define DEBUG_WAR_CONSOLE_ON
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
//#define DEBUG_ERR_CONSOLE_ON
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
#include <applicfg.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
#include "timer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
// ---------  The timer table ---------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
s_timer_entry timers[MAX_NB_TIMER] = {{TIMER_FREE, NULL, NULL, 0, 0, 0},};
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    31
//
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
TIMEVAL total_sleep_time = TIMEVAL_MAX;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
TIMER_HANDLE last_timer_raw = -1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
#define max(a,b) a>b?a:b
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
#define min(a,b) a<b?a:b
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
// ---------  Use this to declare a new alarm ---------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
TIMER_HANDLE SetAlarm(CO_Data* d, UNS32 id, TimerCallback_t callback, TIMEVAL value, TIMEVAL period)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    40
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
	//printf("SetAlarm(UNS32 id=%d, TimerCallback_t callback=%x, TIMEVAL value=%d, TIMEVAL period=%d)\n", id, callback, value, period);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
	TIMER_HANDLE i;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    43
	TIMER_HANDLE row_number = TIMER_NONE;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    45
	// in order to decide new timer setting we have to run over all timer rows
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
	for(i=0; i <= last_timer_raw + 1 && i < MAX_NB_TIMER; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
		s_timer_entry *row = (timers+i);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
		if (callback && 	// if something to store
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
		   row->state == TIMER_FREE) // and empty row
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
		{	// just store
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
			row->callback = callback;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
			row->d = d;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    55
			row->id = id;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    56
			row->val = value;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    57
			row->interval = period;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
			row->state = TIMER_ARMED;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
			row_number = i;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    61
		}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
	if (row_number != TIMER_NONE) // if successfull
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
		if (row_number == last_timer_raw + 1) last_timer_raw++;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
		
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
		// set next wakeup alarm if new entry is sooner than others, or if it is alone
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
		TIMEVAL real_timer_value = min(value, TIMEVAL_MAX);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
		TIMEVAL elapsed_time = getElapsedTime();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
		//printf("elapsed_time=%d real_timer_value=%d total_sleep_time=%d\n", elapsed_time, real_timer_value, total_sleep_time);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
		if (total_sleep_time > elapsed_time && total_sleep_time - elapsed_time > real_timer_value)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
		{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
			total_sleep_time = elapsed_time + real_timer_value;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
			setTimer(real_timer_value);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
		}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
		//printf("SetAlarm() return %d\n", row_number);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
		return row_number;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
	return TIMER_NONE;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
// ---------  Use this to remove an alarm ---------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
TIMER_HANDLE DelAlarm(TIMER_HANDLE handle)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
	// Quick and dirty. system timer will continue to be trigged, but no action will be preformed.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
	MSG_WAR(0x3320, "DelAlarm. handle = ", handle);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
	if(handle != TIMER_NONE)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
		if(handle == last_timer_raw) 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
			last_timer_raw--;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
		timers[handle].state = TIMER_FREE; 		
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
	else {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
	return TIMER_NONE;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
// ---------  TimeDispatch is called on each timer expiration ---------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
void TimeDispatch()
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
	TIMER_HANDLE i;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
	TIMEVAL next_wakeup = TIMEVAL_MAX; // used to compute when should normaly occur next wakeup
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
	// First run : change timer state depending on time
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
	// Get time since timer signal
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
	TIMEVAL overrun = getElapsedTime();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
	TIMEVAL real_total_sleep_time = total_sleep_time + overrun;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   111
	//printf("total_sleep_time %d + overrun %d\n", total_sleep_time , overrun);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   112
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   113
	for(i=0; i <= last_timer_raw; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   114
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
		s_timer_entry *row = (timers+i);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
		if (row->state & TIMER_ARMED) // if row is active
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
		{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   119
			if (row->val <= real_total_sleep_time) // to be trigged
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
			{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   121
				//printf("row->val(%d) <= (%d)real_total_sleep_time\n", row->val, real_total_sleep_time);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
				if (!row->interval) // if simply outdated
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
				{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
					row->state = TIMER_TRIG; // ask for trig
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
				}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
				else // or period have expired
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
				{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
					// set val as interval, with overrun correction
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
					row->val = row->interval - (overrun % row->interval);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
					row->state = TIMER_TRIG_PERIOD; // ask for trig, periodic
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
					// Check if this new timer value is the soonest
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
					next_wakeup = min(row->val,next_wakeup);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
				}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
			}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
			else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
			{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
				// Each armed timer value in decremented.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
				row->val -= real_total_sleep_time;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   140
				// Check if this new timer value is the soonest
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   141
				next_wakeup = min(row->val,next_wakeup);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   142
			}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   143
		}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   144
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   145
	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   146
	// Remember how much time we should sleep.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   147
	total_sleep_time = next_wakeup;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   148
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   149
	// Set timer to soonest occurence
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   150
	setTimer(next_wakeup);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   151
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   152
	// Then trig them or not.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   153
	for(i=0; i<=last_timer_raw; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   154
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   155
		s_timer_entry *row = (timers+i);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   156
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   157
		if (row->state & TIMER_TRIG)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   158
		{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   159
			row->state &= ~TIMER_TRIG; // reset trig state (will be free if not periodic)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   160
			(*row->callback)(row->d, row->id); // trig !
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   161
		}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   162
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   163
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   164