2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
55 + usCriticalNesting now has a volatile qualifier.
\r
58 /* Standard includes. */
\r
62 /* Scheduler includes. */
\r
63 #include "FreeRTOS.h"
\r
66 /*-----------------------------------------------------------
\r
67 * Implementation of functions defined in portable.h for the MSP430 port.
\r
68 *----------------------------------------------------------*/
\r
70 /* Constants required for hardware setup. The tick ISR runs off the ACLK,
\r
72 #define portACLK_FREQUENCY_HZ ( ( portTickType ) 32768 )
\r
73 #define portINITIAL_CRITICAL_NESTING ( ( unsigned portSHORT ) 10 )
\r
74 #define portFLAGS_INT_ENABLED ( ( portSTACK_TYPE ) 0x08 )
\r
76 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
77 any details of its type. */
\r
78 typedef void tskTCB;
\r
79 extern volatile tskTCB * volatile pxCurrentTCB;
\r
81 /* Most ports implement critical sections by placing the interrupt flags on
\r
82 the stack before disabling interrupts. Exiting the critical section is then
\r
83 simply a case of popping the flags from the stack. As mspgcc does not use
\r
84 a frame pointer this cannot be done as modifying the stack will clobber all
\r
85 the stack variables. Instead each task maintains a count of the critical
\r
86 section nesting depth. Each time a critical section is entered the count is
\r
87 incremented. Each time a critical section is left the count is decremented -
\r
88 with interrupts only being re-enabled if the count is zero.
\r
90 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
91 not be initialised to zero as this will cause problems during the startup
\r
93 volatile unsigned portSHORT usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
94 /*-----------------------------------------------------------*/
\r
97 * Macro to save a task context to the task stack. This simply pushes all the
\r
98 * general purpose msp430 registers onto the stack, followed by the
\r
99 * usCriticalNesting value used by the task. Finally the resultant stack
\r
100 * pointer value is saved into the task control block so it can be retrieved
\r
101 * the next time the task executes.
\r
103 #define portSAVE_CONTEXT() \
\r
104 asm volatile ( "push r4 \n\t" \
\r
116 "mov.w usCriticalNesting, r14 \n\t" \
\r
118 "mov.w pxCurrentTCB, r12 \n\t" \
\r
119 "mov.w r1, @r12 \n\t" \
\r
123 * Macro to restore a task context from the task stack. This is effectively
\r
124 * the reverse of portSAVE_CONTEXT(). First the stack pointer value is
\r
125 * loaded from the task control block. Next the value for usCriticalNesting
\r
126 * used by the task is retrieved from the stack - followed by the value of all
\r
127 * the general purpose msp430 registers.
\r
129 * The bic instruction ensures there are no low power bits set in the status
\r
130 * register that is about to be popped from the stack.
\r
132 #define portRESTORE_CONTEXT() \
\r
133 asm volatile ( "mov.w pxCurrentTCB, r12 \n\t" \
\r
134 "mov.w @r12, r1 \n\t" \
\r
136 "mov.w r15, usCriticalNesting \n\t" \
\r
149 "bic #(0xf0),0(r1) \n\t" \
\r
152 /*-----------------------------------------------------------*/
\r
155 * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
\r
156 * could have alternatively used the watchdog timer or timer 1.
\r
158 static void prvSetupTimerInterrupt( void );
\r
159 /*-----------------------------------------------------------*/
\r
162 * Initialise the stack of a task to look exactly as if a call to
\r
163 * portSAVE_CONTEXT had been called.
\r
165 * See the header file portable.h.
\r
167 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
170 Place a few bytes of known values on the bottom of the stack.
\r
171 This is just useful for debugging and can be included if required.
\r
173 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;
\r
175 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;
\r
177 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333;
\r
181 /* The msp430 automatically pushes the PC then SR onto the stack before
\r
182 executing an ISR. We want the stack to look just as if this has happened
\r
183 so place a pointer to the start of the task on the stack first - followed
\r
184 by the flags we want the task to use when it starts up. */
\r
185 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
\r
187 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
190 /* Next the general purpose registers. */
\r
191 *pxTopOfStack = ( portSTACK_TYPE ) 0x4444;
\r
193 *pxTopOfStack = ( portSTACK_TYPE ) 0x5555;
\r
195 *pxTopOfStack = ( portSTACK_TYPE ) 0x6666;
\r
197 *pxTopOfStack = ( portSTACK_TYPE ) 0x7777;
\r
199 *pxTopOfStack = ( portSTACK_TYPE ) 0x8888;
\r
201 *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;
\r
203 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaa;
\r
205 *pxTopOfStack = ( portSTACK_TYPE ) 0xbbbb;
\r
207 *pxTopOfStack = ( portSTACK_TYPE ) 0xcccc;
\r
209 *pxTopOfStack = ( portSTACK_TYPE ) 0xdddd;
\r
211 *pxTopOfStack = ( portSTACK_TYPE ) 0xeeee;
\r
214 /* When the task starts is will expect to find the function parameter in
\r
216 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
219 /* The code generated by the mspgcc compiler does not maintain separate
\r
220 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
\r
221 use the stack as per other ports. Instead a variable is used to keep
\r
222 track of the critical section nesting. This variable has to be stored
\r
223 as part of the task context and is initially set to zero. */
\r
224 *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;
\r
226 /* Return a pointer to the top of the stack we have generated so this can
\r
227 be stored in the task control block for the task. */
\r
228 return pxTopOfStack;
\r
230 /*-----------------------------------------------------------*/
\r
232 portBASE_TYPE xPortStartScheduler( void )
\r
234 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
235 this function is called. */
\r
236 prvSetupTimerInterrupt();
\r
238 /* Restore the context of the first task that is going to run. */
\r
239 portRESTORE_CONTEXT();
\r
241 /* Should not get here as the tasks are now running! */
\r
244 /*-----------------------------------------------------------*/
\r
246 void vPortEndScheduler( void )
\r
248 /* It is unlikely that the MSP430 port will get stopped. If required simply
\r
249 disable the tick interrupt here. */
\r
251 /*-----------------------------------------------------------*/
\r
254 * Manual context switch called by portYIELD or taskYIELD.
\r
256 * The first thing we do is save the registers so we can use a naked attribute.
\r
258 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
259 void vPortYield( void )
\r
261 /* We want the stack of the task being saved to look exactly as if the task
\r
262 was saved during a pre-emptive RTOS tick ISR. Before calling an ISR the
\r
263 msp430 places the status register onto the stack. As this is a function
\r
264 call and not an ISR we have to do this manually. */
\r
265 asm volatile ( "push r2" );
\r
268 /* Save the context of the current task. */
\r
269 portSAVE_CONTEXT();
\r
271 /* Switch to the highest priority task that is ready to run. */
\r
272 vTaskSwitchContext();
\r
274 /* Restore the context of the new task. */
\r
275 portRESTORE_CONTEXT();
\r
277 /*-----------------------------------------------------------*/
\r
280 * Hardware initialisation to generate the RTOS tick. This uses timer 0
\r
281 * but could alternatively use the watchdog timer or timer 1.
\r
283 static void prvSetupTimerInterrupt( void )
\r
285 /* Ensure the timer is stopped. */
\r
288 /* Run the timer of the ACLK. */
\r
291 /* Clear everything to start with. */
\r
294 /* Set the compare match value according to the tick rate we want. */
\r
295 TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
\r
297 /* Enable the interrupts. */
\r
300 /* Start up clean. */
\r
306 /*-----------------------------------------------------------*/
\r
309 * The interrupt service routine used depends on whether the pre-emptive
\r
310 * scheduler is being used or not.
\r
313 #if configUSE_PREEMPTION == 1
\r
316 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
317 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
318 * count is incremented after the context is saved.
\r
320 interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );
\r
321 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
323 /* Save the context of the interrupted task. */
\r
324 portSAVE_CONTEXT();
\r
326 /* Increment the tick count then switch to the highest priority task
\r
327 that is ready to run. */
\r
328 vTaskIncrementTick();
\r
329 vTaskSwitchContext();
\r
331 /* Restore the context of the new task. */
\r
332 portRESTORE_CONTEXT();
\r
338 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
339 * tick count. We don't need to switch context, this can only be done by
\r
340 * manual calls to taskYIELD();
\r
342 interrupt (TIMERA0_VECTOR) prvTickISR( void );
\r
343 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
345 vTaskIncrementTick();
\r