2 FreeRTOS V9.0.0rc2 - 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 inlcludes. */
\r
73 /* FreeRTOS includes. */
\r
74 #include "FreeRTOS.h"
\r
77 /* SiLabs library includes. */
\r
79 #include "em_rtcc.h"
\r
82 #include "em_letimer.h"
\r
85 /* SEE THE COMMENTS ABOVE THE DEFINITION OF configCREATE_LOW_POWER_DEMO IN
\r
87 This file contains functions that will override the default implementations
\r
88 in the RTOS port layer. Therefore only build this file if the low power demo
\r
90 #if( configCREATE_LOW_POWER_DEMO == 1 )
\r
92 /* When lpUSE_TEST_TIMER is 1 a second timer will be used to bring the MCU out
\r
93 of its low power state before the expected idle time has completed. This is
\r
94 done purely for test coverage purposes. */
\r
95 #define lpUSE_TEST_TIMER ( 0 )
\r
97 /* The RTCC channel used to generate the tick interrupt. */
\r
98 #define lpRTCC_CHANNEL ( 1 )
\r
100 /* 32768 clock divided by 1. Don't use a prescale if errata RTCC_E201
\r
102 #define mainTIMER_FREQUENCY_HZ ( 32768UL )
\r
105 * The low power demo does not use the SysTick, so override the
\r
106 * vPortSetupTickInterrupt() function with an implementation that configures
\r
107 * a low power clock source. NOTE: This function name must not be changed as
\r
108 * it is called from the RTOS portable layer.
\r
110 void vPortSetupTimerInterrupt( void );
\r
113 * Override the default definition of vPortSuppressTicksAndSleep() that is
\r
114 * weakly defined in the FreeRTOS Cortex-M port layer with a version that
\r
115 * manages the RTC clock, as the tick is generated from the low power RTC
\r
116 * and not the SysTick as would normally be the case on a Cortex-M.
\r
118 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
\r
120 /*-----------------------------------------------------------*/
\r
122 /* Calculate how many clock increments make up a single tick period. */
\r
123 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );
\r
125 /* Will hold the maximum number of ticks that can be suppressed. */
\r
126 static uint32_t xMaximumPossibleSuppressedTicks = 0;
\r
128 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
129 sleep mode was exited because of a timer interrupt or a different interrupt. */
\r
130 static volatile uint32_t ulTickFlag = pdFALSE;
\r
132 /* As the clock is only 32KHz, it is likely a value of 1 will be enough. */
\r
133 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
135 /* RTCC configuration structures. */
\r
136 static const RTCC_Init_TypeDef xRTCInitStruct =
\r
138 false, /* Don't start counting when init complete. */
\r
139 false, /* Disable counter during debug halt. */
\r
140 false, /* Don't care. */
\r
141 true, /* Enable counter wrap on ch. 1 CCV value. */
\r
142 rtccCntPresc_1, /* NOTE: Do not use a pre-scale if errata RTCC_E201 applies. */
\r
143 rtccCntTickPresc, /* Count using the clock input directly. */
\r
144 #if defined(_RTCC_CTRL_BUMODETSEN_MASK)
\r
145 false, /* Disable storing RTCC counter value in RTCC_CCV2 upon backup mode entry. */
\r
147 false, /* Oscillator fail detection disabled. */
\r
148 rtccCntModeNormal, /* Use RTCC in normal mode (increment by 1 on each tick) and not in calendar mode. */
\r
149 false /* Don't care. */
\r
152 static const RTCC_CCChConf_TypeDef xRTCCChannel1InitStruct =
\r
154 rtccCapComChModeCompare, /* Use Compare mode. */
\r
155 rtccCompMatchOutActionPulse,/* Don't care. */
\r
156 rtccPRSCh0, /* PRS not used. */
\r
157 rtccInEdgeNone, /* Capture Input not used. */
\r
158 rtccCompBaseCnt, /* Compare with Base CNT register. */
\r
159 0, /* Compare mask. */
\r
160 rtccDayCompareModeMonth /* Don't care. */
\r
163 /*-----------------------------------------------------------*/
\r
165 void vPortSetupTimerInterrupt( void )
\r
167 /* Configure the RTCC to generate the RTOS tick interrupt. */
\r
169 /* The maximum number of ticks that can be suppressed depends on the clock
\r
171 xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;
\r
173 /* Ensure LE modules are accessible. */
\r
174 CMU_ClockEnable( cmuClock_CORELE, true );
\r
177 CMU_ClockSelectSet( cmuClock_LFE, cmuSelect_LFXO );
\r
179 /* Enable clock to the RTC module. */
\r
180 CMU_ClockEnable( cmuClock_RTCC, true );
\r
182 /* Use channel 1 to generate the RTOS tick interrupt. */
\r
183 RTCC_ChannelCCVSet( lpRTCC_CHANNEL, ulReloadValueForOneTick );
\r
185 RTCC_Init( &xRTCInitStruct );
\r
186 RTCC_ChannelInit( lpRTCC_CHANNEL, &xRTCCChannel1InitStruct );
\r
187 RTCC_EM4WakeupEnable( true );
\r
189 /* Disable RTCC interrupt. */
\r
190 RTCC_IntDisable( _RTCC_IF_MASK );
\r
191 RTCC_IntClear( _RTCC_IF_MASK );
\r
192 RTCC->CNT = _RTCC_CNT_RESETVALUE;
\r
194 /* The tick interrupt must be set to the lowest priority possible. */
\r
195 NVIC_SetPriority( RTCC_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY );
\r
196 NVIC_ClearPendingIRQ( RTCC_IRQn );
\r
197 NVIC_EnableIRQ( RTCC_IRQn );
\r
198 RTCC_IntEnable( RTCC_IEN_CC1 );
\r
199 RTCC_Enable( true );
\r
201 #if( lpUSE_TEST_TIMER == 1 )
\r
203 void prvSetupTestTimer( void );
\r
205 /* A second timer is used to test the path where the MCU is brought out
\r
206 of a low power state by a timer other than the tick timer. */
\r
207 prvSetupTestTimer();
\r
211 /*-----------------------------------------------------------*/
\r
213 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
215 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCountAfterSleep;
\r
216 eSleepModeStatus eSleepAction;
\r
217 TickType_t xModifiableIdleTime;
\r
219 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
221 /* Make sure the RTC reload value does not overflow the counter. */
\r
222 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
224 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
227 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
229 ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
230 if( ulReloadValue > ulStoppedTimerCompensation )
\r
232 /* Compensate for the fact that the RTC is going to be stopped
\r
234 ulReloadValue -= ulStoppedTimerCompensation;
\r
237 /* Stop the RTC momentarily. The time the RTC is stopped for is accounted
\r
238 for as best it can be, but using the tickless mode will inevitably result
\r
239 in some tiny drift of the time maintained by the kernel with respect to
\r
241 RTCC_Enable( false );
\r
243 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
244 that will mask interrupts that should exit sleep mode. */
\r
246 __asm volatile( "dsb" );
\r
247 __asm volatile( "isb" );
\r
249 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
250 mode is exited then sleep mode was probably exited because the tick was
\r
251 suppressed for the entire xExpectedIdleTime period. */
\r
252 ulTickFlag = pdFALSE;
\r
254 /* If a context switch is pending then abandon the low power entry as the
\r
255 context switch might have been pended by an external interrupt that requires
\r
257 eSleepAction = eTaskConfirmSleepModeStatus();
\r
258 if( eSleepAction == eAbortSleep )
\r
260 /* Restart tick and continue counting to complete the current time
\r
262 RTCC_Enable( true );
\r
264 /* Re-enable interrupts - see comments above the RTCC_Enable() call
\r
270 RTCC_ChannelCCVSet( lpRTCC_CHANNEL, ulReloadValue );
\r
272 /* Restart the RTC. */
\r
273 RTCC_Enable( true );
\r
275 /* Allow the application to define some pre-sleep processing. */
\r
276 xModifiableIdleTime = xExpectedIdleTime;
\r
277 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
279 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
280 means the application defined code has already executed the WAIT
\r
282 if( xModifiableIdleTime > 0 )
\r
284 __asm volatile( "dsb" );
\r
286 __asm volatile( "isb" );
\r
289 /* Allow the application to define some post sleep processing. */
\r
290 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
292 /* Stop RTC. Again, the time the SysTick is stopped for is accounted
\r
293 for as best it can be, but using the tickless mode will inevitably
\r
294 result in some tiny drift of the time maintained by the kernel with
\r
295 respect to calendar time. */
\r
296 RTCC_Enable( false );
\r
297 ulCountAfterSleep = RTCC_CounterGet();
\r
299 /* Re-enable interrupts - see comments above the INT_Enable() call
\r
302 __asm volatile( "dsb" );
\r
303 __asm volatile( "isb" );
\r
305 if( ulTickFlag != pdFALSE )
\r
307 /* The tick interrupt has already executed, although because this
\r
308 function is called with the scheduler suspended the actual tick
\r
309 processing will not occur until after this function has exited.
\r
310 The tick interrupt handler will already have pended the tick
\r
311 processing in the kernel. As the pending tick will be processed as
\r
312 soon as this function exits, the tick value maintained by the tick
\r
313 is stepped forward by one less than the time spent sleeping. The
\r
314 actual stepping of the tick appears later in this function. */
\r
315 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
317 /* The interrupt should have reset the CCV value. */
\r
318 configASSERT( RTCC_ChannelCCVGet( lpRTCC_CHANNEL ) == ulReloadValueForOneTick );
\r
322 /* Something other than the tick interrupt ended the sleep. How
\r
323 many complete tick periods passed while the processor was
\r
325 ulCompleteTickPeriods = ulCountAfterSleep / ulReloadValueForOneTick;
\r
327 /* The next interrupt is configured to occur at whatever fraction of
\r
328 the current tick period remains by setting the reload value back to
\r
329 that required for one tick, and truncating the count to remove the
\r
330 counts that are greater than the reload value. */
\r
331 RTCC_ChannelCCVSet( lpRTCC_CHANNEL, ulReloadValueForOneTick );
\r
332 ulCountAfterSleep %= ulReloadValueForOneTick;
\r
333 RTCC_CounterSet( ulCountAfterSleep );
\r
336 /* Restart the RTC so it runs up to the alarm value. The alarm value
\r
337 will get set to the value required to generate exactly one tick period
\r
338 the next time the RTC interrupt executes. */
\r
339 RTCC_Enable( true );
\r
341 /* Wind the tick forward by the number of tick periods that the CPU
\r
342 remained in a low power state. */
\r
343 vTaskStepTick( ulCompleteTickPeriods );
\r
346 /*-----------------------------------------------------------*/
\r
348 void RTCC_IRQHandler( void )
\r
350 ulTickFlag = pdTRUE;
\r
352 if( RTCC_ChannelCCVGet( lpRTCC_CHANNEL ) != ulReloadValueForOneTick )
\r
354 /* Set RTC interrupt to one RTOS tick period. */
\r
355 RTCC_Enable( false );
\r
356 RTCC_ChannelCCVSet( lpRTCC_CHANNEL, ulReloadValueForOneTick );
\r
357 RTCC_Enable( true );
\r
360 RTCC_IntClear( _RTCC_IF_MASK );
\r
362 /* Critical section which protect incrementing the tick*/
\r
363 portDISABLE_INTERRUPTS();
\r
365 if( xTaskIncrementTick() != pdFALSE )
\r
367 /* Pend a context switch. */
\r
368 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
371 portENABLE_INTERRUPTS();
\r
373 /*-----------------------------------------------------------*/
\r
375 #if( lpUSE_TEST_TIMER == 1 )
\r
377 /* Juse used to ensure the second timer is executing. */
\r
378 volatile uint32_t ulLETimerIncrements = 0;
\r
380 void LETIMER0_IRQHandler( void )
\r
382 /* This ISR is used purely to bring the MCU out of sleep mode - it has
\r
383 no other purpose. */
\r
384 ulLETimerIncrements++;
\r
385 LETIMER_IntClear( LETIMER0, LETIMER_IF_COMP0 );
\r
388 #endif /* lpUSE_TEST_TIMER == 1 */
\r
389 /*-----------------------------------------------------------*/
\r
391 #if( lpUSE_TEST_TIMER == 1 )
\r
393 /* Set up a timer that used used to bring the MCU out of sleep mode using
\r
394 an interrupt other than the tick interrupt. This is done for code coverage
\r
396 void prvSetupTestTimer( void )
\r
398 static const LETIMER_Init_TypeDef xLETimerInitStruct =
\r
400 true, /* Enable timer when init complete. */
\r
401 false, /* Stop counter during debug halt. */
\r
402 true, /* Load COMP0 into CNT on underflow. */
\r
403 false, /* Do not load COMP1 into COMP0 when REP0 reaches 0. */
\r
404 0, /* Idle value 0 for output 0. */
\r
405 0, /* Idle value 0 for output 1. */
\r
406 letimerUFOANone, /* No action on underflow on output 0. */
\r
407 letimerUFOANone, /* No action on underflow on output 1. */
\r
408 letimerRepeatFree /* Count until stopped by SW. */
\r
410 const uint32_t ulCompareMatch = 32768UL / 10UL;
\r
412 CMU_ClockSelectSet( cmuClock_LFA, cmuSelect_LFXO );
\r
413 CMU_ClockEnable( cmuClock_LETIMER0, true );
\r
415 LETIMER_CompareSet( LETIMER0, 0, ulCompareMatch );
\r
416 LETIMER_IntEnable( LETIMER0, LETIMER_IF_COMP0 );
\r
417 NVIC_EnableIRQ( LETIMER0_IRQn );
\r
418 LETIMER_Init( LETIMER0, &xLETimerInitStruct);
\r
421 #endif /* lpUSE_TEST_TIMER == 1 */
\r
426 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r