2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* Standard includes. */
\r
69 /* FreeRTOS includes. */
\r
70 #include "FreeRTOS.h"
\r
73 /* ST library functions. */
\r
74 #include "stm32l1xx.h"
\r
77 * When configCREATE_LOW_POWER_DEMO is set to 1 then the tick interrupt
\r
78 * is generated by the TIM2 peripheral. The TIM2 configuration and handling
\r
79 * functions are defined in this file.
\r
81 * When configCREATE_LOW_POWER_DEMO is set to 0 the tick interrupt is
\r
82 * generated by the standard FreeRTOS Cortex-M port layer, which uses the
\r
85 #if configCREATE_LOW_POWER_DEMO == 1
\r
87 /* The frequency at which TIM2 will run. */
\r
88 #define lpCLOCK_INPUT_FREQUENCY ( 1000UL )
\r
90 /* STM32 register used to ensure the TIM2 clock stops when the MCU is in debug
\r
92 #define DBGMCU_APB1_FZ ( * ( ( volatile unsigned long * ) 0xE0042008 ) )
\r
94 /*-----------------------------------------------------------*/
\r
97 * The tick interrupt is generated by the TIM2 timer.
\r
99 void TIM2_IRQHandler( void );
\r
101 /*-----------------------------------------------------------*/
\r
103 /* Calculate how many clock increments make up a single tick period. */
\r
104 static const uint32_t ulReloadValueForOneTick = ( ( lpCLOCK_INPUT_FREQUENCY / configTICK_RATE_HZ ) - 1 );
\r
106 /* Holds the maximum number of ticks that can be suppressed - which is
\r
107 basically how far into the future an interrupt can be generated. Set during
\r
109 static portTickType xMaximumPossibleSuppressedTicks = 0;
\r
111 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
112 sleep mode was exited because of an tick interrupt or a different interrupt. */
\r
113 static volatile uint32_t ulTickFlag = pdFALSE;
\r
115 /*-----------------------------------------------------------*/
\r
117 /* The tick interrupt handler. This is always the same other than the part that
\r
118 clears the interrupt, which is specific to the clock being used to generate the
\r
120 void TIM2_IRQHandler( void )
\r
122 /* Clear the interrupt. */
\r
123 TIM_ClearITPendingBit( TIM2, TIM_IT_Update );
\r
125 /* The next block of code is from the standard FreeRTOS tick interrupt
\r
126 handler. The standard handler is not called directly in case future
\r
127 versions contain changes that make it no longer suitable for calling
\r
129 ( void ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
131 if( xTaskIncrementTick() != pdFALSE )
\r
133 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
136 /* Just completely clear the interrupt mask on exit by passing 0 because
\r
137 it is known that this interrupt will only ever execute with the lowest
\r
138 possible interrupt priority. */
\r
140 portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
\r
142 /* In case this is the first tick since the MCU left a low power mode the
\r
143 reload value is reset to its default. */
\r
144 TIM2->ARR = ( uint16_t ) ulReloadValueForOneTick;
\r
146 /* The CPU woke because of a tick. */
\r
147 ulTickFlag = pdTRUE;
\r
149 /*-----------------------------------------------------------*/
\r
151 /* Override the default definition of vPortSetupTimerInterrupt() that is weakly
\r
152 defined in the FreeRTOS Cortex-M3 port layer with a version that configures TIM2
\r
153 to generate the tick interrupt. */
\r
154 void vPortSetupTimerInterrupt( void )
\r
156 NVIC_InitTypeDef NVIC_InitStructure;
\r
157 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
\r
159 /* Enable the TIM2 clock. */
\r
160 RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );
\r
162 /* Ensure clock stops in debug mode. */
\r
163 DBGMCU_APB1_FZ |= DBGMCU_APB1_FZ_DBG_TIM2_STOP;
\r
165 /* Scale the clock so longer tickless periods can be achieved. The SysTick
\r
166 is not used as even when its frequency is divided by 8 the maximum tickless
\r
167 period with a system clock of 16MHz is only 8.3 seconds. Using a prescaled
\r
168 clock on the 16-bit TIM2 allows a tickless period of nearly 66 seconds,
\r
169 albeit at low resolution. */
\r
170 TIM_TimeBaseStructure.TIM_Prescaler = ( uint16_t ) ( SystemCoreClock / lpCLOCK_INPUT_FREQUENCY );
\r
171 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
\r
172 TIM_TimeBaseStructure.TIM_Period = ( uint16_t ) ( lpCLOCK_INPUT_FREQUENCY / configTICK_RATE_HZ );
\r
173 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
\r
174 TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure );
\r
176 /* Enable the TIM2 interrupt. This must execute at the lowest interrupt
\r
178 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
\r
179 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_LOWEST_INTERRUPT_PRIORITY; /* Must be set to lowest priority. */
\r
180 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
\r
181 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
\r
182 NVIC_Init(&NVIC_InitStructure);
\r
183 TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE );
\r
184 TIM_SetCounter( TIM2, 0 );
\r
185 TIM_Cmd( TIM2, ENABLE );
\r
187 /* See the comments where xMaximumPossibleSuppressedTicks is declared. */
\r
188 xMaximumPossibleSuppressedTicks = ( ( unsigned long ) USHRT_MAX ) / ulReloadValueForOneTick;
\r
190 /*-----------------------------------------------------------*/
\r
192 /* Override the default definition of vPortSuppressTicksAndSleep() that is
\r
193 weakly defined in the FreeRTOS Cortex-M3 port layer with a version that manages
\r
194 the TIM2 interrupt, as the tick is generated from TIM2 compare matches events. */
\r
195 void vPortSuppressTicksAndSleep( portTickType xExpectedIdleTime )
\r
197 uint32_t ulCounterValue, ulCompleteTickPeriods;
\r
198 eSleepModeStatus eSleepAction;
\r
199 portTickType xModifiableIdleTime;
\r
200 const portTickType xRegulatorOffIdleTime = 30;
\r
202 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
204 /* Make sure the TIM2 reload value does not overflow the counter. */
\r
205 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
207 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
210 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
212 ulCounterValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
214 /* Stop TIM2 momentarily. The time TIM2 is stopped for is not accounted for
\r
215 in this implementation (as it is in the generic implementation) because the
\r
216 clock is so slow it is unlikely to be stopped for a complete count period
\r
218 TIM_Cmd( TIM2, DISABLE );
\r
220 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
221 that will mask interrupts that should exit sleep mode. */
\r
222 __asm volatile ( "cpsid i" );
\r
223 __asm volatile ( "dsb" );
\r
224 __asm volatile ( "isb" );
\r
226 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
227 mode is exited then sleep mode was probably exited because the tick was
\r
228 suppressed for the entire xExpectedIdleTime period. */
\r
229 ulTickFlag = pdFALSE;
\r
231 /* If a context switch is pending then abandon the low power entry as
\r
232 the context switch might have been pended by an external interrupt that
\r
233 requires processing. */
\r
234 eSleepAction = eTaskConfirmSleepModeStatus();
\r
235 if( eSleepAction == eAbortSleep )
\r
237 /* Restart tick. */
\r
238 TIM_Cmd( TIM2, ENABLE );
\r
240 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
242 __asm volatile ( "cpsie i" );
\r
244 else if( eSleepAction == eNoTasksWaitingTimeout )
\r
246 /* A user definable macro that allows application code to be inserted
\r
247 here. Such application code can be used to minimise power consumption
\r
248 further by turning off IO, peripheral clocks, the Flash, etc. */
\r
249 configPRE_STOP_PROCESSING();
\r
251 /* There are no running state tasks and no tasks that are blocked with a
\r
252 time out. Assuming the application does not care if the tick time slips
\r
253 with respect to calendar time then enter a deep sleep that can only be
\r
254 woken by (in this demo case) the user button being pushed on the
\r
255 STM32L discovery board. If the application does require the tick time
\r
256 to keep better track of the calender time then the RTC peripheral can be
\r
257 used to make rough adjustments. */
\r
258 PWR_EnterSTOPMode( PWR_Regulator_LowPower, PWR_SLEEPEntry_WFI );
\r
260 /* A user definable macro that allows application code to be inserted
\r
261 here. Such application code can be used to reverse any actions taken
\r
262 by the configPRE_STOP_PROCESSING(). In this demo
\r
263 configPOST_STOP_PROCESSING() is used to re-initialise the clocks that
\r
264 were turned off when STOP mode was entered. */
\r
265 configPOST_STOP_PROCESSING();
\r
267 /* Restart tick. */
\r
268 TIM_SetCounter( TIM2, 0 );
\r
269 TIM_Cmd( TIM2, ENABLE );
\r
271 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
273 __asm volatile ( "cpsie i" );
\r
277 /* Trap underflow before the next calculation. */
\r
278 configASSERT( ulCounterValue >= TIM_GetCounter( TIM2 ) );
\r
280 /* Adjust the TIM2 value to take into account that the current time
\r
281 slice is already partially complete. */
\r
282 ulCounterValue -= ( uint32_t ) TIM_GetCounter( TIM2 );
\r
284 /* Trap overflow/underflow before the calculated value is written to
\r
286 configASSERT( ulCounterValue < ( uint32_t ) USHRT_MAX );
\r
287 configASSERT( ulCounterValue != 0 );
\r
289 /* Update to use the calculated overflow value. */
\r
290 TIM_SetAutoreload( TIM2, ( uint16_t ) ulCounterValue );
\r
291 TIM_SetCounter( TIM2, 0 );
\r
293 /* Restart the TIM2. */
\r
294 TIM_Cmd( TIM2, ENABLE );
\r
296 /* Allow the application to define some pre-sleep processing. This is
\r
297 the standard configPRE_SLEEP_PROCESSING() macro as described on the
\r
298 FreeRTOS.org website. */
\r
299 xModifiableIdleTime = xExpectedIdleTime;
\r
300 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
302 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
303 means the application defined code has already executed the wait/sleep
\r
305 if( xModifiableIdleTime > 0 )
\r
307 /* The sleep mode used is dependent on the expected idle time
\r
308 as the deeper the sleep the longer the wake up time. See the
\r
309 comments at the top of main_low_power.c. Note xRegulatorOffIdleTime
\r
310 is set purely for convenience of demonstration and is not intended
\r
311 to be an optimised value. */
\r
312 if( xModifiableIdleTime > xRegulatorOffIdleTime )
\r
314 /* A slightly lower power sleep mode with a longer wake up
\r
316 PWR_EnterSleepMode( PWR_Regulator_LowPower, PWR_SLEEPEntry_WFI );
\r
320 /* A slightly higher power sleep mode with a faster wake up
\r
322 PWR_EnterSleepMode( PWR_Regulator_ON, PWR_SLEEPEntry_WFI );
\r
326 /* Allow the application to define some post sleep processing. This is
\r
327 the standard configPOST_SLEEP_PROCESSING() macro, as described on the
\r
328 FreeRTOS.org website. */
\r
329 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
331 /* Stop TIM2. Again, the time the clock is stopped for in not accounted
\r
332 for here (as it would normally be) because the clock is so slow it is
\r
333 unlikely it will be stopped for a complete count period anyway. */
\r
334 TIM_Cmd( TIM2, DISABLE );
\r
336 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
338 __asm volatile ( "cpsie i" );
\r
339 __asm volatile ( "dsb" );
\r
340 __asm volatile ( "isb" );
\r
342 if( ulTickFlag != pdFALSE )
\r
344 /* Trap overflows before the next calculation. */
\r
345 configASSERT( ulReloadValueForOneTick >= ( uint32_t ) TIM_GetCounter( TIM2 ) );
\r
347 /* The tick interrupt has already executed, although because this
\r
348 function is called with the scheduler suspended the actual tick
\r
349 processing will not occur until after this function has exited.
\r
350 Reset the reload value with whatever remains of this tick period. */
\r
351 ulCounterValue = ulReloadValueForOneTick - ( uint32_t ) TIM_GetCounter( TIM2 );
\r
353 /* Trap under/overflows before the calculated value is used. */
\r
354 configASSERT( ulCounterValue <= ( uint32_t ) USHRT_MAX );
\r
355 configASSERT( ulCounterValue != 0 );
\r
357 /* Use the calculated reload value. */
\r
358 TIM_SetAutoreload( TIM2, ( uint16_t ) ulCounterValue );
\r
359 TIM_SetCounter( TIM2, 0 );
\r
361 /* The tick interrupt handler will already have pended the tick
\r
362 processing in the kernel. As the pending tick will be processed as
\r
363 soon as this function exits, the tick value maintained by the tick
\r
364 is stepped forward by one less than the time spent sleeping. The
\r
365 actual stepping of the tick appears later in this function. */
\r
366 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
370 /* Something other than the tick interrupt ended the sleep. How
\r
371 many complete tick periods passed while the processor was
\r
373 ulCompleteTickPeriods = ( ( uint32_t ) TIM_GetCounter( TIM2 ) ) / ulReloadValueForOneTick;
\r
375 /* Check for over/under flows before the following calculation. */
\r
376 configASSERT( ( ( uint32_t ) TIM_GetCounter( TIM2 ) ) >= ( ulCompleteTickPeriods * ulReloadValueForOneTick ) );
\r
378 /* The reload value is set to whatever fraction of a single tick
\r
380 ulCounterValue = ( ( uint32_t ) TIM_GetCounter( TIM2 ) ) - ( ulCompleteTickPeriods * ulReloadValueForOneTick );
\r
381 configASSERT( ulCounterValue <= ( uint32_t ) USHRT_MAX );
\r
382 if( ulCounterValue == 0 )
\r
384 /* There is no fraction remaining. */
\r
385 ulCounterValue = ulReloadValueForOneTick;
\r
386 ulCompleteTickPeriods++;
\r
388 TIM_SetAutoreload( TIM2, ( uint16_t ) ulCounterValue );
\r
389 TIM_SetCounter( TIM2, 0 );
\r
392 /* Restart TIM2 so it runs up to the reload value. The reload value
\r
393 will get set to the value required to generate exactly one tick period
\r
394 the next time the TIM2 interrupt executes. */
\r
395 TIM_Cmd( TIM2, ENABLE );
\r
397 /* Wind the tick forward by the number of tick periods that the CPU
\r
398 remained in a low power state. */
\r
399 vTaskStepTick( ulCompleteTickPeriods );
\r
403 #endif /* configCREATE_LOW_POWER_DEMO == 1 */
\r