]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_CEC1302_MikroC/main_full/IntQueueTimer.c
df8b2b0eceac3fd74e993417ca16c6b18eceb7bb
[freertos] / FreeRTOS / Demo / CORTEX_M4F_CEC1302_MikroC / 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 "btimer.h"\r
55 #include "interrupt.h"\r
56 \r
57 /* The frequencies at which the first two timers expire are slightly offset to\r
58 ensure they don't remain synchronised.  The frequency of the highest priority\r
59 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
60 code. */\r
61 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
62 #define tmrTIMER_1_FREQUENCY    ( 2003UL )\r
63 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
64 \r
65 /* The basic timer channels used for generating the three interrupts. */\r
66 #define tmrTIMER_CHANNEL_0              0 /* At tmrTIMER_0_FREQUENCY */\r
67 #define tmrTIMER_CHANNEL_1              1 /* At tmrTIMER_1_FREQUENCY */\r
68 #define tmrTIMER_CHANNEL_2              2 /* At tmrTIMER_2_FREQUENCY */\r
69 \r
70 /* The high frequency interrupt is given a priority above the maximum at which\r
71 interrupt safe FreeRTOS calls can be made.  The priority of the lower frequency\r
72 timers must still be above the tick interrupt priority. */\r
73 #define tmrLOWER_PRIORITY               ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1 )\r
74 #define tmrMEDIUM_PRIORITY              ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 0 )\r
75 #define tmrHIGHER_PRIORITY              ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY - 1 )\r
76 \r
77 /* Hardware register locations. */\r
78 #define tmrGIRQ23_ENABLE_SET                    ( * ( volatile uint32_t * ) 0x4000C130 )\r
79 \r
80 #define tmrRECORD_NESTING_DEPTH()                                               \\r
81         ulNestingDepth++;                                                                       \\r
82         if( ulNestingDepth > ulMaxRecordedNestingDepth )        \\r
83         {                                                                                                       \\r
84                 ulMaxRecordedNestingDepth = ulNestingDepth;             \\r
85         }\r
86 \r
87 /* Used to count the nesting depth, and record the maximum nesting depth. */\r
88 volatile uint32_t ulNestingDepth = 0, ulMaxRecordedNestingDepth = 0;\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 void vInitialiseTimerForIntQueueTest( void )\r
93 {\r
94 const uint32_t ulTimer0Count = configCPU_CLOCK_HZ / tmrTIMER_0_FREQUENCY;\r
95 const uint32_t ulTimer1Count = configCPU_CLOCK_HZ / tmrTIMER_1_FREQUENCY;\r
96 const uint32_t ulTimer2Count = configCPU_CLOCK_HZ / tmrTIMER_2_FREQUENCY;\r
97 const uint8_t ucEnable = 1;\r
98 \r
99         tmrGIRQ23_ENABLE_SET = 0x03;\r
100 \r
101         /* Initialise the three timers as described at the top of this file, and\r
102         enable their interrupts in the NVIC. */\r
103         btimer_init( tmrTIMER_CHANNEL_0, BTIMER_AUTO_RESTART | BTIMER_COUNT_DOWN | BTIMER_INT_EN, 0, ulTimer0Count, ulTimer0Count );\r
104         btimer_interrupt_status_get_clr( tmrTIMER_CHANNEL_0 );\r
105         interrupt_device_enable( BTMR0_IROUTE );\r
106         interrupt_device_nvic_priority_set( BTMR0_IROUTE, tmrLOWER_PRIORITY );\r
107         interrupt_device_nvic_pending_clear( BTMR0_IROUTE );\r
108         interrupt_device_nvic_enable( BTMR0_IROUTE, ucEnable );\r
109         btimer_start( tmrTIMER_CHANNEL_0 );\r
110 \r
111         btimer_init( tmrTIMER_CHANNEL_1, BTIMER_AUTO_RESTART | BTIMER_COUNT_DOWN | BTIMER_INT_EN, 0, ulTimer1Count, ulTimer1Count );\r
112         btimer_interrupt_status_get_clr( tmrTIMER_CHANNEL_1 );\r
113         interrupt_device_enable( BTMR1_IROUTE );\r
114         interrupt_device_nvic_priority_set( BTMR1_IROUTE, tmrMEDIUM_PRIORITY );\r
115         interrupt_device_nvic_pending_clear( BTMR1_IROUTE );\r
116         interrupt_device_nvic_enable( BTMR1_IROUTE, ucEnable );\r
117         btimer_start( tmrTIMER_CHANNEL_1 );\r
118 \r
119         btimer_init( tmrTIMER_CHANNEL_2, BTIMER_AUTO_RESTART | BTIMER_COUNT_DOWN | BTIMER_INT_EN, 0, ulTimer2Count, ulTimer2Count );\r
120         btimer_interrupt_status_get_clr( tmrTIMER_CHANNEL_2 );\r
121         interrupt_device_enable( BTMR2_IROUTE );\r
122         interrupt_device_nvic_priority_set( BTMR2_IROUTE, tmrHIGHER_PRIORITY );\r
123         interrupt_device_nvic_pending_clear( BTMR2_IROUTE );\r
124         interrupt_device_nvic_enable( BTMR2_IROUTE, ucEnable );\r
125         btimer_start( tmrTIMER_CHANNEL_2 );\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 /* The TMR0 interrupt is used for different purposes by the low power and full\r
130 demos respectively. */\r
131 #if( configCREATE_LOW_POWER_DEMO == 0 )\r
132 \r
133         void NVIC_Handler_TMR0( void ) iv IVT_INT_TIMER0 ics ICS_AUTO\r
134         {\r
135                 tmrRECORD_NESTING_DEPTH();\r
136 \r
137                 /* Call the IntQ test function for this channel. */\r
138                 portYIELD_FROM_ISR( xFirstTimerHandler() );\r
139 \r
140                 ulNestingDepth--;\r
141         }\r
142 \r
143 #endif /* configCREATE_LOW_POWER_DEMO */\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 void NVIC_Handler_TMR1( void ) iv IVT_INT_TIMER1 ics ICS_AUTO\r
147 {\r
148         tmrRECORD_NESTING_DEPTH();\r
149 \r
150         /* Just testing the xPortIsInsideInterrupt() functionality. */\r
151         configASSERT( xPortIsInsideInterrupt() == pdTRUE );\r
152 \r
153         /* Call the IntQ test function for this channel. */\r
154         portYIELD_FROM_ISR( xSecondTimerHandler() );\r
155 \r
156         ulNestingDepth--;\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 void NVIC_Handler_TMR2( void ) iv IVT_INT_TIMER2 ics ICS_AUTO\r
161 {\r
162         tmrRECORD_NESTING_DEPTH();\r
163         ulNestingDepth--;\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r