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, configKERNEL_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, ulCurrentCount;
\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 ulCurrentCount = BURTC_CounterGet();
\r
193 BURTC_Enable( false );
\r
195 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
196 that will mask interrupts that should exit sleep mode. */
\r
199 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
200 mode is exited then sleep mode was probably exited because the tick was
\r
201 suppressed for the entire xExpectedIdleTime period. */
\r
202 ulTickFlag = pdFALSE;
\r
204 /* If a context switch is pending then abandon the low power entry as the
\r
205 context switch might have been pended by an external interrupt that requires
\r
207 eSleepAction = eTaskConfirmSleepModeStatus();
\r
208 if( eSleepAction == eAbortSleep )
\r
210 /* Restart tick and count up to whatever was left of the current time
\r
212 BURTC_CompareSet( 0, ulReloadValueForOneTick - ulCurrentCount );
\r
213 BURTC_Enable( true );
\r
215 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
221 /* Adjust the reload value to take into account that the current time
\r
222 slice is already partially complete. */
\r
223 ulReloadValue -= ulCurrentCount;
\r
224 BURTC_CompareSet( 0, ulReloadValue );
\r
226 /* Restart the BURTC. */
\r
227 BURTC_Enable( true );
\r
229 /* Allow the application to define some pre-sleep processing. */
\r
230 xModifiableIdleTime = xExpectedIdleTime;
\r
231 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
233 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
234 means the application defined code has already executed the WAIT
\r
236 if( xModifiableIdleTime > 0 )
\r
238 __asm volatile( "dsb" );
\r
240 __asm volatile( "isb" );
\r
243 /* Allow the application to define some post sleep processing. */
\r
244 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
246 /* Stop BURTC. Again, the time the SysTick is stopped for is accounted
\r
247 for as best it can be, but using the tickless mode will inevitably
\r
248 result in some tiny drift of the time maintained by the kernel with
\r
249 respect to calendar time. The count value is latched before stopping
\r
250 the timer as stopping the timer appears to clear the count. */
\r
251 ulCurrentCount = BURTC_CounterGet();
\r
252 BURTC_Enable( false );
\r
254 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
258 if( ulTickFlag != pdFALSE )
\r
260 /* The tick interrupt has already executed, although because this
\r
261 function is called with the scheduler suspended the actual tick
\r
262 processing will not occur until after this function has exited.
\r
263 Reset the reload value with whatever remains of this tick period. */
\r
264 ulReloadValue = ulReloadValueForOneTick - ulCurrentCount;
\r
265 BURTC_CompareSet( 0, ulReloadValue );
\r
267 /* The tick interrupt handler will already have pended the tick
\r
268 processing in the kernel. As the pending tick will be processed as
\r
269 soon as this function exits, the tick value maintained by the tick
\r
270 is stepped forward by one less than the time spent sleeping. The
\r
271 actual stepping of the tick appears later in this function. */
\r
272 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
276 /* Something other than the tick interrupt ended the sleep. How
\r
277 many complete tick periods passed while the processor was
\r
279 ulCompleteTickPeriods = ulCurrentCount / ulReloadValueForOneTick;
\r
281 /* The reload value is set to whatever fraction of a single tick
\r
283 ulReloadValue = ulCurrentCount - ( ulCompleteTickPeriods * ulReloadValueForOneTick );
\r
284 if( ulReloadValue == 0 )
\r
286 /* There is no fraction remaining. */
\r
287 ulReloadValue = ulReloadValueForOneTick;
\r
288 ulCompleteTickPeriods++;
\r
291 BURTC_CompareSet( 0, ulReloadValue );
\r
294 /* Restart the BURTC so it runs up to the alarm value. The alarm value
\r
295 will get set to the value required to generate exactly one tick period
\r
296 the next time the BURTC interrupt executes. */
\r
297 BURTC_Enable( true );
\r
299 /* Wind the tick forward by the number of tick periods that the CPU
\r
300 remained in a low power state. */
\r
301 vTaskStepTick( ulCompleteTickPeriods );
\r
304 /*-----------------------------------------------------------*/
\r
306 void BURTC_IRQHandler( void )
\r
308 if( ulTickFlag == pdFALSE )
\r
310 /* Set BURTC interrupt to one RTOS tick period. */
\r
311 BURTC_Enable( false );
\r
312 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
313 ulTickFlag = pdTRUE;
\r
314 BURTC_Enable( true );
\r
317 BURTC_IntClear( _RTC_IFC_MASK );
\r
319 /* Critical section which protect incrementing the tick*/
\r
320 ( void ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
322 if( xTaskIncrementTick() != pdFALSE )
\r
324 /* Pend a context switch. */
\r
325 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
328 portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
\r
331 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r