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