]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R5_UltraScale_MPSoC/RTOSDemo_R5/src/Full_Demo/IntQueueTimer.c
aadbc02468f66facee2504ae0788ece7ebf59f57
[freertos] / FreeRTOS / Demo / CORTEX_R5_UltraScale_MPSoC / RTOSDemo_R5 / src / Full_Demo / IntQueueTimer.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 #include "task.h"\r
49 \r
50 /* Demo includes. */\r
51 #include "IntQueueTimer.h"\r
52 #include "IntQueue.h"\r
53 \r
54 /* Xilinx includes. */\r
55 #include "xttcps.h"\r
56 #include "xscugic.h"\r
57 \r
58 /* The frequencies at which the first two timers expire are slightly offset to\r
59 ensure they don't remain synchronised.  The frequency of the interrupt that\r
60 operates above the max syscall interrupt priority is 10 times faster so really\r
61 hammers the interrupt entry and exit code. */\r
62 #define tmrTIMERS_USED  3\r
63 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
64 #define tmrTIMER_1_FREQUENCY    ( 2001UL )\r
65 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /*\r
70  * The single interrupt service routines that is used to service all three\r
71  * timers.\r
72  */\r
73 static void prvTimerHandler( void *CallBackRef );\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /* Hardware constants. */\r
78 static const BaseType_t xDeviceIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_2_DEVICE_ID, XPAR_XTTCPS_3_DEVICE_ID, XPAR_XTTCPS_4_DEVICE_ID };\r
79 static const BaseType_t xInterruptIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_2_INTR, XPAR_XTTCPS_3_INTR, XPAR_XTTCPS_4_INTR };\r
80 \r
81 /* Timer configuration settings. */\r
82 typedef struct\r
83 {\r
84         uint32_t OutputHz;      /* Output frequency. */\r
85         uint16_t Interval;      /* Interval value. */\r
86         uint8_t Prescaler;      /* Prescaler value. */\r
87         uint16_t Options;       /* Option settings. */\r
88 } TmrCntrSetup;\r
89 \r
90 static TmrCntrSetup xTimerSettings[ tmrTIMERS_USED ] =\r
91 {\r
92         { tmrTIMER_0_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
93         { tmrTIMER_1_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
94         { tmrTIMER_2_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE }\r
95 };\r
96 \r
97 /* Lower priority number means higher logical priority, so\r
98 configMAX_API_CALL_INTERRUPT_PRIORITY - 1 is above the maximum system call\r
99 interrupt priority. */\r
100 static const UBaseType_t uxInterruptPriorities[ tmrTIMERS_USED ] =\r
101 {\r
102         configMAX_API_CALL_INTERRUPT_PRIORITY + 1,\r
103         configMAX_API_CALL_INTERRUPT_PRIORITY,\r
104         configMAX_API_CALL_INTERRUPT_PRIORITY - 1\r
105 };\r
106 \r
107 static XTtcPs xTimerInstances[ tmrTIMERS_USED ];\r
108 \r
109 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
110 actually being reached. */\r
111 extern uint32_t ulPortInterruptNesting;\r
112 static uint32_t ulMaxRecordedNesting = 0;\r
113 \r
114 /* Used to ensure the high frequency timer is running at the expected\r
115 frequency. */\r
116 static volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 void vInitialiseTimerForIntQueueTest( void )\r
121 {\r
122 BaseType_t xStatus;\r
123 TmrCntrSetup *pxTimerSettings;\r
124 extern XScuGic xInterruptController;\r
125 BaseType_t xTimer;\r
126 XTtcPs *pxTimerInstance;\r
127 XTtcPs_Config *pxTimerConfiguration;\r
128 const uint8_t ucRisingEdge = 3;\r
129 \r
130         for( xTimer = 0; xTimer < tmrTIMERS_USED; xTimer++ )\r
131         {\r
132                 /* Look up the timer's configuration. */\r
133                 pxTimerInstance = &( xTimerInstances[ xTimer ] );\r
134                 pxTimerConfiguration = XTtcPs_LookupConfig( xDeviceIDs[ xTimer ] );\r
135                 configASSERT( pxTimerConfiguration );\r
136 \r
137                 pxTimerSettings = &( xTimerSettings[ xTimer ] );\r
138 \r
139                 /* Initialise the device. */\r
140                 xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
141                 if( xStatus != XST_SUCCESS )\r
142                 {\r
143                         /* Not sure how to do this before XTtcPs_CfgInitialize is called\r
144                         as pxTimerInstance is set within XTtcPs_CfgInitialize(). */\r
145                         XTtcPs_Stop( pxTimerInstance );\r
146                         xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
147                         configASSERT( xStatus == XST_SUCCESS );\r
148                 }\r
149 \r
150                 /* Set the options. */\r
151                 XTtcPs_SetOptions( pxTimerInstance, pxTimerSettings->Options );\r
152 \r
153                 /* The timer frequency is preset in the pxTimerSettings structure.\r
154                 Derive the values for the other structure members. */\r
155                 XTtcPs_CalcIntervalFromFreq( pxTimerInstance, pxTimerSettings->OutputHz, &( pxTimerSettings->Interval ), &( pxTimerSettings->Prescaler ) );\r
156 \r
157                 /* Set the interval and prescale. */\r
158                 XTtcPs_SetInterval( pxTimerInstance, pxTimerSettings->Interval );\r
159                 XTtcPs_SetPrescaler( pxTimerInstance, pxTimerSettings->Prescaler );\r
160 \r
161                 /* The priority must be the lowest possible. */\r
162                 XScuGic_SetPriorityTriggerType( &xInterruptController, xInterruptIDs[ xTimer ], uxInterruptPriorities[ xTimer ] << portPRIORITY_SHIFT, ucRisingEdge );\r
163 \r
164                 /* Connect to the interrupt controller. */\r
165                 xStatus = XScuGic_Connect( &xInterruptController, xInterruptIDs[ xTimer ], ( Xil_InterruptHandler ) prvTimerHandler, ( void * ) pxTimerInstance );\r
166                 configASSERT( xStatus == XST_SUCCESS);\r
167 \r
168                 /* Enable the interrupt in the GIC. */\r
169                 XScuGic_Enable( &xInterruptController, xInterruptIDs[ xTimer ] );\r
170 \r
171                 /* Enable the interrupts in the timer. */\r
172                 XTtcPs_EnableInterrupts( pxTimerInstance, XTTCPS_IXR_INTERVAL_MASK );\r
173 \r
174                 /* Start the timer. */\r
175                 XTtcPs_Start( pxTimerInstance );\r
176         }\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 static void prvTimerHandler( void *pvCallBackRef )\r
181 {\r
182 uint32_t ulInterruptStatus;\r
183 XTtcPs *pxTimer = ( XTtcPs * ) pvCallBackRef;\r
184 BaseType_t xYieldRequired;\r
185 \r
186         /* Read the interrupt status, then write it back to clear the interrupt. */\r
187         ulInterruptStatus = XTtcPs_GetInterruptStatus( pxTimer );\r
188         XTtcPs_ClearInterruptStatus( pxTimer, ulInterruptStatus );\r
189 \r
190         /* Only one interrupt event type is expected. */\r
191         configASSERT( ( XTTCPS_IXR_INTERVAL_MASK & ulInterruptStatus ) != 0 );\r
192 \r
193         /* Check the device ID to know which IntQueue demo to call. */\r
194         if( pxTimer->Config.DeviceId == xDeviceIDs[ 0 ] )\r
195         {\r
196                 xYieldRequired = xFirstTimerHandler();\r
197         }\r
198         else if( pxTimer->Config.DeviceId == xDeviceIDs[ 1 ] )\r
199         {\r
200                 xYieldRequired = xSecondTimerHandler();\r
201         }\r
202         else\r
203         {\r
204                 /* Used to check the timer is running at the expected frequency. */\r
205                 ulHighFrequencyTimerCounts++;\r
206 \r
207                 /* Latch the highest interrupt nesting count detected. */\r
208                 if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
209                 {\r
210                         ulMaxRecordedNesting = ulPortInterruptNesting;\r
211                 }\r
212 \r
213                 xYieldRequired = pdFALSE;\r
214         }\r
215 \r
216         /* If xYieldRequired is not pdFALSE then calling either xFirstTimerHandler()\r
217         or xSecondTimerHandler() resulted in a task leaving the blocked state and\r
218         the task that left the blocked state had a priority higher than the currently\r
219         running task (the task this interrupt interrupted) - so a context switch\r
220         should be performed so the interrupt returns directly to the higher priority\r
221         task.  xYieldRequired is tested inside the following macro. */\r
222         portYIELD_FROM_ISR( xYieldRequired );\r
223 }\r
224 \r