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