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