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