]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53_bsp/psu_cortexa53_0/libsrc/wdtps_v3_0/src/xwdtps.c
Add in the CORTEX_A53_64-bit_UltraScale_MPSoC demo application (a demo has been inclu...
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53_bsp / psu_cortexa53_0 / libsrc / wdtps_v3_0 / src / xwdtps.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2015 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 xwdtps.c
36 *
37 * Contains the implementation of interface functions of the XWdtPs driver.
38 * See xwdtps.h for a description of the driver.
39 *
40 * <pre>
41 * MODIFICATION HISTORY:
42 *
43 * Ver   Who    Date     Changes
44 * ----- ------ -------- ---------------------------------------------
45 * 1.00a ecm/jz 01/15/10 First release
46 * 1.02a  sg    07/15/12 Removed code/APIs related to  External Signal
47 *                       Length functionality for CR 658287
48 *                       Removed APIs XWdtPs_SetExternalSignalLength,
49 *                       XWdtPs_GetExternalSignalLength
50 * 3.00  kvn    02/13/15 Modified code for MISRA-C:2012 compliance.
51 * </pre>
52 *
53 ******************************************************************************/
54
55 /***************************** Include Files *********************************/
56
57 #include "xwdtps.h"
58
59 /************************** Constant Definitions *****************************/
60
61
62 /**************************** Type Definitions *******************************/
63
64
65 /***************** Macros (Inline Functions) Definitions *********************/
66
67
68 /************************** Function Prototypes ******************************/
69
70
71 /************************** Variable Definitions *****************************/
72
73
74 /****************************************************************************/
75 /**
76 *
77 * Initialize a specific watchdog timer instance/driver. This function
78 * must be called before other functions of the driver are called.
79 *
80 * @param        InstancePtr is a pointer to the XWdtPs instance.
81 * @param        ConfigPtr is the config 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 s32 XWdtPs_CfgInitialize(XWdtPs *InstancePtr,
94                         XWdtPs_Config *ConfigPtr, u32 EffectiveAddress)
95 {
96         s32 Status;
97         Xil_AssertNonvoid(InstancePtr != NULL);
98         Xil_AssertNonvoid(ConfigPtr != NULL);
99
100         /*
101          * If the device is started, disallow the initialize and return a
102          * status indicating it is started. This allows the user to stop the
103          * device and reinitialize, but prevents a user from inadvertently
104          * initializing.
105          */
106         if (InstancePtr->IsStarted == XIL_COMPONENT_IS_STARTED) {
107                 Status = XST_DEVICE_IS_STARTED;
108         } else {
109
110                 /*
111                  * Copy configuration into instance.
112                  */
113                 InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
114
115                 /*
116                  * Save the base address pointer such that the registers of the block
117                  * can be accessed and indicate it has not been started yet.
118                  */
119                 InstancePtr->Config.BaseAddress = EffectiveAddress;
120                 InstancePtr->IsStarted = 0U;
121
122                 /*
123                  * Indicate the instance is ready to use, successfully initialized.
124                  */
125                 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
126
127                 Status = XST_SUCCESS;
128         }
129         return Status;
130 }
131
132 /****************************************************************************/
133 /**
134 *
135 * Start the watchdog timer of the device.
136 *
137 * @param        InstancePtr is a pointer to the XWdtPs instance.
138 *
139 * @return       None.
140 *
141 * @note         None.
142 *
143 ******************************************************************************/
144 void XWdtPs_Start(XWdtPs *InstancePtr)
145 {
146         u32 Register;
147
148         Xil_AssertVoid(InstancePtr != NULL);
149         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
150
151         /*
152          * Read the contents of the ZMR register.
153          */
154         Register = XWdtPs_ReadReg(InstancePtr->Config.BaseAddress,
155                                  XWDTPS_ZMR_OFFSET);
156
157         /*
158          * Enable the Timer field in the register and Set the access key so the
159          * write takes place.
160          */
161         Register |= XWDTPS_ZMR_WDEN_MASK;
162         Register |= XWDTPS_ZMR_ZKEY_VAL;
163
164         /*
165          * Update the ZMR with the new value.
166          */
167         XWdtPs_WriteReg(InstancePtr->Config.BaseAddress, XWDTPS_ZMR_OFFSET,
168                           Register);
169
170         /*
171          * Indicate that the device is started.
172          */
173         InstancePtr->IsStarted = XIL_COMPONENT_IS_STARTED;
174
175 }
176
177 /****************************************************************************/
178 /**
179 *
180 * Disable the watchdog timer.
181 *
182 * It is the caller's responsibility to disconnect the interrupt handler
183 * of the watchdog timer from the interrupt source, typically an interrupt
184 * controller, and disable the interrupt in the interrupt controller.
185 *
186 * @param        InstancePtr is a pointer to the XWdtPs instance.
187 *
188 * @return       None.
189 *
190 * @note         None.
191 *
192 ******************************************************************************/
193 void XWdtPs_Stop(XWdtPs *InstancePtr)
194 {
195         u32 Register;
196
197         Xil_AssertVoid(InstancePtr != NULL);
198         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
199
200         /*
201          * Read the contents of the ZMR register.
202          */
203         Register = XWdtPs_ReadReg(InstancePtr->Config.BaseAddress,
204                                  XWDTPS_ZMR_OFFSET);
205
206         /*
207          * Disable the Timer field in the register and
208          * Set the access key for the write to be done the register.
209          */
210         Register &= (u32)(~XWDTPS_ZMR_WDEN_MASK);
211         Register |= XWDTPS_ZMR_ZKEY_VAL;
212
213         /*
214          * Update the ZMR with the new value.
215          */
216         XWdtPs_WriteReg(InstancePtr->Config.BaseAddress, XWDTPS_ZMR_OFFSET,
217                           Register);
218
219         InstancePtr->IsStarted = 0U;
220 }
221
222
223 /****************************************************************************/
224 /**
225 *
226 * Enables the indicated signal/output.
227 * Performs a read/modify/write cycle to update the value correctly.
228 *
229 * @param        InstancePtr is a pointer to the XWdtPs instance.
230 * @param        Signal is the desired signal/output.
231 *               Valid Signal Values are XWDTPS_RESET_SIGNAL and
232 *               XWDTPS_IRQ_SIGNAL.
233 *               Only one of them can be specified at a time.
234 *
235 * @return       None.
236 *
237 * @note         None.
238 *
239 ******************************************************************************/
240 void XWdtPs_EnableOutput(XWdtPs *InstancePtr, u8 Signal)
241 {
242         u32 Register;
243
244         Xil_AssertVoid(InstancePtr != NULL);
245         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
246         Xil_AssertVoid((Signal == XWDTPS_RESET_SIGNAL) ||
247                         (Signal == XWDTPS_IRQ_SIGNAL));
248
249         /*
250          * Read the contents of the ZMR register.
251          */
252         Register = XWdtPs_ReadReg(InstancePtr->Config.BaseAddress,
253                                  XWDTPS_ZMR_OFFSET);
254
255         if (Signal == XWDTPS_RESET_SIGNAL) {
256                 /*
257                  * Enable the field in the register.
258                  */
259                 Register |= XWDTPS_ZMR_RSTEN_MASK;
260
261         } else if (Signal == XWDTPS_IRQ_SIGNAL) {
262                 /*
263                  * Enable the field in the register.
264                  */
265                 Register |= XWDTPS_ZMR_IRQEN_MASK;
266
267         } else {
268                 /* Else was made for misra-c compliance */
269                 ;
270         }
271
272         /*
273          * Set the access key so the write takes.
274          */
275         Register |= XWDTPS_ZMR_ZKEY_VAL;
276
277         /*
278          * Update the ZMR with the new value.
279          */
280         XWdtPs_WriteReg(InstancePtr->Config.BaseAddress, XWDTPS_ZMR_OFFSET,
281                           Register);
282 }
283
284 /****************************************************************************/
285 /**
286 *
287 * Disables the indicated signal/output.
288 * Performs a read/modify/write cycle to update the value correctly.
289 *
290 * @param        InstancePtr is a pointer to the XWdtPs instance.
291 * @param        Signal is the desired signal/output.
292 *               Valid Signal Values are XWDTPS_RESET_SIGNAL and
293 *               XWDTPS_IRQ_SIGNAL
294 *               Only one of them can be specified at a time.
295 *
296 * @return       None.
297 *
298 * @note         None.
299 *
300 ******************************************************************************/
301 void XWdtPs_DisableOutput(XWdtPs *InstancePtr, u8 Signal)
302 {
303         u32 Register;
304
305         Xil_AssertVoid(InstancePtr != NULL);
306         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
307         Xil_AssertVoid((Signal == XWDTPS_RESET_SIGNAL) ||
308                         (Signal == XWDTPS_IRQ_SIGNAL));
309
310         /*
311          * Read the contents of the ZMR register.
312          */
313         Register = XWdtPs_ReadReg(InstancePtr->Config.BaseAddress,
314                                  XWDTPS_ZMR_OFFSET);
315
316         if (Signal == XWDTPS_RESET_SIGNAL) {
317                 /*
318                  * Disable the field in the register.
319                  */
320                 Register &= (u32)(~XWDTPS_ZMR_RSTEN_MASK);
321
322         } else if (Signal == XWDTPS_IRQ_SIGNAL) {
323                 /*
324                  * Disable the field in the register.
325                  */
326                 Register &= (u32)(~XWDTPS_ZMR_IRQEN_MASK);
327
328         } else {
329                 /* Else was made for misra-c compliance */
330                 ;
331         }
332
333         /*
334          * Set the access key so the write takes place.
335          */
336         Register |= XWDTPS_ZMR_ZKEY_VAL;
337
338         /*
339          * Update the ZMR with the new value.
340          */
341         XWdtPs_WriteReg(InstancePtr->Config.BaseAddress, XWDTPS_ZMR_OFFSET,
342                           Register);
343 }
344
345 /****************************************************************************/
346 /**
347 *
348 * Returns the current control setting for the indicated signal/output.
349 * The register referenced is the Counter Control Register (XWDTPS_CCR_OFFSET)
350 *
351 * @param        InstancePtr is a pointer to the XWdtPs instance.
352 * @param        Control is the desired signal/output.
353 *               Valid Control Values are XWDTPS_CLK_PRESCALE and
354 *               XWDTPS_COUNTER_RESET. Only one of them can be specified at a
355 *               time.
356 *
357 * @return       The contents of the requested control field in the Counter
358 *               Control Register (XWDTPS_CCR_OFFSET).
359 *               If the Control is XWDTPS_CLK_PRESCALE then use the
360 *               defintions XWDTEPB_CCR_PSCALE_XXXX.
361 *               If the Control is XWDTPS_COUNTER_RESET then the values are
362 *               0x0 to 0xFFF. This is the Counter Restart value in the CCR
363 *               register.
364 *
365 * @note         None.
366 *
367 ******************************************************************************/
368 u32 XWdtPs_GetControlValue(XWdtPs *InstancePtr, u8 Control)
369 {
370         u32 Register;
371         u32 ReturnValue = 0U;
372
373         Xil_AssertNonvoid(InstancePtr != NULL);
374         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
375         Xil_AssertNonvoid((Control == XWDTPS_CLK_PRESCALE) ||
376                         (Control == XWDTPS_COUNTER_RESET));
377
378         /*
379          * Read the contents of the CCR register.
380          */
381         Register = XWdtPs_ReadReg(InstancePtr->Config.BaseAddress,
382                          XWDTPS_CCR_OFFSET);
383
384         if (Control == XWDTPS_CLK_PRESCALE) {
385                 /*
386                  * Mask off the field in the register.
387                  */
388                 ReturnValue = Register & XWDTPS_CCR_CLKSEL_MASK;
389
390         } else if (Control == XWDTPS_COUNTER_RESET) {
391                 /*
392                  * Mask off the field in the register.
393                  */
394                 Register &= XWDTPS_CCR_CRV_MASK;
395
396                 /*
397                  * Shift over to the right most positions.
398                  */
399                 ReturnValue = Register >> XWDTPS_CCR_CRV_SHIFT;
400         } else {
401                 /* Else was made for misra-c compliance */
402                 ;
403         }
404
405         return ReturnValue;
406 }
407
408 /****************************************************************************/
409 /**
410 *
411 * Updates the current control setting for the indicated signal/output with
412 * the provided value.
413 *
414 * Performs a read/modify/write cycle to update the value correctly.
415 * The register referenced is the Counter Control Register (XWDTPS_CCR_OFFSET)
416 *
417 * @param        InstancePtr is a pointer to the XWdtPs instance.
418 * @param        Control is the desired signal/output.
419 *               Valid Control Values are XWDTPS_CLK_PRESCALE and
420 *               XWDTPS_COUNTER_RESET. Only one of them can be specified at a
421 *               time.
422 * @param        Value is the desired control value.
423 *               If the Control is XWDTPS_CLK_PRESCALE then use the
424 *               defintions XWDTEPB_CCR_PSCALE_XXXX.
425 *               If the Control is XWDTPS_COUNTER_RESET then the valid values
426 *               are 0x0 to 0xFFF, this sets the counter restart value of the CCR
427 *               register.
428 *
429 * @return       None.
430 *
431 * @note         None.
432 *
433 ******************************************************************************/
434 void XWdtPs_SetControlValue(XWdtPs *InstancePtr, u8 Control, u32 Value)
435 {
436         u32 Register;
437         u32 LocalValue = Value;
438
439         Xil_AssertVoid(InstancePtr != NULL);
440         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
441         Xil_AssertVoid((Control == XWDTPS_CLK_PRESCALE) ||
442                         (Control == XWDTPS_COUNTER_RESET));
443
444         /*
445          * Read the contents of the CCR register.
446          */
447         Register = XWdtPs_ReadReg(InstancePtr->Config.BaseAddress,
448                                  XWDTPS_CCR_OFFSET);
449
450         if (Control == XWDTPS_CLK_PRESCALE) {
451                 /*
452                  * Zero the field in the register.
453                  */
454                 Register &= (u32)(~XWDTPS_CCR_CLKSEL_MASK);
455
456         } else if (Control == XWDTPS_COUNTER_RESET) {
457                 /*
458                  * Zero the field in the register.
459                  */
460                 Register &= (u32)(~XWDTPS_CCR_CRV_MASK);
461
462                 /*
463                  * Shift Value over to the proper positions.
464                  */
465                 LocalValue = LocalValue << XWDTPS_CCR_CRV_SHIFT;
466         } else{
467                 /* This was made for misrac compliance. */
468                 ;
469         }
470
471         Register |= LocalValue;
472
473         /*
474          * Set the access key so the write takes.
475          */
476         Register |= XWDTPS_CCR_CKEY_VAL;
477
478         /*
479          * Update the CCR with the new value.
480          */
481         XWdtPs_WriteReg(InstancePtr->Config.BaseAddress, XWDTPS_CCR_OFFSET,
482                           Register);
483 }