]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CM0/port.c
Cortex M0 GCC/IAR/Keil ports -- tickless support.
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CM0 / port.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 /*-----------------------------------------------------------\r
29  * Implementation of functions defined in portable.h for the ARM CM0 port.\r
30  *----------------------------------------------------------*/\r
31 \r
32 /* IAR includes. */\r
33 #include "intrinsics.h"\r
34 \r
35 /* Scheduler includes. */\r
36 #include "FreeRTOS.h"\r
37 #include "task.h"\r
38 \r
39 /* Constants required to manipulate the NVIC. */\r
40 #define portNVIC_SYSTICK_CTRL_REG                       ( * ( ( volatile uint32_t * ) 0xe000e010 ) )\r
41 #define portNVIC_SYSTICK_LOAD_REG                       ( * ( ( volatile uint32_t * ) 0xe000e014 ) )\r
42 #define portNVIC_SYSTICK_CURRENT_VALUE_REG      ( * ( ( volatile uint32_t * ) 0xe000e018 ) )\r
43 #define portNVIC_INT_CTRL_REG                           ( * ( ( volatile uint32_t * ) 0xe000ed04 ) )\r
44 #define portNVIC_SYSPRI2_REG                            ( * ( ( volatile uint32_t *) 0xe000ed20 ) )\r
45 #define portNVIC_SYSTICK_CLK_BIT                        ( 1UL << 2UL )\r
46 #define portNVIC_SYSTICK_INT_BIT                        ( 1UL << 1UL )\r
47 #define portNVIC_SYSTICK_ENABLE_BIT                     ( 1UL << 0UL )\r
48 #define portNVIC_SYSTICK_COUNT_FLAG_BIT         ( 1UL << 16UL )\r
49 #define portMIN_INTERRUPT_PRIORITY      ( 255UL )\r
50 #define portNVIC_PENDSV_PRI                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
51 #define portNVIC_SYSTICK_PRI            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
52 \r
53 /* Constants required to set up the initial stack. */\r
54 #define portINITIAL_XPSR                        ( 0x01000000 )\r
55 \r
56 /* For backward compatibility, ensure configKERNEL_INTERRUPT_PRIORITY is\r
57 defined.  The value 255 should also ensure backward compatibility.\r
58 FreeRTOS.org versions prior to V4.3.0 did not include this definition. */\r
59 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
60         #define configKERNEL_INTERRUPT_PRIORITY 0\r
61 #endif\r
62 \r
63 /* Each task maintains its own interrupt status in the critical nesting\r
64 variable. */\r
65 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
66 \r
67 /* The systick is a 24-bit counter. */\r
68 #define portMAX_24_BIT_NUMBER                           ( 0xffffffUL )\r
69 \r
70 /* A fiddle factor to estimate the number of SysTick counts that would have\r
71 occurred while the SysTick counter is stopped during tickless idle\r
72 calculations. */\r
73 #ifndef portMISSED_COUNTS_FACTOR\r
74         #define portMISSED_COUNTS_FACTOR                        ( 45UL )\r
75 #endif\r
76 \r
77 /* The number of SysTick increments that make up one tick period. */\r
78 #if( configUSE_TICKLESS_IDLE == 1 )\r
79         static uint32_t ulTimerCountsForOneTick = 0;\r
80 #endif /* configUSE_TICKLESS_IDLE */\r
81 \r
82 /* The maximum number of tick periods that can be suppressed is limited by the\r
83 24 bit resolution of the SysTick timer. */\r
84 #if( configUSE_TICKLESS_IDLE == 1 )\r
85         static uint32_t xMaximumPossibleSuppressedTicks = 0;\r
86 #endif /* configUSE_TICKLESS_IDLE */\r
87 \r
88 /* Compensate for the CPU cycles that pass while the SysTick is stopped (low\r
89 power functionality only. */\r
90 #if( configUSE_TICKLESS_IDLE == 1 )\r
91         static uint32_t ulStoppedTimerCompensation = 0;\r
92 #endif /* configUSE_TICKLESS_IDLE */\r
93 \r
94 /*\r
95  * Setup the timer to generate the tick interrupts.\r
96  */\r
97 static void prvSetupTimerInterrupt( void );\r
98 \r
99 /*\r
100  * Exception handlers.\r
101  */\r
102 void xPortSysTickHandler( void );\r
103 \r
104 /*\r
105  * Start first task is a separate function so it can be tested in isolation.\r
106  */\r
107 extern void vPortStartFirstTask( void );\r
108 \r
109 /*\r
110  * Used to catch tasks that attempt to return from their implementing function.\r
111  */\r
112 static void prvTaskExitError( void );\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /*\r
117  * See header file for description.\r
118  */\r
119 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
120 {\r
121         /* Simulate the stack frame as it would be created by a context switch\r
122         interrupt. */\r
123         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
124         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
125         pxTopOfStack--;\r
126         *pxTopOfStack = ( StackType_t ) pxCode; /* PC */\r
127         pxTopOfStack--;\r
128         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* LR */\r
129         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
130         *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
131         pxTopOfStack -= 8; /* R11..R4. */\r
132 \r
133         return pxTopOfStack;\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 static void prvTaskExitError( void )\r
138 {\r
139         /* A function that implements a task must not exit or attempt to return to\r
140         its caller as there is nothing to return to.  If a task wants to exit it\r
141         should instead call vTaskDelete( NULL ).\r
142 \r
143         Artificially force an assert() to be triggered if configASSERT() is\r
144         defined, then stop here so application writers can catch the error. */\r
145         configASSERT( uxCriticalNesting == ~0UL );\r
146         portDISABLE_INTERRUPTS();\r
147         for( ;; );\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /*\r
152  * See header file for description.\r
153  */\r
154 BaseType_t xPortStartScheduler( void )\r
155 {\r
156         /* Make PendSV and SysTick the lowest priority interrupts. */\r
157         portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI;\r
158         portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI;\r
159 \r
160         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
161         here already. */\r
162         prvSetupTimerInterrupt();\r
163 \r
164         /* Initialise the critical nesting count ready for the first task. */\r
165         uxCriticalNesting = 0;\r
166 \r
167         /* Start the first task. */\r
168         vPortStartFirstTask();\r
169 \r
170         /* Should not get here! */\r
171         return 0;\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void vPortEndScheduler( void )\r
176 {\r
177         /* Not implemented in ports where there is nothing to return to.\r
178         Artificially force an assert. */\r
179         configASSERT( uxCriticalNesting == 1000UL );\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 void vPortYield( void )\r
184 {\r
185         /* Set a PendSV to request a context switch. */\r
186         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET;\r
187 \r
188         /* Barriers are normally not required but do ensure the code is completely\r
189         within the specified behaviour for the architecture. */\r
190         __DSB();\r
191         __ISB();\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vPortEnterCritical( void )\r
196 {\r
197         portDISABLE_INTERRUPTS();\r
198         uxCriticalNesting++;\r
199         __DSB();\r
200         __ISB();\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 void vPortExitCritical( void )\r
205 {\r
206         configASSERT( uxCriticalNesting );\r
207         uxCriticalNesting--;\r
208         if( uxCriticalNesting == 0 )\r
209         {\r
210                 portENABLE_INTERRUPTS();\r
211         }\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 void xPortSysTickHandler( void )\r
216 {\r
217 uint32_t ulPreviousMask;\r
218 \r
219         ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
220         {\r
221                 /* Increment the RTOS tick. */\r
222                 if( xTaskIncrementTick() != pdFALSE )\r
223                 {\r
224                         /* Pend a context switch. */\r
225                         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET;\r
226                 }\r
227         }\r
228         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 /*\r
233  * Setup the systick timer to generate the tick interrupts at the required\r
234  * frequency.\r
235  */\r
236 static void prvSetupTimerInterrupt( void )\r
237 {\r
238         /* Calculate the constants required to configure the tick interrupt. */\r
239         #if( configUSE_TICKLESS_IDLE == 1 )\r
240         {\r
241                 ulTimerCountsForOneTick = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );\r
242                 xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;\r
243                 ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR;\r
244         }\r
245         #endif /* configUSE_TICKLESS_IDLE */\r
246         \r
247         /* Stop and reset the SysTick. */\r
248         portNVIC_SYSTICK_CTRL_REG = 0UL;\r
249         portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
250 \r
251         /* Configure SysTick to interrupt at the requested rate. */\r
252         portNVIC_SYSTICK_LOAD_REG = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
253         portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 #if( configUSE_TICKLESS_IDLE == 1 )\r
258 \r
259 __weak void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
260 {\r
261 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;\r
262 TickType_t xModifiableIdleTime;\r
263 \r
264         /* Make sure the SysTick reload value does not overflow the counter. */\r
265         if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )\r
266         {\r
267                 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;\r
268         }\r
269 \r
270         /* Stop the SysTick momentarily.  The time the SysTick is stopped for\r
271         is accounted for as best it can be, but using the tickless mode will\r
272         inevitably result in some tiny drift of the time maintained by the\r
273         kernel with respect to calendar time. */\r
274         portNVIC_SYSTICK_CTRL_REG &= ~portNVIC_SYSTICK_ENABLE_BIT;\r
275 \r
276         /* Calculate the reload value required to wait xExpectedIdleTime\r
277         tick periods.  -1 is used because this code will execute part way\r
278         through one of the tick periods. */\r
279         ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );\r
280         if( ulReloadValue > ulStoppedTimerCompensation )\r
281         {\r
282                 ulReloadValue -= ulStoppedTimerCompensation;\r
283         }\r
284 \r
285         /* Enter a critical section but don't use the taskENTER_CRITICAL()\r
286         method as that will mask interrupts that should exit sleep mode. */\r
287         __disable_interrupt();\r
288         __DSB();\r
289         __ISB();\r
290 \r
291         /* If a context switch is pending or a task is waiting for the scheduler\r
292         to be unsuspended then abandon the low power entry. */\r
293         if( eTaskConfirmSleepModeStatus() == eAbortSleep )\r
294         {\r
295                 /* Restart from whatever is left in the count register to complete\r
296                 this tick period. */\r
297                 portNVIC_SYSTICK_LOAD_REG = portNVIC_SYSTICK_CURRENT_VALUE_REG;\r
298 \r
299                 /* Restart SysTick. */\r
300                 portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
301 \r
302                 /* Reset the reload register to the value required for normal tick\r
303                 periods. */\r
304                 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;\r
305 \r
306                 /* Re-enable interrupts - see comments above __disable_interrupt()\r
307                 call above. */\r
308                 __enable_interrupt();\r
309         }\r
310         else\r
311         {\r
312                 /* Set the new reload value. */\r
313                 portNVIC_SYSTICK_LOAD_REG = ulReloadValue;\r
314 \r
315                 /* Clear the SysTick count flag and set the count value back to\r
316                 zero. */\r
317                 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
318 \r
319                 /* Restart SysTick. */\r
320                 portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
321 \r
322                 /* Sleep until something happens.  configPRE_SLEEP_PROCESSING() can\r
323                 set its parameter to 0 to indicate that its implementation contains\r
324                 its own wait for interrupt or wait for event instruction, and so wfi\r
325                 should not be executed again.  However, the original expected idle\r
326                 time variable must remain unmodified, so a copy is taken. */\r
327                 xModifiableIdleTime = xExpectedIdleTime;\r
328                 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );\r
329                 if( xModifiableIdleTime > 0 )\r
330                 {\r
331                         __DSB();\r
332                         __WFI();\r
333                         __ISB();\r
334                 }\r
335                 configPOST_SLEEP_PROCESSING( xExpectedIdleTime );\r
336 \r
337                 /* Re-enable interrupts to allow the interrupt that brought the MCU\r
338                 out of sleep mode to execute immediately.  see comments above\r
339                 __disable_interrupt() call above. */\r
340                 __enable_interrupt();\r
341                 __DSB();\r
342                 __ISB();\r
343 \r
344                 /* Disable interrupts again because the clock is about to be stopped\r
345                 and interrupts that execute while the clock is stopped will increase\r
346                 any slippage between the time maintained by the RTOS and calendar\r
347                 time. */\r
348                 __disable_interrupt();\r
349                 __DSB();\r
350                 __ISB();\r
351 \r
352                 /* Disable the SysTick clock without reading the \r
353                 portNVIC_SYSTICK_CTRL_REG register to ensure the\r
354                 portNVIC_SYSTICK_COUNT_FLAG_BIT is not cleared if it is set.  Again, \r
355                 the time the SysTick is stopped for is accounted for as best it can \r
356                 be, but using the tickless mode will inevitably result in some tiny \r
357                 drift of the time maintained by the kernel with respect to calendar \r
358                 time*/\r
359                 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT );\r
360 \r
361                 /* Determine if the SysTick clock has already counted to zero and\r
362                 been set back to the current reload value (the reload back being\r
363                 correct for the entire expected idle time) or if the SysTick is yet\r
364                 to count to zero (in which case an interrupt other than the SysTick\r
365                 must have brought the system out of sleep mode). */\r
366                 if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )\r
367                 {\r
368                         uint32_t ulCalculatedLoadValue;\r
369 \r
370                         /* The tick interrupt is already pending, and the SysTick count\r
371                         reloaded with ulReloadValue.  Reset the\r
372                         portNVIC_SYSTICK_LOAD_REG with whatever remains of this tick\r
373                         period. */\r
374                         ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );\r
375 \r
376                         /* Don't allow a tiny value, or values that have somehow\r
377                         underflowed because the post sleep hook did something\r
378                         that took too long. */\r
379                         if( ( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )\r
380                         {\r
381                                 ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );\r
382                         }\r
383 \r
384                         portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;\r
385 \r
386                         /* As the pending tick will be processed as soon as this\r
387                         function exits, the tick value maintained by the tick is stepped\r
388                         forward by one less than the time spent waiting. */\r
389                         ulCompleteTickPeriods = xExpectedIdleTime - 1UL;\r
390                 }\r
391                 else\r
392                 {\r
393                         /* Something other than the tick interrupt ended the sleep.\r
394                         Work out how long the sleep lasted rounded to complete tick\r
395                         periods (not the ulReload value which accounted for part\r
396                         ticks). */\r
397                         ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG ;\r
398 \r
399                         /* How many complete tick periods passed while the processor\r
400                         was waiting? */\r
401                         ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;\r
402 \r
403                         /* The reload value is set to whatever fraction of a single tick\r
404                         period remains. */\r
405                         portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;\r
406                 }\r
407 \r
408                 /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG\r
409                 again, then set portNVIC_SYSTICK_LOAD_REG back to its standard\r
410                 value. */\r
411                 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
412                 portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
413                 vTaskStepTick( ulCompleteTickPeriods );\r
414                 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;\r
415 \r
416                 /* Exit with interrpts enabled. */\r
417                 __enable_interrupt();\r
418         }\r
419 }\r
420 \r
421 #endif /* configUSE_TICKLESS_IDLE */\r