]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CM0/port.c
c34573f6de393197412d392bb06e89d66afb8ce7
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CM0 / port.c
1 /*\r
2     FreeRTOS V7.4.0 - 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 itcan 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 /* Scheduler includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 \r
83 /* Constants required to manipulate the NVIC. */\r
84 #define portNVIC_SYSTICK_CTRL           ( ( volatile unsigned long *) 0xe000e010 )\r
85 #define portNVIC_SYSTICK_LOAD           ( ( volatile unsigned long *) 0xe000e014 )\r
86 #define portNVIC_INT_CTRL                       ( ( volatile unsigned long *) 0xe000ed04 )\r
87 #define portNVIC_SYSPRI2                        ( ( volatile unsigned long *) 0xe000ed20 )\r
88 #define portNVIC_SYSTICK_CLK            0x00000004\r
89 #define portNVIC_SYSTICK_INT            0x00000002\r
90 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
91 #define portNVIC_PENDSVSET                      0x10000000\r
92 #define portMIN_INTERRUPT_PRIORITY      ( 255UL )\r
93 #define portNVIC_PENDSV_PRI                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
94 #define portNVIC_SYSTICK_PRI            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
95 \r
96 /* Constants required to set up the initial stack. */\r
97 #define portINITIAL_XPSR                        ( 0x01000000 )\r
98 \r
99 /* For backward compatibility, ensure configKERNEL_INTERRUPT_PRIORITY is\r
100 defined.  The value 255 should also ensure backward compatibility.\r
101 FreeRTOS.org versions prior to V4.3.0 did not include this definition. */\r
102 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
103         #define configKERNEL_INTERRUPT_PRIORITY 0\r
104 #endif\r
105 \r
106 /* Each task maintains its own interrupt status in the critical nesting\r
107 variable. */\r
108 static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;\r
109 \r
110 /*\r
111  * Setup the timer to generate the tick interrupts.\r
112  */\r
113 static void prvSetupTimerInterrupt( void );\r
114 \r
115 /*\r
116  * Exception handlers.\r
117  */\r
118 void xPortSysTickHandler( void );\r
119 \r
120 /*\r
121  * Start first task is a separate function so it can be tested in isolation.\r
122  */\r
123 extern void vPortStartFirstTask( void );\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /*\r
128  * See header file for description.\r
129  */\r
130 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
131 {\r
132         /* Simulate the stack frame as it would be created by a context switch\r
133         interrupt. */\r
134         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
135         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
136         pxTopOfStack--;\r
137         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
138         pxTopOfStack -= 6;      /* LR, R12, R3..R1 */\r
139         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
140         pxTopOfStack -= 8; /* R11..R4. */\r
141 \r
142         return pxTopOfStack;\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 /*\r
147  * See header file for description.\r
148  */\r
149 portBASE_TYPE xPortStartScheduler( void )\r
150 {\r
151         /* Make PendSV and SysTick the lowest priority interrupts. */\r
152         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
153         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
154 \r
155         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
156         here already. */\r
157         prvSetupTimerInterrupt();\r
158         \r
159         /* Initialise the critical nesting count ready for the first task. */\r
160         uxCriticalNesting = 0;\r
161 \r
162         /* Start the first task. */\r
163         vPortStartFirstTask();\r
164 \r
165         /* Should not get here! */\r
166         return 0;\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 void vPortEndScheduler( void )\r
171 {\r
172         /* It is unlikely that the CM0 port will require this function as there\r
173         is nothing to return to.  */\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 void vPortYieldFromISR( void )\r
178 {\r
179         /* Set a PendSV to request a context switch. */\r
180         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 void vPortEnterCritical( void )\r
185 {\r
186         portDISABLE_INTERRUPTS();\r
187         uxCriticalNesting++;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 void vPortExitCritical( void )\r
192 {\r
193         uxCriticalNesting--;\r
194         if( uxCriticalNesting == 0 )\r
195         {\r
196                 portENABLE_INTERRUPTS();\r
197         }\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 void xPortSysTickHandler( void )\r
202 {\r
203 unsigned long ulDummy;\r
204 \r
205         /* If using preemption, also force a context switch. */\r
206         #if configUSE_PREEMPTION == 1\r
207                 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;      \r
208         #endif\r
209 \r
210         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
211         {\r
212                 vTaskIncrementTick();\r
213         }\r
214         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 /*\r
219  * Setup the systick timer to generate the tick interrupts at the required\r
220  * frequency.\r
221  */\r
222 static void prvSetupTimerInterrupt( void )\r
223 {\r
224         /* Configure SysTick to interrupt at the requested rate. */\r
225         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
226         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r