2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard includes. */
\r
31 /* FreeRTOS includes. */
\r
32 #include "FreeRTOS.h"
\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
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
43 #if( configCREATE_LOW_POWER_DEMO == 1 )
\r
45 /* ID of the hibernation timer used to generate the tick. */
\r
46 #define mainTICK_HTIMER_ID 0
\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
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
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
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
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
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
87 void vPortSetupTimerInterrupt( void );
\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
95 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
96 static void prvSetupBasicTimer( void );
\r
99 /*-----------------------------------------------------------*/
\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
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
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
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
115 static const uint32_t ulStoppedTimerCompensation = 0UL;
\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
121 /*-----------------------------------------------------------*/
\r
123 void NVIC_Handler_GIRQ17( void )
\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
131 /* The htimer interrupted. Clear the interrupt. */
\r
132 lpEC_GIRQ17_SOURCE = lpGIRQ17_BIT_HTIMER;
\r
133 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulHighResolutionReloadValue;
\r
135 /* Increment the RTOS tick. */
\r
136 if( xTaskIncrementTick() != pdFALSE )
\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
143 /* The CPU woke because of a tick. */
\r
144 ulTickFlag = pdTRUE;
\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
153 /*-----------------------------------------------------------*/
\r
155 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
157 static void prvSetupBasicTimer( void )
\r
159 const uint8_t ucTimerChannel = 0;
\r
160 const uint32_t ulTimer0Count = configCPU_CLOCK_HZ / 10;
\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
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
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
180 #endif /* lpINCLUDE_TEST_TIMER */
\r
181 /*-----------------------------------------------------------*/
\r
183 void vPortSetupTimerInterrupt( void )
\r
185 ulMaximumPossibleSuppressedHighResolutionTicks = ( ( uint32_t ) USHRT_MAX ) / ulReloadValueForOneHighResolutionTick;
\r
187 /* Set up the hibernation timer to start at the value required by the
\r
189 lpHTIMER_PRELOAD_REGISTER = ulHighResolutionReloadValue;
\r
190 lpHTIMER_CONTROL_REGISTER = mainHTIMER_HIGH_RESOLUTION;
\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
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
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
210 prvSetupBasicTimer();
\r
214 /*-----------------------------------------------------------*/
\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
222 uint32_t ulCompleteTickPeriods, ulReloadValue, ulCompletedTimerDecrements, ulCountAfterSleep, ulCountBeforeSleep;
\r
223 eSleepModeStatus eSleepAction;
\r
224 TickType_t xModifiableIdleTime;
\r
226 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
228 /* Make sure the hibernation timer reload value does not overflow the
\r
230 if( xExpectedIdleTime > ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks )
\r
232 xExpectedIdleTime = ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks;
\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
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
248 if( ulReloadValue > ulStoppedTimerCompensation )
\r
250 /* Compensate for the fact that the timer is going to be stopped
\r
252 ulReloadValue -= ulStoppedTimerCompensation;
\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
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
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
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
276 if( ulReloadValue == 0 )
\r
278 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
279 ulCompleteTickPeriods = 1UL;
\r
283 ulCompleteTickPeriods = 0UL;
\r
286 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
288 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
290 __asm volatile( "cpsie i" );
\r
291 __asm volatile( "dsb" );
\r
292 __asm volatile( "isb" );
\r
296 /* Write the calculated reload value, which will also start the
\r
298 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
300 /* Allow the application to define some pre-sleep processing. */
\r
301 xModifiableIdleTime = xExpectedIdleTime;
\r
302 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
304 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
305 means the application defined code has already executed the sleep
\r
307 if( xModifiableIdleTime > 0 )
\r
309 __asm volatile( "dsb" );
\r
310 __asm volatile( "wfi" );
\r
311 __asm volatile( "isb" );
\r
314 /* Allow the application to define some post sleep processing. */
\r
315 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\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
325 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
327 __asm volatile( "cpsie i" );
\r
328 __asm volatile( "dsb" );
\r
329 __asm volatile( "isb" );
\r
331 if( ulTickFlag != pdFALSE )
\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
340 /* Sanity check that the timer's reload value has indeed been
\r
342 configASSERT( ( uint32_t ) lpHTIMER_PRELOAD_REGISTER == ulReloadValueForOneHighResolutionTick );
\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
353 /* Something other than the tick interrupt ended the sleep. How
\r
354 many complete tick periods passed while the processor was
\r
356 ulCompletedTimerDecrements = ulReloadValue - ulCountAfterSleep;
\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
367 /* The reload value is set to whatever fraction of a single tick
\r
369 ulReloadValue = ( ( ulCompleteTickPeriods + 1UL ) * ulReloadValueForOneHighResolutionTick ) - ulCompletedTimerDecrements;
\r
372 /* Cannot use a reload value of 0 - it will not start the timer. */
\r
373 if( ulReloadValue == 0 )
\r
375 /* There is no fraction remaining. */
\r
376 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
377 ulCompleteTickPeriods++;
\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
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
390 /*-----------------------------------------------------------*/
\r
392 void NVIC_Handler_GIRQ23( void )
\r
394 static volatile uint32_t ulTimerCounts = 0;
\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
402 /* Btimer0 interrupted. Clear the interrupt. */
\r
403 lpEC_GIRQ23_SOURCE = lpGIRQ23_BIT_TIMER0;
\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
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
417 /*-----------------------------------------------------------*/
\r
419 #endif /* configCREATE_LOW_POWER_DEMO == 1 */
\r