]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/Full_Demo/IntQueueTimer.c
aa5b1dfb53684a9a7dc4691f47d07f30275e7479
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D2x_Xplained_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 "peripherals/pio.h"\r
57 #include "peripherals/pmc.h"\r
58 #include "peripherals/aic.h"\r
59 #include "peripherals/tc.h"\r
60 #include "board.h"\r
61 \r
62 /* The frequencies at which the first two timers expire are slightly offset to\r
63 ensure they don't remain synchronised.  The frequency of the highest priority\r
64 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
65 code. */\r
66 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
67 #define tmrTIMER_1_FREQUENCY    ( 1690UL )\r
68 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
69 \r
70 /* The channels used in TC0 for generating the three interrupts. */\r
71 #define tmrTC0_CHANNEL_0                0 /* At tmrTIMER_0_FREQUENCY */\r
72 #define tmrTC0_CHANNEL_1                1 /* At tmrTIMER_1_FREQUENCY */\r
73 #define tmrTC1_CHANNEL_0                0 /* At tmrTIMER_2_FREQUENCY */\r
74 \r
75 /* The bit within the RC_SR register that indicates an RC compare. */\r
76 #define tmrRC_COMPARE                   ( 1UL << 4UL )\r
77 \r
78 /* The high frequency interrupt given the highest priority or all.  The priority\r
79 of the lower frequency timers must still be above the tick interrupt priority. */\r
80 #define tmrLOWER_PRIORITY               1\r
81 #define tmrHIGHER_PRIORITY              5\r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /* Handlers for the two timer peripherals - two channels are used in the TC0\r
85 timer. */\r
86 static void prvTC0_Handler( void );\r
87 static void prvTC1_Handler( void );\r
88 \r
89 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
90 actually being reached. */\r
91 extern uint32_t ulPortInterruptNesting;\r
92 static uint32_t ulMaxRecordedNesting = 0;\r
93 \r
94 /* For convenience the high frequency timer increments a variable that is then\r
95 used as the time base for the run time stats. */\r
96 volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 void vInitialiseTimerForIntQueueTest( void )\r
101 {\r
102         /* Enable the TC clocks. */\r
103         pmc_enable_peripheral( ID_TC0 );\r
104         pmc_enable_peripheral( ID_TC1 );\r
105 \r
106         /* Configure TC0 channel 0 for a tmrTIMER_0_FREQUENCY frequency and trigger\r
107         on RC compare. */\r
108         tc_trigger_on_freq( TC0, tmrTC0_CHANNEL_0, tmrTIMER_0_FREQUENCY );\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. */\r
113         tc_trigger_on_freq( TC0, tmrTC0_CHANNEL_1, tmrTIMER_1_FREQUENCY );\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. */\r
118         tc_trigger_on_freq( TC1, tmrTC1_CHANNEL_0, tmrTIMER_2_FREQUENCY );\r
119         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
120 \r
121         /* Enable interrupts and start the timers. */\r
122         aic_configure( ID_TC0, tmrLOWER_PRIORITY );\r
123         aic_set_source_vector( ID_TC0, prvTC0_Handler );\r
124         aic_configure( ID_TC1, tmrHIGHER_PRIORITY );\r
125         aic_set_source_vector( ID_TC1, prvTC1_Handler );\r
126         aic_enable( ID_TC0 );\r
127         aic_enable( ID_TC1 );\r
128         tc_start( TC0, tmrTC0_CHANNEL_0 );\r
129         tc_start( TC0, tmrTC0_CHANNEL_1 );\r
130         tc_start( TC1, tmrTC1_CHANNEL_0 );\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 static void prvTC0_Handler( void )\r
135 {\r
136     /* Read will clear the status bit. */\r
137         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
138         {\r
139                 portYIELD_FROM_ISR( xFirstTimerHandler() );\r
140         }\r
141 \r
142         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
143         {\r
144                 portYIELD_FROM_ISR( xSecondTimerHandler() );\r
145         }\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 static void prvTC1_Handler( void )\r
150 {\r
151 volatile uint32_t ulDummy;\r
152 \r
153     /* Dummy read to clear status bit. */\r
154     ulDummy = TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_SR;\r
155 \r
156         /* Latch the maximum nesting count. */\r
157         if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
158         {\r
159                 ulMaxRecordedNesting = ulPortInterruptNesting;\r
160         }\r
161 \r
162         /* Keep a count of the number of interrupts to use as a time base for the\r
163         run-time stats. */\r
164         ulHighFrequencyTimerCounts++;\r
165 }\r
166 \r