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