2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 + usCriticalNesting now has a volatile qualifier.
\r
73 /* Standard includes. */
\r
77 /* Scheduler includes. */
\r
78 #include "FreeRTOS.h"
\r
81 /*-----------------------------------------------------------
\r
82 * Implementation of functions defined in portable.h for the MSP430 port.
\r
83 *----------------------------------------------------------*/
\r
85 /* Constants required for hardware setup. The tick ISR runs off the ACLK,
\r
87 #define portACLK_FREQUENCY_HZ ( ( portTickType ) 32768 )
\r
88 #define portINITIAL_CRITICAL_NESTING ( ( unsigned short ) 10 )
\r
89 #define portFLAGS_INT_ENABLED ( ( portSTACK_TYPE ) 0x08 )
\r
91 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
92 any details of its type. */
\r
93 typedef void tskTCB;
\r
94 extern volatile tskTCB * volatile pxCurrentTCB;
\r
96 /* Most ports implement critical sections by placing the interrupt flags on
\r
97 the stack before disabling interrupts. Exiting the critical section is then
\r
98 simply a case of popping the flags from the stack. As mspgcc does not use
\r
99 a frame pointer this cannot be done as modifying the stack will clobber all
\r
100 the stack variables. Instead each task maintains a count of the critical
\r
101 section nesting depth. Each time a critical section is entered the count is
\r
102 incremented. Each time a critical section is left the count is decremented -
\r
103 with interrupts only being re-enabled if the count is zero.
\r
105 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
106 not be initialised to zero as this will cause problems during the startup
\r
108 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
109 /*-----------------------------------------------------------*/
\r
112 * Macro to save a task context to the task stack. This simply pushes all the
\r
113 * general purpose msp430 registers onto the stack, followed by the
\r
114 * usCriticalNesting value used by the task. Finally the resultant stack
\r
115 * pointer value is saved into the task control block so it can be retrieved
\r
116 * the next time the task executes.
\r
118 #define portSAVE_CONTEXT() \
\r
119 asm volatile ( "push r4 \n\t" \
\r
131 "mov.w usCriticalNesting, r14 \n\t" \
\r
133 "mov.w pxCurrentTCB, r12 \n\t" \
\r
134 "mov.w r1, @r12 \n\t" \
\r
138 * Macro to restore a task context from the task stack. This is effectively
\r
139 * the reverse of portSAVE_CONTEXT(). First the stack pointer value is
\r
140 * loaded from the task control block. Next the value for usCriticalNesting
\r
141 * used by the task is retrieved from the stack - followed by the value of all
\r
142 * the general purpose msp430 registers.
\r
144 * The bic instruction ensures there are no low power bits set in the status
\r
145 * register that is about to be popped from the stack.
\r
147 #define portRESTORE_CONTEXT() \
\r
148 asm volatile ( "mov.w pxCurrentTCB, r12 \n\t" \
\r
149 "mov.w @r12, r1 \n\t" \
\r
151 "mov.w r15, usCriticalNesting \n\t" \
\r
164 "bic #(0xf0),0(r1) \n\t" \
\r
167 /*-----------------------------------------------------------*/
\r
170 * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
\r
171 * could have alternatively used the watchdog timer or timer 1.
\r
173 static void prvSetupTimerInterrupt( void );
\r
174 /*-----------------------------------------------------------*/
\r
177 * Initialise the stack of a task to look exactly as if a call to
\r
178 * portSAVE_CONTEXT had been called.
\r
180 * See the header file portable.h.
\r
182 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
185 Place a few bytes of known values on the bottom of the stack.
\r
186 This is just useful for debugging and can be included if required.
\r
188 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;
\r
190 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;
\r
192 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333;
\r
196 /* The msp430 automatically pushes the PC then SR onto the stack before
\r
197 executing an ISR. We want the stack to look just as if this has happened
\r
198 so place a pointer to the start of the task on the stack first - followed
\r
199 by the flags we want the task to use when it starts up. */
\r
200 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
\r
202 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
205 /* Next the general purpose registers. */
\r
206 *pxTopOfStack = ( portSTACK_TYPE ) 0x4444;
\r
208 *pxTopOfStack = ( portSTACK_TYPE ) 0x5555;
\r
210 *pxTopOfStack = ( portSTACK_TYPE ) 0x6666;
\r
212 *pxTopOfStack = ( portSTACK_TYPE ) 0x7777;
\r
214 *pxTopOfStack = ( portSTACK_TYPE ) 0x8888;
\r
216 *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;
\r
218 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaa;
\r
220 *pxTopOfStack = ( portSTACK_TYPE ) 0xbbbb;
\r
222 *pxTopOfStack = ( portSTACK_TYPE ) 0xcccc;
\r
224 *pxTopOfStack = ( portSTACK_TYPE ) 0xdddd;
\r
226 *pxTopOfStack = ( portSTACK_TYPE ) 0xeeee;
\r
229 /* When the task starts is will expect to find the function parameter in
\r
231 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
234 /* The code generated by the mspgcc compiler does not maintain separate
\r
235 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
\r
236 use the stack as per other ports. Instead a variable is used to keep
\r
237 track of the critical section nesting. This variable has to be stored
\r
238 as part of the task context and is initially set to zero. */
\r
239 *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;
\r
241 /* Return a pointer to the top of the stack we have generated so this can
\r
242 be stored in the task control block for the task. */
\r
243 return pxTopOfStack;
\r
245 /*-----------------------------------------------------------*/
\r
247 portBASE_TYPE xPortStartScheduler( void )
\r
249 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
250 this function is called. */
\r
251 prvSetupTimerInterrupt();
\r
253 /* Restore the context of the first task that is going to run. */
\r
254 portRESTORE_CONTEXT();
\r
256 /* Should not get here as the tasks are now running! */
\r
259 /*-----------------------------------------------------------*/
\r
261 void vPortEndScheduler( void )
\r
263 /* It is unlikely that the MSP430 port will get stopped. If required simply
\r
264 disable the tick interrupt here. */
\r
266 /*-----------------------------------------------------------*/
\r
269 * Manual context switch called by portYIELD or taskYIELD.
\r
271 * The first thing we do is save the registers so we can use a naked attribute.
\r
273 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
274 void vPortYield( void )
\r
276 /* We want the stack of the task being saved to look exactly as if the task
\r
277 was saved during a pre-emptive RTOS tick ISR. Before calling an ISR the
\r
278 msp430 places the status register onto the stack. As this is a function
\r
279 call and not an ISR we have to do this manually. */
\r
280 asm volatile ( "push r2" );
\r
283 /* Save the context of the current task. */
\r
284 portSAVE_CONTEXT();
\r
286 /* Switch to the highest priority task that is ready to run. */
\r
287 vTaskSwitchContext();
\r
289 /* Restore the context of the new task. */
\r
290 portRESTORE_CONTEXT();
\r
292 /*-----------------------------------------------------------*/
\r
295 * Hardware initialisation to generate the RTOS tick. This uses timer 0
\r
296 * but could alternatively use the watchdog timer or timer 1.
\r
298 static void prvSetupTimerInterrupt( void )
\r
300 /* Ensure the timer is stopped. */
\r
303 /* Run the timer of the ACLK. */
\r
306 /* Clear everything to start with. */
\r
309 /* Set the compare match value according to the tick rate we want. */
\r
310 TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
\r
312 /* Enable the interrupts. */
\r
315 /* Start up clean. */
\r
321 /*-----------------------------------------------------------*/
\r
324 * The interrupt service routine used depends on whether the pre-emptive
\r
325 * scheduler is being used or not.
\r
328 #if configUSE_PREEMPTION == 1
\r
331 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
332 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
333 * count is incremented after the context is saved.
\r
335 interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );
\r
336 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
338 /* Save the context of the interrupted task. */
\r
339 portSAVE_CONTEXT();
\r
341 /* Increment the tick count then switch to the highest priority task
\r
342 that is ready to run. */
\r
343 vTaskIncrementTick();
\r
344 vTaskSwitchContext();
\r
346 /* Restore the context of the new task. */
\r
347 portRESTORE_CONTEXT();
\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
357 interrupt (TIMERA0_VECTOR) prvTickISR( void );
\r
358 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
360 vTaskIncrementTick();
\r