]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_EFM32_Giant_Gecko_Simplicity_Studio/Low_Power_Demo/low_power_tick_management_BURTC.c
Replace Gecko Simplicity Studio project that had multiple build configurations with...
[freertos] / FreeRTOS / Demo / CORTEX_EFM32_Giant_Gecko_Simplicity_Studio / Low_Power_Demo / low_power_tick_management_BURTC.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 #warning Not functioning correctly above -O1 optimisation level.\r
71 \r
72 /* Standard includes. */\r
73 #include "limits.h"\r
74 \r
75 /* FreeRTOS includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 \r
79 /* SiLabs library includes. */\r
80 #include "em_cmu.h"\r
81 #include "em_burtc.h"\r
82 #include "em_rmu.h"\r
83 #include "em_int.h"\r
84 #include "sleep.h"\r
85 \r
86 /* SEE THE COMMENTS ABOVE THE DEFINITION OF configCREATE_LOW_POWER_DEMO IN\r
87 FreeRTOSConfig.h\r
88 This file contains functions that will override the default implementations\r
89 in the RTOS port layer.  Therefore only build this file if the low power demo\r
90 is being built. */\r
91 #if( configCREATE_LOW_POWER_DEMO == 1 )\r
92 \r
93 #define mainTIMER_FREQUENCY_HZ  ( 2000UL )\r
94 \r
95 /*\r
96  * The low power demo does not use the SysTick, so override the\r
97  * vPortSetupTickInterrupt() function with an implementation that configures\r
98  * a low power clock source.  NOTE:  This function name must not be changed as\r
99  * it is called from the RTOS portable layer.\r
100  */\r
101 void vPortSetupTimerInterrupt( void );\r
102 \r
103 /*\r
104  * Override the default definition of vPortSuppressTicksAndSleep() that is\r
105  * weakly defined in the FreeRTOS Cortex-M port layer with a version that\r
106  * manages the BURTC clock, as the tick is generated from the low power BURTC\r
107  * and not the SysTick as would normally be the case on a Cortex-M.\r
108  */\r
109 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /* Calculate how many clock increments make up a single tick period. */\r
114 static const uint32_t ulReloadValueForOneTick = ( mainTIMER_FREQUENCY_HZ / configTICK_RATE_HZ );\r
115 \r
116 /* Will hold the maximum number of ticks that can be suppressed. */\r
117 static uint32_t xMaximumPossibleSuppressedTicks = 0;\r
118 \r
119 /* Flag set from the tick interrupt to allow the sleep processing to know if\r
120 sleep mode was exited because of a timer interrupt or a different interrupt. */\r
121 static volatile uint32_t ulTickFlag = pdFALSE;\r
122 \r
123 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so\r
124 use zero - but leave the value here to assist porting to different clock\r
125 speeds. */\r
126 static const uint32_t ulStoppedTimerCompensation = 0UL;\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 void vPortSetupTimerInterrupt( void )\r
131 {\r
132 BURTC_Init_TypeDef xBURTCInitStruct = BURTC_INIT_DEFAULT;\r
133 \r
134         /* Configure the BURTC to generate the RTOS tick interrupt. */\r
135 \r
136         xMaximumPossibleSuppressedTicks = ULONG_MAX / ulReloadValueForOneTick;\r
137 \r
138         /* Ensure LE modules are accessible. */\r
139         CMU_ClockEnable( cmuClock_CORELE, true );\r
140 \r
141         /* Enable access to BURTC registers. */\r
142         RMU_ResetControl( rmuResetBU, false );\r
143 \r
144         /* Generate the tick interrupt from BURTC. */\r
145         xBURTCInitStruct.mode   = burtcModeEM3;         /* Operational in EM3. */\r
146         xBURTCInitStruct.clkSel = burtcClkSelULFRCO;/* ULFRCO clock. */\r
147         xBURTCInitStruct.clkDiv = burtcClkDiv_1;        /* 2kHz ULFRCO clock. */\r
148         xBURTCInitStruct.compare0Top = true;            /* Wrap on COMP0. */\r
149         BURTC_IntDisable( BURTC_IF_COMP0 );\r
150         BURTC_Init( &xBURTCInitStruct );\r
151 \r
152         /* The tick interrupt must be set to the lowest priority possible. */\r
153         NVIC_SetPriority( BURTC_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY );\r
154         NVIC_ClearPendingIRQ( BURTC_IRQn );\r
155         NVIC_EnableIRQ( BURTC_IRQn );\r
156         BURTC_CompareSet( 0, ulReloadValueForOneTick );\r
157         BURTC_IntClear( BURTC_IF_COMP0 );\r
158         BURTC_IntEnable( BURTC_IF_COMP0 );\r
159         BURTC_CounterReset();\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
164 {\r
165 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCountBeforeSleep, ulCountAfterSleep;\r
166 eSleepModeStatus eSleepAction;\r
167 TickType_t xModifiableIdleTime;\r
168 \r
169         /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */\r
170 \r
171         /* Make sure the BURTC reload value does not overflow the counter. */\r
172         if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )\r
173         {\r
174                 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;\r
175         }\r
176 \r
177         /* Calculate the reload value required to wait xExpectedIdleTime tick\r
178         periods. */\r
179         ulReloadValue = ulReloadValueForOneTick * xExpectedIdleTime;\r
180         if( ulReloadValue > ulStoppedTimerCompensation )\r
181         {\r
182                 /* Compensate for the fact that the BURTC is going to be stopped\r
183                 momentarily. */\r
184                 ulReloadValue -= ulStoppedTimerCompensation;\r
185         }\r
186 \r
187         /* Stop the BURTC momentarily.  The time the BURTC is stopped for is\r
188         accounted for as best it can be, but using the tickless mode will inevitably\r
189         result in some tiny drift of the time maintained by the kernel with respect\r
190         to calendar time.  The count is latched before stopping the timer as\r
191         stopping the timer appears to clear the count. */\r
192         ulCountBeforeSleep = BURTC_CounterGet();\r
193         BURTC_Enable( false );\r
194 \r
195         /* If this function is re-entered before one complete tick period then the\r
196         reload value might be set to take into account a partial time slice, but\r
197         just reading the count assumes it is counting up to a full ticks worth - so\r
198         add in the difference if any. */\r
199         ulCountBeforeSleep += ( ulReloadValueForOneTick - BURTC_CompareGet( 0 ) );\r
200 \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
203         INT_Disable();\r
204         __asm volatile( "dsb" );\r
205         __asm volatile( "isb" );\r
206 \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
211 \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
214         processing. */\r
215         eSleepAction = eTaskConfirmSleepModeStatus();\r
216         if( eSleepAction == eAbortSleep )\r
217         {\r
218                 /* Restart tick and count up to whatever was left of the current time\r
219                 slice. */\r
220                 BURTC_CompareSet( 0, ( ulReloadValueForOneTick - ulCountBeforeSleep ) + ulStoppedTimerCompensation );\r
221                 BURTC_Enable( true );\r
222 \r
223                 /* Re-enable interrupts - see comments above the cpsid instruction()\r
224                 above. */\r
225                 INT_Enable();\r
226         }\r
227         else\r
228         {\r
229                 /* Adjust the reload value to take into account that the current time\r
230                 slice is already partially complete. */\r
231                 ulReloadValue -= ulCountBeforeSleep;\r
232                 BURTC_CompareSet( 0, ulReloadValue );\r
233 \r
234                 /* Restart the BURTC. */\r
235                 BURTC_Enable( true );\r
236 \r
237                 /* Allow the application to define some pre-sleep processing. */\r
238                 xModifiableIdleTime = xExpectedIdleTime;\r
239                 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );\r
240 \r
241                 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()\r
242                 means the application defined code has already executed the WAIT\r
243                 instruction. */\r
244                 if( xModifiableIdleTime > 0 )\r
245                 {\r
246                         __asm volatile( "dsb" );\r
247                         SLEEP_Sleep();\r
248                         __asm volatile( "isb" );\r
249                 }\r
250 \r
251                 /* Allow the application to define some post sleep processing. */\r
252                 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );\r
253 \r
254                 /* Stop BURTC.  Again, the time the SysTick is stopped for is accounted\r
255                 for as best it can be, but using the tickless mode will inevitably\r
256                 result in some tiny drift of the time maintained by the kernel with\r
257                 respect to calendar time.  The count value is latched before stopping\r
258                 the timer as stopping the timer appears to clear the count. */\r
259                 ulCountAfterSleep = BURTC_CounterGet();\r
260                 BURTC_Enable( false );\r
261 \r
262                 /* Re-enable interrupts - see comments above the cpsid instruction()\r
263                 above. */\r
264                 INT_Enable();\r
265                 __asm volatile( "dsb" );\r
266                 __asm volatile( "isb" );\r
267 \r
268                 if( ulTickFlag != pdFALSE )\r
269                 {\r
270                         /* The tick interrupt has already executed, although because this\r
271                         function is called with the scheduler suspended the actual tick\r
272                         processing will not occur until after this function has exited.\r
273                         Reset the reload value with whatever remains of this tick period. */\r
274                         ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;\r
275                         BURTC_CompareSet( 0, ulReloadValue );\r
276 \r
277                         /* The tick interrupt handler will already have pended the tick\r
278                         processing in the kernel.  As the pending tick will be processed as\r
279                         soon as this function exits, the tick value     maintained by the tick\r
280                         is stepped forward by one less than the time spent sleeping.  The\r
281                         actual stepping of the tick appears later in this function. */\r
282                         ulCompleteTickPeriods = xExpectedIdleTime - 1UL;\r
283                 }\r
284                 else\r
285                 {\r
286                         /* Something other than the tick interrupt ended the sleep.  How\r
287                         many complete tick periods passed while the processor was\r
288                         sleeping?  Add back in the adjustment that was made to the reload\r
289                         value to account for the fact that a time slice was part way through\r
290                         when this function was called. */\r
291                         ulCountAfterSleep += ulCountBeforeSleep;\r
292                         ulCompleteTickPeriods = ulCountAfterSleep / ulReloadValueForOneTick;\r
293 \r
294                         /* The reload value is set to whatever fraction of a single tick\r
295                         period remains. */\r
296                         ulCountAfterSleep -= ( ulCompleteTickPeriods * ulReloadValueForOneTick );\r
297                         ulReloadValue = ulReloadValueForOneTick - ulCountAfterSleep;\r
298 \r
299                         if( ulReloadValue == 0 )\r
300                         {\r
301                                 /* There is no fraction remaining. */\r
302                                 ulReloadValue = ulReloadValueForOneTick;\r
303                                 ulCompleteTickPeriods++;\r
304                         }\r
305 \r
306                         BURTC_CompareSet( 0, ulReloadValue );\r
307                 }\r
308 \r
309                 /* Restart the BURTC so it runs up to the alarm value.  The alarm value\r
310                 will get set to the value required to generate exactly one tick period\r
311                 the next time the BURTC interrupt executes. */\r
312                 BURTC_Enable( true );\r
313 \r
314                 /* Wind the tick forward by the number of tick periods that the CPU\r
315                 remained in a low power state. */\r
316                 vTaskStepTick( ulCompleteTickPeriods );\r
317         }\r
318 }\r
319 /*-----------------------------------------------------------*/\r
320 \r
321 void BURTC_IRQHandler( void )\r
322 {\r
323         ulTickFlag = pdTRUE;\r
324 \r
325         if( BURTC_CompareGet( 0 ) != ulReloadValueForOneTick )\r
326         {\r
327                 /* Set BURTC interrupt to one RTOS tick period. */\r
328                 BURTC_Enable( false );\r
329                 BURTC_CompareSet( 0, ulReloadValueForOneTick );\r
330                 BURTC_Enable( true );\r
331         }\r
332 \r
333         BURTC_IntClear( _BURTC_IFC_MASK );\r
334 \r
335         /* Critical section which protect incrementing the tick. */\r
336         portDISABLE_INTERRUPTS();\r
337         {\r
338                 if( xTaskIncrementTick() != pdFALSE )\r
339                 {\r
340                         /* Pend a context switch. */\r
341                         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
342                 }\r
343         }\r
344         portENABLE_INTERRUPTS();\r
345 }\r
346 \r
347 #endif /* ( configCREATE_LOW_POWER_DEMO == 1 ) */\r
348 \r