]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/Full_Demo/IntQueueTimer.c
0c03b560c834d785af29f4caa08042387e7ef039
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / Full_Demo / IntQueueTimer.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * This file initialises three timers as follows:\r
30  *\r
31  * Timer 0 and Timer 1 provide the interrupts that are used with the IntQ\r
32  * standard demo tasks, which test interrupt nesting and using queues from\r
33  * interrupts.  Both these interrupts operate below the maximum syscall\r
34  * interrupt priority.\r
35  *\r
36  * Timer 2 is a much higher frequency timer that tests the nesting of interrupts\r
37  * that execute above the maximum syscall interrupt priority.\r
38  *\r
39  * All the timers can nest with the tick interrupt - creating a maximum\r
40  * interrupt nesting depth of 4.\r
41  *\r
42  * For convenience, the high frequency timer is also used to provide the time\r
43  * base for the run time stats.\r
44  */\r
45 \r
46 /* Scheduler includes. */\r
47 #include "FreeRTOS.h"\r
48 \r
49 /* Demo includes. */\r
50 #include "IntQueueTimer.h"\r
51 #include "IntQueue.h"\r
52 \r
53 /* Xilinx includes. */\r
54 #include "xttcps.h"\r
55 #include "xscugic.h"\r
56 \r
57 /* The frequencies at which the first two timers expire are slightly offset to\r
58 ensure they don't remain synchronised.  The frequency of the interrupt that\r
59 operates above the max syscall interrupt priority is 10 times faster so really\r
60 hammers the interrupt entry and exit code. */\r
61 #define tmrTIMERS_USED  3\r
62 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
63 #define tmrTIMER_1_FREQUENCY    ( 2001UL )\r
64 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 /*\r
69  * The single interrupt service routines that is used to service all three\r
70  * timers.\r
71  */\r
72 static void prvTimerHandler( void *CallBackRef );\r
73 \r
74 /*-----------------------------------------------------------*/\r
75 \r
76 /* Hardware constants. */\r
77 static const BaseType_t xDeviceIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_DEVICE_ID, XPAR_XTTCPS_1_DEVICE_ID, XPAR_XTTCPS_2_DEVICE_ID };\r
78 static const BaseType_t xInterruptIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_INTR, XPAR_XTTCPS_1_INTR, XPAR_XTTCPS_2_INTR };\r
79 \r
80 /* Timer configuration settings. */\r
81 typedef struct\r
82 {\r
83         uint32_t OutputHz;      /* Output frequency. */\r
84         uint16_t Interval;      /* Interval value. */\r
85         uint8_t Prescaler;      /* Prescaler value. */\r
86         uint16_t Options;       /* Option settings. */\r
87 } TmrCntrSetup;\r
88 \r
89 static TmrCntrSetup xTimerSettings[ tmrTIMERS_USED ] =\r
90 {\r
91         { tmrTIMER_0_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
92         { tmrTIMER_1_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
93         { tmrTIMER_2_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE }\r
94 };\r
95 \r
96 /* Lower priority number means higher logical priority, so\r
97 configMAX_API_CALL_INTERRUPT_PRIORITY - 1 is above the maximum system call\r
98 interrupt priority. */\r
99 static const UBaseType_t uxInterruptPriorities[ tmrTIMERS_USED ] =\r
100 {\r
101         configMAX_API_CALL_INTERRUPT_PRIORITY + 1,\r
102         configMAX_API_CALL_INTERRUPT_PRIORITY,\r
103         configMAX_API_CALL_INTERRUPT_PRIORITY - 1\r
104 };\r
105 \r
106 static XTtcPs xTimerInstances[ tmrTIMERS_USED ];\r
107 \r
108 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
109 actually being reached. */\r
110 extern uint32_t ulPortInterruptNesting;\r
111 static uint32_t ulMaxRecordedNesting = 0;\r
112 \r
113 /* Used to ensure the high frequency timer is running at the expected\r
114 frequency. */\r
115 static volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void vInitialiseTimerForIntQueueTest( void )\r
120 {\r
121 BaseType_t xStatus;\r
122 TmrCntrSetup *pxTimerSettings;\r
123 extern XScuGic xInterruptController;\r
124 BaseType_t xTimer;\r
125 XTtcPs *pxTimerInstance;\r
126 XTtcPs_Config *pxTimerConfiguration;\r
127 const uint8_t ucRisingEdge = 3;\r
128 \r
129         for( xTimer = 0; xTimer < tmrTIMERS_USED; xTimer++ )\r
130         {\r
131                 /* Look up the timer's configuration. */\r
132                 pxTimerInstance = &( xTimerInstances[ xTimer ] );\r
133                 pxTimerConfiguration = XTtcPs_LookupConfig( xDeviceIDs[ xTimer ] );\r
134                 configASSERT( pxTimerConfiguration );\r
135 \r
136                 pxTimerSettings = &( xTimerSettings[ xTimer ] );\r
137 \r
138                 /* Initialise the device. */\r
139                 xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
140                 if( xStatus != XST_SUCCESS )\r
141                 {\r
142                         /* Not sure how to do this before XTtcPs_CfgInitialize is called\r
143                         as pxTimerInstance is set within XTtcPs_CfgInitialize(). */\r
144                         XTtcPs_Stop( pxTimerInstance );\r
145                         xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
146                         configASSERT( xStatus == XST_SUCCESS );\r
147                 }\r
148 \r
149                 /* Set the options. */\r
150                 XTtcPs_SetOptions( pxTimerInstance, pxTimerSettings->Options );\r
151 \r
152                 /* The timer frequency is preset in the pxTimerSettings structure.\r
153                 Derive the values for the other structure members. */\r
154                 XTtcPs_CalcIntervalFromFreq( pxTimerInstance, pxTimerSettings->OutputHz, &( pxTimerSettings->Interval ), &( pxTimerSettings->Prescaler ) );\r
155 \r
156                 /* Set the interval and prescale. */\r
157                 XTtcPs_SetInterval( pxTimerInstance, pxTimerSettings->Interval );\r
158                 XTtcPs_SetPrescaler( pxTimerInstance, pxTimerSettings->Prescaler );\r
159 \r
160                 /* The priority must be the lowest possible. */\r
161                 XScuGic_SetPriorityTriggerType( &xInterruptController, xInterruptIDs[ xTimer ], uxInterruptPriorities[ xTimer ] << portPRIORITY_SHIFT, ucRisingEdge );\r
162 \r
163                 /* Connect to the interrupt controller. */\r
164                 xStatus = XScuGic_Connect( &xInterruptController, xInterruptIDs[ xTimer ], ( Xil_InterruptHandler ) prvTimerHandler, ( void * ) pxTimerInstance );\r
165                 configASSERT( xStatus == XST_SUCCESS);\r
166 \r
167                 /* Enable the interrupt in the GIC. */\r
168                 XScuGic_Enable( &xInterruptController, xInterruptIDs[ xTimer ] );\r
169 \r
170                 /* Enable the interrupts in the timer. */\r
171                 XTtcPs_EnableInterrupts( pxTimerInstance, XTTCPS_IXR_INTERVAL_MASK );\r
172 \r
173                 /* Start the timer. */\r
174                 XTtcPs_Start( pxTimerInstance );\r
175         }\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static void prvTimerHandler( void *pvCallBackRef )\r
180 {\r
181 uint32_t ulInterruptStatus;\r
182 XTtcPs *pxTimer = ( XTtcPs * ) pvCallBackRef;\r
183 BaseType_t xYieldRequired;\r
184 \r
185 #if( configASSERT_DEFINED == 1 )\r
186         /* Test floating point access within nested interrupts. */\r
187         volatile long double d1, d2;\r
188 #endif\r
189 \r
190 \r
191         /* Read the interrupt status, then write it back to clear the interrupt. */\r
192         ulInterruptStatus = XTtcPs_GetInterruptStatus( pxTimer );\r
193         XTtcPs_ClearInterruptStatus( pxTimer, ulInterruptStatus );\r
194 \r
195         /* Only one interrupt event type is expected. */\r
196         configASSERT( ( XTTCPS_IXR_INTERVAL_MASK & ulInterruptStatus ) != 0 );\r
197 \r
198         /* Check the device ID to know which IntQueue demo to call. */\r
199         if( pxTimer->Config.DeviceId == xDeviceIDs[ 0 ] )\r
200         {\r
201                 #if( configASSERT_DEFINED == 1 )\r
202                 {\r
203                         /* Test floating point access within nested interrupts. */\r
204                         d1 = 1.5L;\r
205                         d2 = 5.25L;\r
206                 }\r
207                 #endif /* configASSERT_DEFINED */\r
208 \r
209                 xYieldRequired = xFirstTimerHandler();\r
210 \r
211                 /* Will fail eventually if flop context switch is not correct in\r
212                 interrupts.  Keep calculation simple so the answer is exact even when\r
213                 using flop. */\r
214                 configASSERT( ( d1 * d2 ) == ( 1.5L * 5.25L ) );\r
215         }\r
216         else if( pxTimer->Config.DeviceId == xDeviceIDs[ 1 ] )\r
217         {\r
218                 #if( configASSERT_DEFINED == 1 )\r
219                 {\r
220                         /* Test floating point access within nested interrupts. */\r
221                         d1 = 10.5L;\r
222                         d2 = 5.5L;\r
223                 }\r
224                 #endif /* configASSERT_DEFINED */\r
225 \r
226                 xYieldRequired = xSecondTimerHandler();\r
227 \r
228                 /* Will fail eventually if flop context switch is not correct in\r
229                 interrupts.  Keep calculation simple so the answer is exact even when\r
230                 using flop. */\r
231                 configASSERT( ( d1 / d2 ) == ( 10.5L / 5.5L ) );\r
232         }\r
233         else\r
234         {\r
235                 /* Used to check the timer is running at the expected frequency. */\r
236                 ulHighFrequencyTimerCounts++;\r
237 \r
238                 /* Latch the highest interrupt nesting count detected. */\r
239                 if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
240                 {\r
241                         ulMaxRecordedNesting = ulPortInterruptNesting;\r
242                 }\r
243 \r
244                 xYieldRequired = pdFALSE;\r
245         }\r
246 \r
247         /* If xYieldRequired is not pdFALSE then calling either xFirstTimerHandler()\r
248         or xSecondTimerHandler() resulted in a task leaving the blocked state and\r
249         the task that left the blocked state had a priority higher than the currently\r
250         running task (the task this interrupt interrupted) - so a context switch\r
251         should be performed so the interrupt returns directly to the higher priority\r
252         task.  xYieldRequired is tested inside the following macro. */\r
253         portYIELD_FROM_ISR( xYieldRequired );\r
254 }\r
255 \r