]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained_AtmelStudio/Full_Demo/IntQueueTimer.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained_AtmelStudio / 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 a priority above the maximum at which\r
75 interrupt safe FreeRTOS calls can be made.  The priority of the lower frequency\r
76 timers must still be above the tick interrupt priority. */\r
77 #define tmrLOWER_PRIORITY               configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY\r
78 #define tmrHIGHER_PRIORITY              configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY - 1\r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* For convenience the high frequency timer increments a variable that is then\r
82 used as the time base for the run time stats. */\r
83 volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 void vInitialiseTimerForIntQueueTest( void )\r
88 {\r
89 const uint32_t ulDivider = 128UL, ulTCCLKS = 3UL;\r
90 \r
91         /* Enable the TC clocks. */\r
92         PMC_EnablePeripheral( ID_TC0 );\r
93         PMC_EnablePeripheral( ID_TC1 );\r
94 \r
95         /* Configure TC0 channel 0 for a tmrTIMER_0_FREQUENCY frequency and trigger\r
96         on RC compare.  This is part of the IntQTimer test. */\r
97         TC_Configure( TC0, tmrTC0_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
98         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_RC = ( configCPU_CLOCK_HZ / 2 ) / ( tmrTIMER_0_FREQUENCY * ulDivider );\r
99         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
100 \r
101         /* Configure TC0 channel 1 for a tmrTIMER_1_FREQUENCY frequency and trigger\r
102         on RC compare.  This is part of the IntQTimer test. */\r
103         TC_Configure( TC0, tmrTC0_CHANNEL_1, ulTCCLKS | TC_CMR_CPCTRG );\r
104         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_RC = ( configCPU_CLOCK_HZ / 2 ) / ( tmrTIMER_1_FREQUENCY * ulDivider );\r
105         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_IER = TC_IER_CPCS;\r
106 \r
107         /* Configure and enable TC0 interrupt on RC compare. */\r
108         NVIC_SetPriority( TC0_IRQn, tmrLOWER_PRIORITY );\r
109         NVIC_ClearPendingIRQ( TC0_IRQn );\r
110         NVIC_EnableIRQ( TC0_IRQn );\r
111 \r
112         /* Configure TC1 channel 0 tmrTIMER_2_FREQUENCY frequency and trigger on\r
113         RC compare.  This is the very high frequency timer. */\r
114         TC_Configure( TC1, tmrTC1_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
115         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_RC = ( configCPU_CLOCK_HZ / 2 ) / ( tmrTIMER_2_FREQUENCY * ulDivider );\r
116         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
117 \r
118     /* Configure and enable TC1 interrupt on RC compare */\r
119 //      NVIC_SetPriority( TC1_IRQn, tmrHIGHER_PRIORITY );\r
120 //    NVIC_ClearPendingIRQ( TC1_IRQn );\r
121 //    NVIC_EnableIRQ( TC1_IRQn );\r
122 \r
123         TC_Start( TC0, tmrTC0_CHANNEL_0 );\r
124         TC_Start( TC0, tmrTC0_CHANNEL_1 );\r
125 //      TC_Start( TC1, tmrTC1_CHANNEL_0 );\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 void TC0_Handler( void )\r
130 {\r
131         /* Read will clear the status bit. */\r
132         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
133         {\r
134                 /* Call the IntQ test function for this channel. */\r
135                 portYIELD_FROM_ISR( xFirstTimerHandler() );\r
136         }\r
137 \r
138         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
139         {\r
140                 /* Call the IntQ test function for this channel. */\r
141                 portYIELD_FROM_ISR( xSecondTimerHandler() );\r
142         }\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 void TC1_Handler( void )\r
147 {\r
148 volatile uint32_t ulDummy;\r
149 \r
150     /* Dummy read to clear status bit. */\r
151     ulDummy = TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_SR;\r
152         NVIC_ClearPendingIRQ( TC1_IRQn );\r
153 \r
154         /* Keep a count of the number of interrupts to use as a time base for the\r
155         run-time stats. */\r
156         ulHighFrequencyTimerCounts++;\r
157 \r
158         /* Prevent compiler warnings about the variable being set but then\r
159         unused. */\r
160         ( void ) ulDummy;\r
161 }\r
162 \r