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 /****************************************************************************/
37 * Contains the implementation of interface functions of the SCU Timer driver.
38 * See xscutimer.h for a description of the driver.
41 * MODIFICATION HISTORY:
43 * Ver Who Date Changes
44 * ----- --- -------- ---------------------------------------------
45 * 1.00a nm 03/10/10 First release
48 ******************************************************************************/
50 /***************************** Include Files *********************************/
52 #include "xscutimer.h"
54 /************************** Constant Definitions *****************************/
56 /**************************** Type Definitions *******************************/
58 /***************** Macros (Inline Functions) Definitions *********************/
60 /************************** Function Prototypes ******************************/
62 /************************** Variable Definitions *****************************/
64 /****************************************************************************/
67 * Initialize a specific timer instance/driver. This function must be called
68 * before other functions of the driver are called.
70 * @param InstancePtr is a pointer to the XScuTimer instance.
71 * @param ConfigPtr points to the XScuTimer configuration structure.
72 * @param EffectiveAddress is the base address for the device. It could be
73 * a virtual address if address translation is supported in the
74 * system, otherwise it is the physical address.
77 * - XST_SUCCESS if initialization was successful.
78 * - XST_DEVICE_IS_STARTED if the device has already been started.
82 ******************************************************************************/
83 int XScuTimer_CfgInitialize(XScuTimer *InstancePtr,
84 XScuTimer_Config *ConfigPtr, u32 EffectiveAddress)
86 Xil_AssertNonvoid(InstancePtr != NULL);
87 Xil_AssertNonvoid(ConfigPtr != NULL);
90 * If the device is started, disallow the initialize and return a
91 * status indicating it is started. This allows the user to stop the
92 * device and reinitialize, but prevents a user from inadvertently
95 if (InstancePtr->IsStarted == XIL_COMPONENT_IS_STARTED) {
96 return XST_DEVICE_IS_STARTED;
100 * Copy configuration into the instance structure.
102 InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
105 * Save the base address pointer such that the registers of the block
106 * can be accessed and indicate it has not been started yet.
108 InstancePtr->Config.BaseAddr = EffectiveAddress;
110 InstancePtr->IsStarted = 0;
113 * Indicate the instance is ready to use, successfully initialized.
115 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
120 /****************************************************************************/
125 * @param InstancePtr is a pointer to the XScuTimer instance.
131 ******************************************************************************/
132 void XScuTimer_Start(XScuTimer *InstancePtr)
136 Xil_AssertVoid(InstancePtr != NULL);
137 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
140 * Read the contents of the Control register.
142 Register = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
143 XSCUTIMER_CONTROL_OFFSET);
146 * Set the 'timer enable' bit in the register.
148 Register |= XSCUTIMER_CONTROL_ENABLE_MASK;
151 * Update the Control register with the new value.
153 XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
154 XSCUTIMER_CONTROL_OFFSET, Register);
157 * Indicate that the device is started.
159 InstancePtr->IsStarted = XIL_COMPONENT_IS_STARTED;
162 /****************************************************************************/
167 * @param InstancePtr is a pointer to the XScuTimer instance.
173 ******************************************************************************/
174 void XScuTimer_Stop(XScuTimer *InstancePtr)
178 Xil_AssertVoid(InstancePtr != NULL);
179 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
182 * Read the contents of the Control register.
184 Register = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
185 XSCUTIMER_CONTROL_OFFSET);
188 * Clear the 'timer enable' bit in the register.
190 Register &= ~XSCUTIMER_CONTROL_ENABLE_MASK;
193 * Update the Control register with the new value.
195 XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
196 XSCUTIMER_CONTROL_OFFSET, Register);
199 * Indicate that the device is stopped.
201 InstancePtr->IsStarted = 0;
204 /*****************************************************************************/
207 * This function sets the prescaler bits in the timer control register.
209 * @param InstancePtr is a pointer to the XScuTimer instance.
210 * @param PrescalerValue is a 8 bit value that sets the prescaler to use.
216 ****************************************************************************/
217 void XScuTimer_SetPrescaler(XScuTimer *InstancePtr, u8 PrescalerValue)
222 * Assert to validate input arguments.
224 Xil_AssertVoid(InstancePtr != NULL);
225 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
227 * Read the Timer control register.
229 ControlReg = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
230 XSCUTIMER_CONTROL_OFFSET);
233 * Clear all of the prescaler control bits in the register.
235 ControlReg &= ~XSCUTIMER_CONTROL_PRESCALER_MASK;
238 * Set the prescaler value.
240 ControlReg |= (PrescalerValue << XSCUTIMER_CONTROL_PRESCALER_SHIFT);
243 * Write the register with the new values.
245 XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
246 XSCUTIMER_CONTROL_OFFSET, ControlReg);
249 /*****************************************************************************/
252 * This function returns the current prescaler value.
254 * @param InstancePtr is a pointer to the XScuTimer instance.
256 * @return The prescaler value.
260 ****************************************************************************/
261 u8 XScuTimer_GetPrescaler(XScuTimer *InstancePtr)
266 * Assert to validate input arguments.
268 Xil_AssertNonvoid(InstancePtr != NULL);
269 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
272 * Read the Timer control register.
274 ControlReg = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
275 XSCUTIMER_CONTROL_OFFSET);
276 ControlReg &= XSCUTIMER_CONTROL_PRESCALER_MASK;
278 return (ControlReg >> XSCUTIMER_CONTROL_PRESCALER_SHIFT);