]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/Full_Demo/IntQueueTimer.c
Update version number in preparation for maintenance release.
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / Full_Demo / IntQueueTimer.c
1 /*\r
2     FreeRTOS V9.0.1 - Copyright (C) 2017 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  * This file initialises three timers as follows:\r
72  *\r
73  * Timer 0 and Timer 1 provide the interrupts that are used with the IntQ\r
74  * standard demo tasks, which test interrupt nesting and using queues from\r
75  * interrupts.  Both these interrupts operate below the maximum syscall\r
76  * interrupt priority.\r
77  *\r
78  * Timer 2 is a much higher frequency timer that tests the nesting of interrupts\r
79  * that execute above the maximum syscall interrupt priority.\r
80  *\r
81  * All the timers can nest with the tick interrupt - creating a maximum\r
82  * interrupt nesting depth of 4.\r
83  *\r
84  * For convenience, the high frequency timer is also used to provide the time\r
85  * base for the run time stats.\r
86  */\r
87 \r
88 /* Scheduler includes. */\r
89 #include "FreeRTOS.h"\r
90 \r
91 /* Demo includes. */\r
92 #include "IntQueueTimer.h"\r
93 #include "IntQueue.h"\r
94 \r
95 /* Xilinx includes. */\r
96 #include "xttcps.h"\r
97 #include "xscugic.h"\r
98 \r
99 /* The frequencies at which the first two timers expire are slightly offset to\r
100 ensure they don't remain synchronised.  The frequency of the interrupt that\r
101 operates above the max syscall interrupt priority is 10 times faster so really\r
102 hammers the interrupt entry and exit code. */\r
103 #define tmrTIMERS_USED  3\r
104 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
105 #define tmrTIMER_1_FREQUENCY    ( 2001UL )\r
106 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /*\r
111  * The single interrupt service routines that is used to service all three\r
112  * timers.\r
113  */\r
114 static void prvTimerHandler( void *CallBackRef );\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /* Hardware constants. */\r
119 static const BaseType_t xDeviceIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_DEVICE_ID, XPAR_XTTCPS_1_DEVICE_ID, XPAR_XTTCPS_2_DEVICE_ID };\r
120 static const BaseType_t xInterruptIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_INTR, XPAR_XTTCPS_1_INTR, XPAR_XTTCPS_2_INTR };\r
121 \r
122 /* Timer configuration settings. */\r
123 typedef struct\r
124 {\r
125         uint32_t OutputHz;      /* Output frequency. */\r
126         uint16_t Interval;      /* Interval value. */\r
127         uint8_t Prescaler;      /* Prescaler value. */\r
128         uint16_t Options;       /* Option settings. */\r
129 } TmrCntrSetup;\r
130 \r
131 static TmrCntrSetup xTimerSettings[ tmrTIMERS_USED ] =\r
132 {\r
133         { tmrTIMER_0_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
134         { tmrTIMER_1_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
135         { tmrTIMER_2_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE }\r
136 };\r
137 \r
138 /* Lower priority number means higher logical priority, so\r
139 configMAX_API_CALL_INTERRUPT_PRIORITY - 1 is above the maximum system call\r
140 interrupt priority. */\r
141 static const UBaseType_t uxInterruptPriorities[ tmrTIMERS_USED ] =\r
142 {\r
143         configMAX_API_CALL_INTERRUPT_PRIORITY + 1,\r
144         configMAX_API_CALL_INTERRUPT_PRIORITY,\r
145         configMAX_API_CALL_INTERRUPT_PRIORITY - 1\r
146 };\r
147 \r
148 static XTtcPs xTimerInstances[ tmrTIMERS_USED ];\r
149 \r
150 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
151 actually being reached. */\r
152 extern uint32_t ulPortInterruptNesting;\r
153 static uint32_t ulMaxRecordedNesting = 0;\r
154 \r
155 /* Used to ensure the high frequency timer is running at the expected\r
156 frequency. */\r
157 static volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
158 \r
159 /*-----------------------------------------------------------*/\r
160 \r
161 void vInitialiseTimerForIntQueueTest( void )\r
162 {\r
163 BaseType_t xStatus;\r
164 TmrCntrSetup *pxTimerSettings;\r
165 extern XScuGic xInterruptController;\r
166 BaseType_t xTimer;\r
167 XTtcPs *pxTimerInstance;\r
168 XTtcPs_Config *pxTimerConfiguration;\r
169 const uint8_t ucRisingEdge = 3;\r
170 \r
171         for( xTimer = 0; xTimer < tmrTIMERS_USED; xTimer++ )\r
172         {\r
173                 /* Look up the timer's configuration. */\r
174                 pxTimerInstance = &( xTimerInstances[ xTimer ] );\r
175                 pxTimerConfiguration = XTtcPs_LookupConfig( xDeviceIDs[ xTimer ] );\r
176                 configASSERT( pxTimerConfiguration );\r
177 \r
178                 pxTimerSettings = &( xTimerSettings[ xTimer ] );\r
179 \r
180                 /* Initialise the device. */\r
181                 xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
182                 if( xStatus != XST_SUCCESS )\r
183                 {\r
184                         /* Not sure how to do this before XTtcPs_CfgInitialize is called\r
185                         as pxTimerInstance is set within XTtcPs_CfgInitialize(). */\r
186                         XTtcPs_Stop( pxTimerInstance );\r
187                         xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
188                         configASSERT( xStatus == XST_SUCCESS );\r
189                 }\r
190 \r
191                 /* Set the options. */\r
192                 XTtcPs_SetOptions( pxTimerInstance, pxTimerSettings->Options );\r
193 \r
194                 /* The timer frequency is preset in the pxTimerSettings structure.\r
195                 Derive the values for the other structure members. */\r
196                 XTtcPs_CalcIntervalFromFreq( pxTimerInstance, pxTimerSettings->OutputHz, &( pxTimerSettings->Interval ), &( pxTimerSettings->Prescaler ) );\r
197 \r
198                 /* Set the interval and prescale. */\r
199                 XTtcPs_SetInterval( pxTimerInstance, pxTimerSettings->Interval );\r
200                 XTtcPs_SetPrescaler( pxTimerInstance, pxTimerSettings->Prescaler );\r
201 \r
202                 /* The priority must be the lowest possible. */\r
203                 XScuGic_SetPriorityTriggerType( &xInterruptController, xInterruptIDs[ xTimer ], uxInterruptPriorities[ xTimer ] << portPRIORITY_SHIFT, ucRisingEdge );\r
204 \r
205                 /* Connect to the interrupt controller. */\r
206                 xStatus = XScuGic_Connect( &xInterruptController, xInterruptIDs[ xTimer ], ( Xil_InterruptHandler ) prvTimerHandler, ( void * ) pxTimerInstance );\r
207                 configASSERT( xStatus == XST_SUCCESS);\r
208 \r
209                 /* Enable the interrupt in the GIC. */\r
210                 XScuGic_Enable( &xInterruptController, xInterruptIDs[ xTimer ] );\r
211 \r
212                 /* Enable the interrupts in the timer. */\r
213                 XTtcPs_EnableInterrupts( pxTimerInstance, XTTCPS_IXR_INTERVAL_MASK );\r
214 \r
215                 /* Start the timer. */\r
216                 XTtcPs_Start( pxTimerInstance );\r
217         }\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvTimerHandler( void *pvCallBackRef )\r
222 {\r
223 uint32_t ulInterruptStatus;\r
224 XTtcPs *pxTimer = ( XTtcPs * ) pvCallBackRef;\r
225 BaseType_t xYieldRequired;\r
226 \r
227 #if( configASSERT_DEFINED == 1 )\r
228         /* Test floating point access within nested interrupts. */\r
229         volatile long double d1, d2;\r
230 #endif\r
231 \r
232 \r
233         /* Read the interrupt status, then write it back to clear the interrupt. */\r
234         ulInterruptStatus = XTtcPs_GetInterruptStatus( pxTimer );\r
235         XTtcPs_ClearInterruptStatus( pxTimer, ulInterruptStatus );\r
236 \r
237         /* Only one interrupt event type is expected. */\r
238         configASSERT( ( XTTCPS_IXR_INTERVAL_MASK & ulInterruptStatus ) != 0 );\r
239 \r
240         /* Check the device ID to know which IntQueue demo to call. */\r
241         if( pxTimer->Config.DeviceId == xDeviceIDs[ 0 ] )\r
242         {\r
243                 #if( configASSERT_DEFINED == 1 )\r
244                 {\r
245                         /* Test floating point access within nested interrupts. */\r
246                         d1 = 1.5L;\r
247                         d2 = 5.25L;\r
248                 }\r
249                 #endif /* configASSERT_DEFINED */\r
250 \r
251                 xYieldRequired = xFirstTimerHandler();\r
252 \r
253                 /* Will fail eventually if flop context switch is not correct in\r
254                 interrupts.  Keep calculation simple so the answer is exact even when\r
255                 using flop. */\r
256                 configASSERT( ( d1 * d2 ) == ( 1.5L * 5.25L ) );\r
257         }\r
258         else if( pxTimer->Config.DeviceId == xDeviceIDs[ 1 ] )\r
259         {\r
260                 #if( configASSERT_DEFINED == 1 )\r
261                 {\r
262                         /* Test floating point access within nested interrupts. */\r
263                         d1 = 10.5L;\r
264                         d2 = 5.5L;\r
265                 }\r
266                 #endif /* configASSERT_DEFINED */\r
267 \r
268                 xYieldRequired = xSecondTimerHandler();\r
269 \r
270                 /* Will fail eventually if flop context switch is not correct in\r
271                 interrupts.  Keep calculation simple so the answer is exact even when\r
272                 using flop. */\r
273                 configASSERT( ( d1 / d2 ) == ( 10.5L / 5.5L ) );\r
274         }\r
275         else\r
276         {\r
277                 /* Used to check the timer is running at the expected frequency. */\r
278                 ulHighFrequencyTimerCounts++;\r
279 \r
280                 /* Latch the highest interrupt nesting count detected. */\r
281                 if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
282                 {\r
283                         ulMaxRecordedNesting = ulPortInterruptNesting;\r
284                 }\r
285 \r
286                 xYieldRequired = pdFALSE;\r
287         }\r
288 \r
289         /* If xYieldRequired is not pdFALSE then calling either xFirstTimerHandler()\r
290         or xSecondTimerHandler() resulted in a task leaving the blocked state and\r
291         the task that left the blocked state had a priority higher than the currently\r
292         running task (the task this interrupt interrupted) - so a context switch\r
293         should be performed so the interrupt returns directly to the higher priority\r
294         task.  xYieldRequired is tested inside the following macro. */\r
295         portYIELD_FROM_ISR( xYieldRequired );\r
296 }\r
297 \r