1 | /* ---------------------------------------------------------------------------- */ |
---|
2 | /* Atmel Microcontroller Software Support */ |
---|
3 | /* SAM Software Package License */ |
---|
4 | /* ---------------------------------------------------------------------------- */ |
---|
5 | /* Copyright (c) 2015, Atmel Corporation */ |
---|
6 | /* */ |
---|
7 | /* All rights reserved. */ |
---|
8 | /* */ |
---|
9 | /* Redistribution and use in source and binary forms, with or without */ |
---|
10 | /* modification, are permitted provided that the following condition is met: */ |
---|
11 | /* */ |
---|
12 | /* - Redistributions of source code must retain the above copyright notice, */ |
---|
13 | /* this list of conditions and the disclaimer below. */ |
---|
14 | /* */ |
---|
15 | /* Atmel's name may not be used to endorse or promote products derived from */ |
---|
16 | /* this software without specific prior written permission. */ |
---|
17 | /* */ |
---|
18 | /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */ |
---|
19 | /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ |
---|
20 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */ |
---|
21 | /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */ |
---|
22 | /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ |
---|
23 | /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ |
---|
24 | /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ |
---|
25 | /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ |
---|
26 | /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ |
---|
27 | /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
---|
28 | /* ---------------------------------------------------------------------------- */ |
---|
29 | |
---|
30 | /** \addtogroup tc_module |
---|
31 | * The TC driver provides the Interface to configure the Timer Counter (TC). |
---|
32 | * |
---|
33 | * \section Usage |
---|
34 | * <ul> |
---|
35 | * <li> Optionally, use TC_FindMckDivisor() to let the program find the best |
---|
36 | * TCCLKS field value automatically.</li> |
---|
37 | * <li> Configure a Timer Counter in the desired mode using TC_Configure().</li> |
---|
38 | * <li> Start or stop the timer clock using TC_Start() and TC_Stop().</li> |
---|
39 | * |
---|
40 | * </ul> |
---|
41 | * For more accurate information, please look at the TC section of the Datasheet. |
---|
42 | * |
---|
43 | * Related files :\n |
---|
44 | * \ref tc.c\n |
---|
45 | * \ref tc.h.\n |
---|
46 | */ |
---|
47 | |
---|
48 | /** |
---|
49 | * \file |
---|
50 | * |
---|
51 | * \section Purpose |
---|
52 | * |
---|
53 | * Interface for configuring and using Timer Counter (TC) peripherals. |
---|
54 | * |
---|
55 | * \section Usage |
---|
56 | * -# Optionally, use TC_FindMckDivisor() to let the program find the best |
---|
57 | * TCCLKS field value automatically. |
---|
58 | * -# Configure a Timer Counter in the desired mode using TC_Configure(). |
---|
59 | * -# Start or stop the timer clock using TC_Start() and TC_Stop(). |
---|
60 | */ |
---|
61 | |
---|
62 | /** |
---|
63 | * \file |
---|
64 | * |
---|
65 | * Implementation of Timer Counter (TC). |
---|
66 | * |
---|
67 | */ |
---|
68 | |
---|
69 | /*------------------------------------------------------------------------------ |
---|
70 | * Headers |
---|
71 | *-----------------------------------------------------------------------------*/ |
---|
72 | |
---|
73 | #include "board.h" |
---|
74 | |
---|
75 | #include <assert.h> |
---|
76 | |
---|
77 | /*------------------------------------------------------------------------------ |
---|
78 | * Global functions |
---|
79 | *----------------------------------------------------------------------------*/ |
---|
80 | |
---|
81 | /** |
---|
82 | * \brief Configures a Timer Counter Channel |
---|
83 | * |
---|
84 | * Configures a Timer Counter to operate in the given mode. Timer is stopped |
---|
85 | * after configuration and must be restarted with TC_Start(). All the |
---|
86 | * interrupts of the timer are also disabled. |
---|
87 | * |
---|
88 | * \param pTc Pointer to a Tc instance. |
---|
89 | * \param channel Channel number. |
---|
90 | * \param mode Operating mode (TC_CMR value). |
---|
91 | */ |
---|
92 | extern void TC_Configure(Tc *pTc, uint32_t dwChannel, uint32_t dwMode) |
---|
93 | { |
---|
94 | TcChannel *pTcCh; |
---|
95 | |
---|
96 | assert(dwChannel < (sizeof(pTc->TC_CHANNEL) / sizeof( |
---|
97 | pTc->TC_CHANNEL[0]))); |
---|
98 | pTcCh = pTc->TC_CHANNEL + dwChannel; |
---|
99 | |
---|
100 | /* Disable TC clock */ |
---|
101 | pTcCh->TC_CCR = TC_CCR_CLKDIS; |
---|
102 | |
---|
103 | /* Disable interrupts */ |
---|
104 | pTcCh->TC_IDR = 0xFFFFFFFF; |
---|
105 | |
---|
106 | /* Clear status register */ |
---|
107 | pTcCh->TC_SR; |
---|
108 | |
---|
109 | /* Set mode */ |
---|
110 | pTcCh->TC_CMR = dwMode; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * \brief Reset and Start the TC Channel |
---|
115 | * |
---|
116 | * Enables the timer clock and performs a software reset to start the counting. |
---|
117 | * |
---|
118 | * \param pTc Pointer to a Tc instance. |
---|
119 | * \param dwChannel Channel number. |
---|
120 | */ |
---|
121 | extern void TC_Start(Tc *pTc, uint32_t dwChannel) |
---|
122 | { |
---|
123 | TcChannel *pTcCh; |
---|
124 | |
---|
125 | assert(dwChannel < (sizeof(pTc->TC_CHANNEL) / sizeof( |
---|
126 | pTc->TC_CHANNEL[0]))); |
---|
127 | |
---|
128 | pTcCh = pTc->TC_CHANNEL + dwChannel; |
---|
129 | pTcCh->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG; |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * \brief Stop TC Channel |
---|
134 | * |
---|
135 | * Disables the timer clock, stopping the counting. |
---|
136 | * |
---|
137 | * \param pTc Pointer to a Tc instance. |
---|
138 | * \param dwChannel Channel number. |
---|
139 | */ |
---|
140 | extern void TC_Stop(Tc *pTc, uint32_t dwChannel) |
---|
141 | { |
---|
142 | TcChannel *pTcCh; |
---|
143 | |
---|
144 | assert(dwChannel < (sizeof(pTc->TC_CHANNEL) / sizeof( |
---|
145 | pTc->TC_CHANNEL[0]))); |
---|
146 | |
---|
147 | pTcCh = pTc->TC_CHANNEL + dwChannel; |
---|
148 | pTcCh->TC_CCR = TC_CCR_CLKDIS; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * \brief Find best MCK divisor |
---|
153 | * |
---|
154 | * Finds the best MCK divisor given the timer frequency and MCK. The result |
---|
155 | * is guaranteed to satisfy the following equation: |
---|
156 | * \code |
---|
157 | * (MCK / (DIV * 65536)) <= freq <= (MCK / DIV) |
---|
158 | * \endcode |
---|
159 | * with DIV being the highest possible value. |
---|
160 | * |
---|
161 | * \param dwFreq Desired timer frequency. |
---|
162 | * \param dwMCk Master clock frequency. |
---|
163 | * \param dwDiv Divisor value. |
---|
164 | * \param dwTcClks TCCLKS field value for divisor. |
---|
165 | * \param dwBoardMCK Board clock frequency. |
---|
166 | * |
---|
167 | * \return 1 if a proper divisor has been found, otherwise 0. |
---|
168 | */ |
---|
169 | extern uint32_t TC_FindMckDivisor(uint32_t dwFreq, uint32_t dwMCk, |
---|
170 | uint32_t *dwDiv, uint32_t *dwTcClks, uint32_t dwBoardMCK) |
---|
171 | { |
---|
172 | /* |
---|
173 | TCCLKS: |
---|
174 | 0 - PCK6: default source - Slow Clock; update according to actual case |
---|
175 | 4 - SLCK: (slow clock) is either "Embedded 32kHz RC Oscillator" or |
---|
176 | "32768Hz Crystal Oscillator" |
---|
177 | */ |
---|
178 | const uint32_t adwDivisors[5] = { BOARD_MCK / 32768, 8, 32, 128, BOARD_MCK / 32768 }; |
---|
179 | |
---|
180 | uint32_t dwIndex = 0; |
---|
181 | dwBoardMCK = dwBoardMCK; |
---|
182 | |
---|
183 | /* Satisfy lower bound */ |
---|
184 | while (dwFreq < ((dwMCk / adwDivisors[dwIndex]) / 65536)) { |
---|
185 | dwIndex++; |
---|
186 | |
---|
187 | /* If no divisor can be found, return 0 */ |
---|
188 | if (dwIndex == (sizeof(adwDivisors) / sizeof(adwDivisors[0]))) |
---|
189 | return 0; |
---|
190 | } |
---|
191 | |
---|
192 | /* Try to maximize DIV while satisfying upper bound */ |
---|
193 | while (dwIndex < 4) { |
---|
194 | if (dwFreq > (dwMCk / adwDivisors[dwIndex + 1])) |
---|
195 | break; |
---|
196 | |
---|
197 | dwIndex++; |
---|
198 | } |
---|
199 | |
---|
200 | /* Store results */ |
---|
201 | if (dwDiv) |
---|
202 | *dwDiv = adwDivisors[dwIndex]; |
---|
203 | |
---|
204 | if (dwTcClks) |
---|
205 | *dwTcClks = dwIndex; |
---|
206 | |
---|
207 | return 1; |
---|
208 | } |
---|
209 | |
---|