]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53/src/Full_Demo/IntQueueTimer.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53 / src / Full_Demo / IntQueueTimer.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * NOTE:  Currently only timer 1 and timer 2 are used -\r
31  *\r
32  * This file initialises two timers as follows:\r
33  *\r
34  * Timer 0 and Timer 1 provide the interrupts that are used with the IntQ\r
35  * standard demo tasks, which test interrupt nesting and using queues from\r
36  * interrupts.  Both these interrupts operate below the maximum syscall\r
37  * interrupt priority.\r
38  *\r
39  * Timer 2 is a much higher frequency timer that tests the nesting of interrupts\r
40  * that execute above the maximum syscall interrupt priority.\r
41  *\r
42  * All the timers can nest with the tick interrupt - creating a maximum\r
43  * interrupt nesting depth of 4.\r
44  *\r
45  * For convenience, the high frequency timer is also used to provide the time\r
46  * base for the run time stats.\r
47  */\r
48 \r
49 /* Scheduler includes. */\r
50 #include "FreeRTOS.h"\r
51 \r
52 /* Demo includes. */\r
53 #include "IntQueueTimer.h"\r
54 #include "IntQueue.h"\r
55 \r
56 /* Xilinx includes. */\r
57 #include "xttcps.h"\r
58 #include "xscugic.h"\r
59 \r
60 /* The frequencies at which the first two timers expire are slightly offset to\r
61 ensure they don't remain synchronised.  The frequency of the interrupt that\r
62 operates above the max syscall interrupt priority is 10 times faster so really\r
63 hammers the interrupt entry and exit code. */\r
64 #define tmrTIMERS_USED  3\r
65 #define tmrTIMER_0_FREQUENCY    ( 100UL )\r
66 #define tmrTIMER_1_FREQUENCY    ( 111UL )\r
67 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /*\r
72  * The single interrupt service routines that is used to service all three\r
73  * timers.\r
74  */\r
75 static void prvTimerHandler( void *CallBackRef );\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /* Hardware constants. */\r
80 static const BaseType_t xDeviceIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_DEVICE_ID, XPAR_XTTCPS_1_DEVICE_ID, XPAR_XTTCPS_2_DEVICE_ID };\r
81 static const BaseType_t xInterruptIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_INTR, XPAR_XTTCPS_1_INTR, XPAR_XTTCPS_2_INTR };\r
82 \r
83 /* Timer configuration settings. */\r
84 typedef struct\r
85 {\r
86         uint32_t OutputHz;      /* Output frequency. */\r
87         uint16_t Interval;      /* Interval value. */\r
88         uint8_t Prescaler;      /* Prescaler value. */\r
89         uint16_t Options;       /* Option settings. */\r
90 } TmrCntrSetup;\r
91 \r
92 static TmrCntrSetup xTimerSettings[ tmrTIMERS_USED ] =\r
93 {\r
94         { tmrTIMER_0_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
95         { tmrTIMER_1_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },\r
96         { tmrTIMER_2_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE }\r
97 };\r
98 \r
99 /* Lower priority number means higher logical priority, so\r
100 configMAX_API_CALL_INTERRUPT_PRIORITY - 1 is above the maximum system call\r
101 interrupt priority. */\r
102 static const UBaseType_t uxInterruptPriorities[ tmrTIMERS_USED ] =\r
103 {\r
104         configMAX_API_CALL_INTERRUPT_PRIORITY + 1,\r
105         configMAX_API_CALL_INTERRUPT_PRIORITY,\r
106         configMAX_API_CALL_INTERRUPT_PRIORITY - 1\r
107 };\r
108 \r
109 static XTtcPs xTimerInstances[ tmrTIMERS_USED ];\r
110 \r
111 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
112 actually being reached. */\r
113 extern uint64_t ullPortInterruptNesting;\r
114 static volatile uint32_t ulMaxRecordedNesting = 1;\r
115 \r
116 /* Used to ensure the high frequency timer is running at the expected\r
117 frequency. */\r
118 static volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 void vInitialiseTimerForIntQueueTest( void )\r
123 {\r
124 BaseType_t xStatus;\r
125 TmrCntrSetup *pxTimerSettings;\r
126 extern XScuGic xInterruptController;\r
127 BaseType_t xTimer;\r
128 XTtcPs *pxTimerInstance;\r
129 XTtcPs_Config *pxTimerConfiguration;\r
130 const uint8_t ucRisingEdge = 3;\r
131 \r
132         /*_RB_ Currently only timer 1 and timer 2 are used. */\r
133         for( xTimer = 0; xTimer < ( tmrTIMERS_USED - 1 ); xTimer++ )\r
134         {\r
135                 /* Look up the timer's configuration. */\r
136                 pxTimerInstance = &( xTimerInstances[ xTimer ] );\r
137                 pxTimerConfiguration = XTtcPs_LookupConfig( xDeviceIDs[ xTimer ] );\r
138                 configASSERT( pxTimerConfiguration );\r
139 \r
140                 pxTimerSettings = &( xTimerSettings[ xTimer ] );\r
141 \r
142                 /* Initialise the device. */\r
143                 xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
144                 if( xStatus != XST_SUCCESS )\r
145                 {\r
146                         /* Not sure how to do this before XTtcPs_CfgInitialize is called\r
147                         as pxTimerInstance is set within XTtcPs_CfgInitialize(). */\r
148                         XTtcPs_Stop( pxTimerInstance );\r
149                         xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );\r
150                         configASSERT( xStatus == XST_SUCCESS );\r
151                 }\r
152 \r
153                 /* Set the options. */\r
154                 XTtcPs_SetOptions( pxTimerInstance, pxTimerSettings->Options );\r
155 \r
156                 /* The timer frequency is preset in the pxTimerSettings structure.\r
157                 Derive the values for the other structure members. */\r
158                 XTtcPs_CalcIntervalFromFreq( pxTimerInstance, pxTimerSettings->OutputHz, &( pxTimerSettings->Interval ), &( pxTimerSettings->Prescaler ) );\r
159 \r
160                 /* Set the interval and prescale. */\r
161                 XTtcPs_SetInterval( pxTimerInstance, pxTimerSettings->Interval );\r
162                 XTtcPs_SetPrescaler( pxTimerInstance, pxTimerSettings->Prescaler );\r
163 \r
164                 /* The priority must be the lowest possible. */\r
165                 XScuGic_SetPriorityTriggerType( &xInterruptController, xInterruptIDs[ xTimer ], uxInterruptPriorities[ xTimer ] << portPRIORITY_SHIFT, ucRisingEdge );\r
166 \r
167                 /* Connect to the interrupt controller. */\r
168                 xStatus = XScuGic_Connect( &xInterruptController, xInterruptIDs[ xTimer ], ( Xil_InterruptHandler ) prvTimerHandler, ( void * ) pxTimerInstance );\r
169                 configASSERT( xStatus == XST_SUCCESS);\r
170 \r
171                 /* Enable the interrupt in the GIC. */\r
172                 XScuGic_Enable( &xInterruptController, xInterruptIDs[ xTimer ] );\r
173 \r
174                 /* Enable the interrupts in the timer. */\r
175                 XTtcPs_EnableInterrupts( pxTimerInstance, XTTCPS_IXR_INTERVAL_MASK );\r
176 \r
177                 /* Start the timer. */\r
178                 XTtcPs_Start( pxTimerInstance );\r
179         }\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void prvTimerHandler( void *pvCallBackRef )\r
184 {\r
185 uint32_t ulInterruptStatus;\r
186 XTtcPs *pxTimer = ( XTtcPs * ) pvCallBackRef;\r
187 BaseType_t xYieldRequired;\r
188 \r
189         /* Read the interrupt status, then write it back to clear the interrupt. */\r
190         ulInterruptStatus = XTtcPs_GetInterruptStatus( pxTimer );\r
191         XTtcPs_ClearInterruptStatus( pxTimer, ulInterruptStatus );\r
192         __asm volatile( "DSB SY" );\r
193         __asm volatile( "ISB SY" );\r
194 \r
195 \r
196         /* Now the interrupt has been cleared, interrupts can be re-enabled. */\r
197         portENABLE_INTERRUPTS();\r
198 \r
199         /* Only one interrupt event type is expected. */\r
200         configASSERT( ( XTTCPS_IXR_INTERVAL_MASK & ulInterruptStatus ) != 0 );\r
201 \r
202         /* Check the device ID to know which IntQueue demo to call. */\r
203         if( pxTimer->Config.DeviceId == xDeviceIDs[ 0 ] )\r
204         {\r
205                 xYieldRequired = xFirstTimerHandler();\r
206         }\r
207         else if( pxTimer->Config.DeviceId == xDeviceIDs[ 1 ] )\r
208         {\r
209                 xYieldRequired = xSecondTimerHandler();\r
210         }\r
211         else\r
212         {\r
213                 /* Used to check the timer is running at the expected frequency. */\r
214                 ulHighFrequencyTimerCounts++;\r
215                 xYieldRequired = pdFALSE;\r
216         }\r
217 \r
218         /* Latch the highest interrupt nesting count detected. */\r
219         if( ullPortInterruptNesting > ulMaxRecordedNesting )\r
220         {\r
221                 ulMaxRecordedNesting = ullPortInterruptNesting;\r
222         }\r
223 \r
224         /* If xYieldRequired is not pdFALSE then calling either xFirstTimerHandler()\r
225         or xSecondTimerHandler() resulted in a task leaving the blocked state and\r
226         the task that left the blocked state had a priority higher than the currently\r
227         running task (the task this interrupt interrupted) - so a context switch\r
228         should be performed so the interrupt returns directly to the higher priority\r
229         task.  xYieldRequired is tested inside the following macro. */\r
230         portYIELD_FROM_ISR( xYieldRequired );\r
231 }\r
232 \r