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