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 /* 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
101 /* Override the default definition of vPortSuppressTicksAndSleep() that is
\r
102 * weakly defined in the FreeRTOS Cortex-M port layer with a version that
\r
103 * manages the BURTC clock, as the tick is generated from the low power BURTC
\r
104 * and not the SysTick as would normally be the case on a Cortex-M.
\r
106 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
\r
108 /*-----------------------------------------------------------*/
\r
110 /* Calculate how many clock increments make up a single tick period. */
\r
111 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );
\r
113 /* Calculate the maximum number of ticks that can be suppressed when using the
\r
114 high resolution clock and low resolution clock respectively. */
\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 an 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 xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;
\r
134 /* Ensure LE modules are accessible. */
\r
135 CMU_ClockEnable( cmuClock_CORELE, true );
\r
137 /* Enable access to BURTC registers. */
\r
138 RMU_ResetControl( rmuResetBU, false );
\r
140 /* Generate the tick interrupt from BURTC. */
\r
141 xBURTCInitStruct.mode = burtcModeEM3; /* Operational in EM3. */
\r
142 xBURTCInitStruct.clkSel = burtcClkSelULFRCO;/* ULFRCO clock. */
\r
143 xBURTCInitStruct.clkDiv = burtcClkDiv_1; /* 2kHz ULFRCO clock. */
\r
144 xBURTCInitStruct.compare0Top = true; /* Wrap on COMP0. */
\r
145 BURTC_IntDisable( BURTC_IF_COMP0 );
\r
146 BURTC_Init( &xBURTCInitStruct );
\r
148 /* The tick interrupt must be set to the lowest possible. */
\r
149 NVIC_SetPriority( BURTC_IRQn, configKERNEL_INTERRUPT_PRIORITY );
\r
150 NVIC_ClearPendingIRQ( BURTC_IRQn );
\r
151 NVIC_EnableIRQ( BURTC_IRQn );
\r
152 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
153 BURTC_IntClear( BURTC_IF_COMP0 );
\r
154 BURTC_IntEnable( BURTC_IF_COMP0 );
\r
155 BURTC_CounterReset();
\r
157 /*-----------------------------------------------------------*/
\r
159 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
161 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCurrentCount;
\r
162 eSleepModeStatus eSleepAction;
\r
163 TickType_t xModifiableIdleTime;
\r
165 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
167 /* Make sure the BURTC reload value does not overflow the counter. */
\r
168 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
170 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
173 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
175 ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
176 if( ulReloadValue > ulStoppedTimerCompensation )
\r
178 /* Compensate for the fact that the BURTC is going to be stopped
\r
180 ulReloadValue -= ulStoppedTimerCompensation;
\r
183 /* Stop the BURTC momentarily. The time the BURTC is stopped for is
\r
184 accounted for as best it can be, but using the tickless mode will inevitably
\r
185 result in some tiny drift of the time maintained by the kernel with respect
\r
186 to calendar time. The count is latched before stopping the timer as
\r
187 stopping the timer appears to clear the count. */
\r
188 ulCurrentCount = BURTC_CounterGet();
\r
189 BURTC_Enable( false );
\r
191 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
192 that will mask interrupts that should exit sleep mode. */
\r
195 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
196 mode is exited then sleep mode was probably exited because the tick was
\r
197 suppressed for the entire xExpectedIdleTime period. */
\r
198 ulTickFlag = pdFALSE;
\r
200 /* If a context switch is pending then abandon the low power entry as the
\r
201 context switch might have been pended by an external interrupt that requires
\r
203 eSleepAction = eTaskConfirmSleepModeStatus();
\r
204 if( eSleepAction == eAbortSleep )
\r
206 /* Restart tick and count up to whatever was left of the current time
\r
208 BURTC_CompareSet( 0, ulReloadValueForOneTick - ulCurrentCount );
\r
209 BURTC_Enable( true );
\r
211 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
217 /* Adjust the reload value to take into account that the current time
\r
218 slice is already partially complete. */
\r
219 ulReloadValue -= ulCurrentCount;
\r
220 BURTC_CompareSet( 0, ulReloadValue );
\r
222 /* Restart the BURTC. */
\r
223 BURTC_Enable( true );
\r
225 /* Allow the application to define some pre-sleep processing. */
\r
226 xModifiableIdleTime = xExpectedIdleTime;
\r
227 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
229 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
230 means the application defined code has already executed the WAIT
\r
232 if( xModifiableIdleTime > 0 )
\r
234 __asm volatile( "dsb" );
\r
236 __asm volatile( "isb" );
\r
239 /* Allow the application to define some post sleep processing. */
\r
240 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
242 /* Stop BURTC. Again, the time the SysTick is stopped for is accounted
\r
243 for as best it can be, but using the tickless mode will inevitably
\r
244 result in some tiny drift of the time maintained by the kernel with
\r
245 respect to calendar time. The count value is latched before stopping
\r
246 the timer as stopping the timer appears to clear the count. */
\r
247 ulCurrentCount = BURTC_CounterGet();
\r
248 BURTC_Enable( false );
\r
250 /* Re-enable interrupts - see comments above the cpsid instruction()
\r
254 if( ulTickFlag != pdFALSE )
\r
256 /* The tick interrupt has already executed, although because this
\r
257 function is called with the scheduler suspended the actual tick
\r
258 processing will not occur until after this function has exited.
\r
259 Reset the reload value with whatever remains of this tick period. */
\r
260 ulReloadValue = ulReloadValueForOneTick - ulCurrentCount;
\r
261 BURTC_CompareSet( 0, ulReloadValue );
\r
263 /* The tick interrupt handler will already have pended the tick
\r
264 processing in the kernel. As the pending tick will be processed as
\r
265 soon as this function exits, the tick value maintained by the tick
\r
266 is stepped forward by one less than the time spent sleeping. The
\r
267 actual stepping of the tick appears later in this function. */
\r
268 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
272 /* Something other than the tick interrupt ended the sleep. How
\r
273 many complete tick periods passed while the processor was
\r
275 ulCompleteTickPeriods = ulCurrentCount / ulReloadValueForOneTick;
\r
277 /* The reload value is set to whatever fraction of a single tick
\r
279 ulReloadValue = ulCurrentCount - ( ulCompleteTickPeriods * ulReloadValueForOneTick );
\r
280 if( ulReloadValue == 0 )
\r
282 /* There is no fraction remaining. */
\r
283 ulReloadValue = ulReloadValueForOneTick;
\r
284 ulCompleteTickPeriods++;
\r
287 BURTC_CompareSet( 0, ulReloadValue );
\r
290 /* Restart the BURTC so it runs up to the alarm value. The alarm value
\r
291 will get set to the value required to generate exactly one tick period
\r
292 the next time the BURTC interrupt executes. */
\r
293 BURTC_Enable( true );
\r
295 /* Wind the tick forward by the number of tick periods that the CPU
\r
296 remained in a low power state. */
\r
297 vTaskStepTick( ulCompleteTickPeriods );
\r
300 /*-----------------------------------------------------------*/
\r
302 void BURTC_IRQHandler(void)
\r
304 if( ulTickFlag == pdFALSE )
\r
306 /* Set BURTC interrupt to one system tick period*/
\r
307 BURTC_Enable( false );
\r
308 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
309 ulTickFlag = pdTRUE;
\r
310 BURTC_Enable( true );
\r
313 BURTC_IntClear( _RTC_IFC_MASK );
\r
315 /* Critical section which protect incrementing the tick*/
\r
316 ( void ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
318 if( xTaskIncrementTick() != pdFALSE )
\r
320 /* Pend a context switch. */
\r
321 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
324 portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
\r
327 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r