1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2008, Atmel Corporation
\r
6 * All rights reserved.
\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
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\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
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
30 //------------------------------------------------------------------------------
\r
32 //------------------------------------------------------------------------------
\r
36 //------------------------------------------------------------------------------
\r
38 //------------------------------------------------------------------------------
\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
50 pTc->TC_CCR = AT91C_TC_CLKDIS;
\r
52 // Disable interrupts
\r
53 pTc->TC_IDR = 0xFFFFFFFF;
\r
55 // Clear status register
\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
68 pTc->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
\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
77 pTc->TC_CCR = AT91C_TC_CLKDIS;
\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
95 unsigned int *tcclks)
\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
104 unsigned int index = 0;
\r
106 // Satisfy lower bound
\r
107 while (freq < ((mck / divisors[index]) / 65536)) {
\r
111 // If no divisor can be found, return 0
\r
118 // Try to maximise DIV while satisfying upper bound
\r
119 while (index < 4) {
\r
121 if (freq > (mck / divisors[index + 1])) {
\r
131 *div = divisors[index];
\r