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