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