]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_CEC1302_Keil_GCC/main_full/IntQueueTimer.c
67c80a0e639ae73ee201fb9344fc31ad8ac6db98
[freertos] / FreeRTOS / Demo / CORTEX_M4F_CEC1302_Keil_GCC / main_full / IntQueueTimer.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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  * Basic timer channels 0 and 1 provide the interrupts that are used with the\r
32  * IntQ standard demo tasks, which test interrupt nesting and using queues from\r
33  * interrupts.  The interrupts use slightly different frequencies so will\r
34  * occasionally nest.\r
35  *\r
36  * Basic timer channel 2 provides a much higher frequency timer that tests the\r
37  * nesting of interrupts that don't use the FreeRTOS API.\r
38  *\r
39  * All the timers can nest with the tick interrupt - creating a maximum\r
40  * interrupt nesting depth of 4 (which is shown as a max nest count of 3 as the\r
41  * tick interrupt does not increment the nesting count variable).\r
42  *\r
43  */\r
44 \r
45 /* Scheduler includes. */\r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 \r
49 /* Demo includes. */\r
50 #include "IntQueueTimer.h"\r
51 #include "IntQueue.h"\r
52 \r
53 /* Library includes. */\r
54 #include "common_lib.h"\r
55 #include "peripheral_library/interrupt/interrupt.h"\r
56 #include "peripheral_library/basic_timer/btimer.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 basic timer channels used for generating the three interrupts. */\r
67 #define tmrTIMER_CHANNEL_0              0 /* At tmrTIMER_0_FREQUENCY */\r
68 #define tmrTIMER_CHANNEL_1              1 /* At tmrTIMER_1_FREQUENCY */\r
69 #define tmrTIMER_CHANNEL_2              2 /* At tmrTIMER_2_FREQUENCY */\r
70 \r
71 /* The high frequency interrupt is given a priority above the maximum at which\r
72 interrupt safe FreeRTOS calls can be made.  The priority of the lower frequency\r
73 timers must still be above the tick interrupt priority. */\r
74 #define tmrLOWER_PRIORITY               ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1 )\r
75 #define tmrMEDIUM_PRIORITY              ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 0 )\r
76 #define tmrHIGHER_PRIORITY              ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY - 1 )\r
77 \r
78 /* Hardware register locations and bit definitions to enable the btimer \r
79 interrupts. */\r
80 #define tmrGIRQ23_ENABLE_SET    ( * ( volatile uint32_t * ) 0x4000C130 )\r
81 #define tmrGIRQ23_BIT_TIMER0    ( 1UL << 0UL )\r
82 #define tmrGIRQ23_BIT_TIMER1    ( 1UL << 1UL )\r
83 #define tmrGIRQ23_BIT_TIMER2    ( 1UL << 2UL )\r
84 #define tmrGIRQ23_TIMER_BITS    ( tmrGIRQ23_BIT_TIMER0 | tmrGIRQ23_BIT_TIMER1 | tmrGIRQ23_BIT_TIMER2 )\r
85 \r
86 #define tmrRECORD_NESTING_DEPTH()                                               \\r
87         ulNestingDepth++;                                                                       \\r
88         if( ulNestingDepth > ulMaxRecordedNestingDepth )        \\r
89         {                                                                                                       \\r
90                 ulMaxRecordedNestingDepth = ulNestingDepth;             \\r
91         }\r
92 \r
93 /* Used to count the nesting depth, and record the maximum nesting depth. */\r
94 volatile uint32_t ulNestingDepth = 0, ulMaxRecordedNestingDepth = 0;\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 void vInitialiseTimerForIntQueueTest( void )\r
99 {\r
100 const uint32_t ulTimer0Count = configCPU_CLOCK_HZ / tmrTIMER_0_FREQUENCY;\r
101 const uint32_t ulTimer1Count = configCPU_CLOCK_HZ / tmrTIMER_1_FREQUENCY;\r
102 const uint32_t ulTimer2Count = configCPU_CLOCK_HZ / tmrTIMER_2_FREQUENCY;\r
103 \r
104         tmrGIRQ23_ENABLE_SET = tmrGIRQ23_TIMER_BITS;\r
105 \r
106         /* Initialise the three timers as described at the top of this file, and\r
107         enable their interrupts in the NVIC. */\r
108         btimer_init( tmrTIMER_CHANNEL_0, BTIMER_AUTO_RESTART | BTIMER_COUNT_DOWN | BTIMER_INT_EN, 0, ulTimer0Count, ulTimer0Count );\r
109         btimer_interrupt_status_get_clr( tmrTIMER_CHANNEL_0 );\r
110         NVIC_SetPriority( TIMER0_IRQn, tmrLOWER_PRIORITY ); //0xc0 into 0xe000e431\r
111         NVIC_ClearPendingIRQ( TIMER0_IRQn );\r
112         NVIC_EnableIRQ( TIMER0_IRQn );\r
113         btimer_start( tmrTIMER_CHANNEL_0 );\r
114 \r
115         btimer_init( tmrTIMER_CHANNEL_1, BTIMER_AUTO_RESTART | BTIMER_COUNT_DOWN | BTIMER_INT_EN, 0, ulTimer1Count, ulTimer1Count );\r
116         btimer_interrupt_status_get_clr( tmrTIMER_CHANNEL_1 );\r
117         NVIC_SetPriority( TIMER1_IRQn, tmrMEDIUM_PRIORITY ); //0xa0 into 0xe000e432\r
118         NVIC_ClearPendingIRQ( TIMER1_IRQn );\r
119         NVIC_EnableIRQ( TIMER1_IRQn );\r
120         btimer_start( tmrTIMER_CHANNEL_1 );\r
121 \r
122         btimer_init( tmrTIMER_CHANNEL_2, BTIMER_AUTO_RESTART | BTIMER_COUNT_DOWN | BTIMER_INT_EN, 0, ulTimer2Count, ulTimer2Count );\r
123         btimer_interrupt_status_get_clr( tmrTIMER_CHANNEL_2 );\r
124         NVIC_SetPriority( TIMER2_IRQn, tmrHIGHER_PRIORITY );\r
125         NVIC_ClearPendingIRQ( TIMER2_IRQn );\r
126         NVIC_EnableIRQ( TIMER2_IRQn );\r
127         btimer_start( tmrTIMER_CHANNEL_2 );\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /* The TMR0 interrupt is used for different purposes by the low power and full\r
132 demos respectively. */\r
133 #if( configCREATE_LOW_POWER_DEMO == 0 )\r
134 \r
135         void NVIC_Handler_TMR0( void )\r
136         {\r
137                 tmrRECORD_NESTING_DEPTH();\r
138 \r
139                 /* Call the IntQ test function for this channel. */\r
140                 portYIELD_FROM_ISR( xFirstTimerHandler() );\r
141 \r
142                 ulNestingDepth--;\r
143         }\r
144 \r
145 #endif /* configCREATE_LOW_POWER_DEMO */\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 void NVIC_Handler_TMR1( void )\r
149 {\r
150         tmrRECORD_NESTING_DEPTH();\r
151 \r
152         /* Just testing the xPortIsInsideInterrupt() functionality. */\r
153         configASSERT( xPortIsInsideInterrupt() == pdTRUE );\r
154 \r
155         /* Call the IntQ test function for this channel. */\r
156         portYIELD_FROM_ISR( xSecondTimerHandler() );\r
157 \r
158         ulNestingDepth--;\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void NVIC_Handler_TMR2( void )\r
163 {\r
164         tmrRECORD_NESTING_DEPTH();\r
165         ulNestingDepth--;\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r