peter@520: /* peter@520: This file is part of CanFestival, a library implementing CanOpen Stack. peter@520: peter@520: Copyright (C): Edouard TISSERANT and Francis DUPIN peter@520: AT91 Port: Peter CHRISTEN peter@520: peter@520: See COPYING file for copyrights details. peter@520: peter@520: This library is free software; you can redistribute it and/or peter@520: modify it under the terms of the GNU Lesser General Public peter@520: License as published by the Free Software Foundation; either peter@520: version 2.1 of the License, or (at your option) any later version. peter@520: peter@520: This library is distributed in the hope that it will be useful, peter@520: but WITHOUT ANY WARRANTY; without even the implied warranty of peter@520: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU peter@520: Lesser General Public License for more details. peter@520: peter@520: You should have received a copy of the GNU Lesser General Public peter@520: License along with this library; if not, write to the Free Software peter@520: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA peter@520: */ peter@520: peter@520: // Includes for the Canfestival driver peter@520: #include "canfestival.h" Edouard@801: #include "timers.h" peter@520: peter@520: // Define the timer registers peter@520: #define AT91C_BASE_TC AT91C_BASE_TC2 peter@520: #define AT91C_ID_TC AT91C_ID_TC2 peter@520: #define TimerAlarm AT91C_BASE_TC2->TC_RC peter@520: #define TimerCounter AT91C_BASE_TC2->TC_CV peter@520: peter@520: #define TIMER_INTERRUPT_LEVEL 1 peter@520: peter@520: void timer_can_irq_handler(void); peter@520: peter@520: peter@520: /************************** Modul variables **********************************/ peter@520: // Store the last timer value to calculate the elapsed time peter@520: static TIMEVAL last_time_set = TIMEVAL_MAX; peter@520: peter@520: void initTimer(void) peter@520: /****************************************************************************** peter@520: Initializes the timer, turn on the interrupt and put the interrupt time to zero peter@520: INPUT void peter@520: OUTPUT void peter@520: ******************************************************************************/ peter@520: { peter@520: unsigned int dummy; peter@520: peter@520: // First, enable the clock of the TIMER peter@520: AT91F_PMC_EnablePeriphClock (AT91C_BASE_PMC, 1 << AT91C_ID_TC); peter@520: // Disable the clock and the interrupts peter@520: AT91C_BASE_TC->TC_CCR = AT91C_TC_CLKDIS ; peter@520: AT91C_BASE_TC->TC_IDR = 0xFFFFFFFF ; peter@520: // Clear status bit peter@520: dummy = AT91C_BASE_TC->TC_SR; peter@520: // Suppress warning variable "dummy" was set but never used peter@520: dummy = dummy; peter@520: peter@520: // Set the Mode of the Timer Counter (MCK / 128) peter@520: AT91C_BASE_TC->TC_CMR = AT91C_TC_CLKS_TIMER_DIV4_CLOCK; peter@520: peter@520: // Enable the clock peter@520: AT91C_BASE_TC->TC_CCR = AT91C_TC_CLKEN ; peter@520: peter@520: // Open Timer interrupt peter@520: AT91F_AIC_ConfigureIt (AT91C_BASE_AIC, AT91C_ID_TC, TIMER_INTERRUPT_LEVEL, peter@520: AT91C_AIC_SRCTYPE_INT_POSITIVE_EDGE, timer_can_irq_handler); peter@520: peter@520: AT91C_BASE_TC->TC_IER = AT91C_TC_CPCS; // IRQ enable CPC peter@520: AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC2); peter@520: peter@520: // Start Timer peter@520: AT91C_BASE_TC->TC_CCR = AT91C_TC_SWTRG ; peter@520: } peter@520: peter@520: void setTimer(TIMEVAL value) peter@520: /****************************************************************************** peter@520: Set the timer for the next alarm. peter@520: INPUT value TIMEVAL (unsigned long) peter@520: OUTPUT void peter@520: ******************************************************************************/ peter@520: { peter@520: TimerAlarm += value; // Add the desired time to timer interrupt time peter@520: } peter@520: peter@520: TIMEVAL getElapsedTime(void) peter@520: /****************************************************************************** peter@520: Return the elapsed time to tell the stack how much time is spent since last call. peter@520: INPUT void peter@520: OUTPUT value TIMEVAL (unsigned long) the elapsed time peter@520: ******************************************************************************/ peter@520: { peter@520: unsigned int timer = TimerCounter; // Copy the value of the running timer peter@520: // Calculate the time difference peter@520: return timer > last_time_set ? timer - last_time_set : last_time_set - timer; peter@520: } peter@520: peter@520: peter@520: //*---------------------------------------------------------------------------- peter@520: //* Function Name : timer_can_irq_handler peter@520: //* Object : C handler interrupt function by the interrupts peter@520: //* assembling routine peter@520: //* Output Parameters : calls TimeDispatch peter@520: //*---------------------------------------------------------------------------- peter@520: void timer_can_irq_handler(void) peter@520: { peter@520: AT91PS_TC TC_pt = AT91C_BASE_TC; peter@520: unsigned int dummy; peter@520: // AcknowAT91B_LEDge interrupt status peter@520: dummy = TC_pt->TC_SR; peter@520: // Suppress warning variable "dummy" was set but never used peter@520: dummy = dummy; peter@520: last_time_set = TimerCounter; peter@520: TimeDispatch(); // Call the time handler of the stack to adapt the elapsed time peter@520: } peter@520: