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