]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_A5_SAMA5D4x_EK_IAR/Full_Demo/IntQueueTimer.c
Updated pack to FreeRTOS 10.4.4
[cmsis-freertos] / Demo / CORTEX_A5_SAMA5D4x_EK_IAR / Full_Demo / IntQueueTimer.c
1 /*
2  * FreeRTOS V202107.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29  * This file initialises three timers as follows:
30  *
31  * TC0 channels 0 and 1 provide the interrupts that are used with the IntQ
32  * standard demo tasks, which test interrupt nesting and using queues from
33  * interrupts.  As the interrupt is shared the nesting achieved is not as deep
34  * as normal when this test is executed, but still worth while.
35  *
36  * TC2 channel 0 provides a much higher frequency timer that tests the nesting
37  * of interrupts that don't use the FreeRTOS API.  For convenience, the high
38  * frequency timer also keeps a count of the number of time it executes, and the
39  * count is used as the time base for the run time stats (which can be viewed
40  * through the CLI).
41  *
42  * All the timers can nest with the tick interrupt - creating a maximum
43  * interrupt nesting depth of 3 (normally 4, if the first two timers used
44  * separate interrupts).
45  *
46  */
47
48 /* Scheduler includes. */
49 #include "FreeRTOS.h"
50
51 /* Demo includes. */
52 #include "IntQueueTimer.h"
53 #include "IntQueue.h"
54
55 /* Library includes. */
56 #include "board.h"
57
58 /* The frequencies at which the first two timers expire are slightly offset to
59 ensure they don't remain synchronised.  The frequency of the highest priority
60 interrupt is 20 times faster so really hammers the interrupt entry and exit
61 code. */
62 #define tmrTIMER_0_FREQUENCY    ( 2000UL )
63 #define tmrTIMER_1_FREQUENCY    ( 2003UL )
64 #define tmrTIMER_2_FREQUENCY    ( 20000UL )
65
66 /* The channels used in TC0 for generating the three interrupts. */
67 #define tmrTC0_CHANNEL_0                0 /* At tmrTIMER_0_FREQUENCY */
68 #define tmrTC0_CHANNEL_1                1 /* At tmrTIMER_1_FREQUENCY */
69 #define tmrTC1_CHANNEL_0                0 /* At tmrTIMER_2_FREQUENCY */
70
71 /* The bit within the RC_SR register that indicates an RC compare. */
72 #define tmrRC_COMPARE                   ( 1UL << 4UL )
73
74 /* The high frequency interrupt given the highest priority or all.  The priority
75 of the lower frequency timers must still be above the tick interrupt priority. */
76 #define tmrLOWER_PRIORITY               3
77 #define tmrHIGHER_PRIORITY              5
78 /*-----------------------------------------------------------*/
79
80 /* Handlers for the two timer peripherals - two channels are used in the TC0
81 timer. */
82 static void prvTC0_Handler( void );
83 static void prvTC1_Handler( void );
84
85 /* Used to provide a means of ensuring the intended interrupt nesting depth is
86 actually being reached. */
87 extern uint32_t ulPortInterruptNesting;
88 static uint32_t ulMaxRecordedNesting = 0;
89
90 /* For convenience the high frequency timer increments a variable that is then
91 used as the time base for the run time stats. */
92 volatile uint32_t ulHighFrequencyTimerCounts = 0;
93
94 /*-----------------------------------------------------------*/
95
96 void vInitialiseTimerForIntQueueTest( void )
97 {
98 const uint32_t ulDivider = 128UL, ulTCCLKS = 3UL;
99
100         /* Enable the TC clocks. */
101         PMC_EnablePeripheral( ID_TC0 );
102         PMC_EnablePeripheral( ID_TC1 );
103
104         /* Configure TC0 channel 0 for a tmrTIMER_0_FREQUENCY frequency and trigger
105         on RC compare.  This is part of the IntQTimer test. */
106         TC_Configure( TC0, tmrTC0_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );
107         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_RC = ( BOARD_MCK / 2 ) / ( tmrTIMER_0_FREQUENCY * ulDivider );
108         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_IER = TC_IER_CPCS;
109
110         /* Configure TC0 channel 1 for a tmrTIMER_1_FREQUENCY frequency and trigger
111         on RC compare.  This is part of the IntQTimer test. */
112         TC_Configure( TC0, tmrTC0_CHANNEL_1, ulTCCLKS | TC_CMR_CPCTRG );
113         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_RC = ( BOARD_MCK / 2 ) / ( tmrTIMER_1_FREQUENCY * ulDivider );
114         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_IER = TC_IER_CPCS;
115
116         /* Configure TC1 channel 0 tmrTIMER_2_FREQUENCY frequency and trigger on
117         RC compare.  This is the very high frequency timer. */
118         TC_Configure( TC1, tmrTC1_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );
119         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_RC = BOARD_MCK / ( tmrTIMER_2_FREQUENCY * ulDivider );
120         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_IER = TC_IER_CPCS;
121
122         /* First setup TC0 interrupt, in which two channels are used. */
123     AIC->AIC_SSR = ID_TC0;
124
125         /* Ensure the interrupt is disabled before setting mode and handler. */
126     AIC->AIC_IDCR = AIC_IDCR_INTD;
127     AIC->AIC_SMR  = AIC_SMR_SRCTYPE_EXT_POSITIVE_EDGE |  tmrLOWER_PRIORITY;
128     AIC->AIC_SVR = ( uint32_t ) prvTC0_Handler;
129
130         /* Start with the interrupt clear. */
131     AIC->AIC_ICCR = AIC_ICCR_INTCLR;
132
133         /* Do the same for TC1 - which is the high frequency timer. */
134     AIC->AIC_SSR = ID_TC1;
135     AIC->AIC_IDCR = AIC_IDCR_INTD;
136     AIC->AIC_SMR  = AIC_SMR_SRCTYPE_EXT_POSITIVE_EDGE | tmrHIGHER_PRIORITY;
137     AIC->AIC_SVR = ( uint32_t ) prvTC1_Handler;
138     AIC->AIC_ICCR = AIC_ICCR_INTCLR;
139
140         /* Finally enable the interrupts and start the timers. */
141         AIC_EnableIT( ID_TC0 );
142         AIC_EnableIT( ID_TC1 );
143         TC_Start( TC0, tmrTC0_CHANNEL_0 );
144         TC_Start( TC0, tmrTC0_CHANNEL_1 );
145         TC_Start( TC1, tmrTC1_CHANNEL_0 );
146 }
147 /*-----------------------------------------------------------*/
148
149 static void prvTC0_Handler( void )
150 {
151 uint32_t ulDidSomething;
152
153         do
154         {
155                 ulDidSomething = pdFALSE;
156
157                 /* Read will clear the status bit. */
158                 if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_SR & tmrRC_COMPARE ) != 0 )
159                 {
160                         /* Call the IntQ test function for this channel. */
161                         portYIELD_FROM_ISR( xFirstTimerHandler() );
162                         ulDidSomething = pdTRUE;
163                 }
164
165                 if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_SR & tmrRC_COMPARE ) != 0 )
166                 {
167                         /* Call the IntQ test function for this channel. */
168                         portYIELD_FROM_ISR( xSecondTimerHandler() );
169                         ulDidSomething = pdTRUE;
170                 }
171
172         } while( ulDidSomething == pdTRUE );
173 }
174 /*-----------------------------------------------------------*/
175
176 static void prvTC1_Handler( void )
177 {
178 volatile uint32_t ulDummy;
179
180     /* Dummy read to clear status bit. */
181     ulDummy = TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_SR;
182
183         /* Latch the maximum nesting count. */
184         if( ulPortInterruptNesting > ulMaxRecordedNesting )
185         {
186                 ulMaxRecordedNesting = ulPortInterruptNesting;
187         }
188
189         /* Keep a count of the number of interrupts to use as a time base for the
190         run-time stats. */
191         ulHighFrequencyTimerCounts++;
192 }
193