4 * A simple program of using the Timer and RTTC.
6 * Copyright (c) 2009 Tran Duy Khanh. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
22 #include <system_def.h>
23 #include <lib_AT91SAM7XC256.h>
26 #include <system_stub.h>
27 #include <at91_leds.h>
28 #include <at91_timer.h>
29 #include <at91_dbgu.h>
31 void timer_init_ms(void);
32 void timer_init_ms(void)
34 /* Enable the clock of the TIMER */
35 AT91F_PMC_EnablePeriphClock(AT91C_BASE_PMC, 1<<AT91C_ID_TC0);
36 /* Disable the clock and the interupts */
37 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
38 AT91C_BASE_TC0->TC_IDR = AT91C_TC_COVFS | AT91C_TC_LOVRS |
39 AT91C_TC_CPAS | AT91C_TC_CPBS |
40 AT91C_TC_CPCS | AT91C_TC_LDRAS |
41 AT91C_TC_LDRBS | AT91C_TC_ETRGS ;
42 /* Set the Mode of the Timer Counter (fastest mode MCK/32)
44 AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV5_CLOCK |
45 AT91C_TC_CPCTRG | AT91C_TC_WAVE |
46 AT91C_TC_WAVESEL_UP_AUTO;
47 /* Write the compare register with the magical value */
48 AT91C_BASE_TC0->TC_RC = 1000;
49 /* Enable the clock */
50 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN;
53 void timer_init_us(void);
54 void timer_init_us(void)
56 /* Enable the clock of the TIMER */
57 AT91F_PMC_EnablePeriphClock(AT91C_BASE_PMC, 1<<AT91C_ID_TC2);
58 /* Disable the clock and the interupts */
59 AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKDIS;
60 AT91C_BASE_TC2->TC_IDR = AT91C_TC_COVFS | AT91C_TC_LOVRS |
61 AT91C_TC_CPAS | AT91C_TC_CPBS |
62 AT91C_TC_CPCS | AT91C_TC_LDRAS |
63 AT91C_TC_LDRBS | AT91C_TC_ETRGS ;
64 /* Set the Mode of the Timer Counter (fastest mode MCK/32)
66 AT91C_BASE_TC2->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK |
68 /* Write the compare register with the magical value */
69 AT91C_BASE_TC2->TC_RC = 24;
70 /* Enable the clock */
71 AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKEN;
84 at91_dbgu_init_printf();
87 /* timer initialization */
91 status = AT91C_BASE_TC0->TC_SR;
92 status = AT91C_BASE_TC2->TC_SR;
93 AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
94 AT91C_BASE_TC2->TC_CCR = AT91C_TC_SWTRG;
96 AT91C_BASE_RTTC->RTTC_RTMR = 1;
99 /* toggle the led so we know the task is running */
100 at91_toggle_led(0, &toggle);
101 at91_wait_20_millisec(50);
102 new = AT91C_BASE_RTTC->RTTC_RTVR*30;
103 printf("%d) RTTC_RTVR=%u diff=%u TC0_CV=%u TC1_CV=%u\n",
107 AT91C_BASE_TC0->TC_CV,
108 AT91C_BASE_TC2->TC_CV);