source: rtems/c/src/lib/libbsp/arm/atsam/libraries/libchip/source/tc.c @ c354fac

5
Last change on this file since c354fac was c354fac, checked in by Sebastian Huber <sebastian.huber@…>, on 01/14/16 at 14:48:44

bsp/atsam: Port SAM Software Package to RTEMS

Update #2529.

  • Property mode set to 100644
File size: 6.9 KB
Line 
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#ifndef __rtems__
74#include "board.h"
75#else /* __rtems__ */
76#include <chip.h>
77#endif /* __rtems__ */
78
79#include <assert.h>
80
81/*------------------------------------------------------------------------------
82 *         Global functions
83 *----------------------------------------------------------------------------*/
84
85/**
86 * \brief Configures a Timer Counter Channel
87 *
88 * Configures a Timer Counter to operate in the given mode. Timer is stopped
89 * after configuration and must be restarted with TC_Start(). All the
90 * interrupts of the timer are also disabled.
91 *
92 * \param pTc  Pointer to a Tc instance.
93 * \param channel Channel number.
94 * \param mode  Operating mode (TC_CMR value).
95 */
96extern void TC_Configure(Tc *pTc, uint32_t dwChannel, uint32_t dwMode)
97{
98        TcChannel *pTcCh;
99
100        assert(dwChannel < (sizeof(pTc->TC_CHANNEL) / sizeof(
101                                                         pTc->TC_CHANNEL[0])));
102        pTcCh = pTc->TC_CHANNEL + dwChannel;
103
104        /*  Disable TC clock */
105        pTcCh->TC_CCR = TC_CCR_CLKDIS;
106
107        /*  Disable interrupts */
108        pTcCh->TC_IDR = 0xFFFFFFFF;
109
110        /*  Clear status register */
111        pTcCh->TC_SR;
112
113        /*  Set mode */
114        pTcCh->TC_CMR = dwMode;
115}
116
117/**
118 * \brief Reset and Start the TC Channel
119 *
120 * Enables the timer clock and performs a software reset to start the counting.
121 *
122 * \param pTc  Pointer to a Tc instance.
123 * \param dwChannel Channel number.
124 */
125extern void TC_Start(Tc *pTc, uint32_t dwChannel)
126{
127        TcChannel *pTcCh;
128
129        assert(dwChannel < (sizeof(pTc->TC_CHANNEL) / sizeof(
130                                                         pTc->TC_CHANNEL[0])));
131
132        pTcCh = pTc->TC_CHANNEL + dwChannel;
133        pTcCh->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
134}
135
136/**
137 * \brief Stop TC Channel
138 *
139 * Disables the timer clock, stopping the counting.
140 *
141 * \param pTc     Pointer to a Tc instance.
142 * \param dwChannel Channel number.
143 */
144extern void TC_Stop(Tc *pTc, uint32_t dwChannel)
145{
146        TcChannel *pTcCh;
147
148        assert(dwChannel < (sizeof(pTc->TC_CHANNEL) / sizeof(
149                                                         pTc->TC_CHANNEL[0])));
150
151        pTcCh = pTc->TC_CHANNEL + dwChannel;
152        pTcCh->TC_CCR = TC_CCR_CLKDIS;
153}
154
155/**
156 * \brief Find best MCK divisor
157 *
158 * Finds the best MCK divisor given the timer frequency and MCK. The result
159 * is guaranteed to satisfy the following equation:
160 * \code
161 *   (MCK / (DIV * 65536)) <= freq <= (MCK / DIV)
162 * \endcode
163 * with DIV being the highest possible value.
164 *
165 * \param dwFreq  Desired timer frequency.
166 * \param dwMCk  Master clock frequency.
167 * \param dwDiv  Divisor value.
168 * \param dwTcClks  TCCLKS field value for divisor.
169 * \param dwBoardMCK  Board clock frequency.
170 *
171 * \return 1 if a proper divisor has been found, otherwise 0.
172 */
173extern uint32_t TC_FindMckDivisor(uint32_t dwFreq, uint32_t dwMCk,
174                                                                   uint32_t *dwDiv, uint32_t *dwTcClks, uint32_t dwBoardMCK)
175{
176        /*
177        TCCLKS:
178            0 - PCK6: default source - Slow Clock; update according to actual case
179            4 - SLCK: (slow clock) is either "Embedded 32kHz RC Oscillator" or
180                      "32768Hz Crystal Oscillator"
181        */
182        const uint32_t adwDivisors[5] = { BOARD_MCK / 32768, 8, 32, 128, BOARD_MCK / 32768 };
183
184        uint32_t dwIndex = 0;
185        dwBoardMCK = dwBoardMCK;
186
187        /*  Satisfy lower bound */
188        while (dwFreq < ((dwMCk / adwDivisors[dwIndex]) / 65536)) {
189                dwIndex++;
190
191                /*  If no divisor can be found, return 0 */
192                if (dwIndex == (sizeof(adwDivisors) / sizeof(adwDivisors[0])))
193                        return 0;
194        }
195
196        /*  Try to maximize DIV while satisfying upper bound */
197        while (dwIndex < 4) {
198                if (dwFreq > (dwMCk / adwDivisors[dwIndex + 1]))
199                        break;
200
201                dwIndex++;
202        }
203
204        /*  Store results */
205        if (dwDiv)
206                *dwDiv = adwDivisors[dwIndex];
207
208        if (dwTcClks)
209                *dwTcClks = dwIndex;
210
211        return 1;
212}
213
Note: See TracBrowser for help on using the repository browser.