2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
73 + usCriticalNesting now has a volatile qualifier.
\r
76 /* Standard includes. */
\r
80 /* Scheduler includes. */
\r
81 #include "FreeRTOS.h"
\r
84 /*-----------------------------------------------------------
\r
85 * Implementation of functions defined in portable.h for the MSP430 port.
\r
86 *----------------------------------------------------------*/
\r
88 /* Constants required for hardware setup. The tick ISR runs off the ACLK,
\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
94 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
95 any details of its type. */
\r
97 extern volatile TCB_t * volatile pxCurrentTCB;
\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
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
111 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
112 /*-----------------------------------------------------------*/
\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
121 #define portSAVE_CONTEXT() \
\r
122 asm volatile ( "push r4 \n\t" \
\r
134 "mov.w usCriticalNesting, r14 \n\t" \
\r
136 "mov.w pxCurrentTCB, r12 \n\t" \
\r
137 "mov.w r1, @r12 \n\t" \
\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
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
150 #define portRESTORE_CONTEXT() \
\r
151 asm volatile ( "mov.w pxCurrentTCB, r12 \n\t" \
\r
152 "mov.w @r12, r1 \n\t" \
\r
154 "mov.w r15, usCriticalNesting \n\t" \
\r
167 "bic #(0xf0),0(r1) \n\t" \
\r
170 /*-----------------------------------------------------------*/
\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
176 static void prvSetupTimerInterrupt( void );
\r
177 /*-----------------------------------------------------------*/
\r
180 * Initialise the stack of a task to look exactly as if a call to
\r
181 * portSAVE_CONTEXT had been called.
\r
183 * See the header file portable.h.
\r
185 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\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
191 *pxTopOfStack = ( StackType_t ) 0x1111;
\r
193 *pxTopOfStack = ( StackType_t ) 0x2222;
\r
195 *pxTopOfStack = ( StackType_t ) 0x3333;
\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
205 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
208 /* Next the general purpose registers. */
\r
209 *pxTopOfStack = ( StackType_t ) 0x4444;
\r
211 *pxTopOfStack = ( StackType_t ) 0x5555;
\r
213 *pxTopOfStack = ( StackType_t ) 0x6666;
\r
215 *pxTopOfStack = ( StackType_t ) 0x7777;
\r
217 *pxTopOfStack = ( StackType_t ) 0x8888;
\r
219 *pxTopOfStack = ( StackType_t ) 0x9999;
\r
221 *pxTopOfStack = ( StackType_t ) 0xaaaa;
\r
223 *pxTopOfStack = ( StackType_t ) 0xbbbb;
\r
225 *pxTopOfStack = ( StackType_t ) 0xcccc;
\r
227 *pxTopOfStack = ( StackType_t ) 0xdddd;
\r
229 *pxTopOfStack = ( StackType_t ) 0xeeee;
\r
232 /* When the task starts is will expect to find the function parameter in
\r
234 *pxTopOfStack = ( StackType_t ) pvParameters;
\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
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
248 /*-----------------------------------------------------------*/
\r
250 BaseType_t xPortStartScheduler( void )
\r
252 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
253 this function is called. */
\r
254 prvSetupTimerInterrupt();
\r
256 /* Restore the context of the first task that is going to run. */
\r
257 portRESTORE_CONTEXT();
\r
259 /* Should not get here as the tasks are now running! */
\r
262 /*-----------------------------------------------------------*/
\r
264 void vPortEndScheduler( void )
\r
266 /* It is unlikely that the MSP430 port will get stopped. If required simply
\r
267 disable the tick interrupt here. */
\r
269 /*-----------------------------------------------------------*/
\r
272 * Manual context switch called by portYIELD or taskYIELD.
\r
274 * The first thing we do is save the registers so we can use a naked attribute.
\r
276 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
277 void vPortYield( void )
\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
286 /* Save the context of the current task. */
\r
287 portSAVE_CONTEXT();
\r
289 /* Switch to the highest priority task that is ready to run. */
\r
290 vTaskSwitchContext();
\r
292 /* Restore the context of the new task. */
\r
293 portRESTORE_CONTEXT();
\r
295 /*-----------------------------------------------------------*/
\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
301 static void prvSetupTimerInterrupt( void )
\r
303 /* Ensure the timer is stopped. */
\r
306 /* Run the timer of the ACLK. */
\r
309 /* Clear everything to start with. */
\r
312 /* Set the compare match value according to the tick rate we want. */
\r
313 TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
\r
315 /* Enable the interrupts. */
\r
318 /* Start up clean. */
\r
324 /*-----------------------------------------------------------*/
\r
327 * The interrupt service routine used depends on whether the pre-emptive
\r
328 * scheduler is being used or not.
\r
331 #if configUSE_PREEMPTION == 1
\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
338 interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );
\r
339 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
341 /* Save the context of the interrupted task. */
\r
342 portSAVE_CONTEXT();
\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
348 vTaskSwitchContext();
\r
351 /* Restore the context of the new task. */
\r
352 portRESTORE_CONTEXT();
\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
362 interrupt (TIMERA0_VECTOR) prvTickISR( void );
\r
363 interrupt (TIMERA0_VECTOR) prvTickISR( void )
\r
365 xTaskIncrementTick();
\r