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 /*****************************************************************************/
36 * @addtogroup ttcps_v3_0
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
56 ******************************************************************************/
58 /***************************** Include Files *********************************/
62 /************************** Constant Definitions *****************************/
64 /**************************** Type Definitions *******************************/
66 /***************** Macros (Inline Functions) Definitions *********************/
68 /************************** Function Prototypes ******************************/
70 /************************** Variable Definitions *****************************/
73 /*****************************************************************************/
76 * Initializes a specific XTtcPs instance such that the driver is ready to use.
77 * This function initializes a single timer counter in the triple timer counter
80 * The state of the device after initialization is:
82 * - Internal (pclk) selected
84 * - All Interrupts disabled
85 * - Output waveforms disabled
87 * @param InstancePtr is a pointer to the XTtcPs instance.
88 * @param ConfigPtr is a reference to a structure containing information
89 * about a specific TTC device.
90 * @param EffectiveAddr is the device base address in the virtual memory
91 * address space. The caller is responsible for keeping the address
92 * mapping from EffectiveAddr to the device physical base address
93 * unchanged once this function is invoked. Unexpected errors may
94 * occur if the address mapping changes after this function is
95 * called. If address translation is not used, then use
96 * ConfigPtr->BaseAddress for this parameter, passing the physical
101 * - XST_SUCCESS if the initialization is successful.
102 * - XST_DEVICE_IS_STARTED if the device is started. It must be
103 * stopped to re-initialize.
105 * @note Device has to be stopped first to call this function to
108 ******************************************************************************/
109 s32 XTtcPs_CfgInitialize(XTtcPs *InstancePtr, XTtcPs_Config *ConfigPtr,
115 * Assert to validate input arguments.
117 Xil_AssertNonvoid(InstancePtr != NULL);
118 Xil_AssertNonvoid(ConfigPtr != NULL);
121 * Set some default values
123 InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
124 InstancePtr->Config.BaseAddress = EffectiveAddr;
125 InstancePtr->Config.InputClockHz = ConfigPtr->InputClockHz;
127 IsStartResult = XTtcPs_IsStarted(InstancePtr);
129 * If the timer counter has already started, return an error
130 * Device should be stopped first.
132 if(IsStartResult == (u32)TRUE) {
133 Status = XST_DEVICE_IS_STARTED;
137 * stop the timer before configuring
139 XTtcPs_Stop(InstancePtr);
141 * Reset the count control register to it's default value.
143 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
144 XTTCPS_CNT_CNTRL_OFFSET,
145 XTTCPS_CNT_CNTRL_RESET_VALUE);
148 * Reset the rest of the registers to the default values.
150 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
151 XTTCPS_CLK_CNTRL_OFFSET, 0x00U);
152 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
153 XTTCPS_INTERVAL_VAL_OFFSET, 0x00U);
154 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
155 XTTCPS_MATCH_1_OFFSET, 0x00U);
156 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
157 XTTCPS_MATCH_2_OFFSET, 0x00U);
158 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
159 XTTCPS_MATCH_2_OFFSET, 0x00U);
160 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
161 XTTCPS_IER_OFFSET, 0x00U);
162 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
163 XTTCPS_ISR_OFFSET, XTTCPS_IXR_ALL_MASK);
165 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
168 * Reset the counter value
170 XTtcPs_ResetCounterValue(InstancePtr);
171 Status = XST_SUCCESS;
176 /*****************************************************************************/
179 * This function is used to set the match registers. There are three match
182 * The match 0 register is special. If the waveform output mode is enabled, the
183 * waveform will change polarity when the count matches the value in the match 0
184 * register. The polarity of the waveform output can also be set using the
185 * XTtcPs_SetOptions() function.
187 * @param InstancePtr is a pointer to the XTtcPs instance.
188 * @param MatchIndex is the index to the match register to be set.
189 * Valid values are 0, 1, or 2.
190 * @param Value is the 16-bit value to be set in the match register.
196 ****************************************************************************/
197 void XTtcPs_SetMatchValue(XTtcPs *InstancePtr, u8 MatchIndex, u16 Value)
200 * Assert to validate input arguments.
202 Xil_AssertVoid(InstancePtr != NULL);
203 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
204 Xil_AssertVoid(MatchIndex < (u8)XTTCPS_NUM_MATCH_REG);
207 * Write the value to the correct match register with MatchIndex
209 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
210 XTtcPs_Match_N_Offset(MatchIndex), Value);
213 /*****************************************************************************/
216 * This function is used to get the value of the match registers. There are
217 * three match registers.
219 * @param InstancePtr is a pointer to the XTtcPs instance.
220 * @param MatchIndex is the index to the match register to be set.
221 * Valid values are 0, 1, or 2.
227 ****************************************************************************/
228 u16 XTtcPs_GetMatchValue(XTtcPs *InstancePtr, u8 MatchIndex)
233 * Assert to validate input arguments.
235 Xil_AssertNonvoid(InstancePtr != NULL);
236 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
237 Xil_AssertNonvoid(MatchIndex < XTTCPS_NUM_MATCH_REG);
239 MatchReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
240 XTtcPs_Match_N_Offset(MatchIndex));
242 return (u16) MatchReg;
245 /*****************************************************************************/
248 * This function sets the prescaler enable bit and if needed sets the prescaler
249 * bits in the control register.
251 * @param InstancePtr is a pointer to the XTtcPs instance.
252 * @param PrescalerValue is a number from 0-16 that sets the prescaler
254 * If the parameter is 0 - 15, use a prescaler on the clock of
255 * 2^(PrescalerValue+1), or 2-65536.
256 * If the parameter is XTTCPS_CLK_CNTRL_PS_DISABLE, do not use a
263 ****************************************************************************/
264 void XTtcPs_SetPrescaler(XTtcPs *InstancePtr, u8 PrescalerValue)
269 * Assert to validate input arguments.
271 Xil_AssertVoid(InstancePtr != NULL);
272 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
273 Xil_AssertVoid(PrescalerValue <= XTTCPS_CLK_CNTRL_PS_DISABLE);
276 * Read the clock control register
278 ClockReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
279 XTTCPS_CLK_CNTRL_OFFSET);
282 * Clear all of the prescaler control bits in the register
285 ~(XTTCPS_CLK_CNTRL_PS_VAL_MASK | XTTCPS_CLK_CNTRL_PS_EN_MASK);
287 if (PrescalerValue < XTTCPS_CLK_CNTRL_PS_DISABLE) {
289 * Set the prescaler value and enable prescaler
291 ClockReg |= (u32)(((u32)PrescalerValue << (u32)XTTCPS_CLK_CNTRL_PS_VAL_SHIFT) &
292 (u32)XTTCPS_CLK_CNTRL_PS_VAL_MASK);
293 ClockReg |= (u32)XTTCPS_CLK_CNTRL_PS_EN_MASK;
297 * Write the register with the new values.
299 XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
300 XTTCPS_CLK_CNTRL_OFFSET, ClockReg);
303 /*****************************************************************************/
306 * This function gets the input clock prescaler
308 * @param InstancePtr is a pointer to the XTtcPs instance.
311 * @return The value(n) from which the prescalar value is calculated
312 * as 2^(n+1). Some example values are given below :
324 ****************************************************************************/
325 u8 XTtcPs_GetPrescaler(XTtcPs *InstancePtr)
331 * Assert to validate input arguments.
333 Xil_AssertNonvoid(InstancePtr != NULL);
334 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
337 * Read the clock control register
339 ClockReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
340 XTTCPS_CLK_CNTRL_OFFSET);
342 if (0 == (ClockReg & XTTCPS_CLK_CNTRL_PS_EN_MASK)) {
344 * Prescaler is disabled. Return the correct flag value
346 Status = (u8)XTTCPS_CLK_CNTRL_PS_DISABLE;
350 Status = (u8)((ClockReg & (u32)XTTCPS_CLK_CNTRL_PS_VAL_MASK) >>
351 (u32)XTTCPS_CLK_CNTRL_PS_VAL_SHIFT);
356 /*****************************************************************************/
359 * This function calculates the interval value as well as the prescaler value
360 * for a given frequency.
362 * @param InstancePtr is a pointer to the XTtcPs instance.
363 * @param Freq is the requested output frequency for the device.
364 * @param Interval is the interval value for the given frequency,
365 * it is the output value for this function.
366 * @param Prescaler is the prescaler value for the given frequency,
367 * it is the output value for this function.
372 * Upon successful calculation for the given frequency, Interval and Prescaler
373 * carry the settings for the timer counter; Upon unsuccessful calculation,
374 * Interval and Prescaler are set to 0xFF(FF) for their maximum values to
375 * signal the caller of failure. Therefore, caller needs to check the return
376 * interval or prescaler values for whether the function has succeeded.
378 ****************************************************************************/
379 void XTtcPs_CalcIntervalFromFreq(XTtcPs *InstancePtr, u32 Freq,
380 u16 *Interval, u8 *Prescaler)
386 InputClock = InstancePtr->Config.InputClockHz;
388 * Find the smallest prescaler that will work for a given frequency. The
389 * smaller the prescaler, the larger the count and the more accurate the
392 TempValue = InputClock/ Freq;
394 if (TempValue < 4U) {
396 * The frequency is too high, it is too close to the input
397 * clock value. Use maximum values to signal caller.
405 * First, do we need a prescaler or not?
407 if (((u32)65536U) > TempValue) {
409 * We do not need a prescaler, so set the values appropriately
411 *Interval = (u16)TempValue;
412 *Prescaler = XTTCPS_CLK_CNTRL_PS_DISABLE;
417 for (TmpPrescaler = 0U; TmpPrescaler < XTTCPS_CLK_CNTRL_PS_DISABLE;
419 TempValue = InputClock/ (Freq * (1U << (TmpPrescaler + 1U)));
422 * The first value less than 2^16 is the best bet
424 if (((u32)65536U) > TempValue) {
426 * Set the values appropriately
428 *Interval = (u16)TempValue;
429 *Prescaler = TmpPrescaler;
434 /* Can not find interval values that work for the given frequency.
435 * Return maximum values to signal caller.