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