]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D4x_EK_IAR/Full_Demo/IntQueueTimer.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D4x_EK_IAR / 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  * TC0 channels 0 and 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.  As the interrupt is shared the nesting achieved is not as deep\r
35  * as normal when this test is executed, but still worth while.\r
36  *\r
37  * TC2 channel 0 provides a much higher frequency timer that tests the nesting\r
38  * of interrupts that don't use the FreeRTOS API.  For convenience, the high\r
39  * frequency timer also keeps a count of the number of time it executes, and the\r
40  * count is used as the time base for the run time stats (which can be viewed\r
41  * through the CLI).\r
42  *\r
43  * All the timers can nest with the tick interrupt - creating a maximum\r
44  * interrupt nesting depth of 3 (normally 4, if the first two timers used\r
45  * separate interrupts).\r
46  *\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 /* Library includes. */\r
57 #include "board.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 highest priority\r
61 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
62 code. */\r
63 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
64 #define tmrTIMER_1_FREQUENCY    ( 2003UL )\r
65 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
66 \r
67 /* The channels used in TC0 for generating the three interrupts. */\r
68 #define tmrTC0_CHANNEL_0                0 /* At tmrTIMER_0_FREQUENCY */\r
69 #define tmrTC0_CHANNEL_1                1 /* At tmrTIMER_1_FREQUENCY */\r
70 #define tmrTC1_CHANNEL_0                0 /* At tmrTIMER_2_FREQUENCY */\r
71 \r
72 /* The bit within the RC_SR register that indicates an RC compare. */\r
73 #define tmrRC_COMPARE                   ( 1UL << 4UL )\r
74 \r
75 /* The high frequency interrupt given the highest priority or all.  The priority\r
76 of the lower frequency timers must still be above the tick interrupt priority. */\r
77 #define tmrLOWER_PRIORITY               3\r
78 #define tmrHIGHER_PRIORITY              5\r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* Handlers for the two timer peripherals - two channels are used in the TC0\r
82 timer. */\r
83 static void prvTC0_Handler( void );\r
84 static void prvTC1_Handler( void );\r
85 \r
86 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
87 actually being reached. */\r
88 extern uint32_t ulPortInterruptNesting;\r
89 static uint32_t ulMaxRecordedNesting = 0;\r
90 \r
91 /* For convenience the high frequency timer increments a variable that is then\r
92 used as the time base for the run time stats. */\r
93 volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 void vInitialiseTimerForIntQueueTest( void )\r
98 {\r
99 const uint32_t ulDivider = 128UL, ulTCCLKS = 3UL;\r
100 \r
101         /* Enable the TC clocks. */\r
102         PMC_EnablePeripheral( ID_TC0 );\r
103         PMC_EnablePeripheral( ID_TC1 );\r
104 \r
105         /* Configure TC0 channel 0 for a tmrTIMER_0_FREQUENCY frequency and trigger\r
106         on RC compare.  This is part of the IntQTimer test. */\r
107         TC_Configure( TC0, tmrTC0_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
108         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_RC = ( BOARD_MCK / 2 ) / ( tmrTIMER_0_FREQUENCY * ulDivider );\r
109         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
110 \r
111         /* Configure TC0 channel 1 for a tmrTIMER_1_FREQUENCY frequency and trigger\r
112         on RC compare.  This is part of the IntQTimer test. */\r
113         TC_Configure( TC0, tmrTC0_CHANNEL_1, ulTCCLKS | TC_CMR_CPCTRG );\r
114         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_RC = ( BOARD_MCK / 2 ) / ( tmrTIMER_1_FREQUENCY * ulDivider );\r
115         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_IER = TC_IER_CPCS;\r
116 \r
117         /* Configure TC1 channel 0 tmrTIMER_2_FREQUENCY frequency and trigger on\r
118         RC compare.  This is the very high frequency timer. */\r
119         TC_Configure( TC1, tmrTC1_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
120         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_RC = BOARD_MCK / ( tmrTIMER_2_FREQUENCY * ulDivider );\r
121         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
122 \r
123         /* First setup TC0 interrupt, in which two channels are used. */\r
124     AIC->AIC_SSR = ID_TC0;\r
125 \r
126         /* Ensure the interrupt is disabled before setting mode and handler. */\r
127     AIC->AIC_IDCR = AIC_IDCR_INTD;\r
128     AIC->AIC_SMR  = AIC_SMR_SRCTYPE_EXT_POSITIVE_EDGE |  tmrLOWER_PRIORITY;\r
129     AIC->AIC_SVR = ( uint32_t ) prvTC0_Handler;\r
130 \r
131         /* Start with the interrupt clear. */\r
132     AIC->AIC_ICCR = AIC_ICCR_INTCLR;\r
133 \r
134         /* Do the same for TC1 - which is the high frequency timer. */\r
135     AIC->AIC_SSR = ID_TC1;\r
136     AIC->AIC_IDCR = AIC_IDCR_INTD;\r
137     AIC->AIC_SMR  = AIC_SMR_SRCTYPE_EXT_POSITIVE_EDGE | tmrHIGHER_PRIORITY;\r
138     AIC->AIC_SVR = ( uint32_t ) prvTC1_Handler;\r
139     AIC->AIC_ICCR = AIC_ICCR_INTCLR;\r
140 \r
141         /* Finally enable the interrupts and start the timers. */\r
142         AIC_EnableIT( ID_TC0 );\r
143         AIC_EnableIT( ID_TC1 );\r
144         TC_Start( TC0, tmrTC0_CHANNEL_0 );\r
145         TC_Start( TC0, tmrTC0_CHANNEL_1 );\r
146         TC_Start( TC1, tmrTC1_CHANNEL_0 );\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 static void prvTC0_Handler( void )\r
151 {\r
152 uint32_t ulDidSomething;\r
153 \r
154         do\r
155         {\r
156                 ulDidSomething = pdFALSE;\r
157 \r
158                 /* Read will clear the status bit. */\r
159                 if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
160                 {\r
161                         /* Call the IntQ test function for this channel. */\r
162                         portYIELD_FROM_ISR( xFirstTimerHandler() );\r
163                         ulDidSomething = pdTRUE;\r
164                 }\r
165 \r
166                 if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
167                 {\r
168                         /* Call the IntQ test function for this channel. */\r
169                         portYIELD_FROM_ISR( xSecondTimerHandler() );\r
170                         ulDidSomething = pdTRUE;\r
171                 }\r
172 \r
173         } while( ulDidSomething == pdTRUE );\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 static void prvTC1_Handler( void )\r
178 {\r
179 volatile uint32_t ulDummy;\r
180 \r
181     /* Dummy read to clear status bit. */\r
182     ulDummy = TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_SR;\r
183 \r
184         /* Latch the maximum nesting count. */\r
185         if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
186         {\r
187                 ulMaxRecordedNesting = ulPortInterruptNesting;\r
188         }\r
189 \r
190         /* Keep a count of the number of interrupts to use as a time base for the\r
191         run-time stats. */\r
192         ulHighFrequencyTimerCounts++;\r
193 }\r
194 \r