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