author | Edouard Tisserant |
Mon, 23 Apr 2018 12:32:03 +0200 | |
changeset 805 | 570e3a444023 |
parent 801 | 32d146b64a35 |
permissions | -rw-r--r-- |
520 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
AT91 Port: 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 |
// Includes for the Canfestival driver |
|
25 |
#include "canfestival.h" |
|
801
32d146b64a35
Rename timer.h into timers.h to avoid clash with Xenomai includes.
Edouard Tisserant
parents:
520
diff
changeset
|
26 |
#include "timers.h" |
520 | 27 |
|
28 |
// Define the timer registers |
|
29 |
#define AT91C_BASE_TC AT91C_BASE_TC2 |
|
30 |
#define AT91C_ID_TC AT91C_ID_TC2 |
|
31 |
#define TimerAlarm AT91C_BASE_TC2->TC_RC |
|
32 |
#define TimerCounter AT91C_BASE_TC2->TC_CV |
|
33 |
||
34 |
#define TIMER_INTERRUPT_LEVEL 1 |
|
35 |
||
36 |
void timer_can_irq_handler(void); |
|
37 |
||
38 |
||
39 |
/************************** Modul variables **********************************/ |
|
40 |
// Store the last timer value to calculate the elapsed time |
|
41 |
static TIMEVAL last_time_set = TIMEVAL_MAX; |
|
42 |
||
43 |
void initTimer(void) |
|
44 |
/****************************************************************************** |
|
45 |
Initializes the timer, turn on the interrupt and put the interrupt time to zero |
|
46 |
INPUT void |
|
47 |
OUTPUT void |
|
48 |
******************************************************************************/ |
|
49 |
{ |
|
50 |
unsigned int dummy; |
|
51 |
||
52 |
// First, enable the clock of the TIMER |
|
53 |
AT91F_PMC_EnablePeriphClock (AT91C_BASE_PMC, 1 << AT91C_ID_TC); |
|
54 |
// Disable the clock and the interrupts |
|
55 |
AT91C_BASE_TC->TC_CCR = AT91C_TC_CLKDIS ; |
|
56 |
AT91C_BASE_TC->TC_IDR = 0xFFFFFFFF ; |
|
57 |
// Clear status bit |
|
58 |
dummy = AT91C_BASE_TC->TC_SR; |
|
59 |
// Suppress warning variable "dummy" was set but never used |
|
60 |
dummy = dummy; |
|
61 |
||
62 |
// Set the Mode of the Timer Counter (MCK / 128) |
|
63 |
AT91C_BASE_TC->TC_CMR = AT91C_TC_CLKS_TIMER_DIV4_CLOCK; |
|
64 |
||
65 |
// Enable the clock |
|
66 |
AT91C_BASE_TC->TC_CCR = AT91C_TC_CLKEN ; |
|
67 |
||
68 |
// Open Timer interrupt |
|
69 |
AT91F_AIC_ConfigureIt (AT91C_BASE_AIC, AT91C_ID_TC, TIMER_INTERRUPT_LEVEL, |
|
70 |
AT91C_AIC_SRCTYPE_INT_POSITIVE_EDGE, timer_can_irq_handler); |
|
71 |
||
72 |
AT91C_BASE_TC->TC_IER = AT91C_TC_CPCS; // IRQ enable CPC |
|
73 |
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC2); |
|
74 |
||
75 |
// Start Timer |
|
76 |
AT91C_BASE_TC->TC_CCR = AT91C_TC_SWTRG ; |
|
77 |
} |
|
78 |
||
79 |
void setTimer(TIMEVAL value) |
|
80 |
/****************************************************************************** |
|
81 |
Set the timer for the next alarm. |
|
82 |
INPUT value TIMEVAL (unsigned long) |
|
83 |
OUTPUT void |
|
84 |
******************************************************************************/ |
|
85 |
{ |
|
86 |
TimerAlarm += value; // Add the desired time to timer interrupt time |
|
87 |
} |
|
88 |
||
89 |
TIMEVAL getElapsedTime(void) |
|
90 |
/****************************************************************************** |
|
91 |
Return the elapsed time to tell the stack how much time is spent since last call. |
|
92 |
INPUT void |
|
93 |
OUTPUT value TIMEVAL (unsigned long) the elapsed time |
|
94 |
******************************************************************************/ |
|
95 |
{ |
|
96 |
unsigned int timer = TimerCounter; // Copy the value of the running timer |
|
97 |
// Calculate the time difference |
|
98 |
return timer > last_time_set ? timer - last_time_set : last_time_set - timer; |
|
99 |
} |
|
100 |
||
101 |
||
102 |
//*---------------------------------------------------------------------------- |
|
103 |
//* Function Name : timer_can_irq_handler |
|
104 |
//* Object : C handler interrupt function by the interrupts |
|
105 |
//* assembling routine |
|
106 |
//* Output Parameters : calls TimeDispatch |
|
107 |
//*---------------------------------------------------------------------------- |
|
108 |
void timer_can_irq_handler(void) |
|
109 |
{ |
|
110 |
AT91PS_TC TC_pt = AT91C_BASE_TC; |
|
111 |
unsigned int dummy; |
|
112 |
// AcknowAT91B_LEDge interrupt status |
|
113 |
dummy = TC_pt->TC_SR; |
|
114 |
// Suppress warning variable "dummy" was set but never used |
|
115 |
dummy = dummy; |
|
116 |
last_time_set = TimerCounter; |
|
117 |
TimeDispatch(); // Call the time handler of the stack to adapt the elapsed time |
|
118 |
} |
|
119 |