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