]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_CEC1302_MikroC/main_low_power/low_power_tick_config.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_M4F_CEC1302_MikroC / main_low_power / low_power_tick_config.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 /* Library includes. */\r
37 #include "htimer.h"\r
38 \r
39 /* This file contains functions that will override the default implementations\r
40 in the RTOS port layer.  Therefore only build this file if the low power demo\r
41 is being built. */\r
42 #if( configCREATE_LOW_POWER_DEMO == 1 )\r
43 \r
44 /* ID of the hibernation timer used to generate the tick. */\r
45 #define mainTICK_HTIMER_ID      0\r
46 \r
47 /* Written to the hibernation timer control register to configure the timer for\r
48 its higher resolution. */\r
49 #define mainHTIMER_HIGH_RESOLUTION      0\r
50 \r
51 /* The frequency of the hibernation timer when it is running at its higher\r
52 resolution and low resolution respectively. */\r
53 #define mainHIGHER_RESOLUTION_TIMER_HZ  ( 32787 ) /* (1000000us / 30.5us) as each LSB is 30.5us. */\r
54 #define mainLOW_RESOLUTION_TIMER_HZ             ( 8UL )  /* ( 1000ms / 125ms ) as each LSB is 0.125s. */\r
55 \r
56 /* Some registers are accessed directly as the library is not compatible with\r
57 all the compilers used. */\r
58 #define lpHTIMER_PRELOAD_REGISTER               ( * ( volatile uint16_t * ) 0x40009800 )\r
59 #define lpHTIMER_COUNT_REGISTER                 ( * ( volatile uint16_t * ) 0x40009808 )\r
60 #define lpEC_GIRQ17_ENABLE_SET                  ( * ( volatile uint32_t * ) 0x4000C0B8 )\r
61 #define lpHTIMER_INTERRUPT_CONTROL_BIT  ( 1UL << 20UL )\r
62 \r
63 /*\r
64  * The low power demo does not use the SysTick, so override the\r
65  * vPortSetupTickInterrupt() function with an implementation that configures\r
66  * the low power clock.  NOTE:  This function name must not be changed as it\r
67  * is called from the RTOS portable layer.\r
68  */\r
69 void vPortSetupTimerInterrupt( void );\r
70 \r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /* The reload value to use in the timer to generate the tick interrupt -\r
74 assumes the timer is running at its higher resolution. */\r
75 static const uint16_t usHighResolutionReloadValue = ( mainHIGHER_RESOLUTION_TIMER_HZ / ( uint16_t ) configTICK_RATE_HZ );\r
76 \r
77 /* Calculate how many clock increments make up a single tick period. */\r
78 static const uint32_t ulReloadValueForOneHighResolutionTick = ( mainHIGHER_RESOLUTION_TIMER_HZ / configTICK_RATE_HZ );\r
79 \r
80 /* Calculate the maximum number of ticks that can be suppressed when using the\r
81 high resolution clock and low resolution clock respectively. */\r
82 static uint32_t ulMaximumPossibleSuppressedHighResolutionTicks = 0;\r
83 \r
84 /* As the clock is only 2KHz, it is likely a value of 1 will be too much, so\r
85 use zero - but leave the value here to assist porting to different clock\r
86 speeds. */\r
87 static const uint32_t ulStoppedTimerCompensation = 0UL;\r
88 \r
89 /* Flag set from the tick interrupt to allow the sleep processing to know if\r
90 sleep mode was exited because of an timer interrupt or a different interrupt. */\r
91 static volatile uint32_t ulTickFlag = pdFALSE;\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 void NVIC_Handler_HIB_TMR( void ) iv IVT_INT_HTIMER ics ICS_AUTO\r
96 {\r
97         lpHTIMER_PRELOAD_REGISTER = usHighResolutionReloadValue;\r
98 \r
99         /* Increment the RTOS tick. */\r
100         if( xTaskIncrementTick() != pdFALSE )\r
101         {\r
102                 /* A context switch is required.  Context switching is performed in\r
103                 the PendSV interrupt.  Pend the PendSV interrupt. */\r
104                 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
105         }\r
106 \r
107         /* The CPU woke because of a tick. */\r
108         ulTickFlag = pdTRUE;\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 void vPortSetupTimerInterrupt( void )\r
113 {\r
114         /* Cannot be a const when using the MikroC compiler. */\r
115         ulMaximumPossibleSuppressedHighResolutionTicks = ( ( uint32_t ) USHRT_MAX ) / ulReloadValueForOneHighResolutionTick;\r
116 \r
117         /* Set up the hibernation timer to start at the value required by the\r
118         tick interrupt. */\r
119         htimer_enable( mainTICK_HTIMER_ID, usHighResolutionReloadValue, mainHTIMER_HIGH_RESOLUTION );\r
120 \r
121         /* Enable the HTIMER interrupt.  Equivalent to enable_htimer0_irq(); */\r
122         lpEC_GIRQ17_ENABLE_SET |= lpHTIMER_INTERRUPT_CONTROL_BIT;\r
123 \r
124         /* The hibernation timer is not an auto-reload timer, so gets reset\r
125         from within the ISR itself.  For that reason it's interrupt is set\r
126         to the highest possible priority to ensure clock slippage is minimised. */\r
127         NVIC_SetIntPriority( IVT_INT_HTIMER, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
128         NVIC_IntEnable( IVT_INT_HTIMER );\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 /* Override the default definition of vPortSuppressTicksAndSleep() that is\r
133 weakly defined in the FreeRTOS Cortex-M port layer with a version that manages\r
134 the hibernation timer, as the tick is generated from the low power hibernation\r
135 timer and not the SysTick as would normally be the case on a Cortex-M. */\r
136 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
137 {\r
138 uint32_t ulCompleteTickPeriods, ulReloadValue, ulCompletedTimerDecrements, ulCountAfterSleep, ulCountBeforeSleep;\r
139 eSleepModeStatus eSleepAction;\r
140 TickType_t xModifiableIdleTime;\r
141 \r
142         /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */\r
143 \r
144         /* Make sure the hibernation timer reload value does not overflow the\r
145         counter. */\r
146         if( xExpectedIdleTime > ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks )\r
147         {\r
148                 xExpectedIdleTime = ( TickType_t ) ulMaximumPossibleSuppressedHighResolutionTicks;\r
149         }\r
150 \r
151         /* Stop the timer momentarily.  The time the timer is stopped for is\r
152         accounted for as best it can be, but using the tickless mode will\r
153         inevitably result in some tiny drift of the time maintained by the kernel\r
154         with respect to calendar time.  Take the count value first as clearing\r
155         the preload value also seems to clear the count. */\r
156         ulCountBeforeSleep = ( uint32_t ) lpHTIMER_COUNT_REGISTER;\r
157         lpHTIMER_PRELOAD_REGISTER = 0;\r
158 \r
159         /* Calculate the reload value required to wait xExpectedIdleTime tick\r
160         periods.  -1 is used as the current time slice will already be part way\r
161         through, the part value coming from the current timer count value. */\r
162         ulReloadValue = ulCountBeforeSleep + ( ulReloadValueForOneHighResolutionTick * ( xExpectedIdleTime - 1UL ) );\r
163 \r
164         if( ulReloadValue > ulStoppedTimerCompensation )\r
165         {\r
166                 /* Compensate for the fact that the timer is going to be stopped\r
167                 momentarily. */\r
168                 ulReloadValue -= ulStoppedTimerCompensation;\r
169         }\r
170 \r
171         /* Enter a critical section but don't use the taskENTER_CRITICAL() method as\r
172         that will mask interrupts that should exit sleep mode. */\r
173         __asm { cpsid i\r
174                         dsb\r
175                         isb };\r
176 \r
177         /* The tick flag is set to false before sleeping.  If it is true when sleep\r
178         mode is exited then sleep mode was probably exited because the tick was\r
179         suppressed for the entire xExpectedIdleTime period. */\r
180         ulTickFlag = pdFALSE;\r
181 \r
182         /* If a context switch is pending then abandon the low power entry as\r
183         the context switch might have been pended by an external interrupt that\r
184         requires processing. */\r
185         eSleepAction = eTaskConfirmSleepModeStatus();\r
186         if( eSleepAction == eAbortSleep )\r
187         {\r
188                 /* Resetart the timer from whatever remains in the counter register,\r
189                 but 0 is not a valid value. */\r
190                 ulReloadValue = ulCountBeforeSleep - ulStoppedTimerCompensation;\r
191 \r
192                 if( ulReloadValue == 0 )\r
193                 {\r
194                         ulReloadValue = ulReloadValueForOneHighResolutionTick;\r
195                         ulCompleteTickPeriods = 1UL;\r
196                 }\r
197                 else\r
198                 {\r
199                         ulCompleteTickPeriods = 0UL;\r
200                 }\r
201 \r
202                 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;\r
203 \r
204                 /* Re-enable interrupts - see comments above the cpsid instruction()\r
205                 above. */\r
206                 __asm { cpsie i\r
207                                 dsb\r
208                                 isb };\r
209 \r
210         }\r
211         else\r
212         {\r
213                 /* Write the calculated reload value, which will also start the\r
214                 timer. */\r
215                 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;\r
216 \r
217                 /* Allow the application to define some pre-sleep processing. */\r
218                 xModifiableIdleTime = xExpectedIdleTime;\r
219                 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );\r
220 \r
221                 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()\r
222                 means the application defined code has already executed the sleep\r
223                 instructions. */\r
224                 if( xModifiableIdleTime > 0 )\r
225                 {\r
226                         __asm { dsb\r
227                                         wfi\r
228                                         isb };\r
229                 }\r
230 \r
231                 /* Allow the application to define some post sleep processing. */\r
232                 configPOST_SLEEP_PROCESSING( xModifiableIdleTime );\r
233 \r
234                 /* Stop the hibernation timer.  Again, the time the tiemr is stopped\r
235                 for is accounted for as best it can be, but using the tickless mode\r
236                 will inevitably result in some tiny drift of the time maintained by the\r
237                 kernel with respect to calendar time.  Take the count value first as\r
238                 setting the preload to zero also seems to clear the count. */\r
239                 ulCountAfterSleep = ( uint32_t ) lpHTIMER_COUNT_REGISTER;\r
240                 lpHTIMER_PRELOAD_REGISTER = 0;\r
241 \r
242                 /* Re-enable interrupts - see comments above the cpsid instruction()\r
243                 above. */\r
244                 __asm { cpsie i\r
245                                 dsb\r
246                                 isb };\r
247 \r
248 \r
249                 if( ulTickFlag != pdFALSE )\r
250                 {\r
251                         /* The tick interrupt has already executed, although because this\r
252                         function is called with the scheduler suspended the actual tick\r
253                         processing will not occur until after this function has exited.\r
254                         The timer has already been reloaded to count in ticks, and can just\r
255                         continue counting down from its current value. */\r
256                         ulReloadValue = ulCountAfterSleep;\r
257 \r
258                         /* Sanity check that the timer's reload value has indeed been\r
259                         reset. */\r
260                         configASSERT( ( uint32_t ) lpHTIMER_PRELOAD_REGISTER == ulReloadValueForOneHighResolutionTick );\r
261 \r
262                         /* The tick interrupt handler will already have pended the tick\r
263                         processing in the kernel.  As the pending tick will be processed as\r
264                         soon as this function exits, the tick value     maintained by the tick\r
265                         is stepped forward by one less than the time spent sleeping.  The\r
266                         actual stepping of the tick appears later in this function. */\r
267                         ulCompleteTickPeriods = xExpectedIdleTime - 1UL;\r
268                 }\r
269                 else\r
270                 {\r
271                         /* Something other than the tick interrupt ended the sleep.  How\r
272                         many complete tick periods passed while the processor was\r
273                         sleeping? */\r
274                         ulCompletedTimerDecrements = ulReloadValue - ulCountAfterSleep;\r
275 \r
276                         /* Undo the adjustment that was made to the reload value to account\r
277                         for the fact that a time slice was part way through when this\r
278                         function was called before working out how many complete tick\r
279                         periods this represents.  (could have used [ulExpectedIdleTime *\r
280                         ulReloadValueForOneHighResolutionTick] instead of ulReloadValue on\r
281                         the previous line, but this way avoids the multiplication). */\r
282                         ulCompletedTimerDecrements += ( ulReloadValueForOneHighResolutionTick - ulCountBeforeSleep );\r
283                         ulCompleteTickPeriods = ulCompletedTimerDecrements / ulReloadValueForOneHighResolutionTick;\r
284 \r
285                         /* The reload value is set to whatever fraction of a single tick\r
286                         period remains. */\r
287                         ulReloadValue = ( ( ulCompleteTickPeriods + 1UL ) * ulReloadValueForOneHighResolutionTick ) - ulCompletedTimerDecrements;\r
288                 }\r
289 \r
290                 /* Cannot use a reload value of 0 - it will not start the timer. */\r
291                 if( ulReloadValue == 0 )\r
292                 {\r
293                         /* There is no fraction remaining. */\r
294                         ulReloadValue = ulReloadValueForOneHighResolutionTick;\r
295                         ulCompleteTickPeriods++;\r
296                 }\r
297 \r
298                 /* Restart the timer so it runs down from the reload value.  The reload\r
299                 value will get set to the value required to generate exactly one tick\r
300                 period the next time the tick interrupt executes. */\r
301                 lpHTIMER_PRELOAD_REGISTER = ( uint16_t ) ulReloadValue;\r
302         }\r
303 \r
304         /* Wind the tick forward by the number of tick periods that the CPU\r
305         remained in a low power state. */\r
306         vTaskStepTick( ulCompleteTickPeriods );\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 \r
311 #endif /* configCREATE_LOW_POWER_DEMO */\r
312 \r