2 FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* Standard includes. */
\r
73 /* FreeRTOS includes. */
\r
74 #include "FreeRTOS.h"
\r
77 /* Library includes. */
\r
78 #include "common_lib.h"
\r
79 #include "peripheral_library/interrupt/interrupt.h"
\r
80 #include "peripheral_library/basic_timer/btimer.h"
\r
82 /* This file contains functions that will override the default implementations
\r
83 in the RTOS port layer. Therefore only build this file if the low power demo
\r
85 #if( configCREATE_LOW_POWER_DEMO == 1 )
\r
87 /* ID of the hibernation timer used to generate the tick. */
\r
88 #define mainTICK_HTIMER_ID 0
\r
90 /* Written to the hibernation timer control register to configure the timer for
\r
91 its higher resolution. */
\r
92 #define mainHTIMER_HIGH_RESOLUTION 0
\r
94 /* The frequency of the hibernation timer when it is running at its higher
\r
95 resolution and low resolution respectively. */
\r
96 #define mainHIGHER_RESOLUTION_TIMER_HZ ( 32787UL ) /* (1000000us / 30.5us) as each LSB is 30.5us. */
\r
97 #define mainLOW_RESOLUTION_TIMER_HZ ( 8UL ) /* ( 1000ms / 125ms ) as each LSB is 0.125s. */
\r
99 /* When lpINCLUDE_TEST_TIMER is set to 1 a basic timer is used to generate
\r
100 interrupts at a low frequency. The purpose being to bring the CPU out of its
\r
101 sleep mode by an interrupt other than the tick interrupt, and therefore
\r
102 allowing an additional paths through the code to be tested. */
\r
103 #define lpINCLUDE_TEST_TIMER 0
\r
105 /* Registers and bits required to use the htimer in aggregated mode. */
\r
106 #define lpHTIMER_PRELOAD_REGISTER ( * ( volatile uint16_t * ) 0x40009800 )
\r
107 #define lpHTIMER_CONTROL_REGISTER ( * ( volatile uint16_t * ) 0x40009804 )
\r
108 #define lpHTIMER_COUNT_REGISTER ( * ( volatile uint16_t * ) 0x40009808 )
\r
109 #define lpEC_GIRQ17_ENABLE_SET ( * ( volatile uint32_t * ) 0x4000C0B8 )
\r
110 #define lpEC_GIRQ17_SOURCE ( * ( volatile uint32_t * ) 0x4000C0B4 )
\r
111 #define lpEC_GIRQ17_ENABLE_CLEAR ( * ( volatile uint32_t * ) 0x4000C0C0 )
\r
112 #define lpBLOCK_ENABLE_SET ( * ( volatile uint32_t * ) 0x4000c200 )
\r
113 #define lpGIRQ17_BIT_HTIMER ( 1UL << 20UL )
\r
114 #define lpHTIMER_GIRQ_BLOCK ( 1Ul << 17UL )
\r
116 /* Registers and bits required to use btimer 0 in aggregated mode. */
\r
117 #define lpGIRQ23_ENABLE_SET ( * ( volatile uint32_t * ) 0x4000C130 )
\r
118 #define lpEC_GIRQ23_SOURCE ( * ( volatile uint32_t * ) 0x4000C12C )
\r
119 #define lpEC_GIRQ23_ENABLE_CLEAR ( * ( volatile uint32_t * ) 0x4000C138 )
\r
120 #define lpGIRQ23_BIT_TIMER0 ( 1UL << 0UL )
\r
121 #define lpBTIMER_GIRQ_BLOCK ( 1UL << 23UL )
\r
124 * The low power demo does not use the SysTick, so override the
\r
125 * vPortSetupTickInterrupt() function with an implementation that configures
\r
126 * the low power clock. NOTE: This function name must not be changed as it
\r
127 * is called from the RTOS portable layer.
\r
129 void vPortSetupTimerInterrupt( void );
\r
132 * To fully test the low power tick processing it is necessary to sometimes
\r
133 * bring the MCU out of its sleep state by a method other than the tick
\r
134 * interrupt. Interrupts generated from a basic timer are used for this
\r
137 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
138 static void prvSetupBasicTimer( void );
\r
141 /*-----------------------------------------------------------*/
\r
143 /* The reload value to use in the timer to generate the tick interrupt -
\r
144 assumes the timer is running at its higher resolution. */
\r
145 static const uint32_t ulHighResolutionReloadValue = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );
\r
147 /* Calculate how many clock increments make up a single tick period. */
\r
148 static const uint32_t ulReloadValueForOneHighResolutionTick = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );
\r
150 /* Calculate the maximum number of ticks that can be suppressed when using the
\r
151 high resolution clock and low resolution clock respectively. */
\r
152 static uint32_t ulMaximumPossibleSuppressedHighResolutionTicks = 0;
\r
154 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so
\r
155 use zero - but leave the value here to assist porting to different clock
\r
157 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
159 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
160 sleep mode was exited because of an timer interrupt or a different interrupt. */
\r
161 static volatile uint32_t ulTickFlag = pdFALSE;
\r
163 /*-----------------------------------------------------------*/
\r
165 void NVIC_Handler_GIRQ17( void )
\r
167 /* The low power demo is using aggregated interrupts, so although in the
\r
168 demo the htimer is the only peripheral that will generate interrupts on
\r
169 this vector, in a real application it would be necessary to first check the
\r
170 interrupt source. */
\r
171 if( ( lpEC_GIRQ17_SOURCE & lpGIRQ17_BIT_HTIMER ) != 0 )
\r
173 /* The htimer interrupted. Clear the interrupt. */
\r
174 lpEC_GIRQ17_SOURCE = lpGIRQ17_BIT_HTIMER;
\r
175 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulHighResolutionReloadValue;
\r
177 /* Increment the RTOS tick. */
\r
178 if( xTaskIncrementTick() != pdFALSE )
\r
180 /* A context switch is required. Context switching is performed in
\r
181 the PendSV interrupt. Pend the PendSV interrupt. */
\r
182 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
185 /* The CPU woke because of a tick. */
\r
186 ulTickFlag = pdTRUE;
\r
190 /* Don't expect any other interrupts to use this vector in this
\r
191 demo. Force an assert. */
\r
192 configASSERT( lpEC_GIRQ17_SOURCE == 0 );
\r
195 /*-----------------------------------------------------------*/
\r
197 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
199 static void prvSetupBasicTimer( void )
\r
201 const uint8_t ucTimerChannel = 0;
\r
202 const uint32_t ulTimer0Count = configCPU_CLOCK_HZ / 10;
\r
204 /* Enable btimer 0 interrupt in the aggregated GIRQ23 block. */
\r
205 lpEC_GIRQ23_SOURCE = lpGIRQ23_BIT_TIMER0;
\r
206 lpEC_GIRQ23_ENABLE_CLEAR = lpGIRQ23_BIT_TIMER0;
\r
207 lpBLOCK_ENABLE_SET = lpBTIMER_GIRQ_BLOCK;
\r
208 lpGIRQ23_ENABLE_SET = lpGIRQ23_BIT_TIMER0;
\r
210 /* To fully test the low power tick processing it is necessary to sometimes
\r
211 bring the MCU out of its sleep state by a method other than the tick
\r
212 interrupt. Interrupts generated from a basic timer are used for this
\r
214 btimer_init( ucTimerChannel, BTIMER_AUTO_RESTART | BTIMER_COUNT_DOWN | BTIMER_INT_EN, 0, ulTimer0Count, ulTimer0Count );
\r
215 btimer_interrupt_status_get_clr( ucTimerChannel );
\r
216 NVIC_SetPriority( GIRQ23_IRQn, ucTimerChannel );
\r
217 NVIC_ClearPendingIRQ( GIRQ23_IRQn );
\r
218 NVIC_EnableIRQ( GIRQ23_IRQn );
\r
219 btimer_start( ucTimerChannel );
\r
222 #endif /* lpINCLUDE_TEST_TIMER */
\r
223 /*-----------------------------------------------------------*/
\r
225 void vPortSetupTimerInterrupt( void )
\r
227 ulMaximumPossibleSuppressedHighResolutionTicks = ( ( uint32_t ) USHRT_MAX ) / ulReloadValueForOneHighResolutionTick;
\r
229 /* Set up the hibernation timer to start at the value required by the
\r
231 lpHTIMER_PRELOAD_REGISTER = ulHighResolutionReloadValue;
\r
232 lpHTIMER_CONTROL_REGISTER = mainHTIMER_HIGH_RESOLUTION;
\r
234 /* Enable the HTIMER interrupt in the aggregated GIR17 block. */
\r
235 lpEC_GIRQ17_SOURCE = lpGIRQ17_BIT_HTIMER;
\r
236 lpEC_GIRQ17_ENABLE_CLEAR = lpGIRQ17_BIT_HTIMER;
\r
237 lpBLOCK_ENABLE_SET = lpHTIMER_GIRQ_BLOCK;
\r
238 lpEC_GIRQ17_ENABLE_SET = lpGIRQ17_BIT_HTIMER;
\r
240 /* The hibernation timer is not an auto-reload timer, so gets reset
\r
241 from within the ISR itself. For that reason it's interrupt is set
\r
242 to the highest possible priority to ensure clock slippage is minimised. */
\r
243 NVIC_SetPriority( GIRQ17_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
244 NVIC_ClearPendingIRQ( GIRQ17_IRQn );
\r
245 NVIC_EnableIRQ( GIRQ17_IRQn );
\r
247 /* A basic timer is also started, purely for test purposes. Its only
\r
248 purpose is to bring the CPU out of its sleep mode by an interrupt other
\r
249 than the tick interrupt in order to get more code test coverage. */
\r
250 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
252 prvSetupBasicTimer();
\r
256 /*-----------------------------------------------------------*/
\r
258 /* Override the default definition of vPortSuppressTicksAndSleep() that is
\r
259 weakly defined in the FreeRTOS Cortex-M port layer with a version that manages
\r
260 the hibernation timer, as the tick is generated from the low power hibernation
\r
261 timer and not the SysTick as would normally be the case on a Cortex-M. */
\r
262 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
264 uint32_t ulCompleteTickPeriods, ulReloadValue, ulCompletedTimerDecrements, ulCountAfterSleep, ulCountBeforeSleep;
\r
265 eSleepModeStatus eSleepAction;
\r
266 TickType_t xModifiableIdleTime;
\r
268 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
270 /* Make sure the hibernation timer reload value does not overflow the
\r
272 if( xExpectedIdleTime > ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks )
\r
274 xExpectedIdleTime = ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks;
\r
277 /* Stop the timer momentarily. The time the timer is stopped for is
\r
278 accounted for as best it can be, but using the tickless mode will
\r
279 inevitably result in some tiny drift of the time maintained by the kernel
\r
280 with respect to calendar time. Take the count value first as clearing
\r
281 the preload value also seems to clear the count. */
\r
282 ulCountBeforeSleep = ( uint32_t ) lpHTIMER_COUNT_REGISTER;
\r
283 lpHTIMER_PRELOAD_REGISTER = 0;
\r
285 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
286 periods. -1 is used as the current time slice will already be part way
\r
287 through, the part value coming from the current timer count value. */
\r
288 ulReloadValue = ulCountBeforeSleep + ( ulReloadValueForOneHighResolutionTick * ( xExpectedIdleTime - 1UL ) );
\r
290 if( ulReloadValue > ulStoppedTimerCompensation )
\r
292 /* Compensate for the fact that the timer is going to be stopped
\r
294 ulReloadValue -= ulStoppedTimerCompensation;
\r
297 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
298 that will mask interrupts that should exit sleep mode. */
\r
299 __asm volatile( "cpsid i" );
\r
300 __asm volatile( "dsb" );
\r
301 __asm volatile( "isb" );
\r
303 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
304 mode is exited then sleep mode was probably exited because the tick was
\r
305 suppressed for the entire xExpectedIdleTime period. */
\r
306 ulTickFlag = pdFALSE;
\r
308 /* If a context switch is pending then abandon the low power entry as
\r
309 the context switch might have been pended by an external interrupt that
\r
310 requires processing. */
\r
311 eSleepAction = eTaskConfirmSleepModeStatus();
\r
312 if( eSleepAction == eAbortSleep )
\r
314 /* Restart the timer from whatever remains in the counter register,
\r
315 but 0 is not a valid value. */
\r
316 ulReloadValue = ulCountBeforeSleep - ulStoppedTimerCompensation;
\r
318 if( ulReloadValue == 0 )
\r
320 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
321 ulCompleteTickPeriods = 1UL;
\r
325 ulCompleteTickPeriods = 0UL;
\r
328 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
330 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
332 __asm volatile( "cpsie i" );
\r
333 __asm volatile( "dsb" );
\r
334 __asm volatile( "isb" );
\r
338 /* Write the calculated reload value, which will also start the
\r
340 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
342 /* Allow the application to define some pre-sleep processing. */
\r
343 xModifiableIdleTime = xExpectedIdleTime;
\r
344 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
346 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
347 means the application defined code has already executed the sleep
\r
349 if( xModifiableIdleTime > 0 )
\r
351 __asm volatile( "dsb" );
\r
352 __asm volatile( "wfi" );
\r
353 __asm volatile( "isb" );
\r
356 /* Allow the application to define some post sleep processing. */
\r
357 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
359 /* Stop the hibernation timer. Again, the time the timer is stopped
\r
360 for is accounted for as best it can be, but using the tickless mode
\r
361 will inevitably result in some tiny drift of the time maintained by the
\r
362 kernel with respect to calendar time. Take the count value first as
\r
363 setting the preload to zero also seems to clear the count. */
\r
364 ulCountAfterSleep = lpHTIMER_COUNT_REGISTER;
\r
365 lpHTIMER_PRELOAD_REGISTER = 0;
\r
367 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
369 __asm volatile( "cpsie i" );
\r
370 __asm volatile( "dsb" );
\r
371 __asm volatile( "isb" );
\r
373 if( ulTickFlag != pdFALSE )
\r
375 /* The tick interrupt has already executed, although because this
\r
376 function is called with the scheduler suspended the actual tick
\r
377 processing will not occur until after this function has exited.
\r
378 The timer has already been reloaded to count in ticks, and can just
\r
379 continue counting down from its current value. */
\r
380 ulReloadValue = ulCountAfterSleep;
\r
382 /* Sanity check that the timer's reload value has indeed been
\r
384 configASSERT( ( uint32_t ) lpHTIMER_PRELOAD_REGISTER == ulReloadValueForOneHighResolutionTick );
\r
386 /* The tick interrupt handler will already have pended the tick
\r
387 processing in the kernel. As the pending tick will be processed as
\r
388 soon as this function exits, the tick value maintained by the tick
\r
389 is stepped forward by one less than the time spent sleeping. The
\r
390 actual stepping of the tick appears later in this function. */
\r
391 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
395 /* Something other than the tick interrupt ended the sleep. How
\r
396 many complete tick periods passed while the processor was
\r
398 ulCompletedTimerDecrements = ulReloadValue - ulCountAfterSleep;
\r
400 /* Undo the adjustment that was made to the reload value to account
\r
401 for the fact that a time slice was part way through when this
\r
402 function was called before working out how many complete tick
\r
403 periods this represents. (could have used [ulExpectedIdleTime *
\r
404 ulReloadValueForOneHighResolutionTick] instead of ulReloadValue on
\r
405 the previous line, but this way avoids the multiplication). */
\r
406 ulCompletedTimerDecrements += ( ulReloadValueForOneHighResolutionTick - ulCountBeforeSleep );
\r
407 ulCompleteTickPeriods = ulCompletedTimerDecrements / ulReloadValueForOneHighResolutionTick;
\r
409 /* The reload value is set to whatever fraction of a single tick
\r
411 ulReloadValue = ( ( ulCompleteTickPeriods + 1UL ) * ulReloadValueForOneHighResolutionTick ) - ulCompletedTimerDecrements;
\r
414 /* Cannot use a reload value of 0 - it will not start the timer. */
\r
415 if( ulReloadValue == 0 )
\r
417 /* There is no fraction remaining. */
\r
418 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
419 ulCompleteTickPeriods++;
\r
422 /* Restart the timer so it runs down from the reload value. The reload
\r
423 value will get set to the value required to generate exactly one tick
\r
424 period the next time the tick interrupt executes. */
\r
425 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
428 /* Wind the tick forward by the number of tick periods that the CPU
\r
429 remained in a low power state. */
\r
430 vTaskStepTick( ulCompleteTickPeriods );
\r
432 /*-----------------------------------------------------------*/
\r
434 void NVIC_Handler_GIRQ23( void )
\r
436 static volatile uint32_t ulTimerCounts = 0;
\r
438 /* The low power demo is using aggregated interrupts, so although in the
\r
439 demo btimer 0 is the only peripheral that will generate interrupts on
\r
440 this vector, in a real application it would be necessary to first check the
\r
441 interrupt source. */
\r
442 if( ( lpEC_GIRQ23_SOURCE & lpGIRQ23_BIT_TIMER0 ) != 0 )
\r
444 /* Btimer0 interrupted. Clear the interrupt. */
\r
445 lpEC_GIRQ23_SOURCE = lpGIRQ23_BIT_TIMER0;
\r
447 /* This timer is used for test purposes. Its only function is to
\r
448 generate interrupts while the MCU is sleeping, so the MCU is sometimes
\r
449 brought out of sleep by a means other than the tick interrupt. */
\r
454 /* Don't expect any other interrupts to use this vector in this
\r
455 demo. Force an assert. */
\r
456 configASSERT( lpEC_GIRQ23_SOURCE == 0 );
\r
459 /*-----------------------------------------------------------*/
\r
461 #endif /* configCREATE_LOW_POWER_DEMO == 1 */
\r