2 FreeRTOS V9.0.0 - 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 /* Standard includes. */
\r
73 /* FreeRTOS includes. */
\r
74 #include "FreeRTOS.h"
\r
77 /* SiLabs library includes. */
\r
79 #include "em_burtc.h"
\r
84 /* SEE THE COMMENTS ABOVE THE DEFINITION OF configCREATE_LOW_POWER_DEMO IN
\r
86 This file contains functions that will override the default implementations
\r
87 in the RTOS port layer. Therefore only build this file if the low power demo
\r
89 #if( configCREATE_LOW_POWER_DEMO == 1 )
\r
91 #define mainTIMER_FREQUENCY_HZ ( 2000UL )
\r
94 * The low power demo does not use the SysTick, so override the
\r
95 * vPortSetupTickInterrupt() function with an implementation that configures
\r
96 * a low power clock source. NOTE: This function name must not be changed as
\r
97 * it is called from the RTOS portable layer.
\r
99 void vPortSetupTimerInterrupt( void );
\r
102 * Override the default definition of vPortSuppressTicksAndSleep() that is
\r
103 * weakly defined in the FreeRTOS Cortex-M port layer with a version that
\r
104 * manages the BURTC clock, as the tick is generated from the low power BURTC
\r
105 * and not the SysTick as would normally be the case on a Cortex-M.
\r
107 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
\r
109 /*-----------------------------------------------------------*/
\r
111 /* Calculate how many clock increments make up a single tick period. */
\r
112 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );
\r
114 /* Will hold the maximum number of ticks that can be suppressed. */
\r
115 static uint32_t xMaximumPossibleSuppressedTicks = 0;
\r
117 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
118 sleep mode was exited because of a timer interrupt or a different interrupt. */
\r
119 static volatile uint32_t ulTickFlag = pdFALSE;
\r
121 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so
\r
122 use zero - but leave the value here to assist porting to different clock
\r
124 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
126 /*-----------------------------------------------------------*/
\r
128 void vPortSetupTimerInterrupt( void )
\r
130 BURTC_Init_TypeDef xBURTCInitStruct = BURTC_INIT_DEFAULT;
\r
132 /* Configure the BURTC to generate the RTOS tick interrupt. */
\r
134 xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;
\r
136 /* Ensure LE modules are accessible. */
\r
137 CMU_ClockEnable( cmuClock_CORELE, true );
\r
139 /* Enable access to BURTC registers. */
\r
140 RMU_ResetControl( rmuResetBU, false );
\r
142 /* Generate the tick interrupt from BURTC. */
\r
143 xBURTCInitStruct.mode = burtcModeEM3; /* Operational in EM3. */
\r
144 xBURTCInitStruct.clkSel = burtcClkSelULFRCO;/* ULFRCO clock. */
\r
145 xBURTCInitStruct.clkDiv = burtcClkDiv_1; /* 2kHz ULFRCO clock. */
\r
146 xBURTCInitStruct.compare0Top = true; /* Wrap on COMP0. */
\r
147 BURTC_IntDisable( BURTC_IF_COMP0 );
\r
148 BURTC_Init( &xBURTCInitStruct );
\r
150 /* The tick interrupt must be set to the lowest priority possible. */
\r
151 NVIC_SetPriority( BURTC_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY );
\r
152 NVIC_ClearPendingIRQ( BURTC_IRQn );
\r
153 NVIC_EnableIRQ( BURTC_IRQn );
\r
154 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
155 BURTC_IntClear( BURTC_IF_COMP0 );
\r
156 BURTC_IntEnable( BURTC_IF_COMP0 );
\r
157 BURTC_CounterReset();
\r
159 /*-----------------------------------------------------------*/
\r
161 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
163 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCountBeforeSleep, ulCountAfterSleep;
\r
164 eSleepModeStatus eSleepAction;
\r
165 TickType_t xModifiableIdleTime;
\r
167 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
169 /* Make sure the BURTC reload value does not overflow the counter. */
\r
170 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
172 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
175 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
177 ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
178 if( ulReloadValue > ulStoppedTimerCompensation )
\r
180 /* Compensate for the fact that the BURTC is going to be stopped
\r
182 ulReloadValue -= ulStoppedTimerCompensation;
\r
185 /* Stop the BURTC momentarily. The time the BURTC is stopped for is
\r
186 accounted for as best it can be, but using the tickless mode will inevitably
\r
187 result in some tiny drift of the time maintained by the kernel with respect
\r
188 to calendar time. The count is latched before stopping the timer as
\r
189 stopping the timer appears to clear the count. */
\r
190 ulCountBeforeSleep = BURTC_CounterGet();
\r
191 BURTC_Enable( false );
\r
193 /* If this function is re-entered before one complete tick period then the
\r
194 reload value might be set to take into account a partial time slice, but
\r
195 just reading the count assumes it is counting up to a full ticks worth - so
\r
196 add in the difference if any. */
\r
197 ulCountBeforeSleep += ( ulReloadValueForOneTick - BURTC_CompareGet( 0 ) );
\r
199 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
200 that will mask interrupts that should exit sleep mode. */
\r
202 __asm volatile( "dsb" );
\r
203 __asm volatile( "isb" );
\r
205 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
206 mode is exited then sleep mode was probably exited because the tick was
\r
207 suppressed for the entire xExpectedIdleTime period. */
\r
208 ulTickFlag = pdFALSE;
\r
210 /* If a context switch is pending then abandon the low power entry as the
\r
211 context switch might have been pended by an external interrupt that requires
\r
213 eSleepAction = eTaskConfirmSleepModeStatus();
\r
214 if( eSleepAction == eAbortSleep )
\r
216 /* Restart tick and count up to whatever was left of the current time
\r
218 BURTC_CompareSet( 0, ( ulReloadValueForOneTick - ulCountBeforeSleep ) + ulStoppedTimerCompensation );
\r
219 BURTC_Enable( true );
\r
221 /* Re-enable interrupts - see comments above the INT_Enable() call
\r
227 /* Adjust the reload value to take into account that the current time
\r
228 slice is already partially complete. */
\r
229 ulReloadValue -= ulCountBeforeSleep;
\r
230 BURTC_CompareSet( 0, ulReloadValue );
\r
232 /* Restart the BURTC. */
\r
233 BURTC_Enable( true );
\r
235 /* Allow the application to define some pre-sleep processing. */
\r
236 xModifiableIdleTime = xExpectedIdleTime;
\r
237 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
239 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
240 means the application defined code has already executed the WAIT
\r
242 if( xModifiableIdleTime > 0 )
\r
244 __asm volatile( "dsb" );
\r
246 __asm volatile( "isb" );
\r
249 /* Allow the application to define some post sleep processing. */
\r
250 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
252 /* Stop BURTC. Again, the time the SysTick is stopped for is accounted
\r
253 for as best it can be, but using the tickless mode will inevitably
\r
254 result in some tiny drift of the time maintained by the kernel with
\r
255 respect to calendar time. The count value is latched before stopping
\r
256 the timer as stopping the timer appears to clear the count. */
\r
257 ulCountAfterSleep = BURTC_CounterGet();
\r
258 BURTC_Enable( false );
\r
260 /* Re-enable interrupts - see comments above the INT_Enable() call
\r
263 __asm volatile( "dsb" );
\r
264 __asm volatile( "isb" );
\r
266 if( ulTickFlag != pdFALSE )
\r
268 /* The tick interrupt has already executed, although because this
\r
269 function is called with the scheduler suspended the actual tick
\r
270 processing will not occur until after this function has exited.
\r
271 Reset the reload value with whatever remains of this tick period. */
\r
272 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
273 BURTC_CompareSet( 0, ulReloadValue );
\r
275 /* The tick interrupt handler will already have pended the tick
\r
276 processing in the kernel. As the pending tick will be processed as
\r
277 soon as this function exits, the tick value maintained by the tick
\r
278 is stepped forward by one less than the time spent sleeping. The
\r
279 actual stepping of the tick appears later in this function. */
\r
280 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
284 /* Something other than the tick interrupt ended the sleep. How
\r
285 many complete tick periods passed while the processor was
\r
286 sleeping? Add back in the adjustment that was made to the reload
\r
287 value to account for the fact that a time slice was part way through
\r
288 when this function was called. */
\r
289 ulCountAfterSleep += ulCountBeforeSleep;
\r
290 ulCompleteTickPeriods = ulCountAfterSleep / ulReloadValueForOneTick;
\r
292 /* The reload value is set to whatever fraction of a single tick
\r
294 ulCountAfterSleep -= ( ulCompleteTickPeriods * ulReloadValueForOneTick );
\r
295 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
297 if( ulReloadValue == 0 )
\r
299 /* There is no fraction remaining. */
\r
300 ulReloadValue = ulReloadValueForOneTick;
\r
301 ulCompleteTickPeriods++;
\r
304 BURTC_CompareSet( 0, ulReloadValue );
\r
307 /* Restart the BURTC so it runs up to the alarm value. The alarm value
\r
308 will get set to the value required to generate exactly one tick period
\r
309 the next time the BURTC interrupt executes. */
\r
310 BURTC_Enable( true );
\r
312 /* Wind the tick forward by the number of tick periods that the CPU
\r
313 remained in a low power state. */
\r
314 vTaskStepTick( ulCompleteTickPeriods );
\r
317 /*-----------------------------------------------------------*/
\r
319 void BURTC_IRQHandler( void )
\r
321 ulTickFlag = pdTRUE;
\r
323 if( BURTC_CompareGet( 0 ) != ulReloadValueForOneTick )
\r
325 /* Set BURTC interrupt to one RTOS tick period. */
\r
326 BURTC_Enable( false );
\r
327 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
328 BURTC_Enable( true );
\r
331 BURTC_IntClear( _BURTC_IFC_MASK );
\r
333 /* Critical section which protect incrementing the tick. */
\r
334 portDISABLE_INTERRUPTS();
\r
336 if( xTaskIncrementTick() != pdFALSE )
\r
338 /* Pend a context switch. */
\r
339 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
342 portENABLE_INTERRUPTS();
\r
345 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r