author | Edouard Tisserant |
Thu, 24 Jan 2019 13:49:40 +0100 | |
changeset 807 | 46027bb24429 |
parent 801 | 32d146b64a35 |
permissions | -rw-r--r-- |
375 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
AVR Port: Andreas GLAUSER and Peter CHRISTEN |
|
6 |
||
7 |
See COPYING file for copyrights details. |
|
8 |
||
9 |
This library is free software; you can redistribute it and/or |
|
10 |
modify it under the terms of the GNU Lesser General Public |
|
11 |
License as published by the Free Software Foundation; either |
|
12 |
version 2.1 of the License, or (at your option) any later version. |
|
13 |
||
14 |
This library is distributed in the hope that it will be useful, |
|
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
17 |
Lesser General Public License for more details. |
|
18 |
||
19 |
You should have received a copy of the GNU Lesser General Public |
|
20 |
License along with this library; if not, write to the Free Software |
|
21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
22 |
*/ |
|
23 |
||
24 |
// AVR implementation of the CANopen timer driver, uses Timer 3 (16 bit) |
|
25 |
||
26 |
// Includes for the Canfestival driver |
|
27 |
#include "canfestival.h" |
|
801
32d146b64a35
Rename timer.h into timers.h to avoid clash with Xenomai includes.
Edouard Tisserant
parents:
375
diff
changeset
|
28 |
#include "timers.h" |
375 | 29 |
|
30 |
// Define the timer registers |
|
31 |
#define TimerAlarm OCR3B |
|
32 |
#define TimerCounter TCNT3 |
|
33 |
||
34 |
/************************** Modul variables **********************************/ |
|
35 |
// Store the last timer value to calculate the elapsed time |
|
36 |
static TIMEVAL last_time_set = TIMEVAL_MAX; |
|
37 |
||
38 |
void initTimer(void) |
|
39 |
/****************************************************************************** |
|
40 |
Initializes the timer, turn on the interrupt and put the interrupt time to zero |
|
41 |
INPUT void |
|
42 |
OUTPUT void |
|
43 |
******************************************************************************/ |
|
44 |
{ |
|
45 |
TimerAlarm = 0; // Set it back to the zero |
|
46 |
// Set timer 3 for CANopen operation tick 8us max, time is 524ms |
|
47 |
TCCR3B = 1 << CS31 | 1 << CS30; // Timer 3 normal, mit CK/64 |
|
48 |
TIMSK3 = 1 << OCIE3B; // Enable the interrupt |
|
49 |
} |
|
50 |
||
51 |
void setTimer(TIMEVAL value) |
|
52 |
/****************************************************************************** |
|
53 |
Set the timer for the next alarm. |
|
54 |
INPUT value TIMEVAL (unsigned long) |
|
55 |
OUTPUT void |
|
56 |
******************************************************************************/ |
|
57 |
{ |
|
58 |
TimerAlarm += (int)value; // Add the desired time to timer interrupt time |
|
59 |
} |
|
60 |
||
61 |
TIMEVAL getElapsedTime(void) |
|
62 |
/****************************************************************************** |
|
63 |
Return the elapsed time to tell the Stack how much time is spent since last call. |
|
64 |
INPUT void |
|
65 |
OUTPUT value TIMEVAL (unsigned long) the elapsed time |
|
66 |
******************************************************************************/ |
|
67 |
{ |
|
68 |
unsigned int timer = TimerCounter; // Copy the value of the running timer |
|
69 |
if (timer > last_time_set) // In case the timer value is higher than the last time. |
|
70 |
return (timer - last_time_set); // Calculate the time difference |
|
71 |
else if (timer < last_time_set) |
|
72 |
return (last_time_set - timer); // Calculate the time difference |
|
73 |
else |
|
74 |
return TIMEVAL_MAX; |
|
75 |
} |
|
76 |
||
77 |
#ifdef __IAR_SYSTEMS_ICC__ |
|
78 |
#pragma type_attribute = __interrupt |
|
79 |
#pragma vector=TIMER3_COMPB_vect |
|
80 |
void TIMER3_COMPB_interrupt(void) |
|
81 |
#else // GCC |
|
82 |
ISR(TIMER3_COMPB_vect) |
|
83 |
#endif // GCC |
|
84 |
/****************************************************************************** |
|
85 |
Interruptserviceroutine Timer 3 Compare B for the CAN timer |
|
86 |
******************************************************************************/ |
|
87 |
{ |
|
88 |
last_time_set = TimerCounter; |
|
89 |
TimeDispatch(); // Call the time handler of the stack to adapt the elapsed time |
|
90 |
} |
|
91 |
||
92 |
||
93 |