examples/AT91/Master/interrupt_timer.c
author Edouard Tisserant
Wed, 13 Apr 2011 16:29:59 +0200
changeset 659 1041153c5fd2
parent 521 731bbe1b67b4
permissions -rw-r--r--
Fix some side effects of EDS import, while editing afterward
521
peter
parents:
diff changeset
     1
// ----------------------------------------------------------------------------
peter
parents:
diff changeset
     2
//         ATMEL Microcontroller Software Support  -  ROUSSET  -
peter
parents:
diff changeset
     3
// ----------------------------------------------------------------------------
peter
parents:
diff changeset
     4
// DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
peter
parents:
diff changeset
     5
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
peter
parents:
diff changeset
     6
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
peter
parents:
diff changeset
     7
// DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
peter
parents:
diff changeset
     8
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
peter
parents:
diff changeset
     9
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
peter
parents:
diff changeset
    10
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
peter
parents:
diff changeset
    11
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
peter
parents:
diff changeset
    12
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
peter
parents:
diff changeset
    13
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
peter
parents:
diff changeset
    14
// ----------------------------------------------------------------------------
peter
parents:
diff changeset
    15
// File Name           : interrupt_timer.c
peter
parents:
diff changeset
    16
// Object              : Timer interrupt management
peter
parents:
diff changeset
    17
//                     : Use AT91B_LED7 & AT91B_LED8 for status interrupt
peter
parents:
diff changeset
    18
// Creation            : JPP   08-Sep-2005
peter
parents:
diff changeset
    19
// ----------------------------------------------------------------------------
peter
parents:
diff changeset
    20
peter
parents:
diff changeset
    21
// Include Standard LIB  files
peter
parents:
diff changeset
    22
#include "config.h"
peter
parents:
diff changeset
    23
peter
parents:
diff changeset
    24
//* Global variable
peter
parents:
diff changeset
    25
int timer0_interrupt = 0;
peter
parents:
diff changeset
    26
peter
parents:
diff changeset
    27
#define TIMER0_INTERRUPT_LEVEL		1
peter
parents:
diff changeset
    28
peter
parents:
diff changeset
    29
/*-----------------*/
peter
parents:
diff changeset
    30
/* Clock Selection */
peter
parents:
diff changeset
    31
/*-----------------*/
peter
parents:
diff changeset
    32
#define TC_CLKS                  0x7
peter
parents:
diff changeset
    33
#define TC_CLKS_MCK2             0x0
peter
parents:
diff changeset
    34
#define TC_CLKS_MCK8             0x1
peter
parents:
diff changeset
    35
#define TC_CLKS_MCK32            0x2
peter
parents:
diff changeset
    36
#define TC_CLKS_MCK128           0x3
peter
parents:
diff changeset
    37
#define TC_CLKS_MCK1024          0x4
peter
parents:
diff changeset
    38
peter
parents:
diff changeset
    39
//*------------------------- Internal Function --------------------------------
peter
parents:
diff changeset
    40
//*----------------------------------------------------------------------------
peter
parents:
diff changeset
    41
//* Function Name       : AT91F_TC_Open
peter
parents:
diff changeset
    42
//* Object              : Initialize Timer Counter Channel and enable is clock
peter
parents:
diff changeset
    43
//* Input Parameters    : <tc_pt> = TC Channel Descriptor Pointer
peter
parents:
diff changeset
    44
//*                       <mode> = Timer Counter Mode
peter
parents:
diff changeset
    45
//*                     : <TimerId> = Timer peripheral ID definitions
peter
parents:
diff changeset
    46
//* Output Parameters   : None
peter
parents:
diff changeset
    47
//*----------------------------------------------------------------------------
peter
parents:
diff changeset
    48
void AT91F_TC_Open ( AT91PS_TC TC_pt, unsigned int Mode, unsigned int TimerId)
peter
parents:
diff changeset
    49
{
peter
parents:
diff changeset
    50
  unsigned int dummy;
peter
parents:
diff changeset
    51
peter
parents:
diff changeset
    52
  //* First, enable the clock of the TIMER
peter
parents:
diff changeset
    53
  AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1<< TimerId ) ;
peter
parents:
diff changeset
    54
peter
parents:
diff changeset
    55
  //* Disable the clock and the interrupts
peter
parents:
diff changeset
    56
  TC_pt->TC_CCR = AT91C_TC_CLKDIS ;
peter
parents:
diff changeset
    57
  TC_pt->TC_IDR = 0xFFFFFFFF ;
peter
parents:
diff changeset
    58
peter
parents:
diff changeset
    59
  //* Clear status bit
peter
parents:
diff changeset
    60
  dummy = TC_pt->TC_SR;
peter
parents:
diff changeset
    61
  //* Suppress warning variable "dummy" was set but never used
peter
parents:
diff changeset
    62
  dummy = dummy;
peter
parents:
diff changeset
    63
  //* Set the Mode of the Timer Counter
peter
parents:
diff changeset
    64
  TC_pt->TC_CMR = Mode ;
peter
parents:
diff changeset
    65
peter
parents:
diff changeset
    66
  //* Enable the clock
peter
parents:
diff changeset
    67
  TC_pt->TC_CCR = AT91C_TC_CLKEN ;
peter
parents:
diff changeset
    68
}
peter
parents:
diff changeset
    69
//*------------------------- Interrupt Function -------------------------------
peter
parents:
diff changeset
    70
peter
parents:
diff changeset
    71
//*----------------------------------------------------------------------------
peter
parents:
diff changeset
    72
//* Function Name       : timer0_c_irq_handler
peter
parents:
diff changeset
    73
//* Object              : C handler interrupt function calAT91B_LED by the interrupts
peter
parents:
diff changeset
    74
//*                       assembling routine
peter
parents:
diff changeset
    75
//* Output Parameters   : increment count_timer0_interrupt
peter
parents:
diff changeset
    76
//*----------------------------------------------------------------------------
peter
parents:
diff changeset
    77
void timer0_c_irq_handler(void)
peter
parents:
diff changeset
    78
{
peter
parents:
diff changeset
    79
  AT91PS_TC TC_pt = AT91C_BASE_TC0;
peter
parents:
diff changeset
    80
  unsigned int dummy;
peter
parents:
diff changeset
    81
  //* AcknowAT91B_LEDge interrupt status
peter
parents:
diff changeset
    82
  dummy = TC_pt->TC_SR;
peter
parents:
diff changeset
    83
  //* Suppress warning variable "dummy" was set but never used
peter
parents:
diff changeset
    84
  dummy = dummy;
peter
parents:
diff changeset
    85
  timer0_interrupt = TRUE;
peter
parents:
diff changeset
    86
}
peter
parents:
diff changeset
    87
peter
parents:
diff changeset
    88
//*-------------------------- External Function -------------------------------
peter
parents:
diff changeset
    89
peter
parents:
diff changeset
    90
//*----------------------------------------------------------------------------
peter
parents:
diff changeset
    91
//* Function Name       : timer_init
peter
parents:
diff changeset
    92
//* Object              : Init timer counter
peter
parents:
diff changeset
    93
//* Input Parameters    : none
peter
parents:
diff changeset
    94
//* Output Parameters   : TRUE
peter
parents:
diff changeset
    95
//*----------------------------------------------------------------------------
peter
parents:
diff changeset
    96
void timer_init (unsigned int time)
peter
parents:
diff changeset
    97
{
peter
parents:
diff changeset
    98
  //* Open timer0
peter
parents:
diff changeset
    99
  AT91F_TC_Open(AT91C_BASE_TC0,TC_CLKS_MCK8 | AT91C_TC_WAVESEL_UP_AUTO,AT91C_ID_TC0);
peter
parents:
diff changeset
   100
peter
parents:
diff changeset
   101
  //* Open Timer 0 interrupt
peter
parents:
diff changeset
   102
  AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_TC0, TIMER0_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, timer0_c_irq_handler);
peter
parents:
diff changeset
   103
  AT91C_BASE_TC0->TC_IER = AT91C_TC_CPCS;  //  IRQ enable CPC
peter
parents:
diff changeset
   104
  AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC0);
peter
parents:
diff changeset
   105
peter
parents:
diff changeset
   106
  AT91C_BASE_TC0->TC_RC = (unsigned int)(AT91C_MASTER_CLOCK / 8 * time / 1000);
peter
parents:
diff changeset
   107
peter
parents:
diff changeset
   108
  //* Start timer0
peter
parents:
diff changeset
   109
  AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG ;
peter
parents:
diff changeset
   110
}