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