]> rtime.felk.cvut.cz Git - sysless.git/blob - app/arm/test_at91_timer/test_at91_timer.c
Build framework for the AT91SAM7 architecture
[sysless.git] / app / arm / test_at91_timer / test_at91_timer.c
1 /*
2  * test_at91_timer.c
3  *
4  * A simple program of using the Timer and RTTC.
5  *
6  * Copyright (c) 2009 Tran Duy Khanh. All rights reserved.
7  *
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
11  * later version.
12  *
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.
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <system_def.h>
23 #include <lib_AT91SAM7XC256.h>
24
25 #include <hwinit.h>
26 #include <system_stub.h>
27 #include <at91_leds.h>
28 #include <at91_timer.h>
29 #include <at91_dbgu.h>
30
31 void timer_init_ms(void);
32 void timer_init_ms(void)
33 {
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)
43          * and RC compare */
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;
51 }
52
53 void timer_init_us(void);
54 void timer_init_us(void)
55 {
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)
65          * and RC compare */
66         AT91C_BASE_TC2->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK |
67                                         AT91C_TC_CPCTRG;
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;
72 }
73
74 /* main */
75 int main(void)
76 {
77         int toggle = 0;
78         int cnt = 0;
79         volatile int status;
80         unsigned int old = 0;
81         unsigned int new = 0;
82
83         hwinit();
84         at91_dbgu_init_printf();
85         at91_all_leds_off();
86
87         /* timer initialization */
88         timer_init_ms();
89         timer_init_us();
90
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;
95
96         AT91C_BASE_RTTC->RTTC_RTMR = 1;
97
98         while (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",
104                                 cnt,
105                                 new,
106                                 old - new,
107                                 AT91C_BASE_TC0->TC_CV,
108                                 AT91C_BASE_TC2->TC_CV);
109                 old = new;
110                 cnt++;
111         }
112
113         return 0;
114 }