2 * FreeRTOS Kernel V10.0.1
\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.
\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 includes. */
\r
31 /* FreeRTOS includes. */
\r
32 #include "FreeRTOS.h"
\r
35 /* SiLabs library includes. */
\r
37 #include "em_burtc.h"
\r
42 /* SEE THE COMMENTS ABOVE THE DEFINITION OF configCREATE_LOW_POWER_DEMO IN
\r
44 This file contains functions that will override the default implementations
\r
45 in the RTOS port layer. Therefore only build this file if the low power demo
\r
47 #if( configCREATE_LOW_POWER_DEMO == 1 )
\r
49 #define mainTIMER_FREQUENCY_HZ ( 2000UL )
\r
52 * The low power demo does not use the SysTick, so override the
\r
53 * vPortSetupTickInterrupt() function with an implementation that configures
\r
54 * a low power clock source. NOTE: This function name must not be changed as
\r
55 * it is called from the RTOS portable layer.
\r
57 void vPortSetupTimerInterrupt( void );
\r
60 * Override the default definition of vPortSuppressTicksAndSleep() that is
\r
61 * weakly defined in the FreeRTOS Cortex-M port layer with a version that
\r
62 * manages the BURTC clock, as the tick is generated from the low power BURTC
\r
63 * and not the SysTick as would normally be the case on a Cortex-M.
\r
65 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
\r
67 /*-----------------------------------------------------------*/
\r
69 /* Calculate how many clock increments make up a single tick period. */
\r
70 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );
\r
72 /* Will hold the maximum number of ticks that can be suppressed. */
\r
73 static uint32_t xMaximumPossibleSuppressedTicks = 0;
\r
75 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
76 sleep mode was exited because of a timer interrupt or a different interrupt. */
\r
77 static volatile uint32_t ulTickFlag = pdFALSE;
\r
79 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so
\r
80 use zero - but leave the value here to assist porting to different clock
\r
82 static const uint32_t ulStoppedTimerCompensation = 0UL;
\r
84 /*-----------------------------------------------------------*/
\r
86 void vPortSetupTimerInterrupt( void )
\r
88 BURTC_Init_TypeDef xBURTCInitStruct = BURTC_INIT_DEFAULT;
\r
90 /* Configure the BURTC to generate the RTOS tick interrupt. */
\r
92 xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;
\r
94 /* Ensure LE modules are accessible. */
\r
95 CMU_ClockEnable( cmuClock_CORELE, true );
\r
97 /* Enable access to BURTC registers. */
\r
98 RMU_ResetControl( rmuResetBU, false );
\r
100 /* Generate the tick interrupt from BURTC. */
\r
101 xBURTCInitStruct.mode = burtcModeEM3; /* Operational in EM3. */
\r
102 xBURTCInitStruct.clkSel = burtcClkSelULFRCO;/* ULFRCO clock. */
\r
103 xBURTCInitStruct.clkDiv = burtcClkDiv_1; /* 2kHz ULFRCO clock. */
\r
104 xBURTCInitStruct.compare0Top = true; /* Wrap on COMP0. */
\r
105 BURTC_IntDisable( BURTC_IF_COMP0 );
\r
106 BURTC_Init( &xBURTCInitStruct );
\r
108 /* The tick interrupt must be set to the lowest priority possible. */
\r
109 NVIC_SetPriority( BURTC_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY );
\r
110 NVIC_ClearPendingIRQ( BURTC_IRQn );
\r
111 NVIC_EnableIRQ( BURTC_IRQn );
\r
112 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
113 BURTC_IntClear( BURTC_IF_COMP0 );
\r
114 BURTC_IntEnable( BURTC_IF_COMP0 );
\r
115 BURTC_CounterReset();
\r
117 /*-----------------------------------------------------------*/
\r
119 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
121 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCountBeforeSleep, ulCountAfterSleep;
\r
122 eSleepModeStatus eSleepAction;
\r
123 TickType_t xModifiableIdleTime;
\r
125 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
127 /* Make sure the BURTC reload value does not overflow the counter. */
\r
128 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
130 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
133 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
135 ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;
\r
136 if( ulReloadValue > ulStoppedTimerCompensation )
\r
138 /* Compensate for the fact that the BURTC is going to be stopped
\r
140 ulReloadValue -= ulStoppedTimerCompensation;
\r
143 /* Stop the BURTC momentarily. The time the BURTC is stopped for is
\r
144 accounted for as best it can be, but using the tickless mode will inevitably
\r
145 result in some tiny drift of the time maintained by the kernel with respect
\r
146 to calendar time. The count is latched before stopping the timer as
\r
147 stopping the timer appears to clear the count. */
\r
148 ulCountBeforeSleep = BURTC_CounterGet();
\r
149 BURTC_Enable( false );
\r
151 /* If this function is re-entered before one complete tick period then the
\r
152 reload value might be set to take into account a partial time slice, but
\r
153 just reading the count assumes it is counting up to a full ticks worth - so
\r
154 add in the difference if any. */
\r
155 ulCountBeforeSleep += ( ulReloadValueForOneTick - BURTC_CompareGet( 0 ) );
\r
157 /* Enter a critical section but don't use the taskENTER_CRITICAL() method as
\r
158 that will mask interrupts that should exit sleep mode. */
\r
160 __asm volatile( "dsb" );
\r
161 __asm volatile( "isb" );
\r
163 /* The tick flag is set to false before sleeping. If it is true when sleep
\r
164 mode is exited then sleep mode was probably exited because the tick was
\r
165 suppressed for the entire xExpectedIdleTime period. */
\r
166 ulTickFlag = pdFALSE;
\r
168 /* If a context switch is pending then abandon the low power entry as the
\r
169 context switch might have been pended by an external interrupt that requires
\r
171 eSleepAction = eTaskConfirmSleepModeStatus();
\r
172 if( eSleepAction == eAbortSleep )
\r
174 /* Restart tick and count up to whatever was left of the current time
\r
176 BURTC_CompareSet( 0, ( ulReloadValueForOneTick - ulCountBeforeSleep ) + ulStoppedTimerCompensation );
\r
177 BURTC_Enable( true );
\r
179 /* Re-enable interrupts - see comments above the INT_Enable() call
\r
185 /* Adjust the reload value to take into account that the current time
\r
186 slice is already partially complete. */
\r
187 ulReloadValue -= ulCountBeforeSleep;
\r
188 BURTC_CompareSet( 0, ulReloadValue );
\r
190 /* Restart the BURTC. */
\r
191 BURTC_Enable( true );
\r
193 /* Allow the application to define some pre-sleep processing. */
\r
194 xModifiableIdleTime = xExpectedIdleTime;
\r
195 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
\r
197 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
198 means the application defined code has already executed the WAIT
\r
200 if( xModifiableIdleTime > 0 )
\r
202 __asm volatile( "dsb" );
\r
204 __asm volatile( "isb" );
\r
207 /* Allow the application to define some post sleep processing. */
\r
208 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );
\r
210 /* Stop BURTC. Again, the time the SysTick is stopped for is accounted
\r
211 for as best it can be, but using the tickless mode will inevitably
\r
212 result in some tiny drift of the time maintained by the kernel with
\r
213 respect to calendar time. The count value is latched before stopping
\r
214 the timer as stopping the timer appears to clear the count. */
\r
215 ulCountAfterSleep = BURTC_CounterGet();
\r
216 BURTC_Enable( false );
\r
218 /* Re-enable interrupts - see comments above the INT_Enable() call
\r
221 __asm volatile( "dsb" );
\r
222 __asm volatile( "isb" );
\r
224 if( ulTickFlag != pdFALSE )
\r
226 /* The tick interrupt has already executed, although because this
\r
227 function is called with the scheduler suspended the actual tick
\r
228 processing will not occur until after this function has exited.
\r
229 Reset the reload value with whatever remains of this tick period. */
\r
230 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
231 BURTC_CompareSet( 0, ulReloadValue );
\r
233 /* The tick interrupt handler will already have pended the tick
\r
234 processing in the kernel. As the pending tick will be processed as
\r
235 soon as this function exits, the tick value maintained by the tick
\r
236 is stepped forward by one less than the time spent sleeping. The
\r
237 actual stepping of the tick appears later in this function. */
\r
238 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
242 /* Something other than the tick interrupt ended the sleep. How
\r
243 many complete tick periods passed while the processor was
\r
244 sleeping? Add back in the adjustment that was made to the reload
\r
245 value to account for the fact that a time slice was part way through
\r
246 when this function was called. */
\r
247 ulCountAfterSleep += ulCountBeforeSleep;
\r
248 ulCompleteTickPeriods = ulCountAfterSleep / ulReloadValueForOneTick;
\r
250 /* The reload value is set to whatever fraction of a single tick
\r
252 ulCountAfterSleep -= ( ulCompleteTickPeriods * ulReloadValueForOneTick );
\r
253 ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;
\r
255 if( ulReloadValue == 0 )
\r
257 /* There is no fraction remaining. */
\r
258 ulReloadValue = ulReloadValueForOneTick;
\r
259 ulCompleteTickPeriods++;
\r
262 BURTC_CompareSet( 0, ulReloadValue );
\r
265 /* Restart the BURTC so it runs up to the alarm value. The alarm value
\r
266 will get set to the value required to generate exactly one tick period
\r
267 the next time the BURTC interrupt executes. */
\r
268 BURTC_Enable( true );
\r
270 /* Wind the tick forward by the number of tick periods that the CPU
\r
271 remained in a low power state. */
\r
272 vTaskStepTick( ulCompleteTickPeriods );
\r
275 /*-----------------------------------------------------------*/
\r
277 void BURTC_IRQHandler( void )
\r
279 ulTickFlag = pdTRUE;
\r
281 if( BURTC_CompareGet( 0 ) != ulReloadValueForOneTick )
\r
283 /* Set BURTC interrupt to one RTOS tick period. */
\r
284 BURTC_Enable( false );
\r
285 BURTC_CompareSet( 0, ulReloadValueForOneTick );
\r
286 BURTC_Enable( true );
\r
289 BURTC_IntClear( _BURTC_IFC_MASK );
\r
291 /* Critical section which protect incrementing the tick. */
\r
292 portDISABLE_INTERRUPTS();
\r
294 if( xTaskIncrementTick() != pdFALSE )
\r
296 /* Pend a context switch. */
\r
297 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
300 portENABLE_INTERRUPTS();
\r
303 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */
\r