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 /* Some registers are accessed directly as the library is not compatible with
\r
106 all the compilers used. */
\r
107 #define lpHTIMER_PRELOAD_REGISTER ( * ( volatile uint16_t * ) 0x40009800 )
\r
108 #define lpHTIMER_CONTROL_REGISTER ( * ( volatile uint16_t * ) 0x40009804 )
\r
109 #define lpHTIMER_COUNT_REGISTER ( * ( volatile uint16_t * ) 0x40009808 )
\r
110 #define lpEC_GIRQ17_ENABLE_SET ( * ( volatile uint32_t * ) 0x4000C0B8 )
\r
111 #define lpHTIMER_INTERRUPT_CONTROL_BIT ( 1UL << 20UL )
\r
114 * The low power demo does not use the SysTick, so override the
\r
115 * vPortSetupTickInterrupt() function with an implementation that configures
\r
116 * the low power clock. NOTE: This function name must not be changed as it
\r
117 * is called from the RTOS portable layer.
\r
119 void vPortSetupTimerInterrupt( void );
\r
122 * To fully test the low power tick processing it is necessary to sometimes
\r
123 * bring the MCU out of its sleep state by a method other than the tick
\r
124 * interrupt. Interrupts generated from a basic timer are used for this
\r
127 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
128 static void prvSetupBasicTimer( void );
\r
131 /*-----------------------------------------------------------*/
\r
133 /* The reload value to use in the timer to generate the tick interrupt -
\r
134 assumes the timer is running at its higher resolution. */
\r
135 static const uint32_t ulHighResolutionReloadValue = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );
\r
137 /* Calculate how many clock increments make up a single tick period. */
\r
138 static const uint32_t ulReloadValueForOneHighResolutionTick = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );
\r
140 /* Calculate the maximum number of ticks that can be suppressed when using the
\r
141 high resolution clock and low resolution clock respectively. */
\r
142 static uint32_t ulMaximumPossibleSuppressedHighResolutionTicks = 0;
\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 lpGIRQ23_ENABLE_SET ( * ( uint32_t * ) 0x4000C130 )
\r
175 #define tmrGIRQ23_BIT_TIMER0 ( 1UL << 0UL )
\r
177 static void prvSetupBasicTimer( void )
\r
179 const uint8_t ucTimerChannel = 0;
\r
180 const uint32_t ulTimer0Count = configCPU_CLOCK_HZ / 10;
\r
182 lpGIRQ23_ENABLE_SET = tmrGIRQ23_BIT_TIMER0;
\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 NVIC_SetPriority( TIMER0_IRQn, ucTimerChannel );
\r
191 NVIC_ClearPendingIRQ( TIMER0_IRQn );
\r
192 NVIC_EnableIRQ( TIMER0_IRQn );
\r
193 btimer_start( ucTimerChannel );
\r
196 #endif /* lpINCLUDE_TEST_TIMER */
\r
197 /*-----------------------------------------------------------*/
\r
199 void vPortSetupTimerInterrupt( void )
\r
201 ulMaximumPossibleSuppressedHighResolutionTicks = ( ( uint32_t ) USHRT_MAX ) / ulReloadValueForOneHighResolutionTick;
\r
203 /* Set up the hibernation timer to start at the value required by the
\r
205 lpHTIMER_PRELOAD_REGISTER = ulHighResolutionReloadValue;
\r
206 lpHTIMER_CONTROL_REGISTER = mainHTIMER_HIGH_RESOLUTION;
\r
208 /* Enable the HTIMER interrupt. Equivalent to enable_htimer0_irq(); */
\r
209 lpEC_GIRQ17_ENABLE_SET |= lpHTIMER_INTERRUPT_CONTROL_BIT;
\r
211 /* The hibernation timer is not an auto-reload timer, so gets reset
\r
212 from within the ISR itself. For that reason it's interrupt is set
\r
213 to the highest possible priority to ensure clock slippage is minimised. */
\r
214 NVIC_SetPriority( HTIMER_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
215 NVIC_ClearPendingIRQ( HTIMER_IRQn );
\r
216 NVIC_EnableIRQ( HTIMER_IRQn );
\r
218 /* A basic timer is also started, purely for test purposes. Its only
\r
219 purpose is to bring the CPU out of its sleep mode by an interrupt other
\r
220 than the tick interrupt in order to get more code test coverage. */
\r
221 #if( lpINCLUDE_TEST_TIMER == 1 )
\r
223 prvSetupBasicTimer();
\r
227 /*-----------------------------------------------------------*/
\r
229 /* Override the default definition of vPortSuppressTicksAndSleep() that is
\r
230 weakly defined in the FreeRTOS Cortex-M port layer with a version that manages
\r
231 the hibernation timer, as the tick is generated from the low power hibernation
\r
232 timer and not the SysTick as would normally be the case on a Cortex-M. */
\r
233 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
235 uint32_t ulCompleteTickPeriods, ulReloadValue, ulCompletedTimerDecrements, ulCountAfterSleep, ulCountBeforeSleep;
\r
236 eSleepModeStatus eSleepAction;
\r
237 TickType_t xModifiableIdleTime;
\r
239 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
241 /* Make sure the hibernation timer reload value does not overflow the
\r
243 if( xExpectedIdleTime > ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks )
\r
245 xExpectedIdleTime = ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks;
\r
248 /* Stop the timer momentarily. The time the timer is stopped for is
\r
249 accounted for as best it can be, but using the tickless mode will
\r
250 inevitably result in some tiny drift of the time maintained by the kernel
\r
251 with respect to calendar time. Take the count value first as clearing
\r
252 the preload value also seems to clear the count. */
\r
253 ulCountBeforeSleep = ( uint32_t ) lpHTIMER_COUNT_REGISTER;
\r
254 lpHTIMER_PRELOAD_REGISTER = 0;
\r
256 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
257 periods. -1 is used as the current time slice will already be part way
\r
258 through, the part value coming from the current timer count value. */
\r
259 ulReloadValue = ulCountBeforeSleep + ( ulReloadValueForOneHighResolutionTick * ( xExpectedIdleTime - 1UL ) );
\r
261 if( ulReloadValue > ulStoppedTimerCompensation )
\r
263 /* Compensate for the fact that the timer is going to be stopped
\r
265 ulReloadValue -= ulStoppedTimerCompensation;
\r
268 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
269 that will mask interrupts that should exit sleep mode. */
\r
270 __asm volatile( "cpsid i" );
\r
271 __asm volatile( "dsb" );
\r
272 __asm volatile( "isb" );
\r
274 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
275 mode is exited then sleep mode was probably exited because the tick was
\r
276 suppressed for the entire xExpectedIdleTime period. */
\r
277 ulTickFlag = pdFALSE;
\r
279 /* If a context switch is pending then abandon the low power entry as
\r
280 the context switch might have been pended by an external interrupt that
\r
281 requires processing. */
\r
282 eSleepAction = eTaskConfirmSleepModeStatus();
\r
283 if( eSleepAction == eAbortSleep )
\r
285 /* Restart the timer from whatever remains in the counter register,
\r
286 but 0 is not a valid value. */
\r
287 ulReloadValue = ulCountBeforeSleep - ulStoppedTimerCompensation;
\r
289 if( ulReloadValue == 0 )
\r
291 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
292 ulCompleteTickPeriods = 1UL;
\r
296 ulCompleteTickPeriods = 0UL;
\r
299 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
301 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
303 __asm volatile( "cpsie i" );
\r
304 __asm volatile( "dsb" );
\r
305 __asm volatile( "isb" );
\r
309 /* Write the calculated reload value, which will also start the
\r
311 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
313 /* Allow the application to define some pre-sleep processing. */
\r
314 xModifiableIdleTime = xExpectedIdleTime;
\r
315 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
317 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
318 means the application defined code has already executed the sleep
\r
320 if( xModifiableIdleTime > 0 )
\r
322 __asm volatile( "dsb" );
\r
323 __asm volatile( "wfi" );
\r
324 __asm volatile( "isb" );
\r
327 /* Allow the application to define some post sleep processing. */
\r
328 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
330 /* Stop the hibernation timer. Again, the time the tiemr is stopped
\r
331 for is accounted for as best it can be, but using the tickless mode
\r
332 will inevitably result in some tiny drift of the time maintained by the
\r
333 kernel with respect to calendar time. Take the count value first as
\r
334 setting the preload to zero also seems to clear the count. */
\r
335 ulCountAfterSleep = lpHTIMER_COUNT_REGISTER;
\r
336 lpHTIMER_PRELOAD_REGISTER = 0;
\r
338 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
340 __asm volatile( "cpsie i" );
\r
341 __asm volatile( "dsb" );
\r
342 __asm volatile( "isb" );
\r
344 if( ulTickFlag != pdFALSE )
\r
346 /* The tick interrupt has already executed, although because this
\r
347 function is called with the scheduler suspended the actual tick
\r
348 processing will not occur until after this function has exited.
\r
349 The timer has already been reloaded to count in ticks, and can just
\r
350 continue counting down from its current value. */
\r
351 ulReloadValue = ulCountAfterSleep;
\r
353 /* Sanity check that the timer's reload value has indeed been
\r
355 configASSERT( ( uint32_t ) lpHTIMER_PRELOAD_REGISTER == ulReloadValueForOneHighResolutionTick );
\r
357 /* The tick interrupt handler will already have pended the tick
\r
358 processing in the kernel. As the pending tick will be processed as
\r
359 soon as this function exits, the tick value maintained by the tick
\r
360 is stepped forward by one less than the time spent sleeping. The
\r
361 actual stepping of the tick appears later in this function. */
\r
362 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
366 /* Something other than the tick interrupt ended the sleep. How
\r
367 many complete tick periods passed while the processor was
\r
369 ulCompletedTimerDecrements = ulReloadValue - ulCountAfterSleep;
\r
371 /* Undo the adjustment that was made to the reload value to account
\r
372 for the fact that a time slice was part way through when this
\r
373 function was called before working out how many complete tick
\r
374 periods this represents. (could have used [ulExpectedIdleTime *
\r
375 ulReloadValueForOneHighResolutionTick] instead of ulReloadValue on
\r
376 the previous line, but this way avoids the multiplication). */
\r
377 ulCompletedTimerDecrements += ( ulReloadValueForOneHighResolutionTick - ulCountBeforeSleep );
\r
378 ulCompleteTickPeriods = ulCompletedTimerDecrements / ulReloadValueForOneHighResolutionTick;
\r
380 /* The reload value is set to whatever fraction of a single tick
\r
382 ulReloadValue = ( ( ulCompleteTickPeriods + 1UL ) * ulReloadValueForOneHighResolutionTick ) - ulCompletedTimerDecrements;
\r
385 /* Cannot use a reload value of 0 - it will not start the timer. */
\r
386 if( ulReloadValue == 0 )
\r
388 /* There is no fraction remaining. */
\r
389 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
390 ulCompleteTickPeriods++;
\r
393 /* Restart the timer so it runs down from the reload value. The reload
\r
394 value will get set to the value required to generate exactly one tick
\r
395 period the next time the tick interrupt executes. */
\r
396 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
399 /* Wind the tick forward by the number of tick periods that the CPU
\r
400 remained in a low power state. */
\r
401 vTaskStepTick( ulCompleteTickPeriods );
\r
403 /*-----------------------------------------------------------*/
\r
405 void NVIC_Handler_TMR0( void )
\r
407 /* This timer is used for test purposes. Its only function is to
\r
408 generate interrupts while the MCU is sleeping, so the MCU is sometimes
\r
409 brought out of sleep by a means other than the tick interrupt. */
\r
411 /*-----------------------------------------------------------*/
\r
414 #endif /* configCREATE_LOW_POWER_DEMO */
\r