]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_EFM32_Giant_Gecko_Simplicity_Studio/Low_Power_Demo/low_power_tick_management_BURTC.c
4f7ac1ccf9d9bdc7e9cf56f3ecdf107d04c5cecf
[freertos] / FreeRTOS / Demo / CORTEX_EFM32_Giant_Gecko_Simplicity_Studio / Low_Power_Demo / low_power_tick_management_BURTC.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard includes. */\r
29 #include "limits.h"\r
30 \r
31 /* FreeRTOS includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 \r
35 /* SiLabs library includes. */\r
36 #include "em_cmu.h"\r
37 #include "em_burtc.h"\r
38 #include "em_rmu.h"\r
39 #include "em_int.h"\r
40 #include "sleep.h"\r
41 \r
42 /* SEE THE COMMENTS ABOVE THE DEFINITION OF configCREATE_LOW_POWER_DEMO IN\r
43 FreeRTOSConfig.h\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
46 is being built. */\r
47 #if( configCREATE_LOW_POWER_DEMO == 1 )\r
48 \r
49 #define mainTIMER_FREQUENCY_HZ  ( 2000UL )\r
50 \r
51 /*\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
56  */\r
57 void vPortSetupTimerInterrupt( void );\r
58 \r
59 /*\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
64  */\r
65 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \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
71 \r
72 /* Will hold the maximum number of ticks that can be suppressed. */\r
73 static uint32_t xMaximumPossibleSuppressedTicks = 0;\r
74 \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
78 \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
81 speeds. */\r
82 static const uint32_t ulStoppedTimerCompensation = 0UL;\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 void vPortSetupTimerInterrupt( void )\r
87 {\r
88 BURTC_Init_TypeDef xBURTCInitStruct = BURTC_INIT_DEFAULT;\r
89 \r
90         /* Configure the BURTC to generate the RTOS tick interrupt. */\r
91 \r
92         xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;\r
93 \r
94         /* Ensure LE modules are accessible. */\r
95         CMU_ClockEnable( cmuClock_CORELE, true );\r
96 \r
97         /* Enable access to BURTC registers. */\r
98         RMU_ResetControl( rmuResetBU, false );\r
99 \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
107 \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
116 }\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
120 {\r
121 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCountBeforeSleep, ulCountAfterSleep;\r
122 eSleepModeStatus eSleepAction;\r
123 TickType_t xModifiableIdleTime;\r
124 \r
125         /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */\r
126 \r
127         /* Make sure the BURTC reload value does not overflow the counter. */\r
128         if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )\r
129         {\r
130                 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;\r
131         }\r
132 \r
133         /* Calculate the reload value required to wait xExpectedIdleTime tick\r
134         periods. */\r
135         ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;\r
136         if( ulReloadValue > ulStoppedTimerCompensation )\r
137         {\r
138                 /* Compensate for the fact that the BURTC is going to be stopped\r
139                 momentarily. */\r
140                 ulReloadValue -= ulStoppedTimerCompensation;\r
141         }\r
142 \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
150 \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
156 \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
159         INT_Disable();\r
160         __asm volatile( "dsb" );\r
161         __asm volatile( "isb" );\r
162 \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
167 \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
170         processing. */\r
171         eSleepAction = eTaskConfirmSleepModeStatus();\r
172         if( eSleepAction == eAbortSleep )\r
173         {\r
174                 /* Restart tick and count up to whatever was left of the current time\r
175                 slice. */\r
176                 BURTC_CompareSet( 0, ( ulReloadValueForOneTick - ulCountBeforeSleep ) + ulStoppedTimerCompensation );\r
177                 BURTC_Enable( true );\r
178 \r
179                 /* Re-enable interrupts - see comments above the INT_Enable() call\r
180                 above. */\r
181                 INT_Enable();\r
182         }\r
183         else\r
184         {\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
189 \r
190                 /* Restart the BURTC. */\r
191                 BURTC_Enable( true );\r
192 \r
193                 /* Allow the application to define some pre-sleep processing. */\r
194                 xModifiableIdleTime = xExpectedIdleTime;\r
195                 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );\r
196 \r
197                 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()\r
198                 means the application defined code has already executed the WAIT\r
199                 instruction. */\r
200                 if( xModifiableIdleTime > 0 )\r
201                 {\r
202                         __asm volatile( "dsb" );\r
203                         SLEEP_Sleep();\r
204                         __asm volatile( "isb" );\r
205                 }\r
206 \r
207                 /* Allow the application to define some post sleep processing. */\r
208                 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );\r
209 \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
217 \r
218                 /* Re-enable interrupts - see comments above the INT_Enable() call\r
219                 above. */\r
220                 INT_Enable();\r
221                 __asm volatile( "dsb" );\r
222                 __asm volatile( "isb" );\r
223 \r
224                 if( ulTickFlag != pdFALSE )\r
225                 {\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
232 \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
239                 }\r
240                 else\r
241                 {\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
249 \r
250                         /* The reload value is set to whatever fraction of a single tick\r
251                         period remains. */\r
252                         ulCountAfterSleep -= ( ulCompleteTickPeriods * ulReloadValueForOneTick );\r
253                         ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;\r
254 \r
255                         if( ulReloadValue == 0 )\r
256                         {\r
257                                 /* There is no fraction remaining. */\r
258                                 ulReloadValue = ulReloadValueForOneTick;\r
259                                 ulCompleteTickPeriods++;\r
260                         }\r
261 \r
262                         BURTC_CompareSet( 0, ulReloadValue );\r
263                 }\r
264 \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
269 \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
273         }\r
274 }\r
275 /*-----------------------------------------------------------*/\r
276 \r
277 void BURTC_IRQHandler( void )\r
278 {\r
279         ulTickFlag = pdTRUE;\r
280 \r
281         if( BURTC_CompareGet( 0 ) != ulReloadValueForOneTick )\r
282         {\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
287         }\r
288 \r
289         BURTC_IntClear( _BURTC_IFC_MASK );\r
290 \r
291         /* Critical section which protect incrementing the tick. */\r
292         portDISABLE_INTERRUPTS();\r
293         {\r
294                 if( xTaskIncrementTick() != pdFALSE )\r
295                 {\r
296                         /* Pend a context switch. */\r
297                         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
298                 }\r
299         }\r
300         portENABLE_INTERRUPTS();\r
301 }\r
302 \r
303 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */\r
304 \r