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