2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
5 AT91 Port: Peter CHRISTEN
7 See COPYING file for copyrights details.
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.
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.
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
24 // Includes for the Canfestival driver
25 #include "canfestival.h"
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
34 #define TIMER_INTERRUPT_LEVEL 1
36 void timer_can_irq_handler(void);
39 /************************** Modul variables **********************************/
40 // Store the last timer value to calculate the elapsed time
41 static TIMEVAL last_time_set = TIMEVAL_MAX;
44 /******************************************************************************
45 Initializes the timer, turn on the interrupt and put the interrupt time to zero
48 ******************************************************************************/
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 ;
58 dummy = AT91C_BASE_TC->TC_SR;
59 // Suppress warning variable "dummy" was set but never used
62 // Set the Mode of the Timer Counter (MCK / 128)
63 AT91C_BASE_TC->TC_CMR = AT91C_TC_CLKS_TIMER_DIV4_CLOCK;
66 AT91C_BASE_TC->TC_CCR = AT91C_TC_CLKEN ;
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);
72 AT91C_BASE_TC->TC_IER = AT91C_TC_CPCS; // IRQ enable CPC
73 AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC2);
76 AT91C_BASE_TC->TC_CCR = AT91C_TC_SWTRG ;
79 void setTimer(TIMEVAL value)
80 /******************************************************************************
81 Set the timer for the next alarm.
82 INPUT value TIMEVAL (unsigned long)
84 ******************************************************************************/
86 TimerAlarm += value; // Add the desired time to timer interrupt time
89 TIMEVAL getElapsedTime(void)
90 /******************************************************************************
91 Return the elapsed time to tell the stack how much time is spent since last call.
93 OUTPUT value TIMEVAL (unsigned long) the elapsed time
94 ******************************************************************************/
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;
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)
110 AT91PS_TC TC_pt = AT91C_BASE_TC;
112 // AcknowAT91B_LEDge interrupt status
113 dummy = TC_pt->TC_SR;
114 // Suppress warning variable "dummy" was set but never used
116 last_time_set = TimerCounter;
117 TimeDispatch(); // Call the time handler of the stack to adapt the elapsed time