]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CM0/port.c
Prepare for V9.0.0 release:
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CM0 / port.c
1 /*\r
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*-----------------------------------------------------------\r
71  * Implementation of functions defined in portable.h for the ARM CM0 port.\r
72  *----------------------------------------------------------*/\r
73 \r
74 /* IAR includes. */\r
75 #include "intrinsics.h"\r
76 \r
77 /* Scheduler includes. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 \r
81 /* Constants required to manipulate the NVIC. */\r
82 #define portNVIC_SYSTICK_CTRL           ( ( volatile uint32_t *) 0xe000e010 )\r
83 #define portNVIC_SYSTICK_LOAD           ( ( volatile uint32_t *) 0xe000e014 )\r
84 #define portNVIC_SYSPRI2                        ( ( volatile uint32_t *) 0xe000ed20 )\r
85 #define portNVIC_SYSTICK_CLK            0x00000004\r
86 #define portNVIC_SYSTICK_INT            0x00000002\r
87 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
88 #define portMIN_INTERRUPT_PRIORITY      ( 255UL )\r
89 #define portNVIC_PENDSV_PRI                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
90 #define portNVIC_SYSTICK_PRI            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
91 \r
92 /* Constants required to set up the initial stack. */\r
93 #define portINITIAL_XPSR                        ( 0x01000000 )\r
94 \r
95 /* For backward compatibility, ensure configKERNEL_INTERRUPT_PRIORITY is\r
96 defined.  The value 255 should also ensure backward compatibility.\r
97 FreeRTOS.org versions prior to V4.3.0 did not include this definition. */\r
98 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
99         #define configKERNEL_INTERRUPT_PRIORITY 0\r
100 #endif\r
101 \r
102 /* Each task maintains its own interrupt status in the critical nesting\r
103 variable. */\r
104 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
105 \r
106 /*\r
107  * Setup the timer to generate the tick interrupts.\r
108  */\r
109 static void prvSetupTimerInterrupt( void );\r
110 \r
111 /*\r
112  * Exception handlers.\r
113  */\r
114 void xPortSysTickHandler( void );\r
115 \r
116 /*\r
117  * Start first task is a separate function so it can be tested in isolation.\r
118  */\r
119 extern void vPortStartFirstTask( void );\r
120 \r
121 /*\r
122  * Used to catch tasks that attempt to return from their implementing function.\r
123  */\r
124 static void prvTaskExitError( void );\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /*\r
129  * See header file for description.\r
130  */\r
131 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
132 {\r
133         /* Simulate the stack frame as it would be created by a context switch\r
134         interrupt. */\r
135         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
136         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
137         pxTopOfStack--;\r
138         *pxTopOfStack = ( StackType_t ) pxCode; /* PC */\r
139         pxTopOfStack--;\r
140         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* LR */\r
141         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
142         *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
143         pxTopOfStack -= 8; /* R11..R4. */\r
144 \r
145         return pxTopOfStack;\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 static void prvTaskExitError( void )\r
150 {\r
151         /* A function that implements a task must not exit or attempt to return to\r
152         its caller as there is nothing to return to.  If a task wants to exit it\r
153         should instead call vTaskDelete( NULL ).\r
154 \r
155         Artificially force an assert() to be triggered if configASSERT() is\r
156         defined, then stop here so application writers can catch the error. */\r
157         configASSERT( uxCriticalNesting == ~0UL );\r
158         portDISABLE_INTERRUPTS();\r
159         for( ;; );\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /*\r
164  * See header file for description.\r
165  */\r
166 BaseType_t xPortStartScheduler( void )\r
167 {\r
168         /* Make PendSV and SysTick the lowest priority interrupts. */\r
169         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
170         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
171 \r
172         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
173         here already. */\r
174         prvSetupTimerInterrupt();\r
175 \r
176         /* Initialise the critical nesting count ready for the first task. */\r
177         uxCriticalNesting = 0;\r
178 \r
179         /* Start the first task. */\r
180         vPortStartFirstTask();\r
181 \r
182         /* Should not get here! */\r
183         return 0;\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void vPortEndScheduler( void )\r
188 {\r
189         /* Not implemented in ports where there is nothing to return to.\r
190         Artificially force an assert. */\r
191         configASSERT( uxCriticalNesting == 1000UL );\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vPortYield( void )\r
196 {\r
197         /* Set a PendSV to request a context switch. */\r
198         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
199 \r
200         /* Barriers are normally not required but do ensure the code is completely\r
201         within the specified behaviour for the architecture. */\r
202         __DSB();\r
203         __ISB();\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 void vPortEnterCritical( void )\r
208 {\r
209         portDISABLE_INTERRUPTS();\r
210         uxCriticalNesting++;\r
211         __DSB();\r
212         __ISB();\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 void vPortExitCritical( void )\r
217 {\r
218         configASSERT( uxCriticalNesting );\r
219         uxCriticalNesting--;\r
220         if( uxCriticalNesting == 0 )\r
221         {\r
222                 portENABLE_INTERRUPTS();\r
223         }\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 void xPortSysTickHandler( void )\r
228 {\r
229 uint32_t ulPreviousMask;\r
230 \r
231         ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
232         {\r
233                 /* Increment the RTOS tick. */\r
234                 if( xTaskIncrementTick() != pdFALSE )\r
235                 {\r
236                         /* Pend a context switch. */\r
237                         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
238                 }\r
239         }\r
240         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 /*\r
245  * Setup the systick timer to generate the tick interrupts at the required\r
246  * frequency.\r
247  */\r
248 static void prvSetupTimerInterrupt( void )\r
249 {\r
250         /* Configure SysTick to interrupt at the requested rate. */\r
251         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
252         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r