]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53/src/Full_Demo/IntQueueTimer.c
0af7ac3136406bcdc33d211e58bad5a8a02d8c03
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53 / src / Full_Demo / IntQueueTimer.c
1 /*\r
2     FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71  * NOTE:  Currently only timer 1 and timer 2 are used -\r
72  *\r
73  * This file initialises two timers as follows:\r
74  *\r
75  * Timer 0 and Timer 1 provide the interrupts that are used with the IntQ\r
76  * standard demo tasks, which test interrupt nesting and using queues from\r
77  * interrupts.  Both these interrupts operate below the maximum syscall\r
78  * interrupt priority.\r
79  *\r
80  * Timer 2 is a much higher frequency timer that tests the nesting of interrupts\r
81  * that execute above the maximum syscall interrupt priority.\r
82  *\r
83  * All the timers can nest with the tick interrupt - creating a maximum\r
84  * interrupt nesting depth of 4.\r
85  *\r
86  * For convenience, the high frequency timer is also used to provide the time\r
87  * base for the run time stats.\r
88  */\r
89 \r
90 /* Scheduler includes. */\r
91 #include "FreeRTOS.h"\r
92 \r
93 /* Demo includes. */\r
94 #include "IntQueueTimer.h"\r
95 #include "IntQueue.h"\r
96 \r
97 /* Xilinx includes. */\r
98 #include "xttcps.h"\r
99 #include "xscugic.h"\r
100 \r
101 /* The frequencies at which the first two timers expire are slightly offset to\r
102 ensure they don't remain synchronised.  The frequency of the interrupt that\r
103 operates above the max syscall interrupt priority is 10 times faster so really\r
104 hammers the interrupt entry and exit code. */\r
105 #define tmrTIMERS_USED  3\r
106 #define tmrTIMER_0_FREQUENCY    ( 100UL )\r
107 #define tmrTIMER_1_FREQUENCY    ( 111UL )\r
108 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * The single interrupt service routines that is used to service all three\r
114  * timers.\r
115  */\r
116 static void prvTimerHandler( void *CallBackRef );\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 /* Hardware constants. */\r
121 static const BaseType_t xDeviceIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_DEVICE_ID, XPAR_XTTCPS_1_DEVICE_ID, XPAR_XTTCPS_2_DEVICE_ID };\r
122 static const BaseType_t xInterruptIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_INTR, XPAR_XTTCPS_1_INTR, XPAR_XTTCPS_2_INTR };\r
123 \r
124 /* Timer configuration settings. */\r
125 typedef struct\r
126 {\r
127         uint32_t OutputHz;      /* Output frequency. */\r
128         uint16_t Interval;      /* Interval value. */\r
129         uint8_t Prescaler;      /* Prescaler value. */\r
130         uint16_t Options;       /* Option settings. */\r
131 } TmrCntrSetup;\r
132 \r
133 static TmrCntrSetup xTimerSettings[ tmrTIMERS_USED ] =\r
134 {\r
135         { tmrTIMER_0_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
136         { tmrTIMER_1_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
137         { tmrTIMER_2_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE }\r
138 };\r
139 \r
140 /* Lower priority number means higher logical priority, so\r
141 configMAX_API_CALL_INTERRUPT_PRIORITY - 1 is above the maximum system call\r
142 interrupt priority. */\r
143 static const UBaseType_t uxInterruptPriorities[ tmrTIMERS_USED ] =\r
144 {\r
145         configMAX_API_CALL_INTERRUPT_PRIORITY + 1,\r
146         configMAX_API_CALL_INTERRUPT_PRIORITY,\r
147         configMAX_API_CALL_INTERRUPT_PRIORITY - 1\r
148 };\r
149 \r
150 static XTtcPs xTimerInstances[ tmrTIMERS_USED ];\r
151 \r
152 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
153 actually being reached. */\r
154 extern uint64_t ullPortInterruptNesting;\r
155 static volatile uint32_t ulMaxRecordedNesting = 1;\r
156 \r
157 /* Used to ensure the high frequency timer is running at the expected\r
158 frequency. */\r
159 static volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 void vInitialiseTimerForIntQueueTest( void )\r
164 {\r
165 BaseType_t xStatus;\r
166 TmrCntrSetup *pxTimerSettings;\r
167 extern XScuGic xInterruptController;\r
168 BaseType_t xTimer;\r
169 XTtcPs *pxTimerInstance;\r
170 XTtcPs_Config *pxTimerConfiguration;\r
171 const uint8_t ucRisingEdge = 3;\r
172 \r
173         /*_RB_ Currently only timer 1 and timer 2 are used. */\r
174         for( xTimer = 0; xTimer < ( tmrTIMERS_USED - 1 ); xTimer++ )\r
175         {\r
176                 /* Look up the timer's configuration. */\r
177                 pxTimerInstance = &( xTimerInstances[ xTimer ] );\r
178                 pxTimerConfiguration = XTtcPs_LookupConfig( xDeviceIDs[ xTimer ] );\r
179                 configASSERT( pxTimerConfiguration );\r
180 \r
181                 pxTimerSettings = &( xTimerSettings[ xTimer ] );\r
182 \r
183                 /* Initialise the device. */\r
184                 xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
185                 if( xStatus != XST_SUCCESS )\r
186                 {\r
187                         /* Not sure how to do this before XTtcPs_CfgInitialize is called\r
188                         as pxTimerInstance is set within XTtcPs_CfgInitialize(). */\r
189                         XTtcPs_Stop( pxTimerInstance );\r
190                         xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
191                         configASSERT( xStatus == XST_SUCCESS );\r
192                 }\r
193 \r
194                 /* Set the options. */\r
195                 XTtcPs_SetOptions( pxTimerInstance, pxTimerSettings->Options );\r
196 \r
197                 /* The timer frequency is preset in the pxTimerSettings structure.\r
198                 Derive the values for the other structure members. */\r
199                 XTtcPs_CalcIntervalFromFreq( pxTimerInstance, pxTimerSettings->OutputHz, &( pxTimerSettings->Interval ), &( pxTimerSettings->Prescaler ) );\r
200 \r
201                 /* Set the interval and prescale. */\r
202                 XTtcPs_SetInterval( pxTimerInstance, pxTimerSettings->Interval );\r
203                 XTtcPs_SetPrescaler( pxTimerInstance, pxTimerSettings->Prescaler );\r
204 \r
205                 /* The priority must be the lowest possible. */\r
206                 XScuGic_SetPriorityTriggerType( &xInterruptController, xInterruptIDs[ xTimer ], uxInterruptPriorities[ xTimer ] << portPRIORITY_SHIFT, ucRisingEdge );\r
207 \r
208                 /* Connect to the interrupt controller. */\r
209                 xStatus = XScuGic_Connect( &xInterruptController, xInterruptIDs[ xTimer ], ( Xil_InterruptHandler ) prvTimerHandler, ( void * ) pxTimerInstance );\r
210                 configASSERT( xStatus == XST_SUCCESS);\r
211 \r
212                 /* Enable the interrupt in the GIC. */\r
213                 XScuGic_Enable( &xInterruptController, xInterruptIDs[ xTimer ] );\r
214 \r
215                 /* Enable the interrupts in the timer. */\r
216                 XTtcPs_EnableInterrupts( pxTimerInstance, XTTCPS_IXR_INTERVAL_MASK );\r
217 \r
218                 /* Start the timer. */\r
219                 XTtcPs_Start( pxTimerInstance );\r
220         }\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void prvTimerHandler( void *pvCallBackRef )\r
225 {\r
226 uint32_t ulInterruptStatus;\r
227 XTtcPs *pxTimer = ( XTtcPs * ) pvCallBackRef;\r
228 BaseType_t xYieldRequired;\r
229 \r
230         /* Read the interrupt status, then write it back to clear the interrupt. */\r
231         ulInterruptStatus = XTtcPs_GetInterruptStatus( pxTimer );\r
232         XTtcPs_ClearInterruptStatus( pxTimer, ulInterruptStatus );\r
233         __asm volatile( "DSB SY" );\r
234         __asm volatile( "ISB SY" );\r
235 \r
236 \r
237         /* Now the interrupt has been cleared, interrupts can be re-enabled. */\r
238         portENABLE_INTERRUPTS();\r
239 \r
240         /* Only one interrupt event type is expected. */\r
241         configASSERT( ( XTTCPS_IXR_INTERVAL_MASK & ulInterruptStatus ) != 0 );\r
242 \r
243         /* Check the device ID to know which IntQueue demo to call. */\r
244         if( pxTimer->Config.DeviceId == xDeviceIDs[ 0 ] )\r
245         {\r
246                 xYieldRequired = xFirstTimerHandler();\r
247         }\r
248         else if( pxTimer->Config.DeviceId == xDeviceIDs[ 1 ] )\r
249         {\r
250                 xYieldRequired = xSecondTimerHandler();\r
251         }\r
252         else\r
253         {\r
254                 /* Used to check the timer is running at the expected frequency. */\r
255                 ulHighFrequencyTimerCounts++;\r
256                 xYieldRequired = pdFALSE;\r
257         }\r
258 \r
259         /* Latch the highest interrupt nesting count detected. */\r
260         if( ullPortInterruptNesting > ulMaxRecordedNesting )\r
261         {\r
262                 ulMaxRecordedNesting = ullPortInterruptNesting;\r
263         }\r
264 \r
265         /* If xYieldRequired is not pdFALSE then calling either xFirstTimerHandler()\r
266         or xSecondTimerHandler() resulted in a task leaving the blocked state and\r
267         the task that left the blocked state had a priority higher than the currently\r
268         running task (the task this interrupt interrupted) - so a context switch\r
269         should be performed so the interrupt returns directly to the higher priority\r
270         task.  xYieldRequired is tested inside the following macro. */\r
271         portYIELD_FROM_ISR( xYieldRequired );\r
272 }\r
273 \r