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