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