]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/standalone_v6_6/src/xil_sleeptimer.c
Update Zynq, MPSoc Cortex-A53 and MPSoc Cortex-R5 demo projects to build with the...
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / standalone_v6_6 / src / xil_sleeptimer.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2017 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 *
36 * @file xil_sleeptimer.c
37 *
38 * This file provides the common helper routines for the sleep API's
39 *
40 * <pre>
41 * MODIFICATION HISTORY :
42 *
43 * Ver   Who  Date        Changes
44 * ----- ---- -------- -------------------------------------------------------
45 * 6.6   srm  10/18/17 First Release.
46 *
47 * </pre>
48 *****************************************************************************/
49
50 /****************************  Include Files  ********************************/
51
52 #include "xil_io.h"
53 #include "xil_sleeptimer.h"
54 #include "xtime_l.h"
55
56 /****************************  Constant Definitions  *************************/
57
58
59 /* Function definitions are applicable only when TTC3 is present*/
60 #if defined (SLEEP_TIMER_BASEADDR)
61 /****************************************************************************/
62 /**
63 *
64 * This is a helper function used by sleep/usleep APIs to
65 * have delay in sec/usec
66 *
67 * @param            delay - delay time in seconds/micro seconds
68 *
69 * @param            frequency - Number of counts per second/micro second
70 *
71 * @return           none
72 *
73 * @note             none
74 *
75 *****************************************************************************/
76 void Xil_SleepTTCCommon(u32 delay, u64 frequency)
77 {
78         INTPTR tEnd = 0U;
79         INTPTR tCur = 0U;
80         XCntrVal TimeHighVal = 0U;
81         XCntrVal TimeLowVal1 = 0U;
82         XCntrVal TimeLowVal2 = 0U;
83
84         TimeLowVal1 = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
85                         XSLEEP_TIMER_TTC_COUNT_VALUE_OFFSET);
86         tEnd = (INTPTR)TimeLowVal1 + ((INTPTR)(delay) * frequency);
87         do
88         {
89                 TimeLowVal2 = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
90                                                   XSLEEP_TIMER_TTC_COUNT_VALUE_OFFSET);
91                 if (TimeLowVal2 < TimeLowVal1) {
92                         TimeHighVal++;
93                 }
94                 TimeLowVal1 = TimeLowVal2;
95                 tCur = (((INTPTR) TimeHighVal) << XSLEEP_TIMER_REG_SHIFT) |
96                                                                 (INTPTR)TimeLowVal2;
97         }while (tCur < tEnd);
98 }
99
100
101 /*****************************************************************************/
102 /**
103 *
104 * This API starts the Triple Timer Counter
105 *
106 * @param            none
107 *
108 * @return           none
109 *
110 * @note             none
111 *
112 *****************************************************************************/
113 void XTime_StartTTCTimer()
114 {
115         u32 TimerPrescalar;
116         u32 TimerCntrl;
117
118 #if (defined (__aarch64__) && EL3==1) || defined (ARMR5) || defined (ARMA53_32)
119         u32 LpdRst;
120
121         LpdRst = XSleep_ReadCounterVal(RST_LPD_IOU2);
122
123         /* check if the timer is reset */
124         if (((LpdRst & (RST_LPD_IOU2_TTC_BASE_RESET_MASK <<
125                                                XSLEEP_TTC_INSTANCE)) != 0 )) {
126                 LpdRst = LpdRst & (~(RST_LPD_IOU2_TTC_BASE_RESET_MASK <<
127                                                         XSLEEP_TTC_INSTANCE));
128                 Xil_Out32(RST_LPD_IOU2, LpdRst);
129         } else {
130 #endif
131                 TimerCntrl = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
132                                         XSLEEP_TIMER_TTC_CNT_CNTRL_OFFSET);
133                 /* check if Timer is disabled */
134                 if ((TimerCntrl & XSLEEP_TIMER_TTC_CNT_CNTRL_DIS_MASK) == 0) {
135                     TimerPrescalar = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
136                                                XSLEEP_TIMER_TTC_CLK_CNTRL_OFFSET);
137                 /* check if Timer is configured with proper functionalty for sleep */
138                    if ((TimerPrescalar & XSLEEP_TIMER_TTC_CLK_CNTRL_PS_EN_MASK) == 0)
139                                                 return;
140                 }
141 #if (defined (__aarch64__) && EL3==1) || defined (ARMR5) || defined (ARMA53_32)
142         }
143 #endif
144         /* Disable the timer to configure */
145         TimerCntrl = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
146                                         XSLEEP_TIMER_TTC_CNT_CNTRL_OFFSET);
147         TimerCntrl = TimerCntrl | XSLEEP_TIMER_TTC_CNT_CNTRL_DIS_MASK;
148         Xil_Out32(SLEEP_TIMER_BASEADDR + XSLEEP_TIMER_TTC_CNT_CNTRL_OFFSET,
149                                          TimerCntrl);
150         /* Disable the prescalar */
151         TimerPrescalar = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
152                         XSLEEP_TIMER_TTC_CLK_CNTRL_OFFSET);
153         TimerPrescalar = TimerPrescalar & (~XSLEEP_TIMER_TTC_CLK_CNTRL_PS_EN_MASK);
154         Xil_Out32(SLEEP_TIMER_BASEADDR + XSLEEP_TIMER_TTC_CLK_CNTRL_OFFSET,
155                                                                 TimerPrescalar);
156         /* Enable the Timer */
157         TimerCntrl = TimerCntrl & (~XSLEEP_TIMER_TTC_CNT_CNTRL_DIS_MASK);
158         Xil_Out32(SLEEP_TIMER_BASEADDR + XSLEEP_TIMER_TTC_CNT_CNTRL_OFFSET,
159                                                                 TimerCntrl);
160 }
161 #endif