]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CM0/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CM0 / port.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 /*-----------------------------------------------------------\r
30  * Implementation of functions defined in portable.h for the ARM CM0 port.\r
31  *----------------------------------------------------------*/\r
32 \r
33 /* IAR includes. */\r
34 #include "intrinsics.h"\r
35 \r
36 /* Scheduler includes. */\r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 \r
40 /* Constants required to manipulate the NVIC. */\r
41 #define portNVIC_SYSTICK_CTRL                   ( ( volatile uint32_t * ) 0xe000e010 )\r
42 #define portNVIC_SYSTICK_LOAD                   ( ( volatile uint32_t * ) 0xe000e014 )\r
43 #define portNVIC_SYSTICK_CURRENT_VALUE  ( ( volatile uint32_t * ) 0xe000e018 )\r
44 #define portNVIC_SYSPRI2                        ( ( volatile uint32_t *) 0xe000ed20 )\r
45 #define portNVIC_SYSTICK_CLK            0x00000004\r
46 #define portNVIC_SYSTICK_INT            0x00000002\r
47 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
48 #define portMIN_INTERRUPT_PRIORITY      ( 255UL )\r
49 #define portNVIC_PENDSV_PRI                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
50 #define portNVIC_SYSTICK_PRI            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
51 \r
52 /* Constants required to set up the initial stack. */\r
53 #define portINITIAL_XPSR                        ( 0x01000000 )\r
54 \r
55 /* For backward compatibility, ensure configKERNEL_INTERRUPT_PRIORITY is\r
56 defined.  The value 255 should also ensure backward compatibility.\r
57 FreeRTOS.org versions prior to V4.3.0 did not include this definition. */\r
58 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
59         #define configKERNEL_INTERRUPT_PRIORITY 0\r
60 #endif\r
61 \r
62 /* Each task maintains its own interrupt status in the critical nesting\r
63 variable. */\r
64 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
65 \r
66 /*\r
67  * Setup the timer to generate the tick interrupts.\r
68  */\r
69 static void prvSetupTimerInterrupt( void );\r
70 \r
71 /*\r
72  * Exception handlers.\r
73  */\r
74 void xPortSysTickHandler( void );\r
75 \r
76 /*\r
77  * Start first task is a separate function so it can be tested in isolation.\r
78  */\r
79 extern void vPortStartFirstTask( void );\r
80 \r
81 /*\r
82  * Used to catch tasks that attempt to return from their implementing function.\r
83  */\r
84 static void prvTaskExitError( void );\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /*\r
89  * See header file for description.\r
90  */\r
91 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
92 {\r
93         /* Simulate the stack frame as it would be created by a context switch\r
94         interrupt. */\r
95         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
96         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
97         pxTopOfStack--;\r
98         *pxTopOfStack = ( StackType_t ) pxCode; /* PC */\r
99         pxTopOfStack--;\r
100         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* LR */\r
101         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
102         *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
103         pxTopOfStack -= 8; /* R11..R4. */\r
104 \r
105         return pxTopOfStack;\r
106 }\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 static void prvTaskExitError( void )\r
110 {\r
111         /* A function that implements a task must not exit or attempt to return to\r
112         its caller as there is nothing to return to.  If a task wants to exit it\r
113         should instead call vTaskDelete( NULL ).\r
114 \r
115         Artificially force an assert() to be triggered if configASSERT() is\r
116         defined, then stop here so application writers can catch the error. */\r
117         configASSERT( uxCriticalNesting == ~0UL );\r
118         portDISABLE_INTERRUPTS();\r
119         for( ;; );\r
120 }\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /*\r
124  * See header file for description.\r
125  */\r
126 BaseType_t xPortStartScheduler( void )\r
127 {\r
128         /* Make PendSV and SysTick the lowest priority interrupts. */\r
129         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
130         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
131 \r
132         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
133         here already. */\r
134         prvSetupTimerInterrupt();\r
135 \r
136         /* Initialise the critical nesting count ready for the first task. */\r
137         uxCriticalNesting = 0;\r
138 \r
139         /* Start the first task. */\r
140         vPortStartFirstTask();\r
141 \r
142         /* Should not get here! */\r
143         return 0;\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 void vPortEndScheduler( void )\r
148 {\r
149         /* Not implemented in ports where there is nothing to return to.\r
150         Artificially force an assert. */\r
151         configASSERT( uxCriticalNesting == 1000UL );\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 void vPortYield( void )\r
156 {\r
157         /* Set a PendSV to request a context switch. */\r
158         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
159 \r
160         /* Barriers are normally not required but do ensure the code is completely\r
161         within the specified behaviour for the architecture. */\r
162         __DSB();\r
163         __ISB();\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 void vPortEnterCritical( void )\r
168 {\r
169         portDISABLE_INTERRUPTS();\r
170         uxCriticalNesting++;\r
171         __DSB();\r
172         __ISB();\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void vPortExitCritical( void )\r
177 {\r
178         configASSERT( uxCriticalNesting );\r
179         uxCriticalNesting--;\r
180         if( uxCriticalNesting == 0 )\r
181         {\r
182                 portENABLE_INTERRUPTS();\r
183         }\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void xPortSysTickHandler( void )\r
188 {\r
189 uint32_t ulPreviousMask;\r
190 \r
191         ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
192         {\r
193                 /* Increment the RTOS tick. */\r
194                 if( xTaskIncrementTick() != pdFALSE )\r
195                 {\r
196                         /* Pend a context switch. */\r
197                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
198                 }\r
199         }\r
200         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /*\r
205  * Setup the systick timer to generate the tick interrupts at the required\r
206  * frequency.\r
207  */\r
208 static void prvSetupTimerInterrupt( void )\r
209 {\r
210         /* Stop and reset the SysTick. */\r
211         *(portNVIC_SYSTICK_CTRL) = 0UL;\r
212         *(portNVIC_SYSTICK_CURRENT_VALUE) = 0UL;\r
213 \r
214         /* Configure SysTick to interrupt at the requested rate. */\r
215         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
216         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r