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