1 /*
2  * FreeRTOS Kernel V11.1.0
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
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.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28 
29 /*
30  *  Changes from V2.5.2
31  *
32  + usCriticalNesting now has a volatile qualifier.
33  */
34 
35 /* Standard includes. */
36 #include <stdlib.h>
37 #include <signal.h>
38 
39 /* Scheduler includes. */
40 #include "FreeRTOS.h"
41 #include "task.h"
42 
43 /*-----------------------------------------------------------
44 * Implementation of functions defined in portable.h for the MSP430 port.
45 *----------------------------------------------------------*/
46 
47 /* Constants required for hardware setup.  The tick ISR runs off the ACLK,
48  * not the MCLK. */
49 #define portACLK_FREQUENCY_HZ           ( ( TickType_t ) 32768 )
50 #define portINITIAL_CRITICAL_NESTING    ( ( uint16_t ) 10 )
51 #define portFLAGS_INT_ENABLED           ( ( StackType_t ) 0x08 )
52 
53 /* We require the address of the pxCurrentTCB variable, but don't want to know
54  * any details of its type. */
55 typedef void TCB_t;
56 extern volatile TCB_t * volatile pxCurrentTCB;
57 
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.
66  *
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
69  * sequence. */
70 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
71 /*-----------------------------------------------------------*/
72 
73 /*
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.
79  */
80 #define portSAVE_CONTEXT()                               \
81     asm volatile ( "push   r4                      \n\t" \
82                    "push   r5                      \n\t" \
83                    "push   r6                      \n\t" \
84                    "push   r7                      \n\t" \
85                    "push   r8                      \n\t" \
86                    "push   r9                      \n\t" \
87                    "push   r10                     \n\t" \
88                    "push   r11                     \n\t" \
89                    "push   r12                     \n\t" \
90                    "push   r13                     \n\t" \
91                    "push   r14                     \n\t" \
92                    "push   r15                     \n\t" \
93                    "mov.w  usCriticalNesting, r14  \n\t" \
94                    "push   r14                     \n\t" \
95                    "mov.w  pxCurrentTCB, r12       \n\t" \
96                    "mov.w  r1, @r12                \n\t" \
97                    );
98 
99 /*
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.
105  *
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.
108  */
109 #define portRESTORE_CONTEXT()                            \
110     asm volatile ( "mov.w  pxCurrentTCB, r12       \n\t" \
111                    "mov.w  @r12, r1                \n\t" \
112                    "pop    r15                     \n\t" \
113                    "mov.w  r15, usCriticalNesting  \n\t" \
114                    "pop    r15                     \n\t" \
115                    "pop    r14                     \n\t" \
116                    "pop    r13                     \n\t" \
117                    "pop    r12                     \n\t" \
118                    "pop    r11                     \n\t" \
119                    "pop    r10                     \n\t" \
120                    "pop    r9                      \n\t" \
121                    "pop    r8                      \n\t" \
122                    "pop    r7                      \n\t" \
123                    "pop    r6                      \n\t" \
124                    "pop    r5                      \n\t" \
125                    "pop    r4                      \n\t" \
126                    "bic    #(0xf0),0(r1)           \n\t" \
127                    "reti                           \n\t" \
128                    );
129 /*-----------------------------------------------------------*/
130 
131 /*
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.
134  */
135 static void prvSetupTimerInterrupt( void );
136 /*-----------------------------------------------------------*/
137 
138 /*
139  * Initialise the stack of a task to look exactly as if a call to
140  * portSAVE_CONTEXT had been called.
141  *
142  * See the header file portable.h.
143  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)144 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
145                                      TaskFunction_t pxCode,
146                                      void * pvParameters )
147 {
148     /*
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.
151      *
152      * pxTopOfStack = ( StackType_t ) 0x1111;
153      *  pxTopOfStack--;
154      * pxTopOfStack = ( StackType_t ) 0x2222;
155      *  pxTopOfStack--;
156      * pxTopOfStack = ( StackType_t ) 0x3333;
157      *  pxTopOfStack--;
158      */
159 
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;
165     pxTopOfStack--;
166     *pxTopOfStack = portFLAGS_INT_ENABLED;
167     pxTopOfStack--;
168 
169     /* Next the general purpose registers. */
170     *pxTopOfStack = ( StackType_t ) 0x4444;
171     pxTopOfStack--;
172     *pxTopOfStack = ( StackType_t ) 0x5555;
173     pxTopOfStack--;
174     *pxTopOfStack = ( StackType_t ) 0x6666;
175     pxTopOfStack--;
176     *pxTopOfStack = ( StackType_t ) 0x7777;
177     pxTopOfStack--;
178     *pxTopOfStack = ( StackType_t ) 0x8888;
179     pxTopOfStack--;
180     *pxTopOfStack = ( StackType_t ) 0x9999;
181     pxTopOfStack--;
182     *pxTopOfStack = ( StackType_t ) 0xaaaa;
183     pxTopOfStack--;
184     *pxTopOfStack = ( StackType_t ) 0xbbbb;
185     pxTopOfStack--;
186 #ifdef __MSPGCC__
187     *pxTopOfStack = ( StackType_t ) 0xcccc;
188 #else
189     /* The MSP430 EABI expects the function parameter in R12. */
190     *pxTopOfStack = ( StackType_t ) pvParameters;
191 #endif
192     pxTopOfStack--;
193     *pxTopOfStack = ( StackType_t ) 0xdddd;
194     pxTopOfStack--;
195     *pxTopOfStack = ( StackType_t ) 0xeeee;
196     pxTopOfStack--;
197 #ifdef __MSPGCC__
198     /* The mspgcc ABI expects the function parameter in R15. */
199     *pxTopOfStack = ( StackType_t ) pvParameters;
200 #else
201     *pxTopOfStack = ( StackType_t ) 0xffff;
202 #endif
203     pxTopOfStack--;
204 
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;
211 
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. */
214     return pxTopOfStack;
215 }
216 /*-----------------------------------------------------------*/
217 
xPortStartScheduler(void)218 BaseType_t xPortStartScheduler( void )
219 {
220     /* Setup the hardware to generate the tick.  Interrupts are disabled when
221      * this function is called. */
222     prvSetupTimerInterrupt();
223 
224     /* Restore the context of the first task that is going to run. */
225     portRESTORE_CONTEXT();
226 
227     /* Should not get here as the tasks are now running! */
228     return pdTRUE;
229 }
230 /*-----------------------------------------------------------*/
231 
vPortEndScheduler(void)232 void vPortEndScheduler( void )
233 {
234     /* It is unlikely that the MSP430 port will get stopped.  If required simply
235      * disable the tick interrupt here. */
236 }
237 /*-----------------------------------------------------------*/
238 
239 /*
240  * Manual context switch called by portYIELD or taskYIELD.
241  *
242  * The first thing we do is save the registers so we can use a naked attribute.
243  */
244 void vPortYield( void ) __attribute__( ( naked ) );
vPortYield(void)245 void vPortYield( void )
246 {
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" );
252     _DINT();
253 
254     /* Save the context of the current task. */
255     portSAVE_CONTEXT();
256 
257     /* Switch to the highest priority task that is ready to run. */
258     vTaskSwitchContext();
259 
260     /* Restore the context of the new task. */
261     portRESTORE_CONTEXT();
262 }
263 /*-----------------------------------------------------------*/
264 
265 /*
266  * Hardware initialisation to generate the RTOS tick.  This uses timer 0
267  * but could alternatively use the watchdog timer or timer 1.
268  */
prvSetupTimerInterrupt(void)269 static void prvSetupTimerInterrupt( void )
270 {
271     /* Ensure the timer is stopped. */
272     TACTL = 0;
273 
274     /* Run the timer of the ACLK. */
275     TACTL = TASSEL_1;
276 
277     /* Clear everything to start with. */
278     TACTL |= TACLR;
279 
280     /* Set the compare match value according to the tick rate we want. */
281     TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
282 
283     /* Enable the interrupts. */
284     TACCTL0 = CCIE;
285 
286     /* Start up clean. */
287     TACTL |= TACLR;
288 
289     /* Up mode. */
290     TACTL |= MC_1;
291 }
292 /*-----------------------------------------------------------*/
293 
294 /*
295  * The interrupt service routine used depends on whether the pre-emptive
296  * scheduler is being used or not.
297  */
298 
299 #if configUSE_PREEMPTION == 1
300 
301 /*
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.
305  */
306     interrupt( TIMERA0_VECTOR ) void prvTickISR( void ) __attribute__( ( naked ) );
interrupt(TIMERA0_VECTOR)307     interrupt( TIMERA0_VECTOR ) void prvTickISR( void )
308     {
309         /* Save the context of the interrupted task. */
310         portSAVE_CONTEXT();
311 
312         /* Increment the tick count then switch to the highest priority task
313          * that is ready to run. */
314         if( xTaskIncrementTick() != pdFALSE )
315         {
316             vTaskSwitchContext();
317         }
318 
319         /* Restore the context of the new task. */
320         portRESTORE_CONTEXT();
321     }
322 
323 #else /* if configUSE_PREEMPTION == 1 */
324 
325 /*
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();
329  */
330     interrupt( TIMERA0_VECTOR ) void prvTickISR( void );
interrupt(TIMERA0_VECTOR)331     interrupt( TIMERA0_VECTOR ) void prvTickISR( void )
332     {
333         xTaskIncrementTick();
334     }
335 #endif /* if configUSE_PREEMPTION == 1 */
336