]> begriffs open source - freertos/blob - Source/portable/GCC/MSP430F449/port.c
Update to V4.3.0 as described in http://www.FreeRTOS.org/History.txt
[freertos] / Source / portable / GCC / MSP430F449 / port.c
1 /*\r
2         FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37         Changes from V2.5.2\r
38                 \r
39         + usCriticalNesting now has a volatile qualifier.\r
40 */\r
41 \r
42 /* Standard includes. */\r
43 #include <stdlib.h>\r
44 #include <signal.h>\r
45 \r
46 /* Scheduler includes. */\r
47 #include "FreeRTOS.h"\r
48 #include "task.h"\r
49 \r
50 /*-----------------------------------------------------------\r
51  * Implementation of functions defined in portable.h for the MSP430 port.\r
52  *----------------------------------------------------------*/\r
53 \r
54 /* Constants required for hardware setup.  The tick ISR runs off the ACLK, \r
55 not the MCLK. */\r
56 #define portACLK_FREQUENCY_HZ                   ( ( portTickType ) 32768 )\r
57 #define portINITIAL_CRITICAL_NESTING    ( ( unsigned portSHORT ) 10 )\r
58 #define portFLAGS_INT_ENABLED   ( ( portSTACK_TYPE ) 0x08 )\r
59 \r
60 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
61 any details of its type. */\r
62 typedef void tskTCB;\r
63 extern volatile tskTCB * volatile pxCurrentTCB;\r
64 \r
65 /* Most ports implement critical sections by placing the interrupt flags on\r
66 the stack before disabling interrupts.  Exiting the critical section is then\r
67 simply a case of popping the flags from the stack.  As mspgcc does not use\r
68 a frame pointer this cannot be done as modifying the stack will clobber all\r
69 the stack variables.  Instead each task maintains a count of the critical\r
70 section nesting depth.  Each time a critical section is entered the count is\r
71 incremented.  Each time a critical section is left the count is decremented -\r
72 with interrupts only being re-enabled if the count is zero.\r
73 \r
74 usCriticalNesting will get set to zero when the scheduler starts, but must\r
75 not be initialised to zero as this will cause problems during the startup\r
76 sequence. */\r
77 volatile unsigned portSHORT usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /* \r
81  * Macro to save a task context to the task stack.  This simply pushes all the \r
82  * general purpose msp430 registers onto the stack, followed by the \r
83  * usCriticalNesting value used by the task.  Finally the resultant stack \r
84  * pointer value is saved into the task control block so it can be retrieved \r
85  * the next time the task executes.\r
86  */\r
87 #define portSAVE_CONTEXT()                                                                      \\r
88         asm volatile (  "push   r4                                              \n\t"   \\r
89                                         "push   r5                                              \n\t"   \\r
90                                         "push   r6                                              \n\t"   \\r
91                                         "push   r7                                              \n\t"   \\r
92                                         "push   r8                                              \n\t"   \\r
93                                         "push   r9                                              \n\t"   \\r
94                                         "push   r10                                             \n\t"   \\r
95                                         "push   r11                                             \n\t"   \\r
96                                         "push   r12                                             \n\t"   \\r
97                                         "push   r13                                             \n\t"   \\r
98                                         "push   r14                                             \n\t"   \\r
99                                         "push   r15                                             \n\t"   \\r
100                                         "mov.w  usCriticalNesting, r14  \n\t"   \\r
101                                         "push   r14                                             \n\t"   \\r
102                                         "mov.w  pxCurrentTCB, r12               \n\t"   \\r
103                                         "mov.w  r1, @r12                                \n\t"   \\r
104                                 );\r
105 \r
106 /* \r
107  * Macro to restore a task context from the task stack.  This is effectively\r
108  * the reverse of portSAVE_CONTEXT().  First the stack pointer value is\r
109  * loaded from the task control block.  Next the value for usCriticalNesting\r
110  * used by the task is retrieved from the stack - followed by the value of all\r
111  * the general purpose msp430 registers.\r
112  */\r
113 #define portRESTORE_CONTEXT()                                                           \\r
114         asm volatile (  "mov.w  pxCurrentTCB, r12               \n\t"   \\r
115                                         "mov.w  @r12, r1                                \n\t"   \\r
116                                         "pop    r15                                             \n\t"   \\r
117                                         "mov.w  r15, usCriticalNesting  \n\t"   \\r
118                                         "pop    r15                                             \n\t"   \\r
119                                         "pop    r14                                             \n\t"   \\r
120                                         "pop    r13                                             \n\t"   \\r
121                                         "pop    r12                                             \n\t"   \\r
122                                         "pop    r11                                             \n\t"   \\r
123                                         "pop    r10                                             \n\t"   \\r
124                                         "pop    r9                                              \n\t"   \\r
125                                         "pop    r8                                              \n\t"   \\r
126                                         "pop    r7                                              \n\t"   \\r
127                                         "pop    r6                                              \n\t"   \\r
128                                         "pop    r5                                              \n\t"   \\r
129                                         "pop    r4                                              \n\t"   \\r
130                                         "reti                                                   \n\t"   \\r
131                                 );\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 /*\r
135  * Sets up the periodic ISR used for the RTOS tick.  This uses timer 0, but\r
136  * could have alternatively used the watchdog timer or timer 1.\r
137  */\r
138 static void prvSetupTimerInterrupt( void );\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /* \r
142  * Initialise the stack of a task to look exactly as if a call to \r
143  * portSAVE_CONTEXT had been called.\r
144  * \r
145  * See the header file portable.h.\r
146  */\r
147 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
148 {\r
149         /* \r
150                 Place a few bytes of known values on the bottom of the stack. \r
151                 This is just useful for debugging and can be included if required.\r
152 \r
153                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;\r
154                 pxTopOfStack--;\r
155                 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;\r
156                 pxTopOfStack--;\r
157                 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333;\r
158                 pxTopOfStack--; \r
159         */\r
160 \r
161         /* The msp430 automatically pushes the PC then SR onto the stack before \r
162         executing an ISR.  We want the stack to look just as if this has happened\r
163         so place a pointer to the start of the task on the stack first - followed\r
164         by the flags we want the task to use when it starts up. */\r
165         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;\r
166         pxTopOfStack--;\r
167         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
168         pxTopOfStack--;\r
169 \r
170         /* Next the general purpose registers. */\r
171         *pxTopOfStack = ( portSTACK_TYPE ) 0x4444;\r
172         pxTopOfStack--;\r
173         *pxTopOfStack = ( portSTACK_TYPE ) 0x5555;\r
174         pxTopOfStack--;\r
175         *pxTopOfStack = ( portSTACK_TYPE ) 0x6666;\r
176         pxTopOfStack--;\r
177         *pxTopOfStack = ( portSTACK_TYPE ) 0x7777;\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = ( portSTACK_TYPE ) 0x8888;\r
180         pxTopOfStack--;\r
181         *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;\r
182         pxTopOfStack--;\r
183         *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaa;\r
184         pxTopOfStack--;\r
185         *pxTopOfStack = ( portSTACK_TYPE ) 0xbbbb;\r
186         pxTopOfStack--;\r
187         *pxTopOfStack = ( portSTACK_TYPE ) 0xcccc;\r
188         pxTopOfStack--;\r
189         *pxTopOfStack = ( portSTACK_TYPE ) 0xdddd;\r
190         pxTopOfStack--;\r
191         *pxTopOfStack = ( portSTACK_TYPE ) 0xeeee;\r
192         pxTopOfStack--;\r
193 \r
194         /* When the task starts is will expect to find the function parameter in\r
195         R15. */\r
196         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
197         pxTopOfStack--;\r
198 \r
199         /* The code generated by the mspgcc compiler does not maintain separate\r
200         stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
201         use the stack as per other ports.  Instead a variable is used to keep\r
202         track of the critical section nesting.  This variable has to be stored\r
203         as part of the task context and is initially set to zero. */\r
204         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;     \r
205 \r
206         /* Return a pointer to the top of the stack we have generated so this can\r
207         be stored in the task control block for the task. */\r
208         return pxTopOfStack;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 portBASE_TYPE xPortStartScheduler( void )\r
213 {\r
214         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
215         this function is called. */\r
216         prvSetupTimerInterrupt();\r
217 \r
218         /* Restore the context of the first task that is going to run. */\r
219         portRESTORE_CONTEXT();\r
220 \r
221         /* Should not get here as the tasks are now running! */\r
222         return pdTRUE;\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 void vPortEndScheduler( void )\r
227 {\r
228         /* It is unlikely that the MSP430 port will get stopped.  If required simply\r
229         disable the tick interrupt here. */\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 /*\r
234  * Manual context switch called by portYIELD or taskYIELD.  \r
235  *\r
236  * The first thing we do is save the registers so we can use a naked attribute.\r
237  */\r
238 void vPortYield( void ) __attribute__ ( ( naked ) );\r
239 void vPortYield( void )\r
240 {\r
241         /* We want the stack of the task being saved to look exactly as if the task\r
242         was saved during a pre-emptive RTOS tick ISR.  Before calling an ISR the \r
243         msp430 places the status register onto the stack.  As this is a function \r
244         call and not an ISR we have to do this manually. */\r
245         asm volatile ( "push    r2" );\r
246         _DINT();\r
247 \r
248         /* Save the context of the current task. */\r
249         portSAVE_CONTEXT();\r
250 \r
251         /* Switch to the highest priority task that is ready to run. */\r
252         vTaskSwitchContext();\r
253 \r
254         /* Restore the context of the new task. */\r
255         portRESTORE_CONTEXT();\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 /*\r
260  * Hardware initialisation to generate the RTOS tick.  This uses timer 0\r
261  * but could alternatively use the watchdog timer or timer 1. \r
262  */\r
263 static void prvSetupTimerInterrupt( void )\r
264 {\r
265         /* Ensure the timer is stopped. */\r
266         TACTL = 0;\r
267 \r
268         /* Run the timer of the ACLK. */\r
269         TACTL = TASSEL_1;\r
270 \r
271         /* Clear everything to start with. */\r
272         TACTL |= TACLR;\r
273 \r
274         /* Set the compare match value according to the tick rate we want. */\r
275         TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;\r
276 \r
277         /* Enable the interrupts. */\r
278         TACCTL0 = CCIE;\r
279 \r
280         /* Start up clean. */\r
281         TACTL |= TACLR;\r
282 \r
283         /* Up mode. */\r
284         TACTL |= MC_1;\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 /* \r
289  * The interrupt service routine used depends on whether the pre-emptive\r
290  * scheduler is being used or not.\r
291  */\r
292 \r
293 #if configUSE_PREEMPTION == 1\r
294 \r
295         /*\r
296          * Tick ISR for preemptive scheduler.  We can use a naked attribute as\r
297          * the context is saved at the start of vPortYieldFromTick().  The tick\r
298          * count is incremented after the context is saved.\r
299          */\r
300         interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );\r
301         interrupt (TIMERA0_VECTOR) prvTickISR( void )\r
302         {\r
303                 /* Save the context of the interrupted task. */\r
304                 portSAVE_CONTEXT();\r
305 \r
306                 /* Increment the tick count then switch to the highest priority task\r
307                 that is ready to run. */\r
308                 vTaskIncrementTick();\r
309                 vTaskSwitchContext();\r
310 \r
311                 /* Restore the context of the new task. */\r
312                 portRESTORE_CONTEXT();\r
313         }\r
314 \r
315 #else\r
316 \r
317         /*\r
318          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
319          * tick count.  We don't need to switch context, this can only be done by\r
320          * manual calls to taskYIELD();\r
321          */\r
322         interrupt (TIMERA0_VECTOR) prvTickISR( void );\r
323         interrupt (TIMERA0_VECTOR) prvTickISR( void )\r
324         {\r
325                 vTaskIncrementTick();\r
326         }\r
327 #endif\r
328 \r
329 \r
330         \r