2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
32 + usCriticalNesting now has a volatile qualifier.
35 /* Standard includes. */
39 /* Scheduler includes. */
43 /*-----------------------------------------------------------
44 * Implementation of functions defined in portable.h for the MSP430 port.
45 *----------------------------------------------------------*/
47 /* Constants required for hardware setup. The tick ISR runs off the ACLK,
49 #define portACLK_FREQUENCY_HZ ( ( TickType_t ) 32768 )
50 #define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
51 #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x08 )
53 /* We require the address of the pxCurrentTCB variable, but don't want to know
54 * any details of its type. */
56 extern volatile TCB_t * volatile pxCurrentTCB;
58 /* Most ports implement critical sections by placing the interrupt flags on
59 * the stack before disabling interrupts. Exiting the critical section is then
60 * simply a case of popping the flags from the stack. As mspgcc does not use
61 * a frame pointer this cannot be done as modifying the stack will clobber all
62 * the stack variables. Instead each task maintains a count of the critical
63 * section nesting depth. Each time a critical section is entered the count is
64 * incremented. Each time a critical section is left the count is decremented -
65 * with interrupts only being re-enabled if the count is zero.
67 * usCriticalNesting will get set to zero when the scheduler starts, but must
68 * not be initialised to zero as this will cause problems during the startup
70 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
71 /*-----------------------------------------------------------*/
74 * Macro to save a task context to the task stack. This simply pushes all the
75 * general purpose msp430 registers onto the stack, followed by the
76 * usCriticalNesting value used by the task. Finally the resultant stack
77 * pointer value is saved into the task control block so it can be retrieved
78 * the next time the task executes.
80 #define portSAVE_CONTEXT() \
81 asm volatile ( "push r4 \n\t" \
93 "mov.w usCriticalNesting, r14 \n\t" \
95 "mov.w pxCurrentTCB, r12 \n\t" \
96 "mov.w r1, @r12 \n\t" \
100 * Macro to restore a task context from the task stack. This is effectively
101 * the reverse of portSAVE_CONTEXT(). First the stack pointer value is
102 * loaded from the task control block. Next the value for usCriticalNesting
103 * used by the task is retrieved from the stack - followed by the value of all
104 * the general purpose msp430 registers.
106 * The bic instruction ensures there are no low power bits set in the status
107 * register that is about to be popped from the stack.
109 #define portRESTORE_CONTEXT() \
110 asm volatile ( "mov.w pxCurrentTCB, r12 \n\t" \
111 "mov.w @r12, r1 \n\t" \
113 "mov.w r15, usCriticalNesting \n\t" \
126 "bic #(0xf0),0(r1) \n\t" \
129 /*-----------------------------------------------------------*/
132 * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
133 * could have alternatively used the watchdog timer or timer 1.
135 static void prvSetupTimerInterrupt( void );
136 /*-----------------------------------------------------------*/
139 * Initialise the stack of a task to look exactly as if a call to
140 * portSAVE_CONTEXT had been called.
142 * See the header file portable.h.
144 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
145 TaskFunction_t pxCode,
146 void * pvParameters )
149 * Place a few bytes of known values on the bottom of the stack.
150 * This is just useful for debugging and can be included if required.
152 * pxTopOfStack = ( StackType_t ) 0x1111;
154 * pxTopOfStack = ( StackType_t ) 0x2222;
156 * pxTopOfStack = ( StackType_t ) 0x3333;
160 /* The msp430 automatically pushes the PC then SR onto the stack before
161 * executing an ISR. We want the stack to look just as if this has happened
162 * so place a pointer to the start of the task on the stack first - followed
163 * by the flags we want the task to use when it starts up. */
164 *pxTopOfStack = ( StackType_t ) pxCode;
166 *pxTopOfStack = portFLAGS_INT_ENABLED;
169 /* Next the general purpose registers. */
170 *pxTopOfStack = ( StackType_t ) 0x4444;
172 *pxTopOfStack = ( StackType_t ) 0x5555;
174 *pxTopOfStack = ( StackType_t ) 0x6666;
176 *pxTopOfStack = ( StackType_t ) 0x7777;
178 *pxTopOfStack = ( StackType_t ) 0x8888;
180 *pxTopOfStack = ( StackType_t ) 0x9999;
182 *pxTopOfStack = ( StackType_t ) 0xaaaa;
184 *pxTopOfStack = ( StackType_t ) 0xbbbb;
187 *pxTopOfStack = ( StackType_t ) 0xcccc;
189 /* The MSP430 EABI expects the function parameter in R12. */
190 *pxTopOfStack = ( StackType_t ) pvParameters;
193 *pxTopOfStack = ( StackType_t ) 0xdddd;
195 *pxTopOfStack = ( StackType_t ) 0xeeee;
198 /* The mspgcc ABI expects the function parameter in R15. */
199 *pxTopOfStack = ( StackType_t ) pvParameters;
201 *pxTopOfStack = ( StackType_t ) 0xffff;
205 /* The code generated by the mspgcc compiler does not maintain separate
206 * stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
207 * use the stack as per other ports. Instead a variable is used to keep
208 * track of the critical section nesting. This variable has to be stored
209 * as part of the task context and is initially set to zero. */
210 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
212 /* Return a pointer to the top of the stack we have generated so this can
213 * be stored in the task control block for the task. */
216 /*-----------------------------------------------------------*/
218 BaseType_t xPortStartScheduler( void )
220 /* Setup the hardware to generate the tick. Interrupts are disabled when
221 * this function is called. */
222 prvSetupTimerInterrupt();
224 /* Restore the context of the first task that is going to run. */
225 portRESTORE_CONTEXT();
227 /* Should not get here as the tasks are now running! */
230 /*-----------------------------------------------------------*/
232 void vPortEndScheduler( void )
234 /* It is unlikely that the MSP430 port will get stopped. If required simply
235 * disable the tick interrupt here. */
237 /*-----------------------------------------------------------*/
240 * Manual context switch called by portYIELD or taskYIELD.
242 * The first thing we do is save the registers so we can use a naked attribute.
244 void vPortYield( void ) __attribute__( ( naked ) );
245 void vPortYield( void )
247 /* We want the stack of the task being saved to look exactly as if the task
248 * was saved during a pre-emptive RTOS tick ISR. Before calling an ISR the
249 * msp430 places the status register onto the stack. As this is a function
250 * call and not an ISR we have to do this manually. */
251 asm volatile ( "push r2" );
254 /* Save the context of the current task. */
257 /* Switch to the highest priority task that is ready to run. */
258 vTaskSwitchContext();
260 /* Restore the context of the new task. */
261 portRESTORE_CONTEXT();
263 /*-----------------------------------------------------------*/
266 * Hardware initialisation to generate the RTOS tick. This uses timer 0
267 * but could alternatively use the watchdog timer or timer 1.
269 static void prvSetupTimerInterrupt( void )
271 /* Ensure the timer is stopped. */
274 /* Run the timer of the ACLK. */
277 /* Clear everything to start with. */
280 /* Set the compare match value according to the tick rate we want. */
281 TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
283 /* Enable the interrupts. */
286 /* Start up clean. */
292 /*-----------------------------------------------------------*/
295 * The interrupt service routine used depends on whether the pre-emptive
296 * scheduler is being used or not.
299 #if configUSE_PREEMPTION == 1
302 * Tick ISR for preemptive scheduler. We can use a naked attribute as
303 * the context is saved at the start of vPortYieldFromTick(). The tick
304 * count is incremented after the context is saved.
306 interrupt( TIMERA0_VECTOR ) void prvTickISR( void ) __attribute__( ( naked ) );
307 interrupt( TIMERA0_VECTOR ) void prvTickISR( void )
309 /* Save the context of the interrupted task. */
312 /* Increment the tick count then switch to the highest priority task
313 * that is ready to run. */
314 if( xTaskIncrementTick() != pdFALSE )
316 vTaskSwitchContext();
319 /* Restore the context of the new task. */
320 portRESTORE_CONTEXT();
323 #else /* if configUSE_PREEMPTION == 1 */
326 * Tick ISR for the cooperative scheduler. All this does is increment the
327 * tick count. We don't need to switch context, this can only be done by
328 * manual calls to taskYIELD();
330 interrupt( TIMERA0_VECTOR ) void prvTickISR( void );
331 interrupt( TIMERA0_VECTOR ) void prvTickISR( void )
333 xTaskIncrementTick();
335 #endif /* if configUSE_PREEMPTION == 1 */