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