1 /* ----------------------------------------------------------------------------
\r
2 * SAM Software Package License
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2011, 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
33 * Implementation of Timer Counter (TC).
\r
37 /*------------------------------------------------------------------------------
\r
39 *------------------------------------------------------------------------------*/
\r
45 /*------------------------------------------------------------------------------
\r
47 *------------------------------------------------------------------------------*/
\r
50 * \brief Configures a Timer Counter Channel
\r
52 * Configures a Timer Counter to operate in the given mode. Timer is stopped
\r
53 * after configuration and must be restarted with TC_Start(). All the
\r
54 * interrupts of the timer are also disabled.
\r
56 * \param pTc Pointer to a Tc instance.
\r
57 * \param channel Channel number.
\r
58 * \param mode Operating mode (TC_CMR value).
\r
60 extern void TC_Configure( Tc *pTc, uint32_t dwChannel, uint32_t dwMode )
\r
64 assert( dwChannel < (sizeof( pTc->TC_CHANNEL )/sizeof( pTc->TC_CHANNEL[0] )) ) ;
\r
65 pTcCh = pTc->TC_CHANNEL+dwChannel ;
\r
67 /* Disable TC clock */
\r
68 pTcCh->TC_CCR = TC_CCR_CLKDIS ;
\r
70 /* Disable interrupts */
\r
71 pTcCh->TC_IDR = 0xFFFFFFFF ;
\r
73 /* Clear status register */
\r
77 pTcCh->TC_CMR = dwMode ;
\r
81 * \brief Reset and Start the TC Channel
\r
83 * Enables the timer clock and performs a software reset to start the counting.
\r
85 * \param pTc Pointer to a Tc instance.
\r
86 * \param dwChannel Channel number.
\r
88 extern void TC_Start( Tc *pTc, uint32_t dwChannel )
\r
92 assert( dwChannel < (sizeof( pTc->TC_CHANNEL )/sizeof( pTc->TC_CHANNEL[0] )) ) ;
\r
94 pTcCh = pTc->TC_CHANNEL+dwChannel ;
\r
95 pTcCh->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG ;
\r
99 * \brief Stop TC Channel
\r
101 * Disables the timer clock, stopping the counting.
\r
103 * \param pTc Pointer to a Tc instance.
\r
104 * \param dwChannel Channel number.
\r
106 extern void TC_Stop(Tc *pTc, uint32_t dwChannel )
\r
110 assert( dwChannel < (sizeof( pTc->TC_CHANNEL )/sizeof( pTc->TC_CHANNEL[0] )) ) ;
\r
112 pTcCh = pTc->TC_CHANNEL+dwChannel ;
\r
113 pTcCh->TC_CCR = TC_CCR_CLKDIS ;
\r
117 * \brief Find best MCK divisor
\r
119 * Finds the best MCK divisor given the timer frequency and MCK. The result
\r
120 * is guaranteed to satisfy the following equation:
\r
122 * (MCK / (DIV * 65536)) <= freq <= (MCK / DIV)
\r
124 * with DIV being the highest possible value.
\r
126 * \param dwFreq Desired timer frequency.
\r
127 * \param dwMCk Master clock frequency.
\r
128 * \param dwDiv Divisor value.
\r
129 * \param dwTcClks TCCLKS field value for divisor.
\r
130 * \param dwBoardMCK Board clock frequency.
\r
132 * \return 1 if a proper divisor has been found, otherwise 0.
\r
134 extern uint32_t TC_FindMckDivisor( uint32_t dwFreq, uint32_t dwMCk, uint32_t *dwDiv, uint32_t *dwTcClks, uint32_t dwBoardMCK )
\r
136 const uint32_t adwDivisors[5] = { 2, 8, 32, 128, dwBoardMCK / 32768 } ;
\r
138 uint32_t dwIndex = 0 ;
\r
140 /* Satisfy lower bound */
\r
141 while ( dwFreq < ((dwMCk / adwDivisors[dwIndex]) / 65536) )
\r
145 /* If no divisor can be found, return 0 */
\r
146 if ( dwIndex == (sizeof( adwDivisors )/sizeof( adwDivisors[0] )) )
\r
152 /* Try to maximize DIV while satisfying upper bound */
\r
153 while ( dwIndex < 4 )
\r
156 if ( dwFreq > (dwMCk / adwDivisors[dwIndex + 1]) )
\r
163 /* Store results */
\r
166 *dwDiv = adwDivisors[dwIndex] ;
\r
170 *dwTcClks = dwIndex ;
\r