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
80 /* This file contains functions that will override the default implementations
\r
81 in the RTOS port layer. Therefore only build this file if the low power demo
\r
83 #if( configCREATE_LOW_POWER_DEMO == 1 )
\r
85 /* ID of the hibernation timer used to generate the tick. */
\r
86 #define mainTICK_HTIMER_ID 0
\r
88 /* Written to the hibernation timer control register to configure the timer for
\r
89 its higher resolution. */
\r
90 #define mainHTIMER_HIGH_RESOLUTION 0
\r
92 /* The frequency of the hibernation timer when it is running at its higher
\r
93 resolution and low resolution respectively. */
\r
94 #define mainHIGHER_RESOLUTION_TIMER_HZ ( 32787UL ) /* (1000000us / 30.5us) as each LSB is 30.5us. */
\r
95 #define mainLOW_RESOLUTION_TIMER_HZ ( 8UL ) /* ( 1000ms / 125ms ) as each LSB is 0.125s. */
\r
97 /* When lpINCLUDE_TEST_TIMER is set to 1 a basic timer is used to generate
\r
98 interrupts at a low frequency. The purpose being to bring the CPU out of its
\r
99 sleep mode by an interrupt other than the tick interrupt, and therefore
\r
100 allowing an additional paths through the code to be tested. */
\r
101 #define lpINCLUDE_TEST_TIMER 0
\r
103 /* Some registers are accessed directly as the library is not compatible with
\r
104 all the compilers used. */
\r
105 #define lpHTIMER_PRELOAD_REGISTER ( * ( volatile uint16_t * ) 0x40009800 )
\r
106 #define lpHTIMER_CONTROL_REGISTER ( * ( volatile uint16_t * ) 0x40009804 )
\r
107 #define lpHTIMER_COUNT_REGISTER ( * ( volatile uint16_t * ) 0x40009808 )
\r
108 #define lpEC_GIRQ17_ENABLE_SET ( * ( volatile uint32_t * ) 0x4000C0B8 )
\r
109 #define lpHTIMER_INTERRUPT_CONTROL_BIT ( 1UL << 20UL )
\r
112 * The low power demo does not use the SysTick, so override the
\r
113 * vPortSetupTickInterrupt() function with an implementation that configures
\r
114 * the low power clock. NOTE: This function name must not be changed as it
\r
115 * is called from the RTOS portable layer.
\r
117 void vPortSetupTimerInterrupt( void );
\r
120 * To fully test the low power tick processing it is necessary to sometimes
\r
121 * bring the MCU out of its sleep state by a method other than the tick
\r
122 * interrupt. Interrupts generated from a basic timer are used for this
\r
125 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
126 static void prvSetupBasicTimer( void );
\r
129 /*-----------------------------------------------------------*/
\r
131 /* The reload value to use in the timer to generate the tick interrupt -
\r
132 assumes the timer is running at its higher resolution. */
\r
133 static const uint32_t ulHighResolutionReloadValue = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );
\r
135 /* Calculate how many clock increments make up a single tick period. */
\r
136 static const uint32_t ulReloadValueForOneHighResolutionTick = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );
\r
137 //static const uint32_t usReloadValueForOneLowResolutionTick = ( mainLOW_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );
\r
139 /* Calculate the maximum number of ticks that can be suppressed when using the
\r
140 high resolution clock and low resolution clock respectively. */
\r
141 static uint32_t ulMaximumPossibleSuppressedHighResolutionTicks = 0;
\r
142 //static const uint16_t usMaximumPossibleSuppressedLowResolutionTicks = USHRT_MAX / usReloadValueForOneLowResolutionTick;
\r
144 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so
\r
145 use zero - but leave the value here to assist porting to different clock
\r
147 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
149 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
150 sleep mode was exited because of an timer interrupt or a different interrupt. */
\r
151 static volatile uint32_t ulTickFlag = pdFALSE;
\r
153 /*-----------------------------------------------------------*/
\r
155 void NVIC_Handler_HIB_TMR( void )
\r
157 lpHTIMER_PRELOAD_REGISTER = ulHighResolutionReloadValue;
\r
159 /* Increment the RTOS tick. */
\r
160 if( xTaskIncrementTick() != pdFALSE )
\r
162 /* A context switch is required. Context switching is performed in
\r
163 the PendSV interrupt. Pend the PendSV interrupt. */
\r
164 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
167 /* The CPU woke because of a tick. */
\r
168 ulTickFlag = pdTRUE;
\r
170 /*-----------------------------------------------------------*/
\r
172 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
174 #define GIRQ23_ENABLE_SET ( * ( uint32_t * ) 0x4000C130 )
\r
176 static void prvSetupBasicTimer( void )
\r
178 const uint8_t ucTimerChannel = 0;
\r
179 const uint32_t ulTimer0Count = configCPU_CLOCK_HZ / 10;
\r
181 GIRQ23_ENABLE_SET = 0x03;
\r
182 *(unsigned int*)0x4000FC18 = 1;
\r
184 /* To fully test the low power tick processing it is necessary to sometimes
\r
185 bring the MCU out of its sleep state by a method other than the tick
\r
186 interrupt. Interrupts generated from a basic timer are used for this
\r
188 btimer_init( ucTimerChannel, BTIMER_AUTO_RESTART | BTIMER_COUNT_DOWN | BTIMER_INT_EN, 0, ulTimer0Count, ulTimer0Count );
\r
189 btimer_interrupt_status_get_clr( ucTimerChannel );
\r
190 enable_timer0_irq();
\r
191 NVIC_SetPriority( TIMER0_IRQn, ucTimerChannel );
\r
192 NVIC_ClearPendingIRQ( TIMER0_IRQn );
\r
193 NVIC_EnableIRQ( TIMER0_IRQn );
\r
194 btimer_start( ucTimerChannel );
\r
197 #endif /* lpINCLUDE_TEST_TIMER */
\r
198 /*-----------------------------------------------------------*/
\r
200 void vPortSetupTimerInterrupt( void )
\r
202 ulMaximumPossibleSuppressedHighResolutionTicks = ( ( uint32_t ) USHRT_MAX ) / ulReloadValueForOneHighResolutionTick;
\r
204 /* Set up the hibernation timer to start at the value required by the
\r
205 tick interrupt. Equivalent to the following libarary call. The library
\r
206 is not used as it is not compatible with all the compilers used:
\r
207 htimer_enable( mainTICK_HTIMER_ID, ulHighResolutionReloadValue, mainHTIMER_HIGH_RESOLUTION ); */
\r
208 lpHTIMER_PRELOAD_REGISTER = ulHighResolutionReloadValue;
\r
209 lpHTIMER_CONTROL_REGISTER = mainHTIMER_HIGH_RESOLUTION;
\r
211 /* Enable the HTIMER interrupt. Equivalent to enable_htimer0_irq(); */
\r
212 lpEC_GIRQ17_ENABLE_SET |= lpHTIMER_INTERRUPT_CONTROL_BIT;
\r
214 /* The hibernation timer is not an auto-reload timer, so gets reset
\r
215 from within the ISR itself. For that reason it's interrupt is set
\r
216 to the highest possible priority to ensure clock slippage is minimised. */
\r
217 NVIC_SetPriority( HTIMER_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
218 NVIC_ClearPendingIRQ( HTIMER_IRQn );
\r
219 NVIC_EnableIRQ( HTIMER_IRQn );
\r
221 /* A basic timer is also started, purely for test purposes. Its only
\r
222 purpose is to bring the CPU out of its sleep mode by an interrupt other
\r
223 than the tick interrupt in order to get more code test coverage. */
\r
224 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
226 prvSetupBasicTimer();
\r
230 /*-----------------------------------------------------------*/
\r
232 /* Override the default definition of vPortSuppressTicksAndSleep() that is
\r
233 weakly defined in the FreeRTOS Cortex-M port layer with a version that manages
\r
234 the hibernation timer, as the tick is generated from the low power hibernation
\r
235 timer and not the SysTick as would normally be the case on a Cortex-M. */
\r
236 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
238 uint32_t ulCompleteTickPeriods, ulReloadValue, ulCompletedTimerDecrements, ulCountAfterSleep, ulCountBeforeSleep;
\r
239 eSleepModeStatus eSleepAction;
\r
240 TickType_t xModifiableIdleTime;
\r
242 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
244 /* Make sure the hibernation timer reload value does not overflow the
\r
246 if( xExpectedIdleTime > ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks )
\r
248 xExpectedIdleTime = ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks;
\r
251 /* Stop the timer momentarily. The time the timer is stopped for is
\r
252 accounted for as best it can be, but using the tickless mode will
\r
253 inevitably result in some tiny drift of the time maintained by the kernel
\r
254 with respect to calendar time. Take the count value first as clearing
\r
255 the preload value also seems to clear the count. */
\r
256 ulCountBeforeSleep = ( uint32_t ) lpHTIMER_COUNT_REGISTER;
\r
257 lpHTIMER_PRELOAD_REGISTER = 0;
\r
259 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
260 periods. -1 is used as the current time slice will already be part way
\r
261 through, the part value coming from the current timer count value. */
\r
262 ulReloadValue = ulCountBeforeSleep + ( ulReloadValueForOneHighResolutionTick * ( xExpectedIdleTime - 1UL ) );
\r
264 if( ulReloadValue > ulStoppedTimerCompensation )
\r
266 /* Compensate for the fact that the timer is going to be stopped
\r
268 ulReloadValue -= ulStoppedTimerCompensation;
\r
271 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
272 that will mask interrupts that should exit sleep mode. */
\r
273 __asm volatile( "cpsid i" );
\r
274 __asm volatile( "dsb" );
\r
275 __asm volatile( "isb" );
\r
277 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
278 mode is exited then sleep mode was probably exited because the tick was
\r
279 suppressed for the entire xExpectedIdleTime period. */
\r
280 ulTickFlag = pdFALSE;
\r
282 /* If a context switch is pending then abandon the low power entry as
\r
283 the context switch might have been pended by an external interrupt that
\r
284 requires processing. */
\r
285 eSleepAction = eTaskConfirmSleepModeStatus();
\r
286 if( eSleepAction == eAbortSleep )
\r
288 /* Resetart the timer from whatever remains in the counter register,
\r
289 but 0 is not a valid value. */
\r
290 ulReloadValue = ulCountBeforeSleep - ulStoppedTimerCompensation;
\r
292 if( ulReloadValue == 0 )
\r
294 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
295 ulCompleteTickPeriods = 1UL;
\r
299 ulCompleteTickPeriods = 0UL;
\r
302 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
304 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
306 __asm volatile( "cpsie i" );
\r
307 __asm volatile( "dsb" );
\r
308 __asm volatile( "isb" );
\r
312 /* Write the calculated reload value, which will also start the
\r
314 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
316 /* Allow the application to define some pre-sleep processing. */
\r
317 xModifiableIdleTime = xExpectedIdleTime;
\r
318 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
320 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
321 means the application defined code has already executed the sleep
\r
323 if( xModifiableIdleTime > 0 )
\r
325 __asm volatile( "dsb" );
\r
326 __asm volatile( "wfi" );
\r
327 __asm volatile( "isb" );
\r
330 /* Allow the application to define some post sleep processing. */
\r
331 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
333 /* Stop the hibernation timer. Again, the time the tiemr is stopped
\r
334 for is accounted for as best it can be, but using the tickless mode
\r
335 will inevitably result in some tiny drift of the time maintained by the
\r
336 kernel with respect to calendar time. Take the count value first as
\r
337 setting the preload to zero also seems to clear the count. */
\r
338 ulCountAfterSleep = lpHTIMER_COUNT_REGISTER;
\r
339 lpHTIMER_PRELOAD_REGISTER = 0;
\r
341 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
343 __asm volatile( "cpsie i" );
\r
344 __asm volatile( "dsb" );
\r
345 __asm volatile( "isb" );
\r
347 if( ulTickFlag != pdFALSE )
\r
349 /* The tick interrupt has already executed, although because this
\r
350 function is called with the scheduler suspended the actual tick
\r
351 processing will not occur until after this function has exited.
\r
352 The timer has already been reloaded to count in ticks, and can just
\r
353 continue counting down from its current value. */
\r
354 ulReloadValue = ulCountAfterSleep;
\r
356 /* Sanity check that the timer's reload value has indeed been
\r
358 configASSERT( ( uint32_t ) lpHTIMER_PRELOAD_REGISTER == ulReloadValueForOneHighResolutionTick );
\r
360 /* The tick interrupt handler will already have pended the tick
\r
361 processing in the kernel. As the pending tick will be processed as
\r
362 soon as this function exits, the tick value maintained by the tick
\r
363 is stepped forward by one less than the time spent sleeping. The
\r
364 actual stepping of the tick appears later in this function. */
\r
365 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
369 /* Something other than the tick interrupt ended the sleep. How
\r
370 many complete tick periods passed while the processor was
\r
372 ulCompletedTimerDecrements = ulReloadValue - ulCountAfterSleep;
\r
374 /* Undo the adjustment that was made to the reload value to account
\r
375 for the fact that a time slice was part way through when this
\r
376 function was called before working out how many complete tick
\r
377 periods this represents. (could have used [ulExpectedIdleTime *
\r
378 ulReloadValueForOneHighResolutionTick] instead of ulReloadValue on
\r
379 the previous line, but this way avoids the multiplication). */
\r
380 ulCompletedTimerDecrements += ( ulReloadValueForOneHighResolutionTick - ulCountBeforeSleep );
\r
381 ulCompleteTickPeriods = ulCompletedTimerDecrements / ulReloadValueForOneHighResolutionTick;
\r
383 /* The reload value is set to whatever fraction of a single tick
\r
385 ulReloadValue = ( ( ulCompleteTickPeriods + 1UL ) * ulReloadValueForOneHighResolutionTick ) - ulCompletedTimerDecrements;
\r
388 /* Cannot use a reload value of 0 - it will not start the timer. */
\r
389 if( ulReloadValue == 0 )
\r
391 /* There is no fraction remaining. */
\r
392 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
393 ulCompleteTickPeriods++;
\r
396 /* Restart the timer so it runs down from the reload value. The reload
\r
397 value will get set to the value required to generate exactly one tick
\r
398 period the next time the tick interrupt executes. */
\r
399 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
402 /* Wind the tick forward by the number of tick periods that the CPU
\r
403 remained in a low power state. */
\r
404 vTaskStepTick( ulCompleteTickPeriods );
\r
406 /*-----------------------------------------------------------*/
\r
408 void NVIC_Handler_TMR0( void )
\r
410 /* This timer is used for test purposes. Its only function is to
\r
411 generate interrupts while the MCU is sleeping, so the MCU is sometimes
\r
412 brought out of sleep by a means other than the tick interrupt. */
\r
414 /*-----------------------------------------------------------*/
\r
417 #endif /* configCREATE_LOW_POWER_DEMO */
\r