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 #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
86 /* SEE THE COMMENTS ABOVE THE DEFINITION OF configCREATE_LOW_POWER_DEMO IN
\r
88 This file contains functions that will override the default implementations
\r
89 in the RTOS port layer. Therefore only build this file if the low power demo
\r
91 #if( configCREATE_LOW_POWER_DEMO == 1 )
\r
93 #define mainTIMER_FREQUENCY_HZ ( 2000UL )
\r
96 * The low power demo does not use the SysTick, so override the
\r
97 * vPortSetupTickInterrupt() function with an implementation that configures
\r
98 * a low power clock source. NOTE: This function name must not be changed as
\r
99 * it is called from the RTOS portable layer.
\r
101 void vPortSetupTimerInterrupt( void );
\r
104 * Override the default definition of vPortSuppressTicksAndSleep() that is
\r
105 * weakly defined in the FreeRTOS Cortex-M port layer with a version that
\r
106 * manages the BURTC clock, as the tick is generated from the low power BURTC
\r
107 * and not the SysTick as would normally be the case on a Cortex-M.
\r
109 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
\r
111 /*-----------------------------------------------------------*/
\r
113 /* Calculate how many clock increments make up a single tick period. */
\r
114 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );
\r
116 /* Will hold the maximum number of ticks that can be suppressed. */
\r
117 static uint32_t xMaximumPossibleSuppressedTicks = 0;
\r
119 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
120 sleep mode was exited because of a timer interrupt or a different interrupt. */
\r
121 static volatile uint32_t ulTickFlag = pdFALSE;
\r
123 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so
\r
124 use zero - but leave the value here to assist porting to different clock
\r
126 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
128 /*-----------------------------------------------------------*/
\r
130 void vPortSetupTimerInterrupt( void )
\r
132 BURTC_Init_TypeDef xBURTCInitStruct = BURTC_INIT_DEFAULT;
\r
134 /* Configure the BURTC to generate the RTOS tick interrupt. */
\r
136 xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;
\r
138 /* Ensure LE modules are accessible. */
\r
139 CMU_ClockEnable( cmuClock_CORELE, true );
\r
141 /* Enable access to BURTC registers. */
\r
142 RMU_ResetControl( rmuResetBU, false );
\r
144 /* Generate the tick interrupt from BURTC. */
\r
145 xBURTCInitStruct.mode = burtcModeEM3; /* Operational in EM3. */
\r
146 xBURTCInitStruct.clkSel = burtcClkSelULFRCO;/* ULFRCO clock. */
\r
147 xBURTCInitStruct.clkDiv = burtcClkDiv_1; /* 2kHz ULFRCO clock. */
\r
148 xBURTCInitStruct.compare0Top = true; /* Wrap on COMP0. */
\r
149 BURTC_IntDisable( BURTC_IF_COMP0 );
\r
150 BURTC_Init( &xBURTCInitStruct );
\r
152 /* The tick interrupt must be set to the lowest priority possible. */
\r
153 NVIC_SetPriority( BURTC_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY );
\r
154 NVIC_ClearPendingIRQ( BURTC_IRQn );
\r
155 NVIC_EnableIRQ( BURTC_IRQn );
\r
156 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
157 BURTC_IntClear( BURTC_IF_COMP0 );
\r
158 BURTC_IntEnable( BURTC_IF_COMP0 );
\r
159 BURTC_CounterReset();
\r
161 /*-----------------------------------------------------------*/
\r
163 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
165 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCountBeforeSleep, ulCountAfterSleep;
\r
166 eSleepModeStatus eSleepAction;
\r
167 TickType_t xModifiableIdleTime;
\r
169 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
171 /* Make sure the BURTC reload value does not overflow the counter. */
\r
172 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
174 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
177 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
179 ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
180 if( ulReloadValue > ulStoppedTimerCompensation )
\r
182 /* Compensate for the fact that the BURTC is going to be stopped
\r
184 ulReloadValue -= ulStoppedTimerCompensation;
\r
187 /* Stop the BURTC momentarily. The time the BURTC is stopped for is
\r
188 accounted for as best it can be, but using the tickless mode will inevitably
\r
189 result in some tiny drift of the time maintained by the kernel with respect
\r
190 to calendar time. The count is latched before stopping the timer as
\r
191 stopping the timer appears to clear the count. */
\r
192 ulCountBeforeSleep = BURTC_CounterGet();
\r
193 BURTC_Enable( false );
\r
195 /* If this function is re-entered before one complete tick period then the
\r
196 reload value might be set to take into account a partial time slice, but
\r
197 just reading the count assumes it is counting up to a full ticks worth - so
\r
198 add in the difference if any. */
\r
199 ulCountBeforeSleep += ( ulReloadValueForOneTick - BURTC_CompareGet( 0 ) );
\r
201 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
202 that will mask interrupts that should exit sleep mode. */
\r
204 __asm volatile( "dsb" );
\r
205 __asm volatile( "isb" );
\r
207 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
208 mode is exited then sleep mode was probably exited because the tick was
\r
209 suppressed for the entire xExpectedIdleTime period. */
\r
210 ulTickFlag = pdFALSE;
\r
212 /* If a context switch is pending then abandon the low power entry as the
\r
213 context switch might have been pended by an external interrupt that requires
\r
215 eSleepAction = eTaskConfirmSleepModeStatus();
\r
216 if( eSleepAction == eAbortSleep )
\r
218 /* Restart tick and count up to whatever was left of the current time
\r
220 BURTC_CompareSet( 0, ( ulReloadValueForOneTick - ulCountBeforeSleep ) + ulStoppedTimerCompensation );
\r
221 BURTC_Enable( true );
\r
223 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
229 /* Adjust the reload value to take into account that the current time
\r
230 slice is already partially complete. */
\r
231 ulReloadValue -= ulCountBeforeSleep;
\r
232 BURTC_CompareSet( 0, ulReloadValue );
\r
234 /* Restart the BURTC. */
\r
235 BURTC_Enable( true );
\r
237 /* Allow the application to define some pre-sleep processing. */
\r
238 xModifiableIdleTime = xExpectedIdleTime;
\r
239 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
241 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
242 means the application defined code has already executed the WAIT
\r
244 if( xModifiableIdleTime > 0 )
\r
246 __asm volatile( "dsb" );
\r
248 __asm volatile( "isb" );
\r
251 /* Allow the application to define some post sleep processing. */
\r
252 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
254 /* Stop BURTC. Again, the time the SysTick is stopped for is accounted
\r
255 for as best it can be, but using the tickless mode will inevitably
\r
256 result in some tiny drift of the time maintained by the kernel with
\r
257 respect to calendar time. The count value is latched before stopping
\r
258 the timer as stopping the timer appears to clear the count. */
\r
259 ulCountAfterSleep = BURTC_CounterGet();
\r
260 BURTC_Enable( false );
\r
262 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
265 __asm volatile( "dsb" );
\r
266 __asm volatile( "isb" );
\r
268 if( ulTickFlag != pdFALSE )
\r
270 /* The tick interrupt has already executed, although because this
\r
271 function is called with the scheduler suspended the actual tick
\r
272 processing will not occur until after this function has exited.
\r
273 Reset the reload value with whatever remains of this tick period. */
\r
274 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
275 BURTC_CompareSet( 0, ulReloadValue );
\r
277 /* The tick interrupt handler will already have pended the tick
\r
278 processing in the kernel. As the pending tick will be processed as
\r
279 soon as this function exits, the tick value maintained by the tick
\r
280 is stepped forward by one less than the time spent sleeping. The
\r
281 actual stepping of the tick appears later in this function. */
\r
282 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
286 /* Something other than the tick interrupt ended the sleep. How
\r
287 many complete tick periods passed while the processor was
\r
288 sleeping? Add back in the adjustment that was made to the reload
\r
289 value to account for the fact that a time slice was part way through
\r
290 when this function was called. */
\r
291 ulCountAfterSleep += ulCountBeforeSleep;
\r
292 ulCompleteTickPeriods = ulCountAfterSleep / ulReloadValueForOneTick;
\r
294 /* The reload value is set to whatever fraction of a single tick
\r
296 ulCountAfterSleep -= ( ulCompleteTickPeriods * ulReloadValueForOneTick );
\r
297 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
299 if( ulReloadValue == 0 )
\r
301 /* There is no fraction remaining. */
\r
302 ulReloadValue = ulReloadValueForOneTick;
\r
303 ulCompleteTickPeriods++;
\r
306 BURTC_CompareSet( 0, ulReloadValue );
\r
309 /* Restart the BURTC so it runs up to the alarm value. The alarm value
\r
310 will get set to the value required to generate exactly one tick period
\r
311 the next time the BURTC interrupt executes. */
\r
312 BURTC_Enable( true );
\r
314 /* Wind the tick forward by the number of tick periods that the CPU
\r
315 remained in a low power state. */
\r
316 vTaskStepTick( ulCompleteTickPeriods );
\r
319 /*-----------------------------------------------------------*/
\r
321 void BURTC_IRQHandler( void )
\r
323 ulTickFlag = pdTRUE;
\r
325 if( RTC_CompareGet( 0 ) != ulReloadValueForOneTick )
\r
327 /* Set RTC interrupt to one RTOS tick period. */
\r
328 BURTC_Enable( false );
\r
329 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
330 BURTC_Enable( true );
\r
333 BURTC_IntClear( _RTC_IFC_MASK );
\r
335 /* Critical section which protect incrementing the tick*/
\r
336 ( void ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
338 if( xTaskIncrementTick() != pdFALSE )
\r
340 /* Pend a context switch. */
\r
341 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
344 portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
\r
347 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r