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 /* FreeRTOS includes. */
\r
71 #include "FreeRTOS.h"
\r
74 /* SiLabs library includes. */
\r
81 /* SEE THE COMMENTS ABOVE THE DEFINITION OF configCREATE_LOW_POWER_DEMO IN
\r
83 This file contains functions that will override the default implementations
\r
84 in the RTOS port layer. Therefore only build this file if the low power demo
\r
86 #if( configCREATE_LOW_POWER_DEMO == 2 )
\r
88 #define mainTIMER_FREQUENCY_HZ ( 4096UL ) /* 32768 clock divided by 8. */
\r
91 * The low power demo does not use the SysTick, so override the
\r
92 * vPortSetupTickInterrupt() function with an implementation that configures
\r
93 * a low power clock source. NOTE: This function name must not be changed as
\r
94 * it is called from the RTOS portable layer.
\r
96 void vPortSetupTimerInterrupt( void );
\r
99 * Override the default definition of vPortSuppressTicksAndSleep() that is
\r
100 * weakly defined in the FreeRTOS Cortex-M port layer with a version that
\r
101 * manages the RTC clock, as the tick is generated from the low power RTC
\r
102 * and not the SysTick as would normally be the case on a Cortex-M.
\r
104 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
\r
106 /*-----------------------------------------------------------*/
\r
108 /* Calculate how many clock increments make up a single tick period. */
\r
109 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );
\r
111 /* Will hold the maximum number of ticks that can be suppressed. */
\r
112 static uint32_t xMaximumPossibleSuppressedTicks = 0;
\r
114 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
115 sleep mode was exited because of a timer interrupt or a different interrupt. */
\r
116 static volatile uint32_t ulTickFlag = pdFALSE;
\r
118 /* As the clock is only 32KHz, it is likely a value of 1 will be enough. */
\r
119 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
121 /*-----------------------------------------------------------*/
\r
123 void vPortSetupTimerInterrupt( void )
\r
125 RTC_Init_TypeDef xRTCInitStruct;
\r
126 const uint32_t ulMAX24BitValue = 0xffffffUL;
\r
128 xMaximumPossibleSuppressedTicks = ulMAX24BitValue / ulReloadValueForOneTick;
\r
130 /* Configure the RTC to generate the RTOS tick interrupt. */
\r
132 /* LXFO setup. For rev D use 70% boost */
\r
133 CMU->CTRL = ( CMU->CTRL & ~_CMU_CTRL_LFXOBOOST_MASK ) | CMU_CTRL_LFXOBOOST_70PCENT;
\r
134 #if defined( EMU_AUXCTRL_REDLFXOBOOST )
\r
135 EMU->AUXCTRL = (EMU->AUXCTRL & ~_EMU_AUXCTRL_REDLFXOBOOST_MASK) | EMU_AUXCTRL_REDLFXOBOOST;
\r
138 /* Ensure LE modules are accessible. */
\r
139 CMU_ClockEnable( cmuClock_CORELE, true );
\r
142 CMU_ClockSelectSet( cmuClock_LFA, cmuSelect_LFXO );
\r
144 /* Use 8x divider to reduce energy. */
\r
145 CMU_ClockDivSet( cmuClock_RTC, cmuClkDiv_8 );
\r
147 /* Enable clock to the RTC module. */
\r
148 CMU_ClockEnable( cmuClock_RTC, true );
\r
149 xRTCInitStruct.enable = false;
\r
150 xRTCInitStruct.debugRun = false;
\r
151 xRTCInitStruct.comp0Top = true;
\r
152 RTC_Init( &xRTCInitStruct );
\r
154 /* Disable RTC0 interrupt. */
\r
155 RTC_IntDisable( RTC_IFC_COMP0 );
\r
157 /* The tick interrupt must be set to the lowest priority possible. */
\r
158 NVIC_SetPriority( RTC_IRQn, configKERNEL_INTERRUPT_PRIORITY );
\r
159 NVIC_ClearPendingIRQ( RTC_IRQn );
\r
160 NVIC_EnableIRQ( RTC_IRQn );
\r
161 RTC_CompareSet( 0, ulReloadValueForOneTick );
\r
162 RTC_IntClear( RTC_IFC_COMP0 );
\r
163 RTC_IntEnable( RTC_IF_COMP0 );
\r
164 RTC_Enable( true );
\r
166 /*-----------------------------------------------------------*/
\r
168 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
170 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCurrentCount;
\r
171 eSleepModeStatus eSleepAction;
\r
172 TickType_t xModifiableIdleTime;
\r
174 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
176 /* Make sure the RTC reload value does not overflow the counter. */
\r
177 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
179 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
182 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
184 ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
185 if( ulReloadValue > ulStoppedTimerCompensation )
\r
187 /* Compensate for the fact that the RTC is going to be stopped
\r
189 ulReloadValue -= ulStoppedTimerCompensation;
\r
192 /* Stop the RTC momentarily. The time the RTC is stopped for is accounted
\r
193 for as best it can be, but using the tickless mode will inevitably result
\r
194 in some tiny drift of the time maintained by the kernel with respect to
\r
195 calendar time. The count is latched before stopping the timer as stopping
\r
196 the timer appears to clear the count. */
\r
197 ulCurrentCount = RTC_CounterGet();
\r
198 RTC_Enable( false );
\r
200 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
201 that will mask interrupts that should exit sleep mode. */
\r
203 __asm volatile( "dsb" );
\r
204 __asm volatile( "isb" );
\r
206 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
207 mode is exited then sleep mode was probably exited because the tick was
\r
208 suppressed for the entire xExpectedIdleTime period. */
\r
209 ulTickFlag = pdFALSE;
\r
211 /* If a context switch is pending then abandon the low power entry as the
\r
212 context switch might have been pended by an external interrupt that requires
\r
214 eSleepAction = eTaskConfirmSleepModeStatus();
\r
215 if( eSleepAction == eAbortSleep )
\r
217 /* Restart tick and count up to whatever was left of the current time
\r
219 RTC_CompareSet( 0, ulReloadValueForOneTick - ulCurrentCount );
\r
220 RTC_Enable( true );
\r
222 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
228 /* Adjust the reload value to take into account that the current time
\r
229 slice is already partially complete. */
\r
230 ulReloadValue -= ulCurrentCount;
\r
231 RTC_CompareSet( 0, ulReloadValue );
\r
233 /* Restart the RTC. */
\r
234 RTC_Enable( true );
\r
236 /* Allow the application to define some pre-sleep processing. */
\r
237 xModifiableIdleTime = xExpectedIdleTime;
\r
238 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
240 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
241 means the application defined code has already executed the WAIT
\r
243 if( xModifiableIdleTime > 0 )
\r
245 __asm volatile( "dsb" );
\r
247 __asm volatile( "isb" );
\r
250 /* Allow the application to define some post sleep processing. */
\r
251 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
253 /* Stop RTC. Again, the time the SysTick is stopped for is accounted
\r
254 for as best it can be, but using the tickless mode will inevitably
\r
255 result in some tiny drift of the time maintained by the kernel with
\r
256 respect to calendar time. The count value is latched before stopping
\r
257 the timer as stopping the timer appears to clear the count. */
\r
258 ulCurrentCount = RTC_CounterGet();
\r
259 RTC_Enable( false );
\r
261 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
264 __asm volatile( "dsb" );
\r
265 __asm volatile( "isb" );
\r
267 if( ulTickFlag != pdFALSE )
\r
269 /* The tick interrupt has already executed, although because this
\r
270 function is called with the scheduler suspended the actual tick
\r
271 processing will not occur until after this function has exited.
\r
272 Reset the reload value with whatever remains of this tick period. */
\r
273 ulReloadValue = ulReloadValueForOneTick - ulCurrentCount;
\r
274 RTC_CompareSet( 0, ulReloadValue );
\r
276 /* The tick interrupt handler will already have pended the tick
\r
277 processing in the kernel. As the pending tick will be processed as
\r
278 soon as this function exits, the tick value maintained by the tick
\r
279 is stepped forward by one less than the time spent sleeping. The
\r
280 actual stepping of the tick appears later in this function. */
\r
281 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
285 /* Something other than the tick interrupt ended the sleep. How
\r
286 many complete tick periods passed while the processor was
\r
288 ulCompleteTickPeriods = ulCurrentCount / ulReloadValueForOneTick;
\r
290 /* The reload value is set to whatever fraction of a single tick
\r
292 ulReloadValue = ulCurrentCount - ( ulCompleteTickPeriods * ulReloadValueForOneTick );
\r
293 if( ulReloadValue == 0 )
\r
295 /* There is no fraction remaining. */
\r
296 ulReloadValue = ulReloadValueForOneTick;
\r
297 ulCompleteTickPeriods++;
\r
300 RTC_CompareSet( 0, ulReloadValue );
\r
303 /* Restart the RTC so it runs up to the alarm value. The alarm value
\r
304 will get set to the value required to generate exactly one tick period
\r
305 the next time the RTC interrupt executes. */
\r
306 RTC_Enable( true );
\r
308 /* Wind the tick forward by the number of tick periods that the CPU
\r
309 remained in a low power state. */
\r
310 vTaskStepTick( ulCompleteTickPeriods );
\r
313 /*-----------------------------------------------------------*/
\r
315 void RTC_IRQHandler( void )
\r
317 if( ulTickFlag == pdFALSE )
\r
319 /* Set RTC interrupt to one RTOS tick period. */
\r
320 RTC_Enable( false );
\r
321 RTC_CompareSet( 0, ulReloadValueForOneTick );
\r
322 ulTickFlag = pdTRUE;
\r
323 RTC_Enable( true );
\r
326 RTC_IntClear( _RTC_IFC_MASK );
\r
328 /* Critical section which protect incrementing the tick*/
\r
329 ( void ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
331 if( xTaskIncrementTick() != pdFALSE )
\r
333 /* Pend a context switch. */
\r
334 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
337 portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
\r
340 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r