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
40 * This is the driver for one 16-bit timer counter in the Triple Timer Counter
41 * (TTC) module in the Ps block.
43 * The TTC module provides three independent timer/counter modules that can each
44 * be clocked using either the system clock (pclk) or an externally driven
45 * clock (ext_clk). In addition, each counter can independently prescale its
46 * selected clock input (divided by 2 to 65536). Counters can be set to
47 * decrement or increment.
49 * Each of the counters can be programmed to generate interrupt pulses:
50 * . At a regular, predefined period, that is on a timed interval
51 * . When the counter registers overflow
52 * . When the count matches any one of the three 'match' registers
54 * Therefore, up to six different events can trigger a timer interrupt: three
55 * match interrupts, an overflow interrupt, an interval interrupt and an event
56 * timer interrupt. Note that the overflow interrupt and the interval interrupt
57 * are mutually exclusive.
59 * <b>Initialization & Configuration</b>
61 * An XTtcPs_Config structure is used to configure a driver instance.
62 * Information in the XTtcPs_Config structure is the hardware properties
65 * A driver instance is initialized through
66 * XTtcPs_CfgInitialize(InstancePtr, CfgPtr, EffectiveAddr). Where CfgPtr
67 * is a pointer to the XTtcPs_Config structure, it can be looked up statically
68 * through XTtcPs_LookupConfig(DeviceID), or passed in by the caller. The
69 * EffectiveAddr can be the static base address of the device or virtual
70 * mapped address if address translation is supported.
74 * Interrupt handler is not provided by the driver, as handling of interrupt
75 * is application specific.
78 * The default setting for a timer/counter is:
80 * - Internal clock (pclk) selected
82 * - All Interrupts disabled
83 * - Output waveforms disabled
86 * MODIFICATION HISTORY:
88 * Ver Who Date Changes
89 * ----- ------ -------- -----------------------------------------------------
90 * 1.00a drg/jz 01/20/10 First release..
91 * 2.0 adk 12/10/13 Updated as per the New Tcl API's
92 * 3.0 pkp 12/09/14 Added support for Zynq Ultrascale Mp.Also code
93 * modified for MISRA-C:2012 compliance.
96 ******************************************************************************/
98 #ifndef XTTCPS_H /* prevent circular inclusions */
99 #define XTTCPS_H /* by using protection macros */
105 /***************************** Include Files *********************************/
107 #include "xttcps_hw.h"
110 /************************** Constant Definitions *****************************/
112 /** @name Configuration options
114 * Options for the device. Each of the options is bit field, so more than one
115 * options can be specified.
119 #define XTTCPS_OPTION_EXTERNAL_CLK 0x00000001U /**< External clock source */
120 #define XTTCPS_OPTION_CLK_EDGE_NEG 0x00000002U /**< Clock on trailing edge for
122 #define XTTCPS_OPTION_INTERVAL_MODE 0x00000004U /**< Interval mode */
123 #define XTTCPS_OPTION_DECREMENT 0x00000008U /**< Decrement the counter */
124 #define XTTCPS_OPTION_MATCH_MODE 0x00000010U /**< Match mode */
125 #define XTTCPS_OPTION_WAVE_DISABLE 0x00000020U /**< No waveform output */
126 #define XTTCPS_OPTION_WAVE_POLARITY 0x00000040U /**< Waveform polarity */
129 /**************************** Type Definitions *******************************/
132 * This typedef contains configuration information for the device.
135 u16 DeviceId; /**< Unique ID for device */
136 u32 BaseAddress; /**< Base address for device */
137 u32 InputClockHz; /**< Input clock frequency */
141 * The XTtcPs driver instance data. The user is required to allocate a
142 * variable of this type for each PS timer/counter device in the system. A
143 * pointer to a variable of this type is then passed to various driver API
147 XTtcPs_Config Config; /**< Configuration structure */
148 u32 IsReady; /**< Device is initialized and ready */
152 /***************** Macros (Inline Functions) Definitions *********************/
155 * Internal helper macros
157 #define InstReadReg(InstancePtr, RegOffset) \
158 (Xil_In32(((InstancePtr)->Config.BaseAddress) + (u32)(RegOffset)))
160 #define InstWriteReg(InstancePtr, RegOffset, Data) \
161 (Xil_Out32(((InstancePtr)->Config.BaseAddress) + (u32)(RegOffset), (u32)(Data)))
163 /*****************************************************************************/
166 * This function starts the counter/timer without resetting the counter value.
168 * @param InstancePtr is a pointer to the XTtcPs instance.
172 * @note C-style signature:
173 * void XTtcPs_Start(XTtcPs *InstancePtr)
175 ****************************************************************************/
176 #define XTtcPs_Start(InstancePtr) \
177 InstWriteReg((InstancePtr), XTTCPS_CNT_CNTRL_OFFSET, \
178 (InstReadReg((InstancePtr), XTTCPS_CNT_CNTRL_OFFSET) & \
179 ~XTTCPS_CNT_CNTRL_DIS_MASK))
181 /*****************************************************************************/
184 * This function stops the counter/timer. This macro may be called at any time
185 * to stop the counter. The counter holds the last value until it is reset,
186 * restarted or enabled.
188 * @param InstancePtr is a pointer to the XTtcPs instance.
192 * @note C-style signature:
193 * void XTtcPs_Stop(XTtcPs *InstancePtr)
195 ****************************************************************************/
196 #define XTtcPs_Stop(InstancePtr) \
197 InstWriteReg((InstancePtr), XTTCPS_CNT_CNTRL_OFFSET, \
198 (InstReadReg((InstancePtr), XTTCPS_CNT_CNTRL_OFFSET) | \
199 XTTCPS_CNT_CNTRL_DIS_MASK))
201 /*****************************************************************************/
204 * This function checks whether the timer counter has already started.
206 * @param InstancePtr is a pointer to the XTtcPs instance
208 * @return Non-zero if the device has started, '0' otherwise.
210 * @note C-style signature:
211 * int XTtcPs_IsStarted(XTtcPs *InstancePtr)
213 ****************************************************************************/
214 #define XTtcPs_IsStarted(InstancePtr) \
215 ((InstReadReg((InstancePtr), XTTCPS_CNT_CNTRL_OFFSET) & \
216 XTTCPS_CNT_CNTRL_DIS_MASK) == 0U)
218 /*****************************************************************************/
221 * This function returns the current 16-bit counter value. It may be called at
224 * @param InstancePtr is a pointer to the XTtcPs instance.
226 * @return 16-bit counter value.
228 * @note C-style signature:
229 * u16 XTtcPs_GetCounterValue(XTtcPs *InstancePtr)
231 ****************************************************************************/
232 #define XTtcPs_GetCounterValue(InstancePtr) \
233 (u16)InstReadReg((InstancePtr), XTTCPS_COUNT_VALUE_OFFSET)
235 /*****************************************************************************/
238 * This function sets the interval value to be used in interval mode.
240 * @param InstancePtr is a pointer to the XTtcPs instance.
241 * @param Value is the 16-bit value to be set in the interval register.
245 * @note C-style signature:
246 * void XTtcPs_SetInterval(XTtcPs *InstancePtr, u16 Value)
248 ****************************************************************************/
249 #define XTtcPs_SetInterval(InstancePtr, Value) \
250 InstWriteReg((InstancePtr), XTTCPS_INTERVAL_VAL_OFFSET, (Value))
252 /*****************************************************************************/
255 * This function gets the interval value from the interval register.
257 * @param InstancePtr is a pointer to the XTtcPs instance.
259 * @return 16-bit interval value
261 * @note C-style signature:
262 * u16 XTtcPs_GetInterval(XTtcPs *InstancePtr)
264 ****************************************************************************/
265 #define XTtcPs_GetInterval(InstancePtr) \
266 (u16)InstReadReg((InstancePtr), XTTCPS_INTERVAL_VAL_OFFSET)
268 /*****************************************************************************/
271 * This macro resets the count register. It may be called at any time. The
272 * counter is reset to either 0 or 0xFFFF, or the interval value, depending on
273 * the increment/decrement mode. The state of the counter, as started or
274 * stopped, is not affected by calling reset.
276 * @param InstancePtr is a pointer to the XTtcPs instance.
280 * @note C-style signature:
281 * void XTtcPs_ResetCounterValue(XTtcPs *InstancePtr)
283 ****************************************************************************/
284 #define XTtcPs_ResetCounterValue(InstancePtr) \
285 InstWriteReg((InstancePtr), XTTCPS_CNT_CNTRL_OFFSET, \
286 (InstReadReg((InstancePtr), XTTCPS_CNT_CNTRL_OFFSET) | \
287 (u32)XTTCPS_CNT_CNTRL_RST_MASK))
289 /*****************************************************************************/
292 * This function enables the interrupts.
294 * @param InstancePtr is a pointer to the XTtcPs instance.
295 * @param InterruptMask defines which interrupt should be enabled.
296 * Constants are defined in xttcps_hw.h as XTTCPS_IXR_*.
297 * This is a bit mask, all set bits will be enabled, cleared bits
298 * will not be disabled.
304 * void XTtcPs_EnableInterrupts(XTtcPs *InstancePtr, u32 InterruptMask)
306 ******************************************************************************/
307 #define XTtcPs_EnableInterrupts(InstancePtr, InterruptMask) \
308 InstWriteReg((InstancePtr), XTTCPS_IER_OFFSET, \
309 (InstReadReg((InstancePtr), XTTCPS_IER_OFFSET) | \
312 /*****************************************************************************/
315 * This function disables the interrupts.
317 * @param InstancePtr is a pointer to the XTtcPs instance.
318 * @param InterruptMask defines which interrupt should be disabled.
319 * Constants are defined in xttcps_hw.h as XTTCPS_IXR_*.
320 * This is a bit mask, all set bits will be disabled, cleared bits
321 * will not be disabled.
327 * void XTtcPs_DisableInterrupts(XTtcPs *InstancePtr, u32 InterruptMask)
329 ******************************************************************************/
330 #define XTtcPs_DisableInterrupts(InstancePtr, InterruptMask) \
331 InstWriteReg((InstancePtr), XTTCPS_IER_OFFSET, \
332 (InstReadReg((InstancePtr), XTTCPS_IER_OFFSET) & \
335 /*****************************************************************************/
338 * This function reads the interrupt status.
340 * @param InstancePtr is a pointer to the XTtcPs instance.
344 * @note C-style signature:
345 * u32 XTtcPs_GetInterruptStatus(XTtcPs *InstancePtr)
347 ******************************************************************************/
348 #define XTtcPs_GetInterruptStatus(InstancePtr) \
349 InstReadReg((InstancePtr), XTTCPS_ISR_OFFSET)
351 /*****************************************************************************/
354 * This function clears the interrupt status.
356 * @param InstancePtr is a pointer to the XTtcPs instance.
357 * @param InterruptMask defines which interrupt should be cleared.
358 * Constants are defined in xttcps_hw.h as XTTCPS_IXR_*.
359 * This is a bit mask, all set bits will be cleared, cleared bits
360 * will not be cleared.
366 * void XTtcPs_ClearInterruptStatus(XTtcPs *InstancePtr, u32 InterruptMask)
368 ******************************************************************************/
369 #define XTtcPs_ClearInterruptStatus(InstancePtr, InterruptMask) \
370 InstWriteReg((InstancePtr), XTTCPS_ISR_OFFSET, \
374 /************************** Function Prototypes ******************************/
377 * Initialization functions in xttcps_sinit.c
379 XTtcPs_Config *XTtcPs_LookupConfig(u16 DeviceId);
382 * Required functions, in xttcps.c
384 s32 XTtcPs_CfgInitialize(XTtcPs *InstancePtr,
385 XTtcPs_Config * ConfigPtr, u32 EffectiveAddr);
387 void XTtcPs_SetMatchValue(XTtcPs *InstancePtr, u8 MatchIndex, u16 Value);
388 u16 XTtcPs_GetMatchValue(XTtcPs *InstancePtr, u8 MatchIndex);
390 void XTtcPs_SetPrescaler(XTtcPs *InstancePtr, u8 PrescalerValue);
391 u8 XTtcPs_GetPrescaler(XTtcPs *InstancePtr);
393 void XTtcPs_CalcIntervalFromFreq(XTtcPs *InstancePtr, u32 Freq,
394 u16 *Interval, u8 *Prescaler);
397 * Functions for options, in file xttcps_options.c
399 s32 XTtcPs_SetOptions(XTtcPs *InstancePtr, u32 Options);
400 u32 XTtcPs_GetOptions(XTtcPs *InstancePtr);
403 * Function for self-test, in file xttcps_selftest.c
405 s32 XTtcPs_SelfTest(XTtcPs *InstancePtr);
411 #endif /* end of protection macro */