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