2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
78 + usCriticalNesting now has a volatile qualifier.
\r
81 /* Standard includes. */
\r
85 /* Scheduler includes. */
\r
86 #include "FreeRTOS.h"
\r
89 /*-----------------------------------------------------------
\r
90 * Implementation of functions defined in portable.h for the MSP430 port.
\r
91 *----------------------------------------------------------*/
\r
93 /* Constants required for hardware setup. The tick ISR runs off the ACLK,
\r
95 #define portACLK_FREQUENCY_HZ ( ( portTickType ) 32768 )
\r
96 #define portINITIAL_CRITICAL_NESTING ( ( unsigned short ) 10 )
\r
97 #define portFLAGS_INT_ENABLED ( ( portSTACK_TYPE ) 0x08 )
\r
99 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
100 any details of its type. */
\r
101 typedef void tskTCB;
\r
102 extern volatile tskTCB * volatile pxCurrentTCB;
\r
104 /* Most ports implement critical sections by placing the interrupt flags on
\r
105 the stack before disabling interrupts. Exiting the critical section is then
\r
106 simply a case of popping the flags from the stack. As mspgcc does not use
\r
107 a frame pointer this cannot be done as modifying the stack will clobber all
\r
108 the stack variables. Instead each task maintains a count of the critical
\r
109 section nesting depth. Each time a critical section is entered the count is
\r
110 incremented. Each time a critical section is left the count is decremented -
\r
111 with interrupts only being re-enabled if the count is zero.
\r
113 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
114 not be initialised to zero as this will cause problems during the startup
\r
116 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
117 /*-----------------------------------------------------------*/
\r
120 * Macro to save a task context to the task stack. This simply pushes all the
\r
121 * general purpose msp430 registers onto the stack, followed by the
\r
122 * usCriticalNesting value used by the task. Finally the resultant stack
\r
123 * pointer value is saved into the task control block so it can be retrieved
\r
124 * the next time the task executes.
\r
126 #define portSAVE_CONTEXT() \
\r
127 asm volatile ( "push r4 \n\t" \
\r
139 "mov.w usCriticalNesting, r14 \n\t" \
\r
141 "mov.w pxCurrentTCB, r12 \n\t" \
\r
142 "mov.w r1, @r12 \n\t" \
\r
146 * Macro to restore a task context from the task stack. This is effectively
\r
147 * the reverse of portSAVE_CONTEXT(). First the stack pointer value is
\r
148 * loaded from the task control block. Next the value for usCriticalNesting
\r
149 * used by the task is retrieved from the stack - followed by the value of all
\r
150 * the general purpose msp430 registers.
\r
152 * The bic instruction ensures there are no low power bits set in the status
\r
153 * register that is about to be popped from the stack.
\r
155 #define portRESTORE_CONTEXT() \
\r
156 asm volatile ( "mov.w pxCurrentTCB, r12 \n\t" \
\r
157 "mov.w @r12, r1 \n\t" \
\r
159 "mov.w r15, usCriticalNesting \n\t" \
\r
172 "bic #(0xf0),0(r1) \n\t" \
\r
175 /*-----------------------------------------------------------*/
\r
178 * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
\r
179 * could have alternatively used the watchdog timer or timer 1.
\r
181 static void prvSetupTimerInterrupt( void );
\r
182 /*-----------------------------------------------------------*/
\r
185 * Initialise the stack of a task to look exactly as if a call to
\r
186 * portSAVE_CONTEXT had been called.
\r
188 * See the header file portable.h.
\r
190 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
193 Place a few bytes of known values on the bottom of the stack.
\r
194 This is just useful for debugging and can be included if required.
\r
196 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;
\r
198 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;
\r
200 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333;
\r
204 /* The msp430 automatically pushes the PC then SR onto the stack before
\r
205 executing an ISR. We want the stack to look just as if this has happened
\r
206 so place a pointer to the start of the task on the stack first - followed
\r
207 by the flags we want the task to use when it starts up. */
\r
208 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
\r
210 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
213 /* Next the general purpose registers. */
\r
214 *pxTopOfStack = ( portSTACK_TYPE ) 0x4444;
\r
216 *pxTopOfStack = ( portSTACK_TYPE ) 0x5555;
\r
218 *pxTopOfStack = ( portSTACK_TYPE ) 0x6666;
\r
220 *pxTopOfStack = ( portSTACK_TYPE ) 0x7777;
\r
222 *pxTopOfStack = ( portSTACK_TYPE ) 0x8888;
\r
224 *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;
\r
226 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaa;
\r
228 *pxTopOfStack = ( portSTACK_TYPE ) 0xbbbb;
\r
230 *pxTopOfStack = ( portSTACK_TYPE ) 0xcccc;
\r
232 *pxTopOfStack = ( portSTACK_TYPE ) 0xdddd;
\r
234 *pxTopOfStack = ( portSTACK_TYPE ) 0xeeee;
\r
237 /* When the task starts is will expect to find the function parameter in
\r
239 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
242 /* The code generated by the mspgcc compiler does not maintain separate
\r
243 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
\r
244 use the stack as per other ports. Instead a variable is used to keep
\r
245 track of the critical section nesting. This variable has to be stored
\r
246 as part of the task context and is initially set to zero. */
\r
247 *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;
\r
249 /* Return a pointer to the top of the stack we have generated so this can
\r
250 be stored in the task control block for the task. */
\r
251 return pxTopOfStack;
\r
253 /*-----------------------------------------------------------*/
\r
255 portBASE_TYPE xPortStartScheduler( void )
\r
257 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
258 this function is called. */
\r
259 prvSetupTimerInterrupt();
\r
261 /* Restore the context of the first task that is going to run. */
\r
262 portRESTORE_CONTEXT();
\r
264 /* Should not get here as the tasks are now running! */
\r
267 /*-----------------------------------------------------------*/
\r
269 void vPortEndScheduler( void )
\r
271 /* It is unlikely that the MSP430 port will get stopped. If required simply
\r
272 disable the tick interrupt here. */
\r
274 /*-----------------------------------------------------------*/
\r
277 * Manual context switch called by portYIELD or taskYIELD.
\r
279 * The first thing we do is save the registers so we can use a naked attribute.
\r
281 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
282 void vPortYield( void )
\r
284 /* We want the stack of the task being saved to look exactly as if the task
\r
285 was saved during a pre-emptive RTOS tick ISR. Before calling an ISR the
\r
286 msp430 places the status register onto the stack. As this is a function
\r
287 call and not an ISR we have to do this manually. */
\r
288 asm volatile ( "push r2" );
\r
291 /* Save the context of the current task. */
\r
292 portSAVE_CONTEXT();
\r
294 /* Switch to the highest priority task that is ready to run. */
\r
295 vTaskSwitchContext();
\r
297 /* Restore the context of the new task. */
\r
298 portRESTORE_CONTEXT();
\r
300 /*-----------------------------------------------------------*/
\r
303 * Hardware initialisation to generate the RTOS tick. This uses timer 0
\r
304 * but could alternatively use the watchdog timer or timer 1.
\r
306 static void prvSetupTimerInterrupt( void )
\r
308 /* Ensure the timer is stopped. */
\r
311 /* Run the timer of the ACLK. */
\r
314 /* Clear everything to start with. */
\r
317 /* Set the compare match value according to the tick rate we want. */
\r
318 TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
\r
320 /* Enable the interrupts. */
\r
323 /* Start up clean. */
\r
329 /*-----------------------------------------------------------*/
\r
332 * The interrupt service routine used depends on whether the pre-emptive
\r
333 * scheduler is being used or not.
\r
336 #if configUSE_PREEMPTION == 1
\r
339 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
340 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
341 * count is incremented after the context is saved.
\r
343 interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );
\r
344 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
346 /* Save the context of the interrupted task. */
\r
347 portSAVE_CONTEXT();
\r
349 /* Increment the tick count then switch to the highest priority task
\r
350 that is ready to run. */
\r
351 vTaskIncrementTick();
\r
352 vTaskSwitchContext();
\r
354 /* Restore the context of the new task. */
\r
355 portRESTORE_CONTEXT();
\r
361 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
362 * tick count. We don't need to switch context, this can only be done by
\r
363 * manual calls to taskYIELD();
\r
365 interrupt (TIMERA0_VECTOR) prvTickISR( void );
\r
366 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
368 vTaskIncrementTick();
\r