2 FreeRTOS V4.0.1 - Copyright (C) 2003-2006 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
30 ***************************************************************************
\r
36 + usCriticalNesting now has a volatile qualifier.
\r
39 /* Standard includes. */
\r
43 /* Scheduler includes. */
\r
44 #include "FreeRTOS.h"
\r
47 /*-----------------------------------------------------------
\r
48 * Implementation of functions defined in portable.h for the MSP430 port.
\r
49 *----------------------------------------------------------*/
\r
51 /* Constants required for hardware setup. The tick ISR runs off the ACLK,
\r
53 #define portACLK_FREQUENCY_HZ ( ( portTickType ) 32768 )
\r
54 #define portINITIAL_CRITICAL_NESTING ( ( unsigned portSHORT ) 10 )
\r
55 #define portFLAGS_INT_ENABLED ( ( portSTACK_TYPE ) 0x08 )
\r
57 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
58 any details of its type. */
\r
59 typedef void tskTCB;
\r
60 extern volatile tskTCB * volatile pxCurrentTCB;
\r
62 /* Most ports implement critical sections by placing the interrupt flags on
\r
63 the stack before disabling interrupts. Exiting the critical section is then
\r
64 simply a case of popping the flags from the stack. As mspgcc does not use
\r
65 a frame pointer this cannot be done as modifying the stack will clobber all
\r
66 the stack variables. Instead each task maintains a count of the critical
\r
67 section nesting depth. Each time a critical section is entered the count is
\r
68 incremented. Each time a critical section is left the count is decremented -
\r
69 with interrupts only being re-enabled if the count is zero.
\r
71 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
72 not be initialised to zero as this will cause problems during the startup
\r
74 volatile unsigned portSHORT usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
75 /*-----------------------------------------------------------*/
\r
78 * Macro to save a task context to the task stack. This simply pushes all the
\r
79 * general purpose msp430 registers onto the stack, followed by the
\r
80 * usCriticalNesting value used by the task. Finally the resultant stack
\r
81 * pointer value is saved into the task control block so it can be retrieved
\r
82 * the next time the task executes.
\r
84 #define portSAVE_CONTEXT() \
\r
85 asm volatile ( "push r4 \n\t" \
\r
97 "mov.w usCriticalNesting, r14 \n\t" \
\r
99 "mov.w pxCurrentTCB, r12 \n\t" \
\r
100 "mov.w r1, @r12 \n\t" \
\r
104 * Macro to restore a task context from the task stack. This is effectively
\r
105 * the reverse of portSAVE_CONTEXT(). First the stack pointer value is
\r
106 * loaded from the task control block. Next the value for usCriticalNesting
\r
107 * used by the task is retrieved from the stack - followed by the value of all
\r
108 * the general purpose msp430 registers.
\r
110 #define portRESTORE_CONTEXT() \
\r
111 asm volatile ( "mov.w pxCurrentTCB, r12 \n\t" \
\r
112 "mov.w @r12, r1 \n\t" \
\r
114 "mov.w r15, usCriticalNesting \n\t" \
\r
129 /*-----------------------------------------------------------*/
\r
132 * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
\r
133 * could have alternatively used the watchdog timer or timer 1.
\r
135 static void prvSetupTimerInterrupt( void );
\r
136 /*-----------------------------------------------------------*/
\r
139 * Initialise the stack of a task to look exactly as if a call to
\r
140 * portSAVE_CONTEXT had been called.
\r
142 * See the header file portable.h.
\r
144 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
147 Place a few bytes of known values on the bottom of the stack.
\r
148 This is just useful for debugging and can be included if required.
\r
150 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;
\r
152 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;
\r
154 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333;
\r
158 /* The msp430 automatically pushes the PC then SR onto the stack before
\r
159 executing an ISR. We want the stack to look just as if this has happened
\r
160 so place a pointer to the start of the task on the stack first - followed
\r
161 by the flags we want the task to use when it starts up. */
\r
162 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
\r
164 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
167 /* Next the general purpose registers. */
\r
168 *pxTopOfStack = ( portSTACK_TYPE ) 0x4444;
\r
170 *pxTopOfStack = ( portSTACK_TYPE ) 0x5555;
\r
172 *pxTopOfStack = ( portSTACK_TYPE ) 0x6666;
\r
174 *pxTopOfStack = ( portSTACK_TYPE ) 0x7777;
\r
176 *pxTopOfStack = ( portSTACK_TYPE ) 0x8888;
\r
178 *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;
\r
180 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaa;
\r
182 *pxTopOfStack = ( portSTACK_TYPE ) 0xbbbb;
\r
184 *pxTopOfStack = ( portSTACK_TYPE ) 0xcccc;
\r
186 *pxTopOfStack = ( portSTACK_TYPE ) 0xdddd;
\r
188 *pxTopOfStack = ( portSTACK_TYPE ) 0xeeee;
\r
191 /* When the task starts is will expect to find the function parameter in
\r
193 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
196 /* The code generated by the mspgcc compiler does not maintain separate
\r
197 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
\r
198 use the stack as per other ports. Instead a variable is used to keep
\r
199 track of the critical section nesting. This variable has to be stored
\r
200 as part of the task context and is initially set to zero. */
\r
201 *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;
\r
203 /* Return a pointer to the top of the stack we have generated so this can
\r
204 be stored in the task control block for the task. */
\r
205 return pxTopOfStack;
\r
207 /*-----------------------------------------------------------*/
\r
209 portBASE_TYPE xPortStartScheduler( void )
\r
211 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
212 this function is called. */
\r
213 prvSetupTimerInterrupt();
\r
215 /* Restore the context of the first task that is going to run. */
\r
216 portRESTORE_CONTEXT();
\r
218 /* Should not get here as the tasks are now running! */
\r
221 /*-----------------------------------------------------------*/
\r
223 void vPortEndScheduler( void )
\r
225 /* It is unlikely that the MSP430 port will get stopped. If required simply
\r
226 disable the tick interrupt here. */
\r
228 /*-----------------------------------------------------------*/
\r
231 * Manual context switch called by portYIELD or taskYIELD.
\r
233 * The first thing we do is save the registers so we can use a naked attribute.
\r
235 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
236 void vPortYield( void )
\r
238 /* We want the stack of the task being saved to look exactly as if the task
\r
239 was saved during a pre-emptive RTOS tick ISR. Before calling an ISR the
\r
240 msp430 places the status register onto the stack. As this is a function
\r
241 call and not an ISR we have to do this manually. */
\r
242 asm volatile ( "push r2" );
\r
245 /* Save the context of the current task. */
\r
246 portSAVE_CONTEXT();
\r
248 /* Switch to the highest priority task that is ready to run. */
\r
249 vTaskSwitchContext();
\r
251 /* Restore the context of the new task. */
\r
252 portRESTORE_CONTEXT();
\r
254 /*-----------------------------------------------------------*/
\r
257 * Hardware initialisation to generate the RTOS tick. This uses timer 0
\r
258 * but could alternatively use the watchdog timer or timer 1.
\r
260 static void prvSetupTimerInterrupt( void )
\r
262 /* Ensure the timer is stopped. */
\r
265 /* Run the timer of the ACLK. */
\r
268 /* Clear everything to start with. */
\r
271 /* Set the compare match value according to the tick rate we want. */
\r
272 TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
\r
274 /* Enable the interrupts. */
\r
277 /* Start up clean. */
\r
283 /*-----------------------------------------------------------*/
\r
286 * The interrupt service routine used depends on whether the pre-emptive
\r
287 * scheduler is being used or not.
\r
290 #if configUSE_PREEMPTION == 1
\r
293 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
294 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
295 * count is incremented after the context is saved.
\r
297 interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );
\r
298 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
300 /* Save the context of the interrupted task. */
\r
301 portSAVE_CONTEXT();
\r
303 /* Increment the tick count then switch to the highest priority task
\r
304 that is ready to run. */
\r
305 vTaskIncrementTick();
\r
306 vTaskSwitchContext();
\r
308 /* Restore the context of the new task. */
\r
309 portRESTORE_CONTEXT();
\r
315 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
316 * tick count. We don't need to switch context, this can only be done by
\r
317 * manual calls to taskYIELD();
\r
319 interrupt (TIMERA0_VECTOR) prvTickISR( void );
\r
320 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
322 vTaskIncrementTick();
\r