]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/scutimer_v2_0/src/xscutimer.c
Add back Zynq demo - this time using SDK V14.2.
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / scutimer_v2_0 / src / xscutimer.c
1 /******************************************************************************
2 *
3 * (c) Copyright 2010-12 Xilinx, Inc. All rights reserved.
4 *
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.
8 *
9 * DISCLAIMER
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.
25 *
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.
36 *
37 * THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS PART OF THIS FILE
38 * AT ALL TIMES.
39 *
40 ******************************************************************************/
41
42 /****************************************************************************/
43 /**
44 *
45 * @file xscutimer.c
46 *
47 * Contains the implementation of interface functions of the SCU Timer driver.
48 * See xscutimer.h for a description of the driver.
49 *
50 * <pre>
51 * MODIFICATION HISTORY:
52 *
53 * Ver   Who Date     Changes
54 * ----- --- -------- ---------------------------------------------
55 * 1.00a nm  03/10/10 First release
56 * </pre>
57 *
58 ******************************************************************************/
59
60 /***************************** Include Files *********************************/
61
62 #include "xscutimer.h"
63
64 /************************** Constant Definitions *****************************/
65
66 /**************************** Type Definitions *******************************/
67
68 /***************** Macros (Inline Functions) Definitions *********************/
69
70 /************************** Function Prototypes ******************************/
71
72 /************************** Variable Definitions *****************************/
73
74 /****************************************************************************/
75 /**
76 *
77 * Initialize a specific timer instance/driver. This function  must be called
78 * before other functions of the driver are called.
79 *
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.
85 *
86 * @return
87 *               - XST_SUCCESS if initialization was successful.
88 *               - XST_DEVICE_IS_STARTED if the device has already been started.
89 *
90 * @note         None.
91 *
92 ******************************************************************************/
93 int XScuTimer_CfgInitialize(XScuTimer *InstancePtr,
94                          XScuTimer_Config *ConfigPtr, u32 EffectiveAddress)
95 {
96         Xil_AssertNonvoid(InstancePtr != NULL);
97         Xil_AssertNonvoid(ConfigPtr != NULL);
98
99         /*
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
103          * initializing.
104          */
105         if (InstancePtr->IsStarted == XIL_COMPONENT_IS_STARTED) {
106                 return XST_DEVICE_IS_STARTED;
107         }
108
109         /*
110          * Copy configuration into the instance structure.
111          */
112         InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
113
114         /*
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.
117          */
118         InstancePtr->Config.BaseAddr = EffectiveAddress;
119
120         InstancePtr->IsStarted = 0;
121
122         /*
123          * Indicate the instance is ready to use, successfully initialized.
124          */
125         InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
126
127         return XST_SUCCESS;
128 }
129
130 /****************************************************************************/
131 /**
132 *
133 * Start the timer.
134 *
135 * @param        InstancePtr is a pointer to the XScuTimer instance.
136 *
137 * @return       None.
138 *
139 * @note         None.
140 *
141 ******************************************************************************/
142 void XScuTimer_Start(XScuTimer *InstancePtr)
143 {
144         u32 Register;
145
146         Xil_AssertVoid(InstancePtr != NULL);
147         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
148
149         /*
150          * Read the contents of the Control register.
151          */
152         Register = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
153                                   XSCUTIMER_CONTROL_OFFSET);
154
155         /*
156          * Set the 'timer enable' bit in the register.
157          */
158         Register |= XSCUTIMER_CONTROL_ENABLE_MASK;
159
160         /*
161          * Update the Control register with the new value.
162          */
163         XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
164                         XSCUTIMER_CONTROL_OFFSET, Register);
165
166         /*
167          * Indicate that the device is started.
168          */
169         InstancePtr->IsStarted = XIL_COMPONENT_IS_STARTED;
170 }
171
172 /****************************************************************************/
173 /**
174 *
175 * Stop the timer.
176 *
177 * @param        InstancePtr is a pointer to the XScuTimer instance.
178 *
179 * @return       None.
180 *
181 * @note         None.
182 *
183 ******************************************************************************/
184 void XScuTimer_Stop(XScuTimer *InstancePtr)
185 {
186         u32 Register;
187
188         Xil_AssertVoid(InstancePtr != NULL);
189         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
190
191         /*
192          * Read the contents of the Control register.
193          */
194         Register = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
195                                   XSCUTIMER_CONTROL_OFFSET);
196
197         /*
198          * Clear the 'timer enable' bit in the register.
199          */
200         Register &= ~XSCUTIMER_CONTROL_ENABLE_MASK;
201
202         /*
203          * Update the Control register with the new value.
204          */
205         XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
206                         XSCUTIMER_CONTROL_OFFSET, Register);
207
208         /*
209          * Indicate that the device is stopped.
210          */
211         InstancePtr->IsStarted = 0;
212 }
213
214 /*****************************************************************************/
215 /**
216 *
217 * This function sets the prescaler bits in the timer control register.
218 *
219 * @param        InstancePtr is a pointer to the XScuTimer instance.
220 * @param        PrescalerValue is a 8 bit value that sets the prescaler to use.
221 *
222 * @return       None
223 *
224 * @note         None
225 *
226 ****************************************************************************/
227 void XScuTimer_SetPrescaler(XScuTimer *InstancePtr, u8 PrescalerValue)
228 {
229         u32 ControlReg;
230
231         /*
232          * Assert to validate input arguments.
233          */
234         Xil_AssertVoid(InstancePtr != NULL);
235         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
236         /*
237          * Read the Timer control register.
238          */
239         ControlReg = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
240                                         XSCUTIMER_CONTROL_OFFSET);
241
242         /*
243          * Clear all of the prescaler control bits in the register.
244          */
245         ControlReg &= ~XSCUTIMER_CONTROL_PRESCALER_MASK;
246
247         /*
248          * Set the prescaler value.
249          */
250         ControlReg |= (PrescalerValue << XSCUTIMER_CONTROL_PRESCALER_SHIFT);
251
252         /*
253          * Write the register with the new values.
254          */
255         XScuTimer_WriteReg(InstancePtr->Config.BaseAddr,
256                           XSCUTIMER_CONTROL_OFFSET, ControlReg);
257 }
258
259 /*****************************************************************************/
260 /**
261 *
262 * This function returns the current prescaler value.
263 *
264 * @param        InstancePtr is a pointer to the XScuTimer instance.
265 *
266 * @return       The prescaler value.
267 *
268 * @note         None.
269 *
270 ****************************************************************************/
271 u8 XScuTimer_GetPrescaler(XScuTimer *InstancePtr)
272 {
273         u32 ControlReg;
274
275         /*
276          * Assert to validate input arguments.
277          */
278         Xil_AssertNonvoid(InstancePtr != NULL);
279         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
280
281         /*
282          * Read the Timer control register.
283          */
284         ControlReg = XScuTimer_ReadReg(InstancePtr->Config.BaseAddr,
285                                     XSCUTIMER_CONTROL_OFFSET);
286         ControlReg &= XSCUTIMER_CONTROL_PRESCALER_MASK;
287
288         return (ControlReg >> XSCUTIMER_CONTROL_PRESCALER_SHIFT);
289 }