1 /*
2  * FreeRTOS Kernel V11.0.1
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     *pxTopOfStack = ( StackType_t ) 0xcccc;
187     pxTopOfStack--;
188     *pxTopOfStack = ( StackType_t ) 0xdddd;
189     pxTopOfStack--;
190     *pxTopOfStack = ( StackType_t ) 0xeeee;
191     pxTopOfStack--;
192 
193     /* When the task starts is will expect to find the function parameter in
194      * R15. */
195     *pxTopOfStack = ( StackType_t ) pvParameters;
196     pxTopOfStack--;
197 
198     /* The code generated by the mspgcc compiler does not maintain separate
199      * stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
200      * use the stack as per other ports.  Instead a variable is used to keep
201      * track of the critical section nesting.  This variable has to be stored
202      * as part of the task context and is initially set to zero. */
203     *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
204 
205     /* Return a pointer to the top of the stack we have generated so this can
206      * be stored in the task control block for the task. */
207     return pxTopOfStack;
208 }
209 /*-----------------------------------------------------------*/
210 
xPortStartScheduler(void)211 BaseType_t xPortStartScheduler( void )
212 {
213     /* Setup the hardware to generate the tick.  Interrupts are disabled when
214      * this function is called. */
215     prvSetupTimerInterrupt();
216 
217     /* Restore the context of the first task that is going to run. */
218     portRESTORE_CONTEXT();
219 
220     /* Should not get here as the tasks are now running! */
221     return pdTRUE;
222 }
223 /*-----------------------------------------------------------*/
224 
vPortEndScheduler(void)225 void vPortEndScheduler( void )
226 {
227     /* It is unlikely that the MSP430 port will get stopped.  If required simply
228      * disable the tick interrupt here. */
229 }
230 /*-----------------------------------------------------------*/
231 
232 /*
233  * Manual context switch called by portYIELD or taskYIELD.
234  *
235  * The first thing we do is save the registers so we can use a naked attribute.
236  */
237 void vPortYield( void ) __attribute__( ( naked ) );
vPortYield(void)238 void vPortYield( void )
239 {
240     /* We want the stack of the task being saved to look exactly as if the task
241      * was saved during a pre-emptive RTOS tick ISR.  Before calling an ISR the
242      * msp430 places the status register onto the stack.  As this is a function
243      * call and not an ISR we have to do this manually. */
244     asm volatile ( "push    r2" );
245     _DINT();
246 
247     /* Save the context of the current task. */
248     portSAVE_CONTEXT();
249 
250     /* Switch to the highest priority task that is ready to run. */
251     vTaskSwitchContext();
252 
253     /* Restore the context of the new task. */
254     portRESTORE_CONTEXT();
255 }
256 /*-----------------------------------------------------------*/
257 
258 /*
259  * Hardware initialisation to generate the RTOS tick.  This uses timer 0
260  * but could alternatively use the watchdog timer or timer 1.
261  */
prvSetupTimerInterrupt(void)262 static void prvSetupTimerInterrupt( void )
263 {
264     /* Ensure the timer is stopped. */
265     TACTL = 0;
266 
267     /* Run the timer of the ACLK. */
268     TACTL = TASSEL_1;
269 
270     /* Clear everything to start with. */
271     TACTL |= TACLR;
272 
273     /* Set the compare match value according to the tick rate we want. */
274     TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
275 
276     /* Enable the interrupts. */
277     TACCTL0 = CCIE;
278 
279     /* Start up clean. */
280     TACTL |= TACLR;
281 
282     /* Up mode. */
283     TACTL |= MC_1;
284 }
285 /*-----------------------------------------------------------*/
286 
287 /*
288  * The interrupt service routine used depends on whether the pre-emptive
289  * scheduler is being used or not.
290  */
291 
292 #if configUSE_PREEMPTION == 1
293 
294 /*
295  * Tick ISR for preemptive scheduler.  We can use a naked attribute as
296  * the context is saved at the start of vPortYieldFromTick().  The tick
297  * count is incremented after the context is saved.
298  */
299     interrupt( TIMERA0_VECTOR ) prvTickISR( void ) __attribute__( ( naked ) );
prvTickISR(void)300     interrupt( TIMERA0_VECTOR ) prvTickISR( void )
301     {
302         /* Save the context of the interrupted task. */
303         portSAVE_CONTEXT();
304 
305         /* Increment the tick count then switch to the highest priority task
306          * that is ready to run. */
307         if( xTaskIncrementTick() != pdFALSE )
308         {
309             vTaskSwitchContext();
310         }
311 
312         /* Restore the context of the new task. */
313         portRESTORE_CONTEXT();
314     }
315 
316 #else /* if configUSE_PREEMPTION == 1 */
317 
318 /*
319  * Tick ISR for the cooperative scheduler.  All this does is increment the
320  * tick count.  We don't need to switch context, this can only be done by
321  * manual calls to taskYIELD();
322  */
323     interrupt( TIMERA0_VECTOR ) prvTickISR( void );
prvTickISR(void)324     interrupt( TIMERA0_VECTOR ) prvTickISR( void )
325     {
326         xTaskIncrementTick();
327     }
328 #endif /* if configUSE_PREEMPTION == 1 */
329