2 * FreeRTOS Kernel V10.2.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard inlcludes. */
\r
31 /* FreeRTOS includes. */
\r
32 #include "FreeRTOS.h"
\r
35 /* SiLabs library includes. */
\r
37 #include "em_rtcc.h"
\r
40 #include "em_letimer.h"
\r
43 /* SEE THE COMMENTS ABOVE THE DEFINITION OF configCREATE_LOW_POWER_DEMO IN
\r
45 This file contains functions that will override the default implementations
\r
46 in the RTOS port layer. Therefore only build this file if the low power demo
\r
48 #if( configCREATE_LOW_POWER_DEMO == 1 )
\r
50 /* When lpUSE_TEST_TIMER is 1 a second timer will be used to bring the MCU out
\r
51 of its low power state before the expected idle time has completed. This is
\r
52 done purely for test coverage purposes. */
\r
53 #define lpUSE_TEST_TIMER ( 0 )
\r
55 /* The RTCC channel used to generate the tick interrupt. */
\r
56 #define lpRTCC_CHANNEL ( 1 )
\r
58 /* 32768 clock divided by 1. Don't use a prescale if errata RTCC_E201
\r
60 #define mainTIMER_FREQUENCY_HZ ( 32768UL )
\r
63 * The low power demo does not use the SysTick, so override the
\r
64 * vPortSetupTickInterrupt() function with an implementation that configures
\r
65 * a low power clock source. NOTE: This function name must not be changed as
\r
66 * it is called from the RTOS portable layer.
\r
68 void vPortSetupTimerInterrupt( void );
\r
71 * Override the default definition of vPortSuppressTicksAndSleep() that is
\r
72 * weakly defined in the FreeRTOS Cortex-M port layer with a version that
\r
73 * manages the RTC clock, as the tick is generated from the low power RTC
\r
74 * and not the SysTick as would normally be the case on a Cortex-M.
\r
76 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
\r
78 /*-----------------------------------------------------------*/
\r
80 /* Calculate how many clock increments make up a single tick period. */
\r
81 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );
\r
83 /* Will hold the maximum number of ticks that can be suppressed. */
\r
84 static uint32_t xMaximumPossibleSuppressedTicks = 0;
\r
86 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
87 sleep mode was exited because of a timer interrupt or a different interrupt. */
\r
88 static volatile uint32_t ulTickFlag = pdFALSE;
\r
90 /* As the clock is only 32KHz, it is likely a value of 1 will be enough. */
\r
91 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
93 /* RTCC configuration structures. */
\r
94 static const RTCC_Init_TypeDef xRTCInitStruct =
\r
96 false, /* Don't start counting when init complete. */
\r
97 false, /* Disable counter during debug halt. */
\r
98 false, /* Don't care. */
\r
99 true, /* Enable counter wrap on ch. 1 CCV value. */
\r
100 rtccCntPresc_1, /* NOTE: Do not use a pre-scale if errata RTCC_E201 applies. */
\r
101 rtccCntTickPresc, /* Count using the clock input directly. */
\r
102 #if defined(_RTCC_CTRL_BUMODETSEN_MASK)
\r
103 false, /* Disable storing RTCC counter value in RTCC_CCV2 upon backup mode entry. */
\r
105 false, /* Oscillator fail detection disabled. */
\r
106 rtccCntModeNormal, /* Use RTCC in normal mode (increment by 1 on each tick) and not in calendar mode. */
\r
107 false /* Don't care. */
\r
110 static const RTCC_CCChConf_TypeDef xRTCCChannel1InitStruct =
\r
112 rtccCapComChModeCompare, /* Use Compare mode. */
\r
113 rtccCompMatchOutActionPulse,/* Don't care. */
\r
114 rtccPRSCh0, /* PRS not used. */
\r
115 rtccInEdgeNone, /* Capture Input not used. */
\r
116 rtccCompBaseCnt, /* Compare with Base CNT register. */
\r
117 0, /* Compare mask. */
\r
118 rtccDayCompareModeMonth /* Don't care. */
\r
121 /*-----------------------------------------------------------*/
\r
123 void vPortSetupTimerInterrupt( void )
\r
125 /* Configure the RTCC to generate the RTOS tick interrupt. */
\r
127 /* The maximum number of ticks that can be suppressed depends on the clock
\r
129 xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;
\r
131 /* Ensure LE modules are accessible. */
\r
132 CMU_ClockEnable( cmuClock_CORELE, true );
\r
135 CMU_ClockSelectSet( cmuClock_LFE, cmuSelect_LFXO );
\r
137 /* Enable clock to the RTC module. */
\r
138 CMU_ClockEnable( cmuClock_RTCC, true );
\r
140 /* Use channel 1 to generate the RTOS tick interrupt. */
\r
141 RTCC_ChannelCCVSet( lpRTCC_CHANNEL, ulReloadValueForOneTick );
\r
143 RTCC_Init( &xRTCInitStruct );
\r
144 RTCC_ChannelInit( lpRTCC_CHANNEL, &xRTCCChannel1InitStruct );
\r
145 RTCC_EM4WakeupEnable( true );
\r
147 /* Disable RTCC interrupt. */
\r
148 RTCC_IntDisable( _RTCC_IF_MASK );
\r
149 RTCC_IntClear( _RTCC_IF_MASK );
\r
150 RTCC->CNT = _RTCC_CNT_RESETVALUE;
\r
152 /* The tick interrupt must be set to the lowest priority possible. */
\r
153 NVIC_SetPriority( RTCC_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY );
\r
154 NVIC_ClearPendingIRQ( RTCC_IRQn );
\r
155 NVIC_EnableIRQ( RTCC_IRQn );
\r
156 RTCC_IntEnable( RTCC_IEN_CC1 );
\r
157 RTCC_Enable( true );
\r
159 #if( lpUSE_TEST_TIMER == 1 )
\r
161 void prvSetupTestTimer( void );
\r
163 /* A second timer is used to test the path where the MCU is brought out
\r
164 of a low power state by a timer other than the tick timer. */
\r
165 prvSetupTestTimer();
\r
169 /*-----------------------------------------------------------*/
\r
171 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
173 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCountAfterSleep;
\r
174 eSleepModeStatus eSleepAction;
\r
175 TickType_t xModifiableIdleTime;
\r
177 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
179 /* Make sure the RTC reload value does not overflow the counter. */
\r
180 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
182 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
185 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
187 ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
188 if( ulReloadValue > ulStoppedTimerCompensation )
\r
190 /* Compensate for the fact that the RTC is going to be stopped
\r
192 ulReloadValue -= ulStoppedTimerCompensation;
\r
195 /* Stop the RTC momentarily. The time the RTC is stopped for is accounted
\r
196 for as best it can be, but using the tickless mode will inevitably result
\r
197 in some tiny drift of the time maintained by the kernel with respect to
\r
199 RTCC_Enable( false );
\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 continue counting to complete the current time
\r
220 RTCC_Enable( true );
\r
222 /* Re-enable interrupts - see comments above the RTCC_Enable() call
\r
228 RTCC_ChannelCCVSet( lpRTCC_CHANNEL, ulReloadValue );
\r
230 /* Restart the RTC. */
\r
231 RTCC_Enable( true );
\r
233 /* Allow the application to define some pre-sleep processing. */
\r
234 xModifiableIdleTime = xExpectedIdleTime;
\r
235 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
237 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
238 means the application defined code has already executed the WAIT
\r
240 if( xModifiableIdleTime > 0 )
\r
242 __asm volatile( "dsb" );
\r
244 __asm volatile( "isb" );
\r
247 /* Allow the application to define some post sleep processing. */
\r
248 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
250 /* Stop RTC. Again, the time the SysTick is stopped for is accounted
\r
251 for as best it can be, but using the tickless mode will inevitably
\r
252 result in some tiny drift of the time maintained by the kernel with
\r
253 respect to calendar time. */
\r
254 RTCC_Enable( false );
\r
255 ulCountAfterSleep = RTCC_CounterGet();
\r
257 /* Re-enable interrupts - see comments above the INT_Enable() call
\r
260 __asm volatile( "dsb" );
\r
261 __asm volatile( "isb" );
\r
263 if( ulTickFlag != pdFALSE )
\r
265 /* The tick interrupt has already executed, although because this
\r
266 function is called with the scheduler suspended the actual tick
\r
267 processing will not occur until after this function has exited.
\r
268 The tick interrupt handler will already have pended the tick
\r
269 processing in the kernel. As the pending tick will be processed as
\r
270 soon as this function exits, the tick value maintained by the tick
\r
271 is stepped forward by one less than the time spent sleeping. The
\r
272 actual stepping of the tick appears later in this function. */
\r
273 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
275 /* The interrupt should have reset the CCV value. */
\r
276 configASSERT( RTCC_ChannelCCVGet( lpRTCC_CHANNEL ) == ulReloadValueForOneTick );
\r
280 /* Something other than the tick interrupt ended the sleep. How
\r
281 many complete tick periods passed while the processor was
\r
283 ulCompleteTickPeriods = ulCountAfterSleep / ulReloadValueForOneTick;
\r
285 /* The next interrupt is configured to occur at whatever fraction of
\r
286 the current tick period remains by setting the reload value back to
\r
287 that required for one tick, and truncating the count to remove the
\r
288 counts that are greater than the reload value. */
\r
289 RTCC_ChannelCCVSet( lpRTCC_CHANNEL, ulReloadValueForOneTick );
\r
290 ulCountAfterSleep %= ulReloadValueForOneTick;
\r
291 RTCC_CounterSet( ulCountAfterSleep );
\r
294 /* Restart the RTC 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 RTC interrupt executes. */
\r
297 RTCC_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 RTCC_IRQHandler( void )
\r
308 ulTickFlag = pdTRUE;
\r
310 if( RTCC_ChannelCCVGet( lpRTCC_CHANNEL ) != ulReloadValueForOneTick )
\r
312 /* Set RTC interrupt to one RTOS tick period. */
\r
313 RTCC_Enable( false );
\r
314 RTCC_ChannelCCVSet( lpRTCC_CHANNEL, ulReloadValueForOneTick );
\r
315 RTCC_Enable( true );
\r
318 RTCC_IntClear( _RTCC_IF_MASK );
\r
320 /* Critical section which protect incrementing the tick*/
\r
321 portDISABLE_INTERRUPTS();
\r
323 if( xTaskIncrementTick() != pdFALSE )
\r
325 /* Pend a context switch. */
\r
326 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
329 portENABLE_INTERRUPTS();
\r
331 /*-----------------------------------------------------------*/
\r
333 #if( lpUSE_TEST_TIMER == 1 )
\r
335 /* Juse used to ensure the second timer is executing. */
\r
336 volatile uint32_t ulLETimerIncrements = 0;
\r
338 void LETIMER0_IRQHandler( void )
\r
340 /* This ISR is used purely to bring the MCU out of sleep mode - it has
\r
341 no other purpose. */
\r
342 ulLETimerIncrements++;
\r
343 LETIMER_IntClear( LETIMER0, LETIMER_IF_COMP0 );
\r
346 #endif /* lpUSE_TEST_TIMER == 1 */
\r
347 /*-----------------------------------------------------------*/
\r
349 #if( lpUSE_TEST_TIMER == 1 )
\r
351 /* Set up a timer that used used to bring the MCU out of sleep mode using
\r
352 an interrupt other than the tick interrupt. This is done for code coverage
\r
354 void prvSetupTestTimer( void )
\r
356 static const LETIMER_Init_TypeDef xLETimerInitStruct =
\r
358 true, /* Enable timer when init complete. */
\r
359 false, /* Stop counter during debug halt. */
\r
360 true, /* Load COMP0 into CNT on underflow. */
\r
361 false, /* Do not load COMP1 into COMP0 when REP0 reaches 0. */
\r
362 0, /* Idle value 0 for output 0. */
\r
363 0, /* Idle value 0 for output 1. */
\r
364 letimerUFOANone, /* No action on underflow on output 0. */
\r
365 letimerUFOANone, /* No action on underflow on output 1. */
\r
366 letimerRepeatFree /* Count until stopped by SW. */
\r
368 const uint32_t ulCompareMatch = 32768UL / 10UL;
\r
370 CMU_ClockSelectSet( cmuClock_LFA, cmuSelect_LFXO );
\r
371 CMU_ClockEnable( cmuClock_LETIMER0, true );
\r
373 LETIMER_CompareSet( LETIMER0, 0, ulCompareMatch );
\r
374 LETIMER_IntEnable( LETIMER0, LETIMER_IF_COMP0 );
\r
375 NVIC_EnableIRQ( LETIMER0_IRQn );
\r
376 LETIMER_Init( LETIMER0, &xLETimerInitStruct);
\r
379 #endif /* lpUSE_TEST_TIMER == 1 */
\r
384 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r