summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/atsam/libraries/libchip/source/tc.c
blob: 4ef9bd9f50e536e81f5c84235bf55c524867d21c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* ---------------------------------------------------------------------------- */
/*                  Atmel Microcontroller Software Support                      */
/*                       SAM Software Package License                           */
/* ---------------------------------------------------------------------------- */
/* Copyright (c) 2015, Atmel Corporation                                        */
/*                                                                              */
/* All rights reserved.                                                         */
/*                                                                              */
/* Redistribution and use in source and binary forms, with or without           */
/* modification, are permitted provided that the following condition is met:    */
/*                                                                              */
/* - Redistributions of source code must retain the above copyright notice,     */
/* this list of conditions and the disclaimer below.                            */
/*                                                                              */
/* Atmel's name may not be used to endorse or promote products derived from     */
/* this software without specific prior written permission.                     */
/*                                                                              */
/* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
/* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
/* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
/* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
/* ---------------------------------------------------------------------------- */

/** \addtogroup tc_module
 * The TC driver provides the Interface to configure the Timer Counter (TC).
 *
 *  \section Usage
 * <ul>
 *  <li> Optionally, use TC_FindMckDivisor() to let the program find the best
 *     TCCLKS field value automatically.</li>
 *  <li> Configure a Timer Counter in the desired mode using TC_Configure().</li>
 *  <li> Start or stop the timer clock using TC_Start() and TC_Stop().</li>
 *
 * </ul>
 * For more accurate information, please look at the TC section of the Datasheet.
 *
 * Related files :\n
 * \ref tc.c\n
 * \ref tc.h.\n
*/

/**
*  \file
*
*  \section Purpose
*
*  Interface for configuring and using Timer Counter (TC) peripherals.
*
*  \section Usage
*  -# Optionally, use TC_FindMckDivisor() to let the program find the best
*     TCCLKS field value automatically.
*  -# Configure a Timer Counter in the desired mode using TC_Configure().
*  -# Start or stop the timer clock using TC_Start() and TC_Stop().
*/

/**
 * \file
 *
 * Implementation of Timer Counter (TC).
 *
 */

/*------------------------------------------------------------------------------
 *         Headers
 *-----------------------------------------------------------------------------*/

#ifndef __rtems__
#include "board.h"
#else /* __rtems__ */
#include <chip.h>
#endif /* __rtems__ */

#include <assert.h>

/*------------------------------------------------------------------------------
 *         Global functions
 *----------------------------------------------------------------------------*/

/**
 * \brief Configures a Timer Counter Channel
 *
 * Configures a Timer Counter to operate in the given mode. Timer is stopped
 * after configuration and must be restarted with TC_Start(). All the
 * interrupts of the timer are also disabled.
 *
 * \param pTc  Pointer to a Tc instance.
 * \param channel Channel number.
 * \param mode  Operating mode (TC_CMR value).
 */
extern void TC_Configure(Tc *pTc, uint32_t dwChannel, uint32_t dwMode)
{
	TcChannel *pTcCh;

	assert(dwChannel < (sizeof(pTc->TC_CHANNEL) / sizeof(
							 pTc->TC_CHANNEL[0])));
	pTcCh = pTc->TC_CHANNEL + dwChannel;

	/*  Disable TC clock */
	pTcCh->TC_CCR = TC_CCR_CLKDIS;

	/*  Disable interrupts */
	pTcCh->TC_IDR = 0xFFFFFFFF;

	/*  Clear status register */
	pTcCh->TC_SR;

	/*  Set mode */
	pTcCh->TC_CMR = dwMode;
}

/**
 * \brief Reset and Start the TC Channel
 *
 * Enables the timer clock and performs a software reset to start the counting.
 *
 * \param pTc  Pointer to a Tc instance.
 * \param dwChannel Channel number.
 */
extern void TC_Start(Tc *pTc, uint32_t dwChannel)
{
	TcChannel *pTcCh;

	assert(dwChannel < (sizeof(pTc->TC_CHANNEL) / sizeof(
							 pTc->TC_CHANNEL[0])));

	pTcCh = pTc->TC_CHANNEL + dwChannel;
	pTcCh->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
}

/**
 * \brief Stop TC Channel
 *
 * Disables the timer clock, stopping the counting.
 *
 * \param pTc     Pointer to a Tc instance.
 * \param dwChannel Channel number.
 */
extern void TC_Stop(Tc *pTc, uint32_t dwChannel)
{
	TcChannel *pTcCh;

	assert(dwChannel < (sizeof(pTc->TC_CHANNEL) / sizeof(
							 pTc->TC_CHANNEL[0])));

	pTcCh = pTc->TC_CHANNEL + dwChannel;
	pTcCh->TC_CCR = TC_CCR_CLKDIS;
}

/**
 * \brief Find best MCK divisor
 *
 * Finds the best MCK divisor given the timer frequency and MCK. The result
 * is guaranteed to satisfy the following equation:
 * \code
 *   (MCK / (DIV * 65536)) <= freq <= (MCK / DIV)
 * \endcode
 * with DIV being the highest possible value.
 *
 * \param dwFreq  Desired timer frequency.
 * \param dwMCk  Master clock frequency.
 * \param dwDiv  Divisor value.
 * \param dwTcClks  TCCLKS field value for divisor.
 * \param dwBoardMCK  Board clock frequency.
 *
 * \return 1 if a proper divisor has been found, otherwise 0.
 */
extern uint32_t TC_FindMckDivisor(uint32_t dwFreq, uint32_t dwMCk,
								   uint32_t *dwDiv, uint32_t *dwTcClks, uint32_t dwBoardMCK)
{
	/*
	TCCLKS:
	    0 - PCK6: default source - Slow Clock; update according to actual case
	    4 - SLCK: (slow clock) is either "Embedded 32kHz RC Oscillator" or
	              "32768Hz Crystal Oscillator"
	*/
	const uint32_t adwDivisors[5] = { BOARD_MCK / 32768, 8, 32, 128, BOARD_MCK / 32768 };

	uint32_t dwIndex = 0;
	dwBoardMCK = dwBoardMCK;

	/*  Satisfy lower bound */
	while (dwFreq < ((dwMCk / adwDivisors[dwIndex]) / 65536)) {
		dwIndex++;

		/*  If no divisor can be found, return 0 */
		if (dwIndex == (sizeof(adwDivisors) / sizeof(adwDivisors[0])))
			return 0;
	}

	/*  Try to maximize DIV while satisfying upper bound */
	while (dwIndex < 4) {
		if (dwFreq > (dwMCk / adwDivisors[dwIndex + 1]))
			break;

		dwIndex++;
	}

	/*  Store results */
	if (dwDiv)
		*dwDiv = adwDivisors[dwIndex];

	if (dwTcClks)
		*dwTcClks = dwIndex;

	return 1;
}