1 /******************************************************************************
3 * (c) Copyright 2010-12 Xilinx, Inc. All rights reserved.
5 * This file contains confidential and proprietary information of Xilinx, Inc.
6 * and is protected under U.S. and international copyright and other
7 * intellectual property laws.
10 * This disclaimer is not a license and does not grant any rights to the
11 * materials distributed herewith. Except as otherwise provided in a valid
12 * license issued to you by Xilinx, and to the maximum extent permitted by
13 * applicable law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND WITH ALL
14 * FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS,
15 * IMPLIED, OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
16 * MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE;
17 * and (2) Xilinx shall not be liable (whether in contract or tort, including
18 * negligence, or under any other theory of liability) for any loss or damage
19 * of any kind or nature related to, arising under or in connection with these
20 * materials, including for any direct, or any indirect, special, incidental,
21 * or consequential loss or damage (including loss of data, profits, goodwill,
22 * or any type of loss or damage suffered as a result of any action brought by
23 * a third party) even if such damage or loss was reasonably foreseeable or
24 * Xilinx had been advised of the possibility of the same.
26 * CRITICAL APPLICATIONS
27 * Xilinx products are not designed or intended to be fail-safe, or for use in
28 * any application requiring fail-safe performance, such as life-support or
29 * safety devices or systems, Class III medical devices, nuclear facilities,
30 * applications related to the deployment of airbags, or any other applications
31 * that could lead to death, personal injury, or severe property or
32 * environmental damage (individually and collectively, "Critical
33 * Applications"). Customer assumes the sole risk and liability of any use of
34 * Xilinx products in Critical Applications, subject only to applicable laws
35 * and regulations governing limitations on product liability.
37 * THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS PART OF THIS FILE
40 ******************************************************************************/
42 /****************************************************************************/
47 * Contains the implementation of interface functions of the SCU Timer driver.
48 * See xscutimer.h for a description of the driver.
51 * MODIFICATION HISTORY:
53 * Ver Who Date Changes
54 * ----- --- -------- ---------------------------------------------
55 * 1.00a nm 03/10/10 First release
58 ******************************************************************************/
60 /***************************** Include Files *********************************/
62 #include "xscutimer.h"
64 /************************** Constant Definitions *****************************/
66 /**************************** Type Definitions *******************************/
68 /***************** Macros (Inline Functions) Definitions *********************/
70 /************************** Function Prototypes ******************************/
72 /************************** Variable Definitions *****************************/
74 /****************************************************************************/
77 * Initialize a specific timer instance/driver. This function must be called
78 * before other functions of the driver are called.
80 * @param InstancePtr is a pointer to the XScuTimer instance.
81 * @param ConfigPtr points to the XScuTimer configuration structure.
82 * @param EffectiveAddress is the base address for the device. It could be
83 * a virtual address if address translation is supported in the
84 * system, otherwise it is the physical address.
87 * - XST_SUCCESS if initialization was successful.
88 * - XST_DEVICE_IS_STARTED if the device has already been started.
92 ******************************************************************************/
93 int XScuTimer_CfgInitialize(XScuTimer *InstancePtr,
94 XScuTimer_Config *ConfigPtr, u32 EffectiveAddress)
96 Xil_AssertNonvoid(InstancePtr != NULL);
97 Xil_AssertNonvoid(ConfigPtr != NULL);
100 * If the device is started, disallow the initialize and return a
101 * status indicating it is started. This allows the user to stop the
102 * device and reinitialize, but prevents a user from inadvertently
105 if (InstancePtr->IsStarted == XIL_COMPONENT_IS_STARTED) {
106 return XST_DEVICE_IS_STARTED;
110 * Copy configuration into the instance structure.
112 InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
115 * Save the base address pointer such that the registers of the block
116 * can be accessed and indicate it has not been started yet.
118 InstancePtr->Config.BaseAddr = EffectiveAddress;
120 InstancePtr->IsStarted = 0;
123 * Indicate the instance is ready to use, successfully initialized.
125 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
130 /****************************************************************************/
135 * @param InstancePtr is a pointer to the XScuTimer instance.
141 ******************************************************************************/
142 void XScuTimer_Start(XScuTimer *InstancePtr)
146 Xil_AssertVoid(InstancePtr != NULL);
147 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
150 * Read the contents of the Control register.
152 Register = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
153 XSCUTIMER_CONTROL_OFFSET);
156 * Set the 'timer enable' bit in the register.
158 Register |= XSCUTIMER_CONTROL_ENABLE_MASK;
161 * Update the Control register with the new value.
163 XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
164 XSCUTIMER_CONTROL_OFFSET, Register);
167 * Indicate that the device is started.
169 InstancePtr->IsStarted = XIL_COMPONENT_IS_STARTED;
172 /****************************************************************************/
177 * @param InstancePtr is a pointer to the XScuTimer instance.
183 ******************************************************************************/
184 void XScuTimer_Stop(XScuTimer *InstancePtr)
188 Xil_AssertVoid(InstancePtr != NULL);
189 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
192 * Read the contents of the Control register.
194 Register = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
195 XSCUTIMER_CONTROL_OFFSET);
198 * Clear the 'timer enable' bit in the register.
200 Register &= ~XSCUTIMER_CONTROL_ENABLE_MASK;
203 * Update the Control register with the new value.
205 XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
206 XSCUTIMER_CONTROL_OFFSET, Register);
209 * Indicate that the device is stopped.
211 InstancePtr->IsStarted = 0;
214 /*****************************************************************************/
217 * This function sets the prescaler bits in the timer control register.
219 * @param InstancePtr is a pointer to the XScuTimer instance.
220 * @param PrescalerValue is a 8 bit value that sets the prescaler to use.
226 ****************************************************************************/
227 void XScuTimer_SetPrescaler(XScuTimer *InstancePtr, u8 PrescalerValue)
232 * Assert to validate input arguments.
234 Xil_AssertVoid(InstancePtr != NULL);
235 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
237 * Read the Timer control register.
239 ControlReg = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
240 XSCUTIMER_CONTROL_OFFSET);
243 * Clear all of the prescaler control bits in the register.
245 ControlReg &= ~XSCUTIMER_CONTROL_PRESCALER_MASK;
248 * Set the prescaler value.
250 ControlReg |= (PrescalerValue << XSCUTIMER_CONTROL_PRESCALER_SHIFT);
253 * Write the register with the new values.
255 XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
256 XSCUTIMER_CONTROL_OFFSET, ControlReg);
259 /*****************************************************************************/
262 * This function returns the current prescaler value.
264 * @param InstancePtr is a pointer to the XScuTimer instance.
266 * @return The prescaler value.
270 ****************************************************************************/
271 u8 XScuTimer_GetPrescaler(XScuTimer *InstancePtr)
276 * Assert to validate input arguments.
278 Xil_AssertNonvoid(InstancePtr != NULL);
279 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
282 * Read the Timer control register.
284 ControlReg = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
285 XSCUTIMER_CONTROL_OFFSET);
286 ControlReg &= XSCUTIMER_CONTROL_PRESCALER_MASK;
288 return (ControlReg >> XSCUTIMER_CONTROL_PRESCALER_SHIFT);