1 /******************************************************************************
3 * Copyright (C) 2010 - 2017 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
36 * @addtogroup ttcps_v3_5
39 * This file contains the implementation of the XTtcPs driver. This driver
40 * controls the operation of one timer counter in the Triple Timer Counter (TTC)
41 * module in the Ps block. Refer to xttcps.h for more detailed description
45 * MODIFICATION HISTORY:
47 * Ver Who Date Changes
48 * ----- ------ -------- -------------------------------------------------
49 * 1.00a drg/jz 01/21/10 First release
50 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
51 * 3.01 pkp 01/30/16 Modified XTtcPs_CfgInitialize to add XTtcps_Stop
52 * to stop the timer before configuring
53 * 3.2 mus 10/28/16 Modified XTtcPs_CalcIntervalFromFreq to calculate
54 * 32 bit interval count for zynq ultrascale+mpsoc
55 * 3.5 srm 10/06/17 Updated XTtcPs_GetMatchValue and XTtcPs_SetMatchValue
56 * APIs to use correct match register width for zynq
57 * (i.e. 16 bit) and zynq ultrascale+mpsoc (i.e. 32 bit).
61 ******************************************************************************/
63 /***************************** Include Files *********************************/
67 /************************** Constant Definitions *****************************/
69 /**************************** Type Definitions *******************************/
71 /***************** Macros (Inline Functions) Definitions *********************/
73 /************************** Function Prototypes ******************************/
75 /************************** Variable Definitions *****************************/
78 /*****************************************************************************/
81 * Initializes a specific XTtcPs instance such that the driver is ready to use.
82 * This function initializes a single timer counter in the triple timer counter
85 * The state of the device after initialization is:
87 * - Internal (pclk) selected
89 * - All Interrupts disabled
90 * - Output waveforms disabled
92 * @param InstancePtr is a pointer to the XTtcPs instance.
93 * @param ConfigPtr is a reference to a structure containing information
94 * about a specific TTC device.
95 * @param EffectiveAddr is the device base address in the virtual memory
96 * address space. The caller is responsible for keeping the address
97 * mapping from EffectiveAddr to the device physical base address
98 * unchanged once this function is invoked. Unexpected errors may
99 * occur if the address mapping changes after this function is
100 * called. If address translation is not used, then use
101 * ConfigPtr->BaseAddress for this parameter, passing the physical
106 * - XST_SUCCESS if the initialization is successful.
107 * - XST_DEVICE_IS_STARTED if the device is started. It must be
108 * stopped to re-initialize.
110 * @note Device has to be stopped first to call this function to
113 ******************************************************************************/
114 s32 XTtcPs_CfgInitialize(XTtcPs *InstancePtr, XTtcPs_Config *ConfigPtr,
120 * Assert to validate input arguments.
122 Xil_AssertNonvoid(InstancePtr != NULL);
123 Xil_AssertNonvoid(ConfigPtr != NULL);
126 * Set some default values
128 InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
129 InstancePtr->Config.BaseAddress = EffectiveAddr;
130 InstancePtr->Config.InputClockHz = ConfigPtr->InputClockHz;
132 IsStartResult = XTtcPs_IsStarted(InstancePtr);
134 * If the timer counter has already started, return an error
135 * Device should be stopped first.
137 if(IsStartResult == (u32)TRUE) {
138 Status = XST_DEVICE_IS_STARTED;
142 * stop the timer before configuring
144 XTtcPs_Stop(InstancePtr);
146 * Reset the count control register to it's default value.
148 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
149 XTTCPS_CNT_CNTRL_OFFSET,
150 XTTCPS_CNT_CNTRL_RESET_VALUE);
153 * Reset the rest of the registers to the default values.
155 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
156 XTTCPS_CLK_CNTRL_OFFSET, 0x00U);
157 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
158 XTTCPS_INTERVAL_VAL_OFFSET, 0x00U);
159 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
160 XTTCPS_MATCH_1_OFFSET, 0x00U);
161 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
162 XTTCPS_MATCH_2_OFFSET, 0x00U);
163 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
164 XTTCPS_MATCH_2_OFFSET, 0x00U);
165 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
166 XTTCPS_IER_OFFSET, 0x00U);
167 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
168 XTTCPS_ISR_OFFSET, XTTCPS_IXR_ALL_MASK);
170 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
173 * Reset the counter value
175 XTtcPs_ResetCounterValue(InstancePtr);
176 Status = XST_SUCCESS;
181 /*****************************************************************************/
184 * This function is used to set the match registers. There are three match
187 * The match 0 register is special. If the waveform output mode is enabled, the
188 * waveform will change polarity when the count matches the value in the match 0
189 * register. The polarity of the waveform output can also be set using the
190 * XTtcPs_SetOptions() function.
192 * @param InstancePtr is a pointer to the XTtcPs instance.
193 * @param MatchIndex is the index to the match register to be set.
194 * Valid values are 0, 1, or 2.
195 * @param Value is the 16-bit value to be set in the match register.
201 ****************************************************************************/
202 void XTtcPs_SetMatchValue(XTtcPs *InstancePtr, u8 MatchIndex, XMatchRegValue Value)
205 * Assert to validate input arguments.
207 Xil_AssertVoid(InstancePtr != NULL);
208 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
209 Xil_AssertVoid(MatchIndex < (u8)XTTCPS_NUM_MATCH_REG);
212 * Write the value to the correct match register with MatchIndex
214 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
215 XTtcPs_Match_N_Offset(MatchIndex), Value);
218 /*****************************************************************************/
221 * This function is used to get the value of the match registers. There are
222 * three match registers.
224 * @param InstancePtr is a pointer to the XTtcPs instance.
225 * @param MatchIndex is the index to the match register to be set.
226 * Valid values are 0, 1, or 2.
228 * @return The match register value
232 ****************************************************************************/
233 XMatchRegValue XTtcPs_GetMatchValue(XTtcPs *InstancePtr, u8 MatchIndex)
238 * Assert to validate input arguments.
240 Xil_AssertNonvoid(InstancePtr != NULL);
241 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
242 Xil_AssertNonvoid(MatchIndex < XTTCPS_NUM_MATCH_REG);
244 MatchReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
245 XTtcPs_Match_N_Offset(MatchIndex));
247 return (XMatchRegValue) MatchReg;
250 /*****************************************************************************/
253 * This function sets the prescaler enable bit and if needed sets the prescaler
254 * bits in the control register.
256 * @param InstancePtr is a pointer to the XTtcPs instance.
257 * @param PrescalerValue is a number from 0-16 that sets the prescaler
259 * If the parameter is 0 - 15, use a prescaler on the clock of
260 * 2^(PrescalerValue+1), or 2-65536.
261 * If the parameter is XTTCPS_CLK_CNTRL_PS_DISABLE, do not use a
268 ****************************************************************************/
269 void XTtcPs_SetPrescaler(XTtcPs *InstancePtr, u8 PrescalerValue)
274 * Assert to validate input arguments.
276 Xil_AssertVoid(InstancePtr != NULL);
277 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
278 Xil_AssertVoid(PrescalerValue <= XTTCPS_CLK_CNTRL_PS_DISABLE);
281 * Read the clock control register
283 ClockReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
284 XTTCPS_CLK_CNTRL_OFFSET);
287 * Clear all of the prescaler control bits in the register
290 ~(XTTCPS_CLK_CNTRL_PS_VAL_MASK | XTTCPS_CLK_CNTRL_PS_EN_MASK);
292 if (PrescalerValue < XTTCPS_CLK_CNTRL_PS_DISABLE) {
294 * Set the prescaler value and enable prescaler
296 ClockReg |= (u32)(((u32)PrescalerValue << (u32)XTTCPS_CLK_CNTRL_PS_VAL_SHIFT) &
297 (u32)XTTCPS_CLK_CNTRL_PS_VAL_MASK);
298 ClockReg |= (u32)XTTCPS_CLK_CNTRL_PS_EN_MASK;
302 * Write the register with the new values.
304 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
305 XTTCPS_CLK_CNTRL_OFFSET, ClockReg);
308 /*****************************************************************************/
311 * This function gets the input clock prescaler
313 * @param InstancePtr is a pointer to the XTtcPs instance.
316 * @return The value(n) from which the prescalar value is calculated
317 * as 2^(n+1). Some example values are given below :
329 ****************************************************************************/
330 u8 XTtcPs_GetPrescaler(XTtcPs *InstancePtr)
336 * Assert to validate input arguments.
338 Xil_AssertNonvoid(InstancePtr != NULL);
339 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
342 * Read the clock control register
344 ClockReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
345 XTTCPS_CLK_CNTRL_OFFSET);
347 if (0 == (ClockReg & XTTCPS_CLK_CNTRL_PS_EN_MASK)) {
349 * Prescaler is disabled. Return the correct flag value
351 Status = (u8)XTTCPS_CLK_CNTRL_PS_DISABLE;
355 Status = (u8)((ClockReg & (u32)XTTCPS_CLK_CNTRL_PS_VAL_MASK) >>
356 (u32)XTTCPS_CLK_CNTRL_PS_VAL_SHIFT);
361 /*****************************************************************************/
364 * This function calculates the interval value as well as the prescaler value
365 * for a given frequency.
367 * @param InstancePtr is a pointer to the XTtcPs instance.
368 * @param Freq is the requested output frequency for the device.
369 * @param Interval is the interval value for the given frequency,
370 * it is the output value for this function.
371 * @param Prescaler is the prescaler value for the given frequency,
372 * it is the output value for this function.
377 * Upon successful calculation for the given frequency, Interval and Prescaler
378 * carry the settings for the timer counter; Upon unsuccessful calculation,
379 * Interval and Prescaler are set to 0xFF(FF) for their maximum values to
380 * signal the caller of failure. Therefore, caller needs to check the return
381 * interval or prescaler values for whether the function has succeeded.
383 ****************************************************************************/
384 void XTtcPs_CalcIntervalFromFreq(XTtcPs *InstancePtr, u32 Freq,
385 XInterval *Interval, u8 *Prescaler)
391 InputClock = InstancePtr->Config.InputClockHz;
393 * Find the smallest prescaler that will work for a given frequency. The
394 * smaller the prescaler, the larger the count and the more accurate the
397 TempValue = InputClock/ Freq;
399 if (TempValue < 4U) {
401 * The frequency is too high, it is too close to the input
402 * clock value. Use maximum values to signal caller.
404 *Interval = XTTCPS_MAX_INTERVAL_COUNT;
410 * First, do we need a prescaler or not?
412 if (((u32)65536U) > TempValue) {
414 * We do not need a prescaler, so set the values appropriately
416 *Interval = (XInterval)TempValue;
417 *Prescaler = XTTCPS_CLK_CNTRL_PS_DISABLE;
422 for (TmpPrescaler = 0U; TmpPrescaler < XTTCPS_CLK_CNTRL_PS_DISABLE;
424 TempValue = InputClock/ (Freq * (1U << (TmpPrescaler + 1U)));
427 * The first value less than 2^16 is the best bet
429 if (((u32)65536U) > TempValue) {
431 * Set the values appropriately
433 *Interval = (XInterval)TempValue;
434 *Prescaler = TmpPrescaler;
439 /* Can not find interval values that work for the given frequency.
440 * Return maximum values to signal caller.
442 *Interval = XTTCPS_MAX_INTERVAL_COUNT;