]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/AtmelFiles/drivers/peripherals/tc.c
Add SAMA5D2 Xplained IAR demo.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D2x_Xplained_IAR / AtmelFiles / drivers / peripherals / tc.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2015, 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 /** \addtogroup tc_module\r
31  * \section Purpose\r
32  * The TC driver provides the Interface to configure the Timer Counter (TC).\r
33  *\r
34  * \section Usage\r
35  * <ul>\r
36  *  <li> Optionally, use tc_find_mck_divisor() to let the program find the best\r
37  *     TCCLKS field value automatically.</li>\r
38  *  <li> Configure a Timer Counter in the desired mode using tc_configure().</li>\r
39  *  <li> Start or stop the timer clock using tc_start() and tc_stop().</li>\r
40  *  </li>\r
41  * </ul>\r
42  * For more accurate information, please look at the TC section of the Datasheet.\r
43  *\r
44  * Related files :\n\r
45  * \ref tc.c\n\r
46  * \ref tc.h.\n\r
47 */\r
48 \r
49 /**\r
50 *  \file\r
51 *\r
52 *  \section Purpose\r
53 *\r
54 *  Interface for configuring and using Timer Counter (TC) peripherals.\r
55 *\r
56 *  \section Usage\r
57 *  -# Optionally, use tc_find_mck_divisor() to let the program find the best\r
58 *     TCCLKS field value automatically.\r
59 *  -# Configure a Timer Counter in the desired mode using tc_configure().\r
60 *  -# Start or stop the timer clock using tc_start() and tc_stop().\r
61 */\r
62 \r
63 /**\r
64  * \file\r
65  *\r
66  * Implementation of Timer Counter (TC).\r
67  *\r
68  */\r
69 \r
70 /*------------------------------------------------------------------------------\r
71  *         Headers\r
72  *------------------------------------------------------------------------------*/\r
73 \r
74 #include "chip.h"\r
75 #include "peripherals/tc.h"\r
76 #include "peripherals/pmc.h"\r
77 \r
78 #include <assert.h>\r
79 \r
80 /*------------------------------------------------------------------------------\r
81  *         Global functions\r
82  *------------------------------------------------------------------------------*/\r
83 \r
84 /**\r
85  * \brief Configures a Timer Counter Channel\r
86  *\r
87  * Configures a Timer Counter to operate in the given mode. Timer is stopped\r
88  * after configuration and must be restarted with tc_start(). All the\r
89  * interrupts of the timer are also disabled.\r
90  *\r
91  * \param pTc  Pointer to a Tc instance.\r
92  * \param channel Channel number.\r
93  * \param mode  Operating mode (TC_CMR value).\r
94  */\r
95 void tc_configure(Tc * pTc, uint32_t channel, uint32_t mode)\r
96 {\r
97         volatile TcChannel *pTcCh;\r
98 \r
99         assert(channel <\r
100                (sizeof (pTc->TC_CHANNEL) / sizeof (pTc->TC_CHANNEL[0])));\r
101         pTcCh = pTc->TC_CHANNEL + channel;\r
102 \r
103         /*  Disable TC clock */\r
104         pTcCh->TC_CCR = TC_CCR_CLKDIS;\r
105 \r
106         /*  Disable interrupts */\r
107         pTcCh->TC_IDR = 0xFFFFFFFF;\r
108 \r
109         /*  Clear status register */\r
110         pTcCh->TC_SR;\r
111 \r
112         /*  Set mode */\r
113         pTcCh->TC_CMR = mode;\r
114 }\r
115 \r
116 /**\r
117  * \brief Reset and Start the TC Channel\r
118  *\r
119  * Enables the timer clock and performs a software reset to start the counting.\r
120  *\r
121  * \param pTc  Pointer to a Tc instance.\r
122  * \param channel Channel number.\r
123  */\r
124 void tc_start(Tc * pTc, uint32_t channel)\r
125 {\r
126         volatile TcChannel *pTcCh;\r
127 \r
128         assert(channel <\r
129                (sizeof (pTc->TC_CHANNEL) / sizeof (pTc->TC_CHANNEL[0])));\r
130 \r
131         pTcCh = pTc->TC_CHANNEL + channel;\r
132         pTcCh->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;\r
133         pTcCh->TC_IER = TC_IER_COVFS;\r
134 }\r
135 \r
136 /**\r
137  * \brief Stop TC Channel\r
138  *\r
139  * Disables the timer clock, stopping the counting.\r
140  *\r
141  * \param pTc     Pointer to a Tc instance.\r
142  * \param channel Channel number.\r
143  */\r
144 void tc_stop(Tc * pTc, uint32_t channel)\r
145 {\r
146         volatile TcChannel *pTcCh;\r
147 \r
148         assert(channel <\r
149                (sizeof (pTc->TC_CHANNEL) / sizeof (pTc->TC_CHANNEL[0])));\r
150 \r
151         pTcCh = pTc->TC_CHANNEL + channel;\r
152         pTcCh->TC_CCR = TC_CCR_CLKDIS;\r
153         pTcCh->TC_IDR = TC_IER_COVFS;\r
154 }\r
155 \r
156 /**\r
157  * \brief Enables TC channel interrupts\r
158  *\r
159  * \param tc Pointer to Tc instance\r
160  * \param channel Channel number\r
161  * \param mask mask of interrupts to enable\r
162  */\r
163 void tc_enable_it(Tc* tc, uint32_t channel, uint32_t mask)\r
164 {\r
165         assert(channel < (sizeof (tc->TC_CHANNEL) / sizeof (tc->TC_CHANNEL[0])));\r
166         tc->TC_CHANNEL[channel].TC_IER = mask;\r
167 }\r
168 \r
169 /**\r
170  * \brief Find best MCK divisor\r
171  *\r
172  * Finds the best MCK divisor given the timer frequency and MCK. The result\r
173  * is guaranteed to satisfy the following equation:\r
174  * \code\r
175  *   (MCK / (DIV * 65536)) <= freq <= (MCK / DIV)\r
176  * \endcode\r
177  * with DIV being the highest possible value.\r
178  *\r
179  * \param freq  Desired timer freq.\r
180  * \param div  Divisor value.\r
181  * \param tc_clks  TCCLKS field value for divisor.\r
182  *\r
183  * \return 1 if a proper divisor has been found, otherwise 0.\r
184  */\r
185 uint32_t tc_find_mck_divisor (uint32_t freq, uint32_t * div,\r
186                                   uint32_t * tc_clks)\r
187 {\r
188         const uint32_t periph_clock = pmc_get_peripheral_clock(ID_TC0);\r
189         const uint32_t available_freqs[5] = {periph_clock >> 1, periph_clock >> 3, periph_clock >> 5, periph_clock >> 7, 32768};\r
190 \r
191         int i = 0;\r
192         for (i = 0; i < 5; ++i)\r
193         {\r
194                 uint32_t tmp = freq << 1;\r
195                 if (tmp > available_freqs[i])\r
196                         break;\r
197         }\r
198 \r
199         i = (i == 5 ? i-1 : i);\r
200 \r
201         /*  Store results */\r
202         if (div) {\r
203                 *div = periph_clock / available_freqs[i];\r
204         }\r
205         if (tc_clks) {\r
206                 *tc_clks = i;\r
207         }\r
208 \r
209         return 1;\r
210 }\r
211 \r
212 uint32_t tc_get_status(Tc* tc, uint32_t channel_num)\r
213 {\r
214         return tc->TC_CHANNEL[channel_num].TC_SR;\r
215 }\r
216 \r
217 \r
218 void tc_trigger_on_freq(Tc* tc, uint32_t channel_num, uint32_t freq)\r
219 {\r
220         uint32_t div = 0;\r
221         uint32_t tcclks = 0;\r
222         uint32_t tc_id = get_tc_id_from_addr(tc);\r
223         TcChannel* channel = &tc->TC_CHANNEL[channel_num];\r
224 \r
225         tc_find_mck_divisor(freq, &div, &tcclks);\r
226         tc_configure(tc, channel_num, tcclks | TC_CMR_CPCTRG);\r
227         channel->TC_RC = (pmc_get_peripheral_clock(tc_id) / div) / freq;\r
228 }\r