1 /******************************************************************************
3 * Copyright (C) 2010 - 2014 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 scutimer_v2_0
39 * Contains the implementation of interface functions of the SCU Timer driver.
40 * See xscutimer.h for a description of the driver.
43 * MODIFICATION HISTORY:
45 * Ver Who Date Changes
46 * ----- --- -------- ---------------------------------------------
47 * 1.00a nm 03/10/10 First release
50 ******************************************************************************/
52 /***************************** Include Files *********************************/
54 #include "xscutimer.h"
56 /************************** Constant Definitions *****************************/
58 /**************************** Type Definitions *******************************/
60 /***************** Macros (Inline Functions) Definitions *********************/
62 /************************** Function Prototypes ******************************/
64 /************************** Variable Definitions *****************************/
66 /****************************************************************************/
69 * Initialize a specific timer instance/driver. This function must be called
70 * before other functions of the driver are called.
72 * @param InstancePtr is a pointer to the XScuTimer instance.
73 * @param ConfigPtr points to the XScuTimer configuration structure.
74 * @param EffectiveAddress is the base address for the device. It could be
75 * a virtual address if address translation is supported in the
76 * system, otherwise it is the physical address.
79 * - XST_SUCCESS if initialization was successful.
80 * - XST_DEVICE_IS_STARTED if the device has already been started.
84 ******************************************************************************/
85 int XScuTimer_CfgInitialize(XScuTimer *InstancePtr,
86 XScuTimer_Config *ConfigPtr, u32 EffectiveAddress)
88 Xil_AssertNonvoid(InstancePtr != NULL);
89 Xil_AssertNonvoid(ConfigPtr != NULL);
92 * If the device is started, disallow the initialize and return a
93 * status indicating it is started. This allows the user to stop the
94 * device and reinitialize, but prevents a user from inadvertently
97 if (InstancePtr->IsStarted == XIL_COMPONENT_IS_STARTED) {
98 return XST_DEVICE_IS_STARTED;
102 * Copy configuration into the instance structure.
104 InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
107 * Save the base address pointer such that the registers of the block
108 * can be accessed and indicate it has not been started yet.
110 InstancePtr->Config.BaseAddr = EffectiveAddress;
112 InstancePtr->IsStarted = 0;
115 * Indicate the instance is ready to use, successfully initialized.
117 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
122 /****************************************************************************/
127 * @param InstancePtr is a pointer to the XScuTimer instance.
133 ******************************************************************************/
134 void XScuTimer_Start(XScuTimer *InstancePtr)
138 Xil_AssertVoid(InstancePtr != NULL);
139 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
142 * Read the contents of the Control register.
144 Register = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
145 XSCUTIMER_CONTROL_OFFSET);
148 * Set the 'timer enable' bit in the register.
150 Register |= XSCUTIMER_CONTROL_ENABLE_MASK;
153 * Update the Control register with the new value.
155 XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
156 XSCUTIMER_CONTROL_OFFSET, Register);
159 * Indicate that the device is started.
161 InstancePtr->IsStarted = XIL_COMPONENT_IS_STARTED;
164 /****************************************************************************/
169 * @param InstancePtr is a pointer to the XScuTimer instance.
175 ******************************************************************************/
176 void XScuTimer_Stop(XScuTimer *InstancePtr)
180 Xil_AssertVoid(InstancePtr != NULL);
181 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
184 * Read the contents of the Control register.
186 Register = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
187 XSCUTIMER_CONTROL_OFFSET);
190 * Clear the 'timer enable' bit in the register.
192 Register &= ~XSCUTIMER_CONTROL_ENABLE_MASK;
195 * Update the Control register with the new value.
197 XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
198 XSCUTIMER_CONTROL_OFFSET, Register);
201 * Indicate that the device is stopped.
203 InstancePtr->IsStarted = 0;
206 /*****************************************************************************/
209 * This function sets the prescaler bits in the timer control register.
211 * @param InstancePtr is a pointer to the XScuTimer instance.
212 * @param PrescalerValue is a 8 bit value that sets the prescaler to use.
218 ****************************************************************************/
219 void XScuTimer_SetPrescaler(XScuTimer *InstancePtr, u8 PrescalerValue)
224 * Assert to validate input arguments.
226 Xil_AssertVoid(InstancePtr != NULL);
227 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
229 * Read the Timer control register.
231 ControlReg = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
232 XSCUTIMER_CONTROL_OFFSET);
235 * Clear all of the prescaler control bits in the register.
237 ControlReg &= ~XSCUTIMER_CONTROL_PRESCALER_MASK;
240 * Set the prescaler value.
242 ControlReg |= (PrescalerValue << XSCUTIMER_CONTROL_PRESCALER_SHIFT);
245 * Write the register with the new values.
247 XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
248 XSCUTIMER_CONTROL_OFFSET, ControlReg);
251 /*****************************************************************************/
254 * This function returns the current prescaler value.
256 * @param InstancePtr is a pointer to the XScuTimer instance.
258 * @return The prescaler value.
262 ****************************************************************************/
263 u8 XScuTimer_GetPrescaler(XScuTimer *InstancePtr)
268 * Assert to validate input arguments.
270 Xil_AssertNonvoid(InstancePtr != NULL);
271 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
274 * Read the Timer control register.
276 ControlReg = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
277 XSCUTIMER_CONTROL_OFFSET);
278 ControlReg &= XSCUTIMER_CONTROL_PRESCALER_MASK;
280 return (ControlReg >> XSCUTIMER_CONTROL_PRESCALER_SHIFT);