]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Tasking/ARM_CM4F/port.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Source / portable / Tasking / ARM_CM4F / port.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*-----------------------------------------------------------\r
68  * Implementation of functions defined in portable.h for the ARM CM4F port.\r
69  *----------------------------------------------------------*/\r
70 \r
71 /* Scheduler includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 \r
75 #if configMAX_SYSCALL_INTERRUPT_PRIORITY == 0\r
76         #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.  See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html\r
77 #endif\r
78 \r
79 /* Constants required to manipulate the NVIC. */\r
80 #define portNVIC_SYSTICK_CTRL           ( ( volatile unsigned long * ) 0xe000e010 )\r
81 #define portNVIC_SYSTICK_LOAD           ( ( volatile unsigned long * ) 0xe000e014 )\r
82 #define portNVIC_INT_CTRL                       ( ( volatile unsigned long * ) 0xe000ed04 )\r
83 #define portNVIC_SYSPRI2                        ( ( volatile unsigned long * ) 0xe000ed20 )\r
84 #define portNVIC_SYSTICK_CLK            0x00000004\r
85 #define portNVIC_SYSTICK_INT            0x00000002\r
86 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
87 #define portNVIC_PENDSVSET                      0x10000000\r
88 #define portNVIC_PENDSV_PRI                     ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
89 #define portNVIC_SYSTICK_PRI            ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
90 \r
91 /* Constants required to manipulate the VFP. */\r
92 #define portFPCCR                                       ( ( volatile unsigned long * ) 0xe000ef34 ) /* Floating point context control register. */\r
93 #define portASPEN_AND_LSPEN_BITS        ( 0x3UL << 30UL )\r
94 \r
95 /* Constants required to set up the initial stack. */\r
96 #define portINITIAL_XPSR                        ( 0x01000000 )\r
97 #define portINITIAL_EXEC_RETURN         ( 0xfffffffd )\r
98 \r
99 /* The priority used by the kernel is assigned to a variable to make access\r
100 from inline assembler easier. */\r
101 const unsigned long ulKernelPriority = configKERNEL_INTERRUPT_PRIORITY;\r
102 \r
103 /* Each task maintains its own interrupt status in the critical nesting\r
104 variable. */\r
105 static unsigned long ulCriticalNesting = 0xaaaaaaaaUL;\r
106 \r
107 /*\r
108  * Setup the timer to generate the tick interrupts.\r
109  */\r
110 static void prvSetupTimerInterrupt( void );\r
111 \r
112 /*\r
113  * Exception handlers.\r
114  */\r
115 void SysTick_Handler( void );\r
116 \r
117 /*\r
118  * Functions defined in port_asm.asm.\r
119  */\r
120 extern void vPortEnableVFP( void );\r
121 extern void vPortStartFirstTask( void );\r
122 \r
123 /* This exists purely to allow the const to be used from within the\r
124 port_asm.asm assembly file. */\r
125 const unsigned long ulMaxSyscallInterruptPriorityConst = configMAX_SYSCALL_INTERRUPT_PRIORITY;\r
126 \r
127 /*-----------------------------------------------------------*/\r
128 \r
129 /*\r
130  * See header file for description.\r
131  */\r
132 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
133 {\r
134         /* Simulate the stack frame as it would be created by a context switch\r
135         interrupt. */\r
136 \r
137         /* Offset added to account for the way the MCU uses the stack on entry/exit\r
138         of interrupts, and to ensure alignment. */\r
139         pxTopOfStack--;\r
140 \r
141         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
142         pxTopOfStack--;\r
143         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
144         pxTopOfStack--;\r
145         *pxTopOfStack = 0;      /* LR */\r
146 \r
147         /* Save code space by skipping register initialisation. */\r
148         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
149         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
150 \r
151         /* A save method is being used that requires each task to maintain its\r
152         own exec return value. */\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = portINITIAL_EXEC_RETURN;\r
155 \r
156         pxTopOfStack -= 8;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
157 \r
158         return pxTopOfStack;\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 /*\r
163  * See header file for description.\r
164  */\r
165 portBASE_TYPE xPortStartScheduler( void )\r
166 {\r
167         /* Make PendSV and SysTick the lowest priority interrupts. */\r
168         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
169         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
170 \r
171         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
172         here already. */\r
173         prvSetupTimerInterrupt();\r
174 \r
175         /* Initialise the critical nesting count ready for the first task. */\r
176         ulCriticalNesting = 0;\r
177 \r
178         /* Ensure the VFP is enabled - it should be anyway. */\r
179         vPortEnableVFP();\r
180 \r
181         /* Lazy save always. */\r
182         *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\r
183 \r
184         /* Start the first task. */\r
185         vPortStartFirstTask();\r
186 \r
187         /* Should not get here! */\r
188         return 0;\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 void vPortEndScheduler( void )\r
193 {\r
194         /* It is unlikely that the CM4F port will require this function as there\r
195         is nothing to return to.  */\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vPortYieldFromISR( void )\r
200 {\r
201         /* Set a PendSV to request a context switch. */\r
202         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vPortEnterCritical( void )\r
207 {\r
208         portDISABLE_INTERRUPTS();\r
209         ulCriticalNesting++;\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 void vPortExitCritical( void )\r
214 {\r
215         ulCriticalNesting--;\r
216         if( ulCriticalNesting == 0 )\r
217         {\r
218                 portENABLE_INTERRUPTS();\r
219         }\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 void SysTick_Handler( void )\r
224 {\r
225 unsigned long ulDummy;\r
226 \r
227         /* If using preemption, also force a context switch. */\r
228         #if configUSE_PREEMPTION == 1\r
229                 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
230         #endif\r
231 \r
232         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
233         {\r
234                 vTaskIncrementTick();\r
235         }\r
236         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 /*\r
241  * Setup the systick timer to generate the tick interrupts at the required\r
242  * frequency.\r
243  */\r
244 void prvSetupTimerInterrupt( void )\r
245 {\r
246         /* Configure SysTick to interrupt at the requested rate. */\r
247         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
248         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r