]> git.sur5r.net Git - freertos/blob - Demo/Common/drivers/Atmel/at91lib/peripherals/tc/tc.c
Atmel provided hardware specifics.
[freertos] / Demo / Common / drivers / Atmel / at91lib / peripherals / tc / tc.c
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support \r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2008, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 //------------------------------------------------------------------------------\r
31 //         Headers\r
32 //------------------------------------------------------------------------------\r
33 \r
34 #include "tc.h"\r
35 \r
36 //------------------------------------------------------------------------------\r
37 //         Global functions\r
38 //------------------------------------------------------------------------------\r
39 \r
40 //------------------------------------------------------------------------------\r
41 /// Configures a Timer Counter to operate in the given mode. Timer is stopped\r
42 /// after configuration and must be restarted with TC_Start().\r
43 /// to obtain the target frequency.\r
44 /// \param pTc  Pointer to an AT91S_TC instance.\r
45 /// \param mode  Operating mode.\r
46 //------------------------------------------------------------------------------\r
47 void TC_Configure(AT91S_TC *pTc, unsigned int mode)\r
48 {\r
49     // Disable TC clock\r
50     pTc->TC_CCR = AT91C_TC_CLKDIS;\r
51 \r
52     // Disable interrupts\r
53     pTc->TC_IDR = 0xFFFFFFFF;\r
54 \r
55     // Clear status register\r
56     pTc->TC_SR;\r
57 \r
58     // Set mode\r
59     pTc->TC_CMR = mode;\r
60 }\r
61 \r
62 //------------------------------------------------------------------------------\r
63 /// Starts the timer clock.\r
64 /// \param pTc  Pointer to an AT91S_TC instance.\r
65 //------------------------------------------------------------------------------\r
66 void TC_Start(AT91S_TC *pTc)\r
67 {\r
68     pTc->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;\r
69 }\r
70 \r
71 //------------------------------------------------------------------------------\r
72 /// Stops the timer clock.\r
73 /// \param pTc  Pointer to an AT91S_TC instance.\r
74 //------------------------------------------------------------------------------\r
75 void TC_Stop(AT91S_TC *pTc)\r
76 {\r
77     pTc->TC_CCR = AT91C_TC_CLKDIS;\r
78 }\r
79 \r
80 //------------------------------------------------------------------------------\r
81 /// Finds the best MCK divisor given the timer frequency and MCK. The result\r
82 /// is guaranteed to satisfy the following equation:\r
83 ///   (MCK / (DIV * 65536)) <= freq <= (MCK / DIV)\r
84 /// with DIV being the highest possible value.\r
85 /// Returns 1 if a divisor could be found; otherwise returns 0.\r
86 /// \param freq  Desired timer frequency.\r
87 /// \param mck  Master clock frequency.\r
88 /// \param div  Divisor value.\r
89 /// \param tcclks  TCCLKS field value for divisor.\r
90 //------------------------------------------------------------------------------\r
91 unsigned char TC_FindMckDivisor(\r
92     unsigned int freq,\r
93     unsigned int mck,\r
94     unsigned int *div,\r
95     unsigned int *tcclks)\r
96 {\r
97     const unsigned int divisors[5] = {2, 8, 32, 128,\r
98 #if defined(at91sam9260) || defined(at91sam9261) || defined(at91sam9263) \\r
99     || defined(at91sam9xe) || defined(at91sam9rl64) || defined(at91cap9)\r
100         BOARD_MCK / 32768};\r
101 #else\r
102         1024};\r
103 #endif\r
104     unsigned int index = 0;\r
105 \r
106     // Satisfy lower bound\r
107     while (freq < ((mck / divisors[index]) / 65536)) {\r
108 \r
109         index++;\r
110 \r
111         // If no divisor can be found, return 0\r
112         if (index == 5) {\r
113 \r
114             return 0;\r
115         }\r
116     }\r
117 \r
118     // Try to maximise DIV while satisfying upper bound\r
119     while (index < 4) {\r
120 \r
121         if (freq > (mck / divisors[index + 1])) {\r
122 \r
123             break;\r
124         }\r
125         index++;\r
126     }\r
127 \r
128     // Store results\r
129     if (div) {\r
130 \r
131         *div = divisors[index];\r
132     }\r
133     if (tcclks) {\r
134 \r
135         *tcclks = index;\r
136     }\r
137 \r
138     return 1;\r
139 }\r
140 \r