]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/IntQueueTimer.c
Demo code only:
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / IntQueueTimer.c
1 /*\r
2     FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * Provides the two timers sources for the standard demo IntQueue test.  Also\r
68  * includes a high frequency timer to maximise the interrupt nesting achieved.\r
69  */\r
70 \r
71 /* Standard includes. */\r
72 #include <limits.h>\r
73 \r
74 /* Scheduler includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 \r
78 /* Demo includes. */\r
79 #include "IntQueueTimer.h"\r
80 #include "IntQueue.h"\r
81 \r
82 /* System includes. */\r
83 #include "board.h"\r
84 #include "asf.h"\r
85 \r
86 /* The frequencies at which the first two timers expire are slightly offset to\r
87 ensure they don't remain synchronised.  The frequency of the highest priority\r
88 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
89 code. */\r
90 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
91 #define tmrTIMER_1_FREQUENCY    ( 1003UL )\r
92 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
93 \r
94 /* Priorities used by the timer interrupts - these are set differently to make\r
95 nesting likely/common.  The high frequency timer operates above the max\r
96 system call interrupt priority, but does not use the RTOS API. */\r
97 #define tmrTIMER_0_PRIORITY             ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY )\r
98 #define tmrTIMER_1_PRIORITY             ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1 )\r
99 #define tmrTIMER_2_PRIORITY             ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY - 1 )\r
100 \r
101 /* The channels used within the TC0 timer. */\r
102 #define tmrTIMER_0_CHANNEL              ( 0 )\r
103 #define tmrTIMER_1_CHANNEL              ( 1 )\r
104 #define tmrTIMER_2_CHANNEL              ( 2 )\r
105 \r
106 /* TC register bit specifics. */\r
107 #define tmrTRIGGER_ON_RC                ( 1UL << 4UL )\r
108 #define trmDIVIDER                              ( 128 )\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* Handers for the timer interrupts. */\r
113 void TC0_Handler( void );\r
114 void TC1_Handler( void );\r
115 void TC2_Handler( void );\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* Incremented by the high frequency timer, which operates above the max\r
120 syscall interrupt priority.  This is just for inspection. */\r
121 volatile uint32_t ulHighFrequencyTimerInterrupts = 0;\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 void vInitialiseTimerForIntQueueTest( void )\r
126 {\r
127 uint32_t ulInputFrequency;\r
128 \r
129         /* Calculate the frequency of the clock that feeds the TC. */\r
130         ulInputFrequency = configCPU_CLOCK_HZ;\r
131         ulInputFrequency /= trmDIVIDER;\r
132 \r
133         /* Three channels are used - two that run at or under \r
134         configMAX_SYSCALL_INTERRUPT_PRIORITY, and one that runs over\r
135         configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
136         sysclk_enable_peripheral_clock( ID_TC0 );\r
137         sysclk_enable_peripheral_clock( ID_TC1 );\r
138         sysclk_enable_peripheral_clock( ID_TC2 );\r
139         \r
140         /* Init TC channels to waveform mode - up mode clean on RC match. */\r
141         tc_init( TC0, tmrTIMER_0_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVE | TC_CMR_ACPC_CLEAR | TC_CMR_CPCTRG );\r
142         tc_init( TC0, tmrTIMER_1_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVE | TC_CMR_ACPC_CLEAR | TC_CMR_CPCTRG );\r
143         tc_init( TC0, tmrTIMER_2_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVE | TC_CMR_ACPC_CLEAR | TC_CMR_CPCTRG );\r
144         \r
145         tc_enable_interrupt( TC0, tmrTIMER_0_CHANNEL, tmrTRIGGER_ON_RC );\r
146         tc_enable_interrupt( TC0, tmrTIMER_1_CHANNEL, tmrTRIGGER_ON_RC );\r
147         tc_enable_interrupt( TC0, tmrTIMER_2_CHANNEL, tmrTRIGGER_ON_RC );\r
148         \r
149         tc_write_rc( TC0, tmrTIMER_0_CHANNEL, ( ulInputFrequency / tmrTIMER_0_FREQUENCY ) );\r
150         tc_write_rc( TC0, tmrTIMER_1_CHANNEL, ( ulInputFrequency / tmrTIMER_1_FREQUENCY ) );\r
151         tc_write_rc( TC0, tmrTIMER_2_CHANNEL, ( ulInputFrequency / tmrTIMER_2_FREQUENCY ) );\r
152 \r
153         NVIC_SetPriority( TC0_IRQn, tmrTIMER_0_PRIORITY );\r
154         NVIC_SetPriority( TC1_IRQn, tmrTIMER_1_PRIORITY );\r
155         NVIC_SetPriority( TC2_IRQn, tmrTIMER_2_PRIORITY );\r
156 \r
157         NVIC_EnableIRQ( TC0_IRQn );\r
158         NVIC_EnableIRQ( TC1_IRQn );\r
159         NVIC_EnableIRQ( TC2_IRQn );\r
160 \r
161         tc_start( TC0, tmrTIMER_0_CHANNEL );\r
162         tc_start( TC0, tmrTIMER_1_CHANNEL );\r
163         tc_start( TC0, tmrTIMER_2_CHANNEL );\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 void TC0_Handler( void )\r
168 {\r
169         /* Handler for the first timer in the IntQueue test.  Was the interrupt\r
170         caused by a compare on RC? */\r
171         if( ( tc_get_status( TC0, tmrTIMER_0_CHANNEL ) & ~TC_SR_CPCS ) != 0 )\r
172         {\r
173                 portYIELD_FROM_ISR( xFirstTimerHandler() );     \r
174         }\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 void TC1_Handler( void )\r
179 {\r
180         /* Handler for the second timer in the IntQueue test.  Was the interrupt\r
181         caused by a compare on RC? */\r
182         if( ( tc_get_status( TC0, tmrTIMER_1_CHANNEL ) & ~TC_SR_CPCS ) != 0 )\r
183         {\r
184                 portYIELD_FROM_ISR( xSecondTimerHandler() );\r
185         }\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 void TC2_Handler( void )\r
190 {\r
191         /* Handler for the high frequency timer that does nothing but increment a\r
192         variable to give an indication that it is running.  Was the interrupt caused\r
193         by a compare on RC? */\r
194         if( ( tc_get_status( TC0, tmrTIMER_2_CHANNEL ) & ~TC_SR_CPCS ) != 0 )\r
195         {\r
196                 ulHighFrequencyTimerInterrupts++;\r
197         }\r
198 }\r