]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_CEC1302_Keil_GCC/main_low_power/low_power_tick_config.c
0eab0d18586f1832ef4a176f2a8a627b539a030e
[freertos] / FreeRTOS / Demo / CORTEX_M4F_CEC1302_Keil_GCC / main_low_power / low_power_tick_config.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 /* Standard includes. */\r
29 #include <limits.h>\r
30 \r
31 /* FreeRTOS includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 \r
35 /* Library includes. */\r
36 #include "common_lib.h"\r
37 #include "peripheral_library/interrupt/interrupt.h"\r
38 #include "peripheral_library/basic_timer/btimer.h"\r
39 \r
40 /* This file contains functions that will override the default implementations\r
41 in the RTOS port layer.  Therefore only build this file if the low power demo\r
42 is being built. */\r
43 #if( configCREATE_LOW_POWER_DEMO == 1 )\r
44 \r
45 /* ID of the hibernation timer used to generate the tick. */\r
46 #define mainTICK_HTIMER_ID      0\r
47 \r
48 /* Written to the hibernation timer control register to configure the timer for\r
49 its higher resolution. */\r
50 #define mainHTIMER_HIGH_RESOLUTION      0\r
51 \r
52 /* The frequency of the hibernation timer when it is running at its higher\r
53 resolution and low resolution respectively. */\r
54 #define mainHIGHER_RESOLUTION_TIMER_HZ  ( 32787UL ) /* (1000000us / 30.5us) as each LSB is 30.5us. */\r
55 #define mainLOW_RESOLUTION_TIMER_HZ             ( 8UL )  /* ( 1000ms / 125ms ) as each LSB is 0.125s. */\r
56 \r
57 /* When lpINCLUDE_TEST_TIMER is set to 1 a basic timer is used to generate\r
58 interrupts at a low frequency.  The purpose being to bring the CPU out of its\r
59 sleep mode by an interrupt other than the tick interrupt, and therefore\r
60 allowing an additional paths through the code to be tested. */\r
61 #define lpINCLUDE_TEST_TIMER                    0\r
62 \r
63 /* Registers and bits required to use the htimer in aggregated mode. */\r
64 #define lpHTIMER_PRELOAD_REGISTER               ( * ( volatile uint16_t * ) 0x40009800 )\r
65 #define lpHTIMER_CONTROL_REGISTER               ( * ( volatile uint16_t * ) 0x40009804 )\r
66 #define lpHTIMER_COUNT_REGISTER                 ( * ( volatile uint16_t * ) 0x40009808 )\r
67 #define lpEC_GIRQ17_ENABLE_SET                  ( * ( volatile uint32_t * ) 0x4000C0B8 )\r
68 #define lpEC_GIRQ17_SOURCE                              ( * ( volatile uint32_t * ) 0x4000C0B4 )\r
69 #define lpEC_GIRQ17_ENABLE_CLEAR                ( * ( volatile uint32_t * ) 0x4000C0C0 )\r
70 #define lpBLOCK_ENABLE_SET                              ( * ( volatile uint32_t * ) 0x4000c200 )\r
71 #define lpGIRQ17_BIT_HTIMER                             ( 1UL << 20UL )\r
72 #define lpHTIMER_GIRQ_BLOCK                             ( 1Ul << 17UL )\r
73 \r
74 /* Registers and bits required to use btimer 0 in aggregated mode. */\r
75 #define lpGIRQ23_ENABLE_SET                             ( * ( volatile uint32_t * ) 0x4000C130 )\r
76 #define lpEC_GIRQ23_SOURCE                              ( * ( volatile uint32_t * ) 0x4000C12C )\r
77 #define lpEC_GIRQ23_ENABLE_CLEAR                ( * ( volatile uint32_t * ) 0x4000C138 )\r
78 #define lpGIRQ23_BIT_TIMER0                             ( 1UL << 0UL )\r
79 #define lpBTIMER_GIRQ_BLOCK                             ( 1UL << 23UL )\r
80 \r
81 /*\r
82  * The low power demo does not use the SysTick, so override the\r
83  * vPortSetupTickInterrupt() function with an implementation that configures\r
84  * the low power clock.  NOTE:  This function name must not be changed as it\r
85  * is called from the RTOS portable layer.\r
86  */\r
87 void vPortSetupTimerInterrupt( void );\r
88 \r
89 /*\r
90  * To fully test the low power tick processing it is necessary to sometimes\r
91  * bring the MCU out of its sleep state by a method other than the tick\r
92  * interrupt.  Interrupts generated from a basic timer are used for this\r
93  * purpose.\r
94  */\r
95 #if( lpINCLUDE_TEST_TIMER == 1 )\r
96         static void prvSetupBasicTimer( void );\r
97 #endif\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* The reload value to use in the timer to generate the tick interrupt -\r
102 assumes the timer is running at its higher resolution. */\r
103 static const uint32_t ulHighResolutionReloadValue = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );\r
104 \r
105 /* Calculate how many clock increments make up a single tick period. */\r
106 static const uint32_t ulReloadValueForOneHighResolutionTick = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );\r
107 \r
108 /* Calculate the maximum number of ticks that can be suppressed when using the\r
109 high resolution clock and low resolution clock respectively. */\r
110 static uint32_t ulMaximumPossibleSuppressedHighResolutionTicks = 0;\r
111 \r
112 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so\r
113 use zero - but leave the value here to assist porting to different clock\r
114 speeds. */\r
115 static const uint32_t ulStoppedTimerCompensation = 0UL;\r
116 \r
117 /* Flag set from the tick interrupt to allow the sleep processing to know if\r
118 sleep mode was exited because of an timer interrupt or a different interrupt. */\r
119 static volatile uint32_t ulTickFlag = pdFALSE;\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 void NVIC_Handler_GIRQ17( void )\r
124 {\r
125         /* The low power demo is using aggregated interrupts, so although in the\r
126         demo the htimer is the only peripheral that will generate interrupts on\r
127         this vector, in a real application it would be necessary to first check the\r
128         interrupt source. */\r
129         if( ( lpEC_GIRQ17_SOURCE & lpGIRQ17_BIT_HTIMER ) != 0 )\r
130         {\r
131                 /* The htimer interrupted.  Clear the interrupt. */\r
132                 lpEC_GIRQ17_SOURCE = lpGIRQ17_BIT_HTIMER;\r
133                 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulHighResolutionReloadValue;\r
134 \r
135                 /* Increment the RTOS tick. */\r
136                 if( xTaskIncrementTick() != pdFALSE )\r
137                 {\r
138                         /* A context switch is required.  Context switching is performed in\r
139                         the PendSV interrupt.  Pend the PendSV interrupt. */\r
140                         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
141                 }\r
142 \r
143                 /* The CPU woke because of a tick. */\r
144                 ulTickFlag = pdTRUE;\r
145         }\r
146         else\r
147         {\r
148                 /* Don't expect any other interrupts to use this vector in this\r
149                 demo.  Force an assert. */\r
150                 configASSERT( lpEC_GIRQ17_SOURCE == 0 );\r
151         }\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 #if( lpINCLUDE_TEST_TIMER == 1 )\r
156 \r
157         static void prvSetupBasicTimer( void )\r
158         {\r
159         const uint8_t ucTimerChannel = 0;\r
160         const uint32_t ulTimer0Count = configCPU_CLOCK_HZ / 10;\r
161 \r
162                 /* Enable btimer 0 interrupt in the aggregated GIRQ23 block. */\r
163                 lpEC_GIRQ23_SOURCE = lpGIRQ23_BIT_TIMER0;\r
164                 lpEC_GIRQ23_ENABLE_CLEAR = lpGIRQ23_BIT_TIMER0;\r
165                 lpBLOCK_ENABLE_SET = lpBTIMER_GIRQ_BLOCK;\r
166                 lpGIRQ23_ENABLE_SET = lpGIRQ23_BIT_TIMER0;\r
167 \r
168                 /* To fully test the low power tick processing it is necessary to sometimes\r
169                 bring the MCU out of its sleep state by a method other than the tick\r
170                 interrupt.  Interrupts generated from a basic timer are used for this\r
171                 purpose. */\r
172                 btimer_init( ucTimerChannel, BTIMER_AUTO_RESTART | BTIMER_COUNT_DOWN | BTIMER_INT_EN, 0, ulTimer0Count, ulTimer0Count );\r
173                 btimer_interrupt_status_get_clr( ucTimerChannel );\r
174                 NVIC_SetPriority( GIRQ23_IRQn, ucTimerChannel );\r
175                 NVIC_ClearPendingIRQ( GIRQ23_IRQn );\r
176                 NVIC_EnableIRQ( GIRQ23_IRQn );\r
177                 btimer_start( ucTimerChannel );\r
178         }\r
179 \r
180 #endif /* lpINCLUDE_TEST_TIMER */\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 void vPortSetupTimerInterrupt( void )\r
184 {\r
185 ulMaximumPossibleSuppressedHighResolutionTicks = ( ( uint32_t ) USHRT_MAX ) / ulReloadValueForOneHighResolutionTick;\r
186 \r
187         /* Set up the hibernation timer to start at the value required by the\r
188         tick interrupt. */\r
189         lpHTIMER_PRELOAD_REGISTER = ulHighResolutionReloadValue;\r
190         lpHTIMER_CONTROL_REGISTER = mainHTIMER_HIGH_RESOLUTION;\r
191 \r
192         /* Enable the HTIMER interrupt in the aggregated GIR17 block. */\r
193         lpEC_GIRQ17_SOURCE = lpGIRQ17_BIT_HTIMER;\r
194         lpEC_GIRQ17_ENABLE_CLEAR = lpGIRQ17_BIT_HTIMER;\r
195         lpBLOCK_ENABLE_SET = lpHTIMER_GIRQ_BLOCK;\r
196         lpEC_GIRQ17_ENABLE_SET = lpGIRQ17_BIT_HTIMER;\r
197 \r
198         /* The hibernation timer is not an auto-reload timer, so gets reset\r
199         from within the ISR itself.  For that reason it's interrupt is set\r
200         to the highest possible priority to ensure clock slippage is minimised. */\r
201         NVIC_SetPriority( GIRQ17_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
202         NVIC_ClearPendingIRQ( GIRQ17_IRQn );\r
203         NVIC_EnableIRQ( GIRQ17_IRQn );\r
204 \r
205         /* A basic timer is also started, purely for test purposes.  Its only\r
206         purpose is to bring the CPU out of its sleep mode by an interrupt other\r
207         than the tick interrupt in order to get more code test coverage. */\r
208         #if( lpINCLUDE_TEST_TIMER == 1 )\r
209         {\r
210                 prvSetupBasicTimer();\r
211         }\r
212         #endif\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 /* Override the default definition of vPortSuppressTicksAndSleep() that is\r
217 weakly defined in the FreeRTOS Cortex-M port layer with a version that manages\r
218 the hibernation timer, as the tick is generated from the low power hibernation\r
219 timer and not the SysTick as would normally be the case on a Cortex-M. */\r
220 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
221 {\r
222 uint32_t ulCompleteTickPeriods, ulReloadValue, ulCompletedTimerDecrements, ulCountAfterSleep, ulCountBeforeSleep;\r
223 eSleepModeStatus eSleepAction;\r
224 TickType_t xModifiableIdleTime;\r
225 \r
226         /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */\r
227 \r
228         /* Make sure the hibernation timer reload value does not overflow the\r
229         counter. */\r
230         if( xExpectedIdleTime > ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks )\r
231         {\r
232                 xExpectedIdleTime = ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks;\r
233         }\r
234 \r
235         /* Stop the timer momentarily.  The time the timer is stopped for is\r
236         accounted for as best it can be, but using the tickless mode will\r
237         inevitably result in some tiny drift of the time maintained by the kernel\r
238         with respect to calendar time.  Take the count value first as clearing\r
239         the preload value also seems to clear the count. */\r
240         ulCountBeforeSleep = ( uint32_t ) lpHTIMER_COUNT_REGISTER;\r
241         lpHTIMER_PRELOAD_REGISTER = 0;\r
242 \r
243         /* Calculate the reload value required to wait xExpectedIdleTime tick\r
244         periods.  -1 is used as the current time slice will already be part way\r
245         through, the part value coming from the current timer count value. */\r
246         ulReloadValue = ulCountBeforeSleep + ( ulReloadValueForOneHighResolutionTick * ( xExpectedIdleTime - 1UL ) );\r
247 \r
248         if( ulReloadValue > ulStoppedTimerCompensation )\r
249         {\r
250                 /* Compensate for the fact that the timer is going to be stopped\r
251                 momentarily. */\r
252                 ulReloadValue -= ulStoppedTimerCompensation;\r
253         }\r
254 \r
255         /* Enter a critical section but don't use the taskENTER_CRITICAL() method as\r
256         that will mask interrupts that should exit sleep mode. */\r
257         __asm volatile( "cpsid i" );\r
258         __asm volatile( "dsb" );\r
259         __asm volatile( "isb" );\r
260 \r
261         /* The tick flag is set to false before sleeping.  If it is true when sleep\r
262         mode is exited then sleep mode was probably exited because the tick was\r
263         suppressed for the entire xExpectedIdleTime period. */\r
264         ulTickFlag = pdFALSE;\r
265 \r
266         /* If a context switch is pending then abandon the low power entry as\r
267         the context switch might have been pended by an external interrupt that\r
268         requires processing. */\r
269         eSleepAction = eTaskConfirmSleepModeStatus();\r
270         if( eSleepAction == eAbortSleep )\r
271         {\r
272                 /* Restart the timer from whatever remains in the counter register,\r
273                 but 0 is not a valid value. */\r
274                 ulReloadValue = ulCountBeforeSleep - ulStoppedTimerCompensation;\r
275 \r
276                 if( ulReloadValue == 0 )\r
277                 {\r
278                         ulReloadValue = ulReloadValueForOneHighResolutionTick;\r
279                         ulCompleteTickPeriods = 1UL;\r
280                 }\r
281                 else\r
282                 {\r
283                         ulCompleteTickPeriods = 0UL;\r
284                 }\r
285 \r
286                 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;\r
287 \r
288                 /* Re-enable interrupts - see comments above the cpsid instruction()\r
289                 above. */\r
290                 __asm volatile( "cpsie i" );\r
291                 __asm volatile( "dsb" );\r
292                 __asm volatile( "isb" );\r
293         }\r
294         else\r
295         {\r
296                 /* Write the calculated reload value, which will also start the\r
297                 timer. */\r
298                 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;\r
299 \r
300                 /* Allow the application to define some pre-sleep processing. */\r
301                 xModifiableIdleTime = xExpectedIdleTime;\r
302                 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );\r
303 \r
304                 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()\r
305                 means the application defined code has already executed the sleep\r
306                 instructions. */\r
307                 if( xModifiableIdleTime > 0 )\r
308                 {\r
309                         __asm volatile( "dsb" );\r
310                         __asm volatile( "wfi" );\r
311                         __asm volatile( "isb" );\r
312                 }\r
313 \r
314                 /* Allow the application to define some post sleep processing. */\r
315                 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );\r
316 \r
317                 /* Stop the hibernation timer.  Again, the time the timer is stopped\r
318                 for is accounted for as best it can be, but using the tickless mode\r
319                 will inevitably result in some tiny drift of the time maintained by the\r
320                 kernel with respect to calendar time.  Take the count value first as\r
321                 setting the preload to zero also seems to clear the count. */\r
322                 ulCountAfterSleep = lpHTIMER_COUNT_REGISTER;\r
323                 lpHTIMER_PRELOAD_REGISTER = 0;\r
324 \r
325                 /* Re-enable interrupts - see comments above the cpsid instruction()\r
326                 above. */\r
327                 __asm volatile( "cpsie i" );\r
328                 __asm volatile( "dsb" );\r
329                 __asm volatile( "isb" );\r
330 \r
331                 if( ulTickFlag != pdFALSE )\r
332                 {\r
333                         /* The tick interrupt has already executed, although because this\r
334                         function is called with the scheduler suspended the actual tick\r
335                         processing will not occur until after this function has exited.\r
336                         The timer has already been reloaded to count in ticks, and can just\r
337                         continue counting down from its current value. */\r
338                         ulReloadValue = ulCountAfterSleep;\r
339 \r
340                         /* Sanity check that the timer's reload value has indeed been\r
341                         reset. */\r
342                         configASSERT( ( uint32_t ) lpHTIMER_PRELOAD_REGISTER == ulReloadValueForOneHighResolutionTick );\r
343 \r
344                         /* The tick interrupt handler will already have pended the tick\r
345                         processing in the kernel.  As the pending tick will be processed as\r
346                         soon as this function exits, the tick value     maintained by the tick\r
347                         is stepped forward by one less than the time spent sleeping.  The\r
348                         actual stepping of the tick appears later in this function. */\r
349                         ulCompleteTickPeriods = xExpectedIdleTime - 1UL;\r
350                 }\r
351                 else\r
352                 {\r
353                         /* Something other than the tick interrupt ended the sleep.  How\r
354                         many complete tick periods passed while the processor was\r
355                         sleeping? */\r
356                         ulCompletedTimerDecrements = ulReloadValue - ulCountAfterSleep;\r
357 \r
358                         /* Undo the adjustment that was made to the reload value to account\r
359                         for the fact that a time slice was part way through when this\r
360                         function was called before working out how many complete tick\r
361                         periods this represents.  (could have used [ulExpectedIdleTime *\r
362                         ulReloadValueForOneHighResolutionTick] instead of ulReloadValue on\r
363                         the previous line, but this way avoids the multiplication). */\r
364                         ulCompletedTimerDecrements += ( ulReloadValueForOneHighResolutionTick - ulCountBeforeSleep );\r
365                         ulCompleteTickPeriods = ulCompletedTimerDecrements / ulReloadValueForOneHighResolutionTick;\r
366 \r
367                         /* The reload value is set to whatever fraction of a single tick\r
368                         period remains. */\r
369                         ulReloadValue = ( ( ulCompleteTickPeriods + 1UL ) * ulReloadValueForOneHighResolutionTick ) - ulCompletedTimerDecrements;\r
370                 }\r
371 \r
372                 /* Cannot use a reload value of 0 - it will not start the timer. */\r
373                 if( ulReloadValue == 0 )\r
374                 {\r
375                         /* There is no fraction remaining. */\r
376                         ulReloadValue = ulReloadValueForOneHighResolutionTick;\r
377                         ulCompleteTickPeriods++;\r
378                 }\r
379 \r
380                 /* Restart the timer so it runs down from the reload value.  The reload\r
381                 value will get set to the value required to generate exactly one tick\r
382                 period the next time the tick interrupt executes. */\r
383                 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;\r
384         }\r
385 \r
386         /* Wind the tick forward by the number of tick periods that the CPU\r
387         remained in a low power state. */\r
388         vTaskStepTick( ulCompleteTickPeriods );\r
389 }\r
390 /*-----------------------------------------------------------*/\r
391 \r
392 void NVIC_Handler_GIRQ23( void )\r
393 {\r
394 static volatile uint32_t ulTimerCounts = 0;\r
395 \r
396         /* The low power demo is using aggregated interrupts, so although in the\r
397         demo btimer 0 is the only peripheral that will generate interrupts on\r
398         this vector, in a real application it would be necessary to first check the\r
399         interrupt source. */\r
400         if( ( lpEC_GIRQ23_SOURCE & lpGIRQ23_BIT_TIMER0 ) != 0 )\r
401         {\r
402                 /* Btimer0 interrupted.  Clear the interrupt. */\r
403                 lpEC_GIRQ23_SOURCE = lpGIRQ23_BIT_TIMER0;\r
404 \r
405                 /* This timer is used for test purposes.  Its only function is to\r
406                 generate interrupts while the MCU is sleeping, so the MCU is sometimes\r
407                 brought out of sleep by a means other than the tick interrupt. */\r
408                 ulTimerCounts++;\r
409         }\r
410         else\r
411         {\r
412                 /* Don't expect any other interrupts to use this vector in this\r
413                 demo.  Force an assert. */\r
414                 configASSERT( lpEC_GIRQ23_SOURCE == 0 );\r
415         }\r
416 }\r
417 /*-----------------------------------------------------------*/\r
418 \r
419 #endif /* configCREATE_LOW_POWER_DEMO == 1 */\r