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