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