2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
31 + usCriticalNesting now has a volatile qualifier.
34 /* Standard includes. */
38 /* Scheduler includes. */
42 /*-----------------------------------------------------------
43 * Implementation of functions defined in portable.h for the MSP430 port.
44 *----------------------------------------------------------*/
46 /* Constants required for hardware setup. The tick ISR runs off the ACLK,
48 #define portACLK_FREQUENCY_HZ ( ( TickType_t ) 32768 )
49 #define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
50 #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x08 )
52 /* We require the address of the pxCurrentTCB variable, but don't want to know
53 any details of its type. */
55 extern volatile TCB_t * volatile pxCurrentTCB;
57 /* Most ports implement critical sections by placing the interrupt flags on
58 the stack before disabling interrupts. Exiting the critical section is then
59 simply a case of popping the flags from the stack. As mspgcc does not use
60 a frame pointer this cannot be done as modifying the stack will clobber all
61 the stack variables. Instead each task maintains a count of the critical
62 section nesting depth. Each time a critical section is entered the count is
63 incremented. Each time a critical section is left the count is decremented -
64 with interrupts only being re-enabled if the count is zero.
66 usCriticalNesting will get set to zero when the scheduler starts, but must
67 not be initialised to zero as this will cause problems during the startup
69 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
70 /*-----------------------------------------------------------*/
73 * Macro to save a task context to the task stack. This simply pushes all the
74 * general purpose msp430 registers onto the stack, followed by the
75 * usCriticalNesting value used by the task. Finally the resultant stack
76 * pointer value is saved into the task control block so it can be retrieved
77 * the next time the task executes.
79 #define portSAVE_CONTEXT() \
80 asm volatile ( "push r4 \n\t" \
92 "mov.w usCriticalNesting, r14 \n\t" \
94 "mov.w pxCurrentTCB, r12 \n\t" \
95 "mov.w r1, @r12 \n\t" \
99 * Macro to restore a task context from the task stack. This is effectively
100 * the reverse of portSAVE_CONTEXT(). First the stack pointer value is
101 * loaded from the task control block. Next the value for usCriticalNesting
102 * used by the task is retrieved from the stack - followed by the value of all
103 * the general purpose msp430 registers.
105 * The bic instruction ensures there are no low power bits set in the status
106 * register that is about to be popped from the stack.
108 #define portRESTORE_CONTEXT() \
109 asm volatile ( "mov.w pxCurrentTCB, r12 \n\t" \
110 "mov.w @r12, r1 \n\t" \
112 "mov.w r15, usCriticalNesting \n\t" \
125 "bic #(0xf0),0(r1) \n\t" \
128 /*-----------------------------------------------------------*/
131 * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
132 * could have alternatively used the watchdog timer or timer 1.
134 static void prvSetupTimerInterrupt( void );
135 /*-----------------------------------------------------------*/
138 * Initialise the stack of a task to look exactly as if a call to
139 * portSAVE_CONTEXT had been called.
141 * See the header file portable.h.
143 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
146 Place a few bytes of known values on the bottom of the stack.
147 This is just useful for debugging and can be included if required.
149 *pxTopOfStack = ( StackType_t ) 0x1111;
151 *pxTopOfStack = ( StackType_t ) 0x2222;
153 *pxTopOfStack = ( StackType_t ) 0x3333;
157 /* The msp430 automatically pushes the PC then SR onto the stack before
158 executing an ISR. We want the stack to look just as if this has happened
159 so place a pointer to the start of the task on the stack first - followed
160 by the flags we want the task to use when it starts up. */
161 *pxTopOfStack = ( StackType_t ) pxCode;
163 *pxTopOfStack = portFLAGS_INT_ENABLED;
166 /* Next the general purpose registers. */
167 *pxTopOfStack = ( StackType_t ) 0x4444;
169 *pxTopOfStack = ( StackType_t ) 0x5555;
171 *pxTopOfStack = ( StackType_t ) 0x6666;
173 *pxTopOfStack = ( StackType_t ) 0x7777;
175 *pxTopOfStack = ( StackType_t ) 0x8888;
177 *pxTopOfStack = ( StackType_t ) 0x9999;
179 *pxTopOfStack = ( StackType_t ) 0xaaaa;
181 *pxTopOfStack = ( StackType_t ) 0xbbbb;
183 *pxTopOfStack = ( StackType_t ) 0xcccc;
185 *pxTopOfStack = ( StackType_t ) 0xdddd;
187 *pxTopOfStack = ( StackType_t ) 0xeeee;
190 /* When the task starts is will expect to find the function parameter in
192 *pxTopOfStack = ( StackType_t ) pvParameters;
195 /* The code generated by the mspgcc compiler does not maintain separate
196 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
197 use the stack as per other ports. Instead a variable is used to keep
198 track of the critical section nesting. This variable has to be stored
199 as part of the task context and is initially set to zero. */
200 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
202 /* Return a pointer to the top of the stack we have generated so this can
203 be stored in the task control block for the task. */
206 /*-----------------------------------------------------------*/
208 BaseType_t xPortStartScheduler( void )
210 /* Setup the hardware to generate the tick. Interrupts are disabled when
211 this function is called. */
212 prvSetupTimerInterrupt();
214 /* Restore the context of the first task that is going to run. */
215 portRESTORE_CONTEXT();
217 /* Should not get here as the tasks are now running! */
220 /*-----------------------------------------------------------*/
222 void vPortEndScheduler( void )
224 /* It is unlikely that the MSP430 port will get stopped. If required simply
225 disable the tick interrupt here. */
227 /*-----------------------------------------------------------*/
230 * Manual context switch called by portYIELD or taskYIELD.
232 * The first thing we do is save the registers so we can use a naked attribute.
234 void vPortYield( void ) __attribute__ ( ( naked ) );
235 void vPortYield( void )
237 /* We want the stack of the task being saved to look exactly as if the task
238 was saved during a pre-emptive RTOS tick ISR. Before calling an ISR the
239 msp430 places the status register onto the stack. As this is a function
240 call and not an ISR we have to do this manually. */
241 asm volatile ( "push r2" );
244 /* Save the context of the current task. */
247 /* Switch to the highest priority task that is ready to run. */
248 vTaskSwitchContext();
250 /* Restore the context of the new task. */
251 portRESTORE_CONTEXT();
253 /*-----------------------------------------------------------*/
256 * Hardware initialisation to generate the RTOS tick. This uses timer 0
257 * but could alternatively use the watchdog timer or timer 1.
259 static void prvSetupTimerInterrupt( void )
261 /* Ensure the timer is stopped. */
264 /* Run the timer of the ACLK. */
267 /* Clear everything to start with. */
270 /* Set the compare match value according to the tick rate we want. */
271 TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
273 /* Enable the interrupts. */
276 /* Start up clean. */
282 /*-----------------------------------------------------------*/
285 * The interrupt service routine used depends on whether the pre-emptive
286 * scheduler is being used or not.
289 #if configUSE_PREEMPTION == 1
292 * Tick ISR for preemptive scheduler. We can use a naked attribute as
293 * the context is saved at the start of vPortYieldFromTick(). The tick
294 * count is incremented after the context is saved.
296 interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );
297 interrupt (TIMERA0_VECTOR) prvTickISR( void )
299 /* Save the context of the interrupted task. */
302 /* Increment the tick count then switch to the highest priority task
303 that is ready to run. */
304 if( xTaskIncrementTick() != pdFALSE )
306 vTaskSwitchContext();
309 /* Restore the context of the new task. */
310 portRESTORE_CONTEXT();
316 * Tick ISR for the cooperative scheduler. All this does is increment the
317 * tick count. We don't need to switch context, this can only be done by
318 * manual calls to taskYIELD();
320 interrupt (TIMERA0_VECTOR) prvTickISR( void );
321 interrupt (TIMERA0_VECTOR) prvTickISR( void )
323 xTaskIncrementTick();