2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 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 #warning Not functioning correctly above -O1 optimisation level.
\r
72 /* Standard includes. */
\r
75 /* FreeRTOS includes. */
\r
76 #include "FreeRTOS.h"
\r
79 /* SiLabs library includes. */
\r
81 #include "em_burtc.h"
\r
87 /* SEE THE COMMENTS ABOVE THE DEFINITION OF configCREATE_LOW_POWER_DEMO IN
\r
89 This file contains functions that will override the default implementations
\r
90 in the RTOS port layer. Therefore only build this file if the low power demo
\r
92 #if( configCREATE_LOW_POWER_DEMO == 1 )
\r
94 #define mainTIMER_FREQUENCY_HZ ( 2000UL )
\r
97 * The low power demo does not use the SysTick, so override the
\r
98 * vPortSetupTickInterrupt() function with an implementation that configures
\r
99 * a low power clock source. NOTE: This function name must not be changed as
\r
100 * it is called from the RTOS portable layer.
\r
102 void vPortSetupTimerInterrupt( void );
\r
105 * Override the default definition of vPortSuppressTicksAndSleep() that is
\r
106 * weakly defined in the FreeRTOS Cortex-M port layer with a version that
\r
107 * manages the BURTC clock, as the tick is generated from the low power BURTC
\r
108 * and not the SysTick as would normally be the case on a Cortex-M.
\r
110 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
\r
112 /*-----------------------------------------------------------*/
\r
114 /* Calculate how many clock increments make up a single tick period. */
\r
115 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );
\r
117 /* Will hold the maximum number of ticks that can be suppressed. */
\r
118 static uint32_t xMaximumPossibleSuppressedTicks = 0;
\r
120 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
121 sleep mode was exited because of a timer interrupt or a different interrupt. */
\r
122 static volatile uint32_t ulTickFlag = pdFALSE;
\r
124 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so
\r
125 use zero - but leave the value here to assist porting to different clock
\r
127 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
129 /*-----------------------------------------------------------*/
\r
131 void vPortSetupTimerInterrupt( void )
\r
133 BURTC_Init_TypeDef xBURTCInitStruct = BURTC_INIT_DEFAULT;
\r
135 /* Configure the BURTC to generate the RTOS tick interrupt. */
\r
137 xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;
\r
139 /* Ensure LE modules are accessible. */
\r
140 CMU_ClockEnable( cmuClock_CORELE, true );
\r
142 /* Enable access to BURTC registers. */
\r
143 RMU_ResetControl( rmuResetBU, false );
\r
145 /* Generate the tick interrupt from BURTC. */
\r
146 xBURTCInitStruct.mode = burtcModeEM3; /* Operational in EM3. */
\r
147 xBURTCInitStruct.clkSel = burtcClkSelULFRCO;/* ULFRCO clock. */
\r
148 xBURTCInitStruct.clkDiv = burtcClkDiv_1; /* 2kHz ULFRCO clock. */
\r
149 xBURTCInitStruct.compare0Top = true; /* Wrap on COMP0. */
\r
150 BURTC_IntDisable( BURTC_IF_COMP0 );
\r
151 BURTC_Init( &xBURTCInitStruct );
\r
153 /* The tick interrupt must be set to the lowest priority possible. */
\r
154 NVIC_SetPriority( BURTC_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY );
\r
155 NVIC_ClearPendingIRQ( BURTC_IRQn );
\r
156 NVIC_EnableIRQ( BURTC_IRQn );
\r
157 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
158 BURTC_IntClear( BURTC_IF_COMP0 );
\r
159 BURTC_IntEnable( BURTC_IF_COMP0 );
\r
160 BURTC_CounterReset();
\r
162 /*-----------------------------------------------------------*/
\r
164 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
166 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCountBeforeSleep, ulCountAfterSleep;
\r
167 eSleepModeStatus eSleepAction;
\r
168 TickType_t xModifiableIdleTime;
\r
170 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
172 /* Make sure the BURTC reload value does not overflow the counter. */
\r
173 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
175 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
178 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
180 ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
181 if( ulReloadValue > ulStoppedTimerCompensation )
\r
183 /* Compensate for the fact that the BURTC is going to be stopped
\r
185 ulReloadValue -= ulStoppedTimerCompensation;
\r
188 /* Stop the BURTC momentarily. The time the BURTC is stopped for is
\r
189 accounted for as best it can be, but using the tickless mode will inevitably
\r
190 result in some tiny drift of the time maintained by the kernel with respect
\r
191 to calendar time. The count is latched before stopping the timer as
\r
192 stopping the timer appears to clear the count. */
\r
193 ulCountBeforeSleep = BURTC_CounterGet();
\r
194 BURTC_Enable( false );
\r
196 /* If this function is re-entered before one complete tick period then the
\r
197 reload value might be set to take into account a partial time slice, but
\r
198 just reading the count assumes it is counting up to a full ticks worth - so
\r
199 add in the difference if any. */
\r
200 ulCountBeforeSleep += ( ulReloadValueForOneTick - BURTC_CompareGet( 0 ) );
\r
202 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
203 that will mask interrupts that should exit sleep mode. */
\r
205 __asm volatile( "dsb" );
\r
206 __asm volatile( "isb" );
\r
208 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
209 mode is exited then sleep mode was probably exited because the tick was
\r
210 suppressed for the entire xExpectedIdleTime period. */
\r
211 ulTickFlag = pdFALSE;
\r
213 /* If a context switch is pending then abandon the low power entry as the
\r
214 context switch might have been pended by an external interrupt that requires
\r
216 eSleepAction = eTaskConfirmSleepModeStatus();
\r
217 if( eSleepAction == eAbortSleep )
\r
219 /* Restart tick and count up to whatever was left of the current time
\r
221 BURTC_CompareSet( 0, ( ulReloadValueForOneTick - ulCountBeforeSleep ) + ulStoppedTimerCompensation );
\r
222 BURTC_Enable( true );
\r
224 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
230 /* Adjust the reload value to take into account that the current time
\r
231 slice is already partially complete. */
\r
232 ulReloadValue -= ulCountBeforeSleep;
\r
233 BURTC_CompareSet( 0, ulReloadValue );
\r
235 /* Restart the BURTC. */
\r
236 BURTC_Enable( true );
\r
238 /* Allow the application to define some pre-sleep processing. */
\r
239 xModifiableIdleTime = xExpectedIdleTime;
\r
240 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
242 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
243 means the application defined code has already executed the WAIT
\r
245 if( xModifiableIdleTime > 0 )
\r
247 __asm volatile( "dsb" );
\r
249 __asm volatile( "isb" );
\r
252 /* Allow the application to define some post sleep processing. */
\r
253 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
255 /* Stop BURTC. Again, the time the SysTick is stopped for is accounted
\r
256 for as best it can be, but using the tickless mode will inevitably
\r
257 result in some tiny drift of the time maintained by the kernel with
\r
258 respect to calendar time. The count value is latched before stopping
\r
259 the timer as stopping the timer appears to clear the count. */
\r
260 ulCountAfterSleep = BURTC_CounterGet();
\r
261 BURTC_Enable( false );
\r
263 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
266 __asm volatile( "dsb" );
\r
267 __asm volatile( "isb" );
\r
269 if( ulTickFlag != pdFALSE )
\r
271 /* The tick interrupt has already executed, although because this
\r
272 function is called with the scheduler suspended the actual tick
\r
273 processing will not occur until after this function has exited.
\r
274 Reset the reload value with whatever remains of this tick period. */
\r
275 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
276 BURTC_CompareSet( 0, ulReloadValue );
\r
278 /* The tick interrupt handler will already have pended the tick
\r
279 processing in the kernel. As the pending tick will be processed as
\r
280 soon as this function exits, the tick value maintained by the tick
\r
281 is stepped forward by one less than the time spent sleeping. The
\r
282 actual stepping of the tick appears later in this function. */
\r
283 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
287 /* Something other than the tick interrupt ended the sleep. How
\r
288 many complete tick periods passed while the processor was
\r
289 sleeping? Add back in the adjustment that was made to the reload
\r
290 value to account for the fact that a time slice was part way through
\r
291 when this function was called. */
\r
292 ulCountAfterSleep += ulCountBeforeSleep;
\r
293 ulCompleteTickPeriods = ulCountAfterSleep / ulReloadValueForOneTick;
\r
295 /* The reload value is set to whatever fraction of a single tick
\r
297 ulCountAfterSleep -= ( ulCompleteTickPeriods * ulReloadValueForOneTick );
\r
298 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
300 if( ulReloadValue == 0 )
\r
302 /* There is no fraction remaining. */
\r
303 ulReloadValue = ulReloadValueForOneTick;
\r
304 ulCompleteTickPeriods++;
\r
307 BURTC_CompareSet( 0, ulReloadValue );
\r
310 /* Restart the BURTC so it runs up to the alarm value. The alarm value
\r
311 will get set to the value required to generate exactly one tick period
\r
312 the next time the BURTC interrupt executes. */
\r
313 BURTC_Enable( true );
\r
315 /* Wind the tick forward by the number of tick periods that the CPU
\r
316 remained in a low power state. */
\r
317 vTaskStepTick( ulCompleteTickPeriods );
\r
320 /*-----------------------------------------------------------*/
\r
322 void BURTC_IRQHandler( void )
\r
324 ulTickFlag = pdTRUE;
\r
326 if( RTC_CompareGet( 0 ) != ulReloadValueForOneTick )
\r
328 /* Set RTC interrupt to one RTOS tick period. */
\r
329 BURTC_Enable( false );
\r
330 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
331 BURTC_Enable( true );
\r
334 BURTC_IntClear( _RTC_IFC_MASK );
\r
336 /* Critical section which protect incrementing the tick*/
\r
337 ( void ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
339 if( xTaskIncrementTick() != pdFALSE )
\r
341 /* Pend a context switch. */
\r
342 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
345 portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
\r
348 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r