2 FreeRTOS V9.0.0 - 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
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 ( 32787 ) /* (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 /* Some registers are accessed directly as the library is not compatible with
\r
98 all the compilers used. */
\r
99 #define lpHTIMER_PRELOAD_REGISTER ( * ( volatile uint16_t * ) 0x40009800 )
\r
100 #define lpHTIMER_COUNT_REGISTER ( * ( volatile uint16_t * ) 0x40009808 )
\r
101 #define lpEC_GIRQ17_ENABLE_SET ( * ( volatile uint32_t * ) 0x4000C0B8 )
\r
102 #define lpHTIMER_INTERRUPT_CONTROL_BIT ( 1UL << 20UL )
\r
105 * The low power demo does not use the SysTick, so override the
\r
106 * vPortSetupTickInterrupt() function with an implementation that configures
\r
107 * the low power clock. NOTE: This function name must not be changed as it
\r
108 * is called from the RTOS portable layer.
\r
110 void vPortSetupTimerInterrupt( void );
\r
112 /*-----------------------------------------------------------*/
\r
114 /* The reload value to use in the timer to generate the tick interrupt -
\r
115 assumes the timer is running at its higher resolution. */
\r
116 static const uint16_t usHighResolutionReloadValue = ( mainHIGHER_RESOLUTION_TIMER_HZ / ( uint16_t ) configTICK_RATE_HZ );
\r
118 /* Calculate how many clock increments make up a single tick period. */
\r
119 static const uint32_t ulReloadValueForOneHighResolutionTick = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );
\r
121 /* Calculate the maximum number of ticks that can be suppressed when using the
\r
122 high resolution clock and low resolution clock respectively. */
\r
123 static uint32_t ulMaximumPossibleSuppressedHighResolutionTicks = 0;
\r
125 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so
\r
126 use zero - but leave the value here to assist porting to different clock
\r
128 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
130 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
131 sleep mode was exited because of an timer interrupt or a different interrupt. */
\r
132 static volatile uint32_t ulTickFlag = pdFALSE;
\r
134 /*-----------------------------------------------------------*/
\r
136 void NVIC_Handler_HIB_TMR( void ) iv IVT_INT_HTIMER ics ICS_AUTO
\r
138 lpHTIMER_PRELOAD_REGISTER = usHighResolutionReloadValue;
\r
140 /* Increment the RTOS tick. */
\r
141 if( xTaskIncrementTick() != pdFALSE )
\r
143 /* A context switch is required. Context switching is performed in
\r
144 the PendSV interrupt. Pend the PendSV interrupt. */
\r
145 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
148 /* The CPU woke because of a tick. */
\r
149 ulTickFlag = pdTRUE;
\r
151 /*-----------------------------------------------------------*/
\r
153 void vPortSetupTimerInterrupt( void )
\r
155 /* Cannot be a const when using the MikroC compiler. */
\r
156 ulMaximumPossibleSuppressedHighResolutionTicks = ( ( uint32_t ) USHRT_MAX ) / ulReloadValueForOneHighResolutionTick;
\r
158 /* Set up the hibernation timer to start at the value required by the
\r
160 htimer_enable( mainTICK_HTIMER_ID, usHighResolutionReloadValue, mainHTIMER_HIGH_RESOLUTION );
\r
162 /* Enable the HTIMER interrupt. Equivalent to enable_htimer0_irq(); */
\r
163 lpEC_GIRQ17_ENABLE_SET |= lpHTIMER_INTERRUPT_CONTROL_BIT;
\r
165 /* The hibernation timer is not an auto-reload timer, so gets reset
\r
166 from within the ISR itself. For that reason it's interrupt is set
\r
167 to the highest possible priority to ensure clock slippage is minimised. */
\r
168 NVIC_SetIntPriority( IVT_INT_HTIMER, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
169 NVIC_IntEnable( IVT_INT_HTIMER );
\r
171 /*-----------------------------------------------------------*/
\r
173 /* Override the default definition of vPortSuppressTicksAndSleep() that is
\r
174 weakly defined in the FreeRTOS Cortex-M port layer with a version that manages
\r
175 the hibernation timer, as the tick is generated from the low power hibernation
\r
176 timer and not the SysTick as would normally be the case on a Cortex-M. */
\r
177 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
179 uint32_t ulCompleteTickPeriods, ulReloadValue, ulCompletedTimerDecrements, ulCountAfterSleep, ulCountBeforeSleep;
\r
180 eSleepModeStatus eSleepAction;
\r
181 TickType_t xModifiableIdleTime;
\r
183 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
185 /* Make sure the hibernation timer reload value does not overflow the
\r
187 if( xExpectedIdleTime > ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks )
\r
189 xExpectedIdleTime = ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks;
\r
192 /* Stop the timer momentarily. The time the timer is stopped for is
\r
193 accounted for as best it can be, but using the tickless mode will
\r
194 inevitably result in some tiny drift of the time maintained by the kernel
\r
195 with respect to calendar time. Take the count value first as clearing
\r
196 the preload value also seems to clear the count. */
\r
197 ulCountBeforeSleep = ( uint32_t ) lpHTIMER_COUNT_REGISTER;
\r
198 lpHTIMER_PRELOAD_REGISTER = 0;
\r
200 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
201 periods. -1 is used as the current time slice will already be part way
\r
202 through, the part value coming from the current timer count value. */
\r
203 ulReloadValue = ulCountBeforeSleep + ( ulReloadValueForOneHighResolutionTick * ( xExpectedIdleTime - 1UL ) );
\r
205 if( ulReloadValue > ulStoppedTimerCompensation )
\r
207 /* Compensate for the fact that the timer is going to be stopped
\r
209 ulReloadValue -= ulStoppedTimerCompensation;
\r
212 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
213 that will mask interrupts that should exit sleep mode. */
\r
218 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
219 mode is exited then sleep mode was probably exited because the tick was
\r
220 suppressed for the entire xExpectedIdleTime period. */
\r
221 ulTickFlag = pdFALSE;
\r
223 /* If a context switch is pending then abandon the low power entry as
\r
224 the context switch might have been pended by an external interrupt that
\r
225 requires processing. */
\r
226 eSleepAction = eTaskConfirmSleepModeStatus();
\r
227 if( eSleepAction == eAbortSleep )
\r
229 /* Resetart the timer from whatever remains in the counter register,
\r
230 but 0 is not a valid value. */
\r
231 ulReloadValue = ulCountBeforeSleep - ulStoppedTimerCompensation;
\r
233 if( ulReloadValue == 0 )
\r
235 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
236 ulCompleteTickPeriods = 1UL;
\r
240 ulCompleteTickPeriods = 0UL;
\r
243 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
245 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
254 /* Write the calculated reload value, which will also start the
\r
256 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
258 /* Allow the application to define some pre-sleep processing. */
\r
259 xModifiableIdleTime = xExpectedIdleTime;
\r
260 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
262 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
263 means the application defined code has already executed the sleep
\r
265 if( xModifiableIdleTime > 0 )
\r
272 /* Allow the application to define some post sleep processing. */
\r
273 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
275 /* Stop the hibernation timer. Again, the time the tiemr is stopped
\r
276 for is accounted for as best it can be, but using the tickless mode
\r
277 will inevitably result in some tiny drift of the time maintained by the
\r
278 kernel with respect to calendar time. Take the count value first as
\r
279 setting the preload to zero also seems to clear the count. */
\r
280 ulCountAfterSleep = ( uint32_t ) lpHTIMER_COUNT_REGISTER;
\r
281 lpHTIMER_PRELOAD_REGISTER = 0;
\r
283 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
290 if( ulTickFlag != pdFALSE )
\r
292 /* The tick interrupt has already executed, although because this
\r
293 function is called with the scheduler suspended the actual tick
\r
294 processing will not occur until after this function has exited.
\r
295 The timer has already been reloaded to count in ticks, and can just
\r
296 continue counting down from its current value. */
\r
297 ulReloadValue = ulCountAfterSleep;
\r
299 /* Sanity check that the timer's reload value has indeed been
\r
301 configASSERT( ( uint32_t ) lpHTIMER_PRELOAD_REGISTER == ulReloadValueForOneHighResolutionTick );
\r
303 /* The tick interrupt handler will already have pended the tick
\r
304 processing in the kernel. As the pending tick will be processed as
\r
305 soon as this function exits, the tick value maintained by the tick
\r
306 is stepped forward by one less than the time spent sleeping. The
\r
307 actual stepping of the tick appears later in this function. */
\r
308 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
312 /* Something other than the tick interrupt ended the sleep. How
\r
313 many complete tick periods passed while the processor was
\r
315 ulCompletedTimerDecrements = ulReloadValue - ulCountAfterSleep;
\r
317 /* Undo the adjustment that was made to the reload value to account
\r
318 for the fact that a time slice was part way through when this
\r
319 function was called before working out how many complete tick
\r
320 periods this represents. (could have used [ulExpectedIdleTime *
\r
321 ulReloadValueForOneHighResolutionTick] instead of ulReloadValue on
\r
322 the previous line, but this way avoids the multiplication). */
\r
323 ulCompletedTimerDecrements += ( ulReloadValueForOneHighResolutionTick - ulCountBeforeSleep );
\r
324 ulCompleteTickPeriods = ulCompletedTimerDecrements / ulReloadValueForOneHighResolutionTick;
\r
326 /* The reload value is set to whatever fraction of a single tick
\r
328 ulReloadValue = ( ( ulCompleteTickPeriods + 1UL ) * ulReloadValueForOneHighResolutionTick ) - ulCompletedTimerDecrements;
\r
331 /* Cannot use a reload value of 0 - it will not start the timer. */
\r
332 if( ulReloadValue == 0 )
\r
334 /* There is no fraction remaining. */
\r
335 ulReloadValue = ulReloadValueForOneHighResolutionTick;
\r
336 ulCompleteTickPeriods++;
\r
339 /* Restart the timer so it runs down from the reload value. The reload
\r
340 value will get set to the value required to generate exactly one tick
\r
341 period the next time the tick interrupt executes. */
\r
342 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;
\r
345 /* Wind the tick forward by the number of tick periods that the CPU
\r
346 remained in a low power state. */
\r
347 vTaskStepTick( ulCompleteTickPeriods );
\r
349 /*-----------------------------------------------------------*/
\r
352 #endif /* configCREATE_LOW_POWER_DEMO */
\r