fremoclock  1_3A01
FREMO Clock - Software for UTG
Timer.c
Go to the documentation of this file.
1 
18 #include <stdint.h> // typedef int8_t, typedef uint16_t, typedef uint32_t
19  // #define UINT32_MAX
20 #include <stddef.h> // NULL
21 #include "SysHwTimer.h" // Hardware timer
22 #include "Timer.h" // Prototypes of this module
23 
25 static const uint32_t MAX_TIMEDIFF = ((UINT32_MAX / 2) - 1);
26 
27 // for comment see Timer.h
28 int8_t CheckTimer(uint32_t *pulTimer)
29 {
30  uint32_t ulHwTimer;
31 
32  // Timer is inactive:
33  if (*pulTimer == 0) return(-1);
34 
35  // Get hardware driven timer count:
36  ulHwTimer = HwTimerGetValue();
37 
38  if (*pulTimer > ulHwTimer)
39  {
40  if ((*pulTimer - ulHwTimer) < MAX_TIMEDIFF)
41  {
42  // Timer busy:
43  return(1);
44  }
45  else
46  {
47  // Timer elapsed:
48  *pulTimer = 0;
49  return(0);
50  }
51  }
52  else
53  {
54  if ((ulHwTimer - *pulTimer) >= MAX_TIMEDIFF)
55  {
56  // Timer busy:
57  return(1);
58  }
59  else
60  {
61  // Timer elapsed:
62  *pulTimer = 0;
63  return(0);
64  }
65  }
66 }
67 
68 // for comment see Timer.h
69 void SetTimer(uint32_t *pulTimer, uint32_t ulValue)
70 {
71  // Convert ulValue from ms to timer tics, round up:
72  if (ulValue) ulValue--;
73 
74  ulValue = (ulValue / TIMER_RESOLUTION_MS) + 1;
75 
76  if (ulValue >= MAX_TIMEDIFF) ulValue = MAX_TIMEDIFF - 1; // sb 2001-9-12
77 
78  if (ulValue > 1000)
79  {
80  ulValue++;
81  }
82 
83  // Get hardware driven timer value:
84  *pulTimer = HwTimerGetValue();
85 
86  // Add value (overflow may occure):
87  *pulTimer += ulValue;
88 
89  // Timer must be greater than 0, since 0 is the reset value for the timer:
90  if (*pulTimer == 0) (*pulTimer)++;
91 }
92 
93 // for comment see Timer.h
94 int8_t AddTimer(uint32_t *pulTimer, uint32_t ulValue)
95 {
96  // Check if the timer was initialized
97  if ((pulTimer == NULL) || (*pulTimer == 0)) return -1;
98 
99  // Convert ulValue from ms to timer tics:
100  ulValue = ((ulValue - (TIMER_RESOLUTION_MS / 2)) / TIMER_RESOLUTION_MS) + 1;
101 
102  // This is not really a sufficiant check...
103  if (ulValue >= (MAX_TIMEDIFF / 2)) return -1;
104 
105  // Add value (overflow may occure):
106  *pulTimer += ulValue;
107 
108  // Timer must be greater than 0, since 0 is the reset value for the timer:
109  if (*pulTimer == 0) (*pulTimer)++;
110 
111  return 0;
112 }
113 
114 // for comment see Timer.h
115 void ResetTimer(uint32_t *pulTimer)
116 {
117  *pulTimer = 0;
118 }
119 
120 // for comment see Timer.h
121 void WaitTimer(uint16_t usValue)
122 {
123  uint32_t ulTimer;
124 
125  SetTimer(&ulTimer, (uint32_t) usValue);
126 
127  while (CheckTimer(&ulTimer) != 0);
128 }
void SetTimer(uint32_t *pulTimer, uint32_t ulValue)
Set a timer.
Definition: Timer.c:69
int8_t CheckTimer(uint32_t *pulTimer)
Check if timer is elapsed, incactive or still busy.
Definition: Timer.c:28
static const uint32_t MAX_TIMEDIFF
Maximum time difference.
Definition: Timer.c:25
uint32_t HwTimerGetValue(void)
Get the value of globalTimer variable guarded by disable/enable interrupts.
Definition: SysHwTimer.c:210
void WaitTimer(uint16_t usValue)
Wait for usValue milliseconds.
Definition: Timer.c:121
int8_t AddTimer(uint32_t *pulTimer, uint32_t ulValue)
Add time period to an already initialized timer.
Definition: Timer.c:94
#define TIMER_RESOLUTION_MS
Resolution of this timer in milliseconds.
Definition: SysHwTimer.h:49
A timer module.
Hardware dependent timer module for Atmel AVR.
void ResetTimer(uint32_t *pulTimer)
Reset a timer.
Definition: Timer.c:115