1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 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 /*****************************************************************************/
37 * This file contains the implementation of the XTtcPs driver. This driver
38 * controls the operation of one timer counter in the Triple Timer Counter (TTC)
39 * module in the Ps block. Refer to xttcps.h for more detailed description
43 * MODIFICATION HISTORY:
45 * Ver Who Date Changes
46 * ----- ------ -------- -------------------------------------------------
47 * 1.00a drg/jz 01/21/10 First release
48 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
52 ******************************************************************************/
54 /***************************** Include Files *********************************/
58 /************************** Constant Definitions *****************************/
60 /**************************** Type Definitions *******************************/
62 /***************** Macros (Inline Functions) Definitions *********************/
64 /************************** Function Prototypes ******************************/
66 /************************** Variable Definitions *****************************/
69 /*****************************************************************************/
72 * Initializes a specific XTtcPs instance such that the driver is ready to use.
73 * This function initializes a single timer counter in the triple timer counter
76 * The state of the device after initialization is:
78 * - Internal (pclk) selected
80 * - All Interrupts disabled
81 * - Output waveforms disabled
83 * @param InstancePtr is a pointer to the XTtcPs instance.
84 * @param ConfigPtr is a reference to a structure containing information
85 * about a specific TTC device.
86 * @param EffectiveAddr is the device base address in the virtual memory
87 * address space. The caller is responsible for keeping the address
88 * mapping from EffectiveAddr to the device physical base address
89 * unchanged once this function is invoked. Unexpected errors may
90 * occur if the address mapping changes after this function is
91 * called. If address translation is not used, then use
92 * ConfigPtr->BaseAddress for this parameter, passing the physical
97 * - XST_SUCCESS if the initialization is successful.
98 * - XST_DEVICE_IS_STARTED if the device is started. It must be
99 * stopped to re-initialize.
101 * @note Device has to be stopped first to call this function to
104 ******************************************************************************/
105 s32 XTtcPs_CfgInitialize(XTtcPs *InstancePtr, XTtcPs_Config *ConfigPtr,
111 * Assert to validate input arguments.
113 Xil_AssertNonvoid(InstancePtr != NULL);
114 Xil_AssertNonvoid(ConfigPtr != NULL);
117 * Set some default values
119 InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
120 InstancePtr->Config.BaseAddress = EffectiveAddr;
121 InstancePtr->Config.InputClockHz = ConfigPtr->InputClockHz;
123 IsStartResult = XTtcPs_IsStarted(InstancePtr);
125 * If the timer counter has already started, return an error
126 * Device should be stopped first.
128 if(IsStartResult == (u32)TRUE) {
129 Status = XST_DEVICE_IS_STARTED;
132 * Reset the count control register to it's default value.
134 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
135 XTTCPS_CNT_CNTRL_OFFSET,
136 XTTCPS_CNT_CNTRL_RESET_VALUE);
139 * Reset the rest of the registers to the default values.
141 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
142 XTTCPS_CLK_CNTRL_OFFSET, 0x00U);
143 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
144 XTTCPS_INTERVAL_VAL_OFFSET, 0x00U);
145 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
146 XTTCPS_MATCH_1_OFFSET, 0x00U);
147 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
148 XTTCPS_MATCH_2_OFFSET, 0x00U);
149 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
150 XTTCPS_MATCH_2_OFFSET, 0x00U);
151 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
152 XTTCPS_IER_OFFSET, 0x00U);
153 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
154 XTTCPS_ISR_OFFSET, XTTCPS_IXR_ALL_MASK);
156 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
159 * Reset the counter value
161 XTtcPs_ResetCounterValue(InstancePtr);
162 Status = XST_SUCCESS;
167 /*****************************************************************************/
170 * This function is used to set the match registers. There are three match
173 * The match 0 register is special. If the waveform output mode is enabled, the
174 * waveform will change polarity when the count matches the value in the match 0
175 * register. The polarity of the waveform output can also be set using the
176 * XTtcPs_SetOptions() function.
178 * @param InstancePtr is a pointer to the XTtcPs instance.
179 * @param MatchIndex is the index to the match register to be set.
180 * Valid values are 0, 1, or 2.
181 * @param Value is the 16-bit value to be set in the match register.
187 ****************************************************************************/
188 void XTtcPs_SetMatchValue(XTtcPs *InstancePtr, u8 MatchIndex, u16 Value)
191 * Assert to validate input arguments.
193 Xil_AssertVoid(InstancePtr != NULL);
194 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
195 Xil_AssertVoid(MatchIndex < (u8)XTTCPS_NUM_MATCH_REG);
198 * Write the value to the correct match register with MatchIndex
200 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
201 XTtcPs_Match_N_Offset(MatchIndex), Value);
204 /*****************************************************************************/
207 * This function is used to get the value of the match registers. There are
208 * three match registers.
210 * @param InstancePtr is a pointer to the XTtcPs instance.
211 * @param MatchIndex is the index to the match register to be set.
212 * Valid values are 0, 1, or 2.
218 ****************************************************************************/
219 u16 XTtcPs_GetMatchValue(XTtcPs *InstancePtr, u8 MatchIndex)
224 * Assert to validate input arguments.
226 Xil_AssertNonvoid(InstancePtr != NULL);
227 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
228 Xil_AssertNonvoid(MatchIndex < XTTCPS_NUM_MATCH_REG);
230 MatchReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
231 XTtcPs_Match_N_Offset(MatchIndex));
233 return (u16) MatchReg;
236 /*****************************************************************************/
239 * This function sets the prescaler enable bit and if needed sets the prescaler
240 * bits in the control register.
242 * @param InstancePtr is a pointer to the XTtcPs instance.
243 * @param PrescalerValue is a number from 0-16 that sets the prescaler
245 * If the parameter is 0 - 15, use a prescaler on the clock of
246 * 2^(PrescalerValue+1), or 2-65536.
247 * If the parameter is XTTCPS_CLK_CNTRL_PS_DISABLE, do not use a
254 ****************************************************************************/
255 void XTtcPs_SetPrescaler(XTtcPs *InstancePtr, u8 PrescalerValue)
260 * Assert to validate input arguments.
262 Xil_AssertVoid(InstancePtr != NULL);
263 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
264 Xil_AssertVoid(PrescalerValue <= XTTCPS_CLK_CNTRL_PS_DISABLE);
267 * Read the clock control register
269 ClockReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
270 XTTCPS_CLK_CNTRL_OFFSET);
273 * Clear all of the prescaler control bits in the register
276 ~(XTTCPS_CLK_CNTRL_PS_VAL_MASK | XTTCPS_CLK_CNTRL_PS_EN_MASK);
278 if (PrescalerValue < XTTCPS_CLK_CNTRL_PS_DISABLE) {
280 * Set the prescaler value and enable prescaler
282 ClockReg |= (u32)(((u32)PrescalerValue << (u32)XTTCPS_CLK_CNTRL_PS_VAL_SHIFT) &
283 (u32)XTTCPS_CLK_CNTRL_PS_VAL_MASK);
284 ClockReg |= (u32)XTTCPS_CLK_CNTRL_PS_EN_MASK;
288 * Write the register with the new values.
290 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
291 XTTCPS_CLK_CNTRL_OFFSET, ClockReg);
294 /*****************************************************************************/
297 * This function gets the input clock prescaler
299 * @param InstancePtr is a pointer to the XTtcPs instance.
302 * @return The value(n) from which the prescalar value is calculated
303 * as 2^(n+1). Some example values are given below :
315 ****************************************************************************/
316 u8 XTtcPs_GetPrescaler(XTtcPs *InstancePtr)
322 * Assert to validate input arguments.
324 Xil_AssertNonvoid(InstancePtr != NULL);
325 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
328 * Read the clock control register
330 ClockReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
331 XTTCPS_CLK_CNTRL_OFFSET);
333 if (0 == (ClockReg & XTTCPS_CLK_CNTRL_PS_EN_MASK)) {
335 * Prescaler is disabled. Return the correct flag value
337 Status = (u8)XTTCPS_CLK_CNTRL_PS_DISABLE;
341 Status = (u8)((ClockReg & (u32)XTTCPS_CLK_CNTRL_PS_VAL_MASK) >>
342 (u32)XTTCPS_CLK_CNTRL_PS_VAL_SHIFT);
347 /*****************************************************************************/
350 * This function calculates the interval value as well as the prescaler value
351 * for a given frequency.
353 * @param InstancePtr is a pointer to the XTtcPs instance.
354 * @param Freq is the requested output frequency for the device.
355 * @param Interval is the interval value for the given frequency,
356 * it is the output value for this function.
357 * @param Prescaler is the prescaler value for the given frequency,
358 * it is the output value for this function.
363 * Upon successful calculation for the given frequency, Interval and Prescaler
364 * carry the settings for the timer counter; Upon unsuccessful calculation,
365 * Interval and Prescaler are set to 0xFF(FF) for their maximum values to
366 * signal the caller of failure. Therefore, caller needs to check the return
367 * interval or prescaler values for whether the function has succeeded.
369 ****************************************************************************/
370 void XTtcPs_CalcIntervalFromFreq(XTtcPs *InstancePtr, u32 Freq,
371 u16 *Interval, u8 *Prescaler)
377 InputClock = InstancePtr->Config.InputClockHz;
379 * Find the smallest prescaler that will work for a given frequency. The
380 * smaller the prescaler, the larger the count and the more accurate the
383 TempValue = InputClock/ Freq;
385 if (TempValue < 4U) {
387 * The frequency is too high, it is too close to the input
388 * clock value. Use maximum values to signal caller.
396 * First, do we need a prescaler or not?
398 if (((u32)65536U) > TempValue) {
400 * We do not need a prescaler, so set the values appropriately
402 *Interval = (u16)TempValue;
403 *Prescaler = XTTCPS_CLK_CNTRL_PS_DISABLE;
408 for (TmpPrescaler = 0U; TmpPrescaler < XTTCPS_CLK_CNTRL_PS_DISABLE;
410 TempValue = InputClock/ (Freq * (1U << (TmpPrescaler + 1U)));
413 * The first value less than 2^16 is the best bet
415 if (((u32)65536U) > TempValue) {
417 * Set the values appropriately
419 *Interval = (u16)TempValue;
420 *Prescaler = TmpPrescaler;
425 /* Can not find interval values that work for the given frequency.
426 * Return maximum values to signal caller.