]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/IntQueueTimer.c
f454ce2e7c4ee8786608575d0389ac6013e96c24
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / 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  * Provides the two timers sources for the standard demo IntQueue test.  Also\r
30  * includes a high frequency timer to maximise the interrupt nesting achieved.\r
31  */\r
32 \r
33 /* Standard includes. */\r
34 #include <limits.h>\r
35 \r
36 /* Scheduler includes. */\r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 \r
40 /* Demo includes. */\r
41 #include "IntQueueTimer.h"\r
42 #include "IntQueue.h"\r
43 \r
44 /* System includes. */\r
45 #include "board.h"\r
46 #include "asf.h"\r
47 \r
48 /* The frequencies at which the first two timers expire are slightly offset to\r
49 ensure they don't remain synchronised.  The frequency of the highest priority\r
50 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
51 code. */\r
52 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
53 #define tmrTIMER_1_FREQUENCY    ( 1003UL )\r
54 #define tmrTIMER_2_FREQUENCY    ( 5000UL )\r
55 \r
56 /* Priorities used by the timer interrupts - these are set differently to make\r
57 nesting likely/common.  The high frequency timer operates above the max\r
58 system call interrupt priority, but does not use the RTOS API. */\r
59 #define tmrTIMER_0_PRIORITY             ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY )\r
60 #define tmrTIMER_1_PRIORITY             ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1 )\r
61 #define tmrTIMER_2_PRIORITY             ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY - 1 )\r
62 \r
63 /* The channels used within the TC0 timer. */\r
64 #define tmrTIMER_0_CHANNEL              ( 0 )\r
65 #define tmrTIMER_1_CHANNEL              ( 1 )\r
66 #define tmrTIMER_2_CHANNEL              ( 2 )\r
67 \r
68 /* TC register bit specifics. */\r
69 #define tmrTRIGGER_ON_RC                ( 1UL << 4UL )\r
70 #define trmDIVIDER                              ( 128 )\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* Handers for the timer interrupts. */\r
75 void TC0_Handler( void );\r
76 void TC1_Handler( void );\r
77 void TC2_Handler( void );\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* Incremented by the high frequency timer, which operates above the max\r
82 syscall interrupt priority.  This is just for inspection. */\r
83 volatile uint32_t ulHighFrequencyTimerInterrupts = 0;\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 void vInitialiseTimerForIntQueueTest( void )\r
88 {\r
89 uint32_t ulInputFrequency;\r
90 \r
91         /* Calculate the frequency of the clock that feeds the TC. */\r
92         ulInputFrequency = configCPU_CLOCK_HZ;\r
93         ulInputFrequency /= trmDIVIDER;\r
94 \r
95         /* Three channels are used - two that run at or under \r
96         configMAX_SYSCALL_INTERRUPT_PRIORITY, and one that runs over\r
97         configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
98         sysclk_enable_peripheral_clock( ID_TC0 );\r
99         sysclk_enable_peripheral_clock( ID_TC1 );\r
100         sysclk_enable_peripheral_clock( ID_TC2 );\r
101         \r
102         /* Init TC channels to waveform mode - up mode clean on RC match. */\r
103         tc_init( TC0, tmrTIMER_0_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVE | TC_CMR_ACPC_CLEAR | TC_CMR_CPCTRG );\r
104         tc_init( TC0, tmrTIMER_1_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVE | TC_CMR_ACPC_CLEAR | TC_CMR_CPCTRG );\r
105         tc_init( TC0, tmrTIMER_2_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVE | TC_CMR_ACPC_CLEAR | TC_CMR_CPCTRG );\r
106         \r
107         tc_enable_interrupt( TC0, tmrTIMER_0_CHANNEL, tmrTRIGGER_ON_RC );\r
108         tc_enable_interrupt( TC0, tmrTIMER_1_CHANNEL, tmrTRIGGER_ON_RC );\r
109         tc_enable_interrupt( TC0, tmrTIMER_2_CHANNEL, tmrTRIGGER_ON_RC );\r
110         \r
111         tc_write_rc( TC0, tmrTIMER_0_CHANNEL, ( ulInputFrequency / tmrTIMER_0_FREQUENCY ) );\r
112         tc_write_rc( TC0, tmrTIMER_1_CHANNEL, ( ulInputFrequency / tmrTIMER_1_FREQUENCY ) );\r
113         tc_write_rc( TC0, tmrTIMER_2_CHANNEL, ( ulInputFrequency / tmrTIMER_2_FREQUENCY ) );\r
114 \r
115         NVIC_SetPriority( TC0_IRQn, tmrTIMER_0_PRIORITY );\r
116         NVIC_SetPriority( TC1_IRQn, tmrTIMER_1_PRIORITY );\r
117         NVIC_SetPriority( TC2_IRQn, tmrTIMER_2_PRIORITY );\r
118 \r
119         NVIC_EnableIRQ( TC0_IRQn );\r
120         NVIC_EnableIRQ( TC1_IRQn );\r
121         NVIC_EnableIRQ( TC2_IRQn );\r
122 \r
123         tc_start( TC0, tmrTIMER_0_CHANNEL );\r
124         tc_start( TC0, tmrTIMER_1_CHANNEL );\r
125         tc_start( TC0, tmrTIMER_2_CHANNEL );\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 void TC0_Handler( void )\r
130 {\r
131         /* Handler for the first timer in the IntQueue test.  Was the interrupt\r
132         caused by a compare on RC? */\r
133         if( ( tc_get_status( TC0, tmrTIMER_0_CHANNEL ) & ~TC_SR_CPCS ) != 0 )\r
134         {\r
135                 portYIELD_FROM_ISR( xFirstTimerHandler() );     \r
136         }\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 void TC1_Handler( void )\r
141 {\r
142         /* Handler for the second timer in the IntQueue test.  Was the interrupt\r
143         caused by a compare on RC? */\r
144         if( ( tc_get_status( TC0, tmrTIMER_1_CHANNEL ) & ~TC_SR_CPCS ) != 0 )\r
145         {\r
146                 portYIELD_FROM_ISR( xSecondTimerHandler() );\r
147         }\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 void TC2_Handler( void )\r
152 {\r
153         /* Handler for the high frequency timer that does nothing but increment a\r
154         variable to give an indication that it is running.  Was the interrupt caused\r
155         by a compare on RC? */\r
156         if( ( tc_get_status( TC0, tmrTIMER_2_CHANNEL ) & ~TC_SR_CPCS ) != 0 )\r
157         {\r
158                 ulHighFrequencyTimerInterrupts++;\r
159         }\r
160 }\r