2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Standard includes. */
\r
32 /* FreeRTOS includes. */
\r
33 #include "FreeRTOS.h"
\r
36 /* SiLabs library includes. */
\r
38 #include "em_burtc.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 #define mainTIMER_FREQUENCY_HZ ( 2000UL )
\r
53 * The low power demo does not use the SysTick, so override the
\r
54 * vPortSetupTickInterrupt() function with an implementation that configures
\r
55 * a low power clock source. NOTE: This function name must not be changed as
\r
56 * it is called from the RTOS portable layer.
\r
58 void vPortSetupTimerInterrupt( void );
\r
61 * Override the default definition of vPortSuppressTicksAndSleep() that is
\r
62 * weakly defined in the FreeRTOS Cortex-M port layer with a version that
\r
63 * manages the BURTC clock, as the tick is generated from the low power BURTC
\r
64 * and not the SysTick as would normally be the case on a Cortex-M.
\r
66 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
\r
68 /*-----------------------------------------------------------*/
\r
70 /* Calculate how many clock increments make up a single tick period. */
\r
71 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );
\r
73 /* Will hold the maximum number of ticks that can be suppressed. */
\r
74 static uint32_t xMaximumPossibleSuppressedTicks = 0;
\r
76 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
77 sleep mode was exited because of a timer interrupt or a different interrupt. */
\r
78 static volatile uint32_t ulTickFlag = pdFALSE;
\r
80 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so
\r
81 use zero - but leave the value here to assist porting to different clock
\r
83 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
85 /*-----------------------------------------------------------*/
\r
87 void vPortSetupTimerInterrupt( void )
\r
89 BURTC_Init_TypeDef xBURTCInitStruct = BURTC_INIT_DEFAULT;
\r
91 /* Configure the BURTC to generate the RTOS tick interrupt. */
\r
93 xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;
\r
95 /* Ensure LE modules are accessible. */
\r
96 CMU_ClockEnable( cmuClock_CORELE, true );
\r
98 /* Enable access to BURTC registers. */
\r
99 RMU_ResetControl( rmuResetBU, false );
\r
101 /* Generate the tick interrupt from BURTC. */
\r
102 xBURTCInitStruct.mode = burtcModeEM3; /* Operational in EM3. */
\r
103 xBURTCInitStruct.clkSel = burtcClkSelULFRCO;/* ULFRCO clock. */
\r
104 xBURTCInitStruct.clkDiv = burtcClkDiv_1; /* 2kHz ULFRCO clock. */
\r
105 xBURTCInitStruct.compare0Top = true; /* Wrap on COMP0. */
\r
106 BURTC_IntDisable( BURTC_IF_COMP0 );
\r
107 BURTC_Init( &xBURTCInitStruct );
\r
109 /* The tick interrupt must be set to the lowest priority possible. */
\r
110 NVIC_SetPriority( BURTC_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY );
\r
111 NVIC_ClearPendingIRQ( BURTC_IRQn );
\r
112 NVIC_EnableIRQ( BURTC_IRQn );
\r
113 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
114 BURTC_IntClear( BURTC_IF_COMP0 );
\r
115 BURTC_IntEnable( BURTC_IF_COMP0 );
\r
116 BURTC_CounterReset();
\r
118 /*-----------------------------------------------------------*/
\r
120 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
122 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCountBeforeSleep, ulCountAfterSleep;
\r
123 eSleepModeStatus eSleepAction;
\r
124 TickType_t xModifiableIdleTime;
\r
126 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
128 /* Make sure the BURTC reload value does not overflow the counter. */
\r
129 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
131 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
134 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
136 ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
137 if( ulReloadValue > ulStoppedTimerCompensation )
\r
139 /* Compensate for the fact that the BURTC is going to be stopped
\r
141 ulReloadValue -= ulStoppedTimerCompensation;
\r
144 /* Stop the BURTC momentarily. The time the BURTC is stopped for is
\r
145 accounted for as best it can be, but using the tickless mode will inevitably
\r
146 result in some tiny drift of the time maintained by the kernel with respect
\r
147 to calendar time. The count is latched before stopping the timer as
\r
148 stopping the timer appears to clear the count. */
\r
149 ulCountBeforeSleep = BURTC_CounterGet();
\r
150 BURTC_Enable( false );
\r
152 /* If this function is re-entered before one complete tick period then the
\r
153 reload value might be set to take into account a partial time slice, but
\r
154 just reading the count assumes it is counting up to a full ticks worth - so
\r
155 add in the difference if any. */
\r
156 ulCountBeforeSleep += ( ulReloadValueForOneTick - BURTC_CompareGet( 0 ) );
\r
158 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
159 that will mask interrupts that should exit sleep mode. */
\r
161 __asm volatile( "dsb" );
\r
162 __asm volatile( "isb" );
\r
164 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
165 mode is exited then sleep mode was probably exited because the tick was
\r
166 suppressed for the entire xExpectedIdleTime period. */
\r
167 ulTickFlag = pdFALSE;
\r
169 /* If a context switch is pending then abandon the low power entry as the
\r
170 context switch might have been pended by an external interrupt that requires
\r
172 eSleepAction = eTaskConfirmSleepModeStatus();
\r
173 if( eSleepAction == eAbortSleep )
\r
175 /* Restart tick and count up to whatever was left of the current time
\r
177 BURTC_CompareSet( 0, ( ulReloadValueForOneTick - ulCountBeforeSleep ) + ulStoppedTimerCompensation );
\r
178 BURTC_Enable( true );
\r
180 /* Re-enable interrupts - see comments above the INT_Enable() call
\r
186 /* Adjust the reload value to take into account that the current time
\r
187 slice is already partially complete. */
\r
188 ulReloadValue -= ulCountBeforeSleep;
\r
189 BURTC_CompareSet( 0, ulReloadValue );
\r
191 /* Restart the BURTC. */
\r
192 BURTC_Enable( true );
\r
194 /* Allow the application to define some pre-sleep processing. */
\r
195 xModifiableIdleTime = xExpectedIdleTime;
\r
196 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
198 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
199 means the application defined code has already executed the WAIT
\r
201 if( xModifiableIdleTime > 0 )
\r
203 __asm volatile( "dsb" );
\r
205 __asm volatile( "isb" );
\r
208 /* Allow the application to define some post sleep processing. */
\r
209 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
211 /* Stop BURTC. Again, the time the SysTick is stopped for is accounted
\r
212 for as best it can be, but using the tickless mode will inevitably
\r
213 result in some tiny drift of the time maintained by the kernel with
\r
214 respect to calendar time. The count value is latched before stopping
\r
215 the timer as stopping the timer appears to clear the count. */
\r
216 ulCountAfterSleep = BURTC_CounterGet();
\r
217 BURTC_Enable( false );
\r
219 /* Re-enable interrupts - see comments above the INT_Enable() call
\r
222 __asm volatile( "dsb" );
\r
223 __asm volatile( "isb" );
\r
225 if( ulTickFlag != pdFALSE )
\r
227 /* The tick interrupt has already executed, although because this
\r
228 function is called with the scheduler suspended the actual tick
\r
229 processing will not occur until after this function has exited.
\r
230 Reset the reload value with whatever remains of this tick period. */
\r
231 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
232 BURTC_CompareSet( 0, ulReloadValue );
\r
234 /* The tick interrupt handler will already have pended the tick
\r
235 processing in the kernel. As the pending tick will be processed as
\r
236 soon as this function exits, the tick value maintained by the tick
\r
237 is stepped forward by one less than the time spent sleeping. The
\r
238 actual stepping of the tick appears later in this function. */
\r
239 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
243 /* Something other than the tick interrupt ended the sleep. How
\r
244 many complete tick periods passed while the processor was
\r
245 sleeping? Add back in the adjustment that was made to the reload
\r
246 value to account for the fact that a time slice was part way through
\r
247 when this function was called. */
\r
248 ulCountAfterSleep += ulCountBeforeSleep;
\r
249 ulCompleteTickPeriods = ulCountAfterSleep / ulReloadValueForOneTick;
\r
251 /* The reload value is set to whatever fraction of a single tick
\r
253 ulCountAfterSleep -= ( ulCompleteTickPeriods * ulReloadValueForOneTick );
\r
254 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
256 if( ulReloadValue == 0 )
\r
258 /* There is no fraction remaining. */
\r
259 ulReloadValue = ulReloadValueForOneTick;
\r
260 ulCompleteTickPeriods++;
\r
263 BURTC_CompareSet( 0, ulReloadValue );
\r
266 /* Restart the BURTC so it runs up to the alarm value. The alarm value
\r
267 will get set to the value required to generate exactly one tick period
\r
268 the next time the BURTC interrupt executes. */
\r
269 BURTC_Enable( true );
\r
271 /* Wind the tick forward by the number of tick periods that the CPU
\r
272 remained in a low power state. */
\r
273 vTaskStepTick( ulCompleteTickPeriods );
\r
276 /*-----------------------------------------------------------*/
\r
278 void BURTC_IRQHandler( void )
\r
280 ulTickFlag = pdTRUE;
\r
282 if( BURTC_CompareGet( 0 ) != ulReloadValueForOneTick )
\r
284 /* Set BURTC interrupt to one RTOS tick period. */
\r
285 BURTC_Enable( false );
\r
286 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
287 BURTC_Enable( true );
\r
290 BURTC_IntClear( _BURTC_IFC_MASK );
\r
292 /* Critical section which protect incrementing the tick. */
\r
293 portDISABLE_INTERRUPTS();
\r
295 if( xTaskIncrementTick() != pdFALSE )
\r
297 /* Pend a context switch. */
\r
298 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
301 portENABLE_INTERRUPTS();
\r
304 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r