]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/MSP430F449/port.c
b2525ba8a4b90e627a66cbdab3678a71c1dc8414
[freertos] / FreeRTOS / Source / portable / GCC / MSP430F449 / 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         Changes from V2.5.2\r
71                 \r
72         + usCriticalNesting now has a volatile qualifier.\r
73 */\r
74 \r
75 /* Standard includes. */\r
76 #include <stdlib.h>\r
77 #include <signal.h>\r
78 \r
79 /* Scheduler includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 \r
83 /*-----------------------------------------------------------\r
84  * Implementation of functions defined in portable.h for the MSP430 port.\r
85  *----------------------------------------------------------*/\r
86 \r
87 /* Constants required for hardware setup.  The tick ISR runs off the ACLK, \r
88 not the MCLK. */\r
89 #define portACLK_FREQUENCY_HZ                   ( ( portTickType ) 32768 )\r
90 #define portINITIAL_CRITICAL_NESTING    ( ( unsigned short ) 10 )\r
91 #define portFLAGS_INT_ENABLED   ( ( portSTACK_TYPE ) 0x08 )\r
92 \r
93 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
94 any details of its type. */\r
95 typedef void tskTCB;\r
96 extern volatile tskTCB * volatile pxCurrentTCB;\r
97 \r
98 /* Most ports implement critical sections by placing the interrupt flags on\r
99 the stack before disabling interrupts.  Exiting the critical section is then\r
100 simply a case of popping the flags from the stack.  As mspgcc does not use\r
101 a frame pointer this cannot be done as modifying the stack will clobber all\r
102 the stack variables.  Instead each task maintains a count of the critical\r
103 section nesting depth.  Each time a critical section is entered the count is\r
104 incremented.  Each time a critical section is left the count is decremented -\r
105 with interrupts only being re-enabled if the count is zero.\r
106 \r
107 usCriticalNesting will get set to zero when the scheduler starts, but must\r
108 not be initialised to zero as this will cause problems during the startup\r
109 sequence. */\r
110 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /* \r
114  * Macro to save a task context to the task stack.  This simply pushes all the \r
115  * general purpose msp430 registers onto the stack, followed by the \r
116  * usCriticalNesting value used by the task.  Finally the resultant stack \r
117  * pointer value is saved into the task control block so it can be retrieved \r
118  * the next time the task executes.\r
119  */\r
120 #define portSAVE_CONTEXT()                                                                      \\r
121         asm volatile (  "push   r4                                              \n\t"   \\r
122                                         "push   r5                                              \n\t"   \\r
123                                         "push   r6                                              \n\t"   \\r
124                                         "push   r7                                              \n\t"   \\r
125                                         "push   r8                                              \n\t"   \\r
126                                         "push   r9                                              \n\t"   \\r
127                                         "push   r10                                             \n\t"   \\r
128                                         "push   r11                                             \n\t"   \\r
129                                         "push   r12                                             \n\t"   \\r
130                                         "push   r13                                             \n\t"   \\r
131                                         "push   r14                                             \n\t"   \\r
132                                         "push   r15                                             \n\t"   \\r
133                                         "mov.w  usCriticalNesting, r14  \n\t"   \\r
134                                         "push   r14                                             \n\t"   \\r
135                                         "mov.w  pxCurrentTCB, r12               \n\t"   \\r
136                                         "mov.w  r1, @r12                                \n\t"   \\r
137                                 );\r
138 \r
139 /* \r
140  * Macro to restore a task context from the task stack.  This is effectively\r
141  * the reverse of portSAVE_CONTEXT().  First the stack pointer value is\r
142  * loaded from the task control block.  Next the value for usCriticalNesting\r
143  * used by the task is retrieved from the stack - followed by the value of all\r
144  * the general purpose msp430 registers.\r
145  *\r
146  * The bic instruction ensures there are no low power bits set in the status\r
147  * register that is about to be popped from the stack.\r
148  */\r
149 #define portRESTORE_CONTEXT()                                                           \\r
150         asm volatile (  "mov.w  pxCurrentTCB, r12               \n\t"   \\r
151                                         "mov.w  @r12, r1                                \n\t"   \\r
152                                         "pop    r15                                             \n\t"   \\r
153                                         "mov.w  r15, usCriticalNesting  \n\t"   \\r
154                                         "pop    r15                                             \n\t"   \\r
155                                         "pop    r14                                             \n\t"   \\r
156                                         "pop    r13                                             \n\t"   \\r
157                                         "pop    r12                                             \n\t"   \\r
158                                         "pop    r11                                             \n\t"   \\r
159                                         "pop    r10                                             \n\t"   \\r
160                                         "pop    r9                                              \n\t"   \\r
161                                         "pop    r8                                              \n\t"   \\r
162                                         "pop    r7                                              \n\t"   \\r
163                                         "pop    r6                                              \n\t"   \\r
164                                         "pop    r5                                              \n\t"   \\r
165                                         "pop    r4                                              \n\t"   \\r
166                                         "bic    #(0xf0),0(r1)                   \n\t"   \\r
167                                         "reti                                                   \n\t"   \\r
168                                 );\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 /*\r
172  * Sets up the periodic ISR used for the RTOS tick.  This uses timer 0, but\r
173  * could have alternatively used the watchdog timer or timer 1.\r
174  */\r
175 static void prvSetupTimerInterrupt( void );\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 /* \r
179  * Initialise the stack of a task to look exactly as if a call to \r
180  * portSAVE_CONTEXT had been called.\r
181  * \r
182  * See the header file portable.h.\r
183  */\r
184 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
185 {\r
186         /* \r
187                 Place a few bytes of known values on the bottom of the stack. \r
188                 This is just useful for debugging and can be included if required.\r
189 \r
190                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;\r
191                 pxTopOfStack--;\r
192                 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;\r
193                 pxTopOfStack--;\r
194                 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333;\r
195                 pxTopOfStack--; \r
196         */\r
197 \r
198         /* The msp430 automatically pushes the PC then SR onto the stack before \r
199         executing an ISR.  We want the stack to look just as if this has happened\r
200         so place a pointer to the start of the task on the stack first - followed\r
201         by the flags we want the task to use when it starts up. */\r
202         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;\r
203         pxTopOfStack--;\r
204         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
205         pxTopOfStack--;\r
206 \r
207         /* Next the general purpose registers. */\r
208         *pxTopOfStack = ( portSTACK_TYPE ) 0x4444;\r
209         pxTopOfStack--;\r
210         *pxTopOfStack = ( portSTACK_TYPE ) 0x5555;\r
211         pxTopOfStack--;\r
212         *pxTopOfStack = ( portSTACK_TYPE ) 0x6666;\r
213         pxTopOfStack--;\r
214         *pxTopOfStack = ( portSTACK_TYPE ) 0x7777;\r
215         pxTopOfStack--;\r
216         *pxTopOfStack = ( portSTACK_TYPE ) 0x8888;\r
217         pxTopOfStack--;\r
218         *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;\r
219         pxTopOfStack--;\r
220         *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaa;\r
221         pxTopOfStack--;\r
222         *pxTopOfStack = ( portSTACK_TYPE ) 0xbbbb;\r
223         pxTopOfStack--;\r
224         *pxTopOfStack = ( portSTACK_TYPE ) 0xcccc;\r
225         pxTopOfStack--;\r
226         *pxTopOfStack = ( portSTACK_TYPE ) 0xdddd;\r
227         pxTopOfStack--;\r
228         *pxTopOfStack = ( portSTACK_TYPE ) 0xeeee;\r
229         pxTopOfStack--;\r
230 \r
231         /* When the task starts is will expect to find the function parameter in\r
232         R15. */\r
233         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
234         pxTopOfStack--;\r
235 \r
236         /* The code generated by the mspgcc compiler does not maintain separate\r
237         stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
238         use the stack as per other ports.  Instead a variable is used to keep\r
239         track of the critical section nesting.  This variable has to be stored\r
240         as part of the task context and is initially set to zero. */\r
241         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;     \r
242 \r
243         /* Return a pointer to the top of the stack we have generated so this can\r
244         be stored in the task control block for the task. */\r
245         return pxTopOfStack;\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 portBASE_TYPE xPortStartScheduler( void )\r
250 {\r
251         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
252         this function is called. */\r
253         prvSetupTimerInterrupt();\r
254 \r
255         /* Restore the context of the first task that is going to run. */\r
256         portRESTORE_CONTEXT();\r
257 \r
258         /* Should not get here as the tasks are now running! */\r
259         return pdTRUE;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 void vPortEndScheduler( void )\r
264 {\r
265         /* It is unlikely that the MSP430 port will get stopped.  If required simply\r
266         disable the tick interrupt here. */\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 /*\r
271  * Manual context switch called by portYIELD or taskYIELD.  \r
272  *\r
273  * The first thing we do is save the registers so we can use a naked attribute.\r
274  */\r
275 void vPortYield( void ) __attribute__ ( ( naked ) );\r
276 void vPortYield( void )\r
277 {\r
278         /* We want the stack of the task being saved to look exactly as if the task\r
279         was saved during a pre-emptive RTOS tick ISR.  Before calling an ISR the \r
280         msp430 places the status register onto the stack.  As this is a function \r
281         call and not an ISR we have to do this manually. */\r
282         asm volatile ( "push    r2" );\r
283         _DINT();\r
284 \r
285         /* Save the context of the current task. */\r
286         portSAVE_CONTEXT();\r
287 \r
288         /* Switch to the highest priority task that is ready to run. */\r
289         vTaskSwitchContext();\r
290 \r
291         /* Restore the context of the new task. */\r
292         portRESTORE_CONTEXT();\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 /*\r
297  * Hardware initialisation to generate the RTOS tick.  This uses timer 0\r
298  * but could alternatively use the watchdog timer or timer 1. \r
299  */\r
300 static void prvSetupTimerInterrupt( void )\r
301 {\r
302         /* Ensure the timer is stopped. */\r
303         TACTL = 0;\r
304 \r
305         /* Run the timer of the ACLK. */\r
306         TACTL = TASSEL_1;\r
307 \r
308         /* Clear everything to start with. */\r
309         TACTL |= TACLR;\r
310 \r
311         /* Set the compare match value according to the tick rate we want. */\r
312         TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;\r
313 \r
314         /* Enable the interrupts. */\r
315         TACCTL0 = CCIE;\r
316 \r
317         /* Start up clean. */\r
318         TACTL |= TACLR;\r
319 \r
320         /* Up mode. */\r
321         TACTL |= MC_1;\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 /* \r
326  * The interrupt service routine used depends on whether the pre-emptive\r
327  * scheduler is being used or not.\r
328  */\r
329 \r
330 #if configUSE_PREEMPTION == 1\r
331 \r
332         /*\r
333          * Tick ISR for preemptive scheduler.  We can use a naked attribute as\r
334          * the context is saved at the start of vPortYieldFromTick().  The tick\r
335          * count is incremented after the context is saved.\r
336          */\r
337         interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );\r
338         interrupt (TIMERA0_VECTOR) prvTickISR( void )\r
339         {\r
340                 /* Save the context of the interrupted task. */\r
341                 portSAVE_CONTEXT();\r
342 \r
343                 /* Increment the tick count then switch to the highest priority task\r
344                 that is ready to run. */\r
345                 vTaskIncrementTick();\r
346                 vTaskSwitchContext();\r
347 \r
348                 /* Restore the context of the new task. */\r
349                 portRESTORE_CONTEXT();\r
350         }\r
351 \r
352 #else\r
353 \r
354         /*\r
355          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
356          * tick count.  We don't need to switch context, this can only be done by\r
357          * manual calls to taskYIELD();\r
358          */\r
359         interrupt (TIMERA0_VECTOR) prvTickISR( void );\r
360         interrupt (TIMERA0_VECTOR) prvTickISR( void )\r
361         {\r
362                 vTaskIncrementTick();\r
363         }\r
364 #endif\r
365 \r
366 \r
367         \r