NNP STM Generic Remote Module git-main
Loading...
Searching...
No Matches
timer.c
Go to the documentation of this file.
1
13#include <applicfg.h>
14#include "timer.h"
15#include "ObjDict.h"
16#include "main.h"
17
18
19/* --------- The timer table --------- */
20s_timer_entry timers[MAX_NB_TIMER] = {{TIMER_FREE, NULL, NULL, 0, 0, 0},};
21
22TIMEVAL total_sleep_time = TIMEVAL_MAX;
23TIMER_HANDLE last_timer_raw = -1;
24
25#define min_val(a,b) ((a<b)?a:b)
26
37TIMER_HANDLE SetAlarm(CO_Data* d, UNS32 id, TimerCallback_t callback, TIMEVAL value, TIMEVAL period)
38{
39 TIMER_HANDLE row_number;
40 s_timer_entry *row;
41
42
43 /* in order to decide new timer setting we have to run over all timer rows */
44 for(row_number=0, row=timers; row_number <= last_timer_raw + 1 && row_number < MAX_NB_TIMER; row_number++, row++)
45 {
46
47 if (callback && /* if something to store */
48 row->state == TIMER_FREE) /* and empty row */
49 { /* just store */
50 TIMEVAL real_timer_value;
51 TIMEVAL elapsed_time;
52
53 if (row_number == last_timer_raw + 1) last_timer_raw++;
54
55 elapsed_time = getElapsedTime();
56 /* set next wakeup alarm if new entry is sooner than others, or if it is alone */
57 real_timer_value = value;
58 real_timer_value = min_val(real_timer_value, TIMEVAL_MAX);
59
60 if (total_sleep_time > elapsed_time && total_sleep_time - elapsed_time > real_timer_value)
61 {
62 total_sleep_time = elapsed_time + real_timer_value;
63 setTimer(real_timer_value);
64 }
65 row->callback = callback;
66 row->d = d;
67 row->id = id;
68 row->val = value + elapsed_time;
69 row->interval = period;
70 row->state = TIMER_ARMED;
71 return row_number;
72 }
73 }
74 return TIMER_NONE;
75}
76
83TIMER_HANDLE DelAlarm(TIMER_HANDLE handle)
84{
85 /* Quick and dirty. system timer will continue to be trigged, but no action will be preformed. */
86 MSG_WAR(0x3320, "DelAlarm. handle = ", handle);
87 if(handle != TIMER_NONE)
88 {
89 if(handle == last_timer_raw)
90 last_timer_raw--;
91 timers[handle].state = TIMER_FREE;
92 }
93 return TIMER_NONE;
94}
95
96
97int tdcount=0;
102void TimeDispatch(void)
103{
104 TIMER_HANDLE i;
105 TIMEVAL next_wakeup = TIMEVAL_MAX; /* used to compute when should normaly occur next wakeup */
106 //TIMEVAL next_wakeup = 0x8000; /* used to compute when should normaly occur next wakeup */
107 /* First run : change timer state depending on time */
108 /* Get time since timer signal */
109
110 UNS32 overrun = (UNS32)getElapsedTime();
111
112 TIMEVAL real_total_sleep_time = total_sleep_time + overrun;
113
114 s_timer_entry *row;
115
116 for(i=0, row = timers; i <= last_timer_raw; i++, row++)
117 {
118 if (row->state & TIMER_ARMED) /* if row is active */
119 {
120 if (row->val <= real_total_sleep_time) /* to be trigged */
121 {
122 if (!row->interval) /* if simply outdated */
123 {
124 row->state = TIMER_TRIG; /* ask for trig */
125 }
126 else /* or period have expired */
127 {
128 /* set val as interval, with overrun correction */
129 /* modulo for 64 bit not available on all platforms */
130 row->val = row->interval - (overrun % (UNS32)row->interval); //JML
131 row->state = TIMER_TRIG_PERIOD; /* ask for trig, periodic */
132 /* Check if this new timer value is the soonest */
133 if(row->val < next_wakeup)
134 next_wakeup = row->val;
135 }
136 }
137 else
138 {
139 /* Each armed timer value in decremented. */
140 row->val -= real_total_sleep_time;
141
142 /* Check if this new timer value is the soonest */
143 if(row->val < next_wakeup)
144 next_wakeup = row->val;
145 }
146 }
147 }
148
149 /* Remember how much time we should sleep. */
150 total_sleep_time = next_wakeup;
151
152 /* Set timer to soonest occurence */
153 setTimer(next_wakeup);
154
155 /* Then trig them or not. */
156 for(i=0, row = timers; i<=last_timer_raw; i++, row++)
157 {
158 if (row->state & TIMER_TRIG)
159 {
160 row->state &= ~TIMER_TRIG; /* reset trig state (will be free if not periodic) */
161 if(row->callback)
162 {
163 (*row->callback)(row->d, row->id); /* trig ! */
164 }
165 }
166 }
167
168}
This file is generated by the NNP Tool – Object Dictionary Editor, as originally developed by CAN Fes...
#define MSG_WAR(num, str, val)
Definition of MSG_WAR.
Definition applicfg.h:64
#define UNS32
Unsigned int32 representation in CANFest.
Definition applicfg.h:27
TIMER_HANDLE SetAlarm(CO_Data *d, UNS32 id, TimerCallback_t callback, TIMEVAL value, TIMEVAL period)
Set an alarm to execute a callback function when expired.
Definition timer.c:37
TIMER_HANDLE DelAlarm(TIMER_HANDLE handle)
Delete an alarm before expiring.
Definition timer.c:83
void TimeDispatch(void)
TimeDispatch is called on each timer expiration -—.
Definition timer.c:102
: Header for main.c file. This file contains the common defines of the application.
This structure contains all necessary informations to define a CANOpen node.
Definition data.h:44
timer struct
Definition timer.h:33
TimerCallback_t callback
Definition timer.h:36
TIMEVAL interval
Definition timer.h:39
UNS32 id
Definition timer.h:37
TIMEVAL val
Definition timer.h:38
TIMEVAL getElapsedTime(void)
Return the elapsed time to tell the Stack how much time is spent since last call.
Definition timer_stm.c:65
void setTimer(TIMEVAL value)
Set the timer peripheral for the next interrupt.
Definition timer_stm.c:23