]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CM0/port.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CM0 / port.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*-----------------------------------------------------------\r
76  * Implementation of functions defined in portable.h for the ARM CM0 port.\r
77  *----------------------------------------------------------*/\r
78 \r
79 /* IAR includes. */\r
80 #include "intrinsics.h"\r
81 \r
82 /* Scheduler includes. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 \r
86 /* Constants required to manipulate the NVIC. */\r
87 #define portNVIC_SYSTICK_CTRL           ( ( volatile unsigned long *) 0xe000e010 )\r
88 #define portNVIC_SYSTICK_LOAD           ( ( volatile unsigned long *) 0xe000e014 )\r
89 #define portNVIC_SYSPRI2                        ( ( volatile unsigned long *) 0xe000ed20 )\r
90 #define portNVIC_SYSTICK_CLK            0x00000004\r
91 #define portNVIC_SYSTICK_INT            0x00000002\r
92 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
93 #define portMIN_INTERRUPT_PRIORITY      ( 255UL )\r
94 #define portNVIC_PENDSV_PRI                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
95 #define portNVIC_SYSTICK_PRI            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
96 \r
97 /* Constants required to set up the initial stack. */\r
98 #define portINITIAL_XPSR                        ( 0x01000000 )\r
99 \r
100 /* For backward compatibility, ensure configKERNEL_INTERRUPT_PRIORITY is\r
101 defined.  The value 255 should also ensure backward compatibility.\r
102 FreeRTOS.org versions prior to V4.3.0 did not include this definition. */\r
103 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
104         #define configKERNEL_INTERRUPT_PRIORITY 0\r
105 #endif\r
106 \r
107 /* Each task maintains its own interrupt status in the critical nesting\r
108 variable. */\r
109 static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;\r
110 \r
111 /*\r
112  * Setup the timer to generate the tick interrupts.\r
113  */\r
114 static void prvSetupTimerInterrupt( void );\r
115 \r
116 /*\r
117  * Exception handlers.\r
118  */\r
119 void xPortSysTickHandler( void );\r
120 \r
121 /*\r
122  * Start first task is a separate function so it can be tested in isolation.\r
123  */\r
124 extern void vPortStartFirstTask( void );\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /*\r
129  * See header file for description.\r
130  */\r
131 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE 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 = ( portSTACK_TYPE ) pxCode;      /* PC */\r
139         pxTopOfStack -= 6;      /* LR, R12, R3..R1 */\r
140         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
141         pxTopOfStack -= 8; /* R11..R4. */\r
142 \r
143         return pxTopOfStack;\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 /*\r
148  * See header file for description.\r
149  */\r
150 portBASE_TYPE xPortStartScheduler( void )\r
151 {\r
152         /* Make PendSV and SysTick the lowest priority interrupts. */\r
153         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
154         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
155 \r
156         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
157         here already. */\r
158         prvSetupTimerInterrupt();\r
159 \r
160         /* Initialise the critical nesting count ready for the first task. */\r
161         uxCriticalNesting = 0;\r
162 \r
163         /* Start the first task. */\r
164         vPortStartFirstTask();\r
165 \r
166         /* Should not get here! */\r
167         return 0;\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 void vPortEndScheduler( void )\r
172 {\r
173         /* It is unlikely that the CM0 port will require this function as there\r
174         is nothing to return to.  */\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 void vPortYield( void )\r
179 {\r
180         /* Set a PendSV to request a context switch. */\r
181         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
182 \r
183         /* Barriers are normally not required but do ensure the code is completely\r
184         within the specified behaviour for the architecture. */\r
185         __DSB();\r
186         __ISB();\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void vPortEnterCritical( void )\r
191 {\r
192         portDISABLE_INTERRUPTS();\r
193         uxCriticalNesting++;\r
194         __DSB();\r
195         __ISB();\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vPortExitCritical( void )\r
200 {\r
201         uxCriticalNesting--;\r
202         if( uxCriticalNesting == 0 )\r
203         {\r
204                 portENABLE_INTERRUPTS();\r
205         }\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void xPortSysTickHandler( void )\r
210 {\r
211 unsigned long ulDummy;\r
212 \r
213         /* If using preemption, also force a context switch. */\r
214         #if configUSE_PREEMPTION == 1\r
215                 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
216         #endif\r
217 \r
218         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
219         {\r
220                 vTaskIncrementTick();\r
221         }\r
222         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 /*\r
227  * Setup the systick timer to generate the tick interrupts at the required\r
228  * frequency.\r
229  */\r
230 static void prvSetupTimerInterrupt( void )\r
231 {\r
232         /* Configure SysTick to interrupt at the requested rate. */\r
233         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
234         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r