2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Provides the two timers sources for the standard demo IntQueue test. Also
\r
31 * includes a high frequency timer to maximise the interrupt nesting achieved.
\r
34 /* Standard includes. */
\r
37 /* Scheduler includes. */
\r
38 #include "FreeRTOS.h"
\r
41 /* Demo includes. */
\r
42 #include "IntQueueTimer.h"
\r
43 #include "IntQueue.h"
\r
45 /* System includes. */
\r
49 /* The frequencies at which the first two timers expire are slightly offset to
\r
50 ensure they don't remain synchronised. The frequency of the highest priority
\r
51 interrupt is 20 times faster so really hammers the interrupt entry and exit
\r
53 #define tmrTIMER_0_FREQUENCY ( 2000UL )
\r
54 #define tmrTIMER_1_FREQUENCY ( 1003UL )
\r
55 #define tmrTIMER_2_FREQUENCY ( 20000UL )
\r
57 /* Priorities used by the timer interrupts - these are set differently to make
\r
58 nesting likely/common. The high frequency timer operates above the max
\r
59 system call interrupt priority, but does not use the RTOS API. */
\r
60 #define tmrTIMER_0_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY )
\r
61 #define tmrTIMER_1_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1 )
\r
62 #define tmrTIMER_2_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY - 1 )
\r
64 /* The channels used within the TC0 timer. */
\r
65 #define tmrTIMER_0_CHANNEL ( 0 )
\r
66 #define tmrTIMER_1_CHANNEL ( 1 )
\r
67 #define tmrTIMER_2_CHANNEL ( 2 )
\r
69 /* TC register bit specifics. */
\r
70 #define tmrTRIGGER_ON_RC ( 1UL << 4UL )
\r
71 #define trmDIVIDER ( 128 )
\r
73 /*-----------------------------------------------------------*/
\r
75 /* Handers for the timer interrupts. */
\r
76 void TC0_Handler( void );
\r
77 void TC1_Handler( void );
\r
78 void TC2_Handler( void );
\r
80 /*-----------------------------------------------------------*/
\r
82 /* Incremented by the high frequency timer, which operates above the max
\r
83 syscall interrupt priority. This is just for inspection. */
\r
84 volatile uint32_t ulHighFrequencyTimerInterrupts = 0;
\r
86 /*-----------------------------------------------------------*/
\r
88 void vInitialiseTimerForIntQueueTest( void )
\r
90 uint32_t ulInputFrequency;
\r
92 /* Calculate the frequency of the clock that feeds the TC. */
\r
93 ulInputFrequency = configCPU_CLOCK_HZ;
\r
94 ulInputFrequency /= trmDIVIDER;
\r
96 /* Three channels are used - two that run at or under
\r
97 configMAX_SYSCALL_INTERRUPT_PRIORITY, and one that runs over
\r
98 configMAX_SYSCALL_INTERRUPT_PRIORITY. */
\r
99 sysclk_enable_peripheral_clock( ID_TC0 );
\r
100 sysclk_enable_peripheral_clock( ID_TC1 );
\r
101 sysclk_enable_peripheral_clock( ID_TC2 );
\r
103 /* Init TC channels to waveform mode - up mode clean on RC match. */
\r
104 tc_init( TC0, tmrTIMER_0_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVE | TC_CMR_ACPC_CLEAR | TC_CMR_CPCTRG );
\r
105 tc_init( TC0, tmrTIMER_1_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVE | TC_CMR_ACPC_CLEAR | TC_CMR_CPCTRG );
\r
106 tc_init( TC0, tmrTIMER_2_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_WAVE | TC_CMR_ACPC_CLEAR | TC_CMR_CPCTRG );
\r
108 tc_enable_interrupt( TC0, tmrTIMER_0_CHANNEL, tmrTRIGGER_ON_RC );
\r
109 tc_enable_interrupt( TC0, tmrTIMER_1_CHANNEL, tmrTRIGGER_ON_RC );
\r
110 tc_enable_interrupt( TC0, tmrTIMER_2_CHANNEL, tmrTRIGGER_ON_RC );
\r
112 tc_write_rc( TC0, tmrTIMER_0_CHANNEL, ( ulInputFrequency / tmrTIMER_0_FREQUENCY ) );
\r
113 tc_write_rc( TC0, tmrTIMER_1_CHANNEL, ( ulInputFrequency / tmrTIMER_1_FREQUENCY ) );
\r
114 tc_write_rc( TC0, tmrTIMER_2_CHANNEL, ( ulInputFrequency / tmrTIMER_2_FREQUENCY ) );
\r
116 NVIC_SetPriority( TC0_IRQn, tmrTIMER_0_PRIORITY );
\r
117 NVIC_SetPriority( TC1_IRQn, tmrTIMER_1_PRIORITY );
\r
118 NVIC_SetPriority( TC2_IRQn, tmrTIMER_2_PRIORITY );
\r
120 NVIC_EnableIRQ( TC0_IRQn );
\r
121 NVIC_EnableIRQ( TC1_IRQn );
\r
122 NVIC_EnableIRQ( TC2_IRQn );
\r
124 tc_start( TC0, tmrTIMER_0_CHANNEL );
\r
125 tc_start( TC0, tmrTIMER_1_CHANNEL );
\r
126 tc_start( TC0, tmrTIMER_2_CHANNEL );
\r
128 /*-----------------------------------------------------------*/
\r
130 void TC0_Handler( void )
\r
132 /* Handler for the first timer in the IntQueue test. Was the interrupt
\r
133 caused by a compare on RC? */
\r
134 if( ( tc_get_status( TC0, tmrTIMER_0_CHANNEL ) & ~TC_SR_CPCS ) != 0 )
\r
136 portYIELD_FROM_ISR( xFirstTimerHandler() );
\r
139 /*-----------------------------------------------------------*/
\r
141 void TC1_Handler( void )
\r
143 /* Handler for the second timer in the IntQueue test. Was the interrupt
\r
144 caused by a compare on RC? */
\r
145 if( ( tc_get_status( TC0, tmrTIMER_1_CHANNEL ) & ~TC_SR_CPCS ) != 0 )
\r
147 portYIELD_FROM_ISR( xSecondTimerHandler() );
\r
150 /*-----------------------------------------------------------*/
\r
152 void TC2_Handler( void )
\r
154 /* Handler for the high frequency timer that does nothing but increment a
\r
155 variable to give an indication that it is running. Was the interrupt caused
\r
156 by a compare on RC? */
\r
157 if( ( tc_get_status( TC0, tmrTIMER_2_CHANNEL ) & ~TC_SR_CPCS ) != 0 )
\r
159 ulHighFrequencyTimerInterrupts++;
\r