1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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 /* Hardware includes. */
30 #include "msp430.h"
31 
32 /* Scheduler includes. */
33 #include "FreeRTOS.h"
34 #include "task.h"
35 
36 /*-----------------------------------------------------------
37  * Implementation of functions defined in portable.h for the MSP430X port.
38  *----------------------------------------------------------*/
39 
40 /* Constants required for hardware setup.  The tick ISR runs off the ACLK,
41 not the MCLK. */
42 #define portACLK_FREQUENCY_HZ           ( ( TickType_t ) 32768 )
43 #define portINITIAL_CRITICAL_NESTING    ( ( uint16_t ) 10 )
44 #define portFLAGS_INT_ENABLED           ( ( StackType_t ) 0x08 )
45 
46 /* We require the address of the pxCurrentTCB variable, but don't want to know
47 any details of its type. */
48 typedef void TCB_t;
49 extern volatile TCB_t * volatile pxCurrentTCB;
50 
51 /* Each task maintains a count of the critical section nesting depth.  Each
52 time a critical section is entered the count is incremented.  Each time a
53 critical section is exited the count is decremented - with interrupts only
54 being re-enabled if the count is zero.
55 
56 usCriticalNesting will get set to zero when the scheduler starts, but must
57 not be initialised to zero as this will cause problems during the startup
58 sequence. */
59 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
60 /*-----------------------------------------------------------*/
61 
62 
63 /*
64  * Sets up the periodic ISR used for the RTOS tick.  This uses timer 0, but
65  * could have alternatively used the watchdog timer or timer 1.
66  */
67 void vPortSetupTimerInterrupt( void );
68 /*-----------------------------------------------------------*/
69 
70 /*
71  * Initialise the stack of a task to look exactly as if a call to
72  * portSAVE_CONTEXT had been called.
73  *
74  * See the header file portable.h.
75  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)76 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
77 {
78 uint16_t *pusTopOfStack;
79 uint32_t *pulTopOfStack, ulTemp;
80 
81     /*
82         Place a few bytes of known values on the bottom of the stack.
83         This is just useful for debugging and can be included if required.
84 
85         *pxTopOfStack = ( StackType_t ) 0x1111;
86         pxTopOfStack--;
87         *pxTopOfStack = ( StackType_t ) 0x2222;
88         pxTopOfStack--;
89         *pxTopOfStack = ( StackType_t ) 0x3333;
90         pxTopOfStack--;
91     */
92 
93     /* Data types are need either 16 bits or 32 bits depending on the data
94     and code model used. */
95     if( sizeof( pxCode ) == sizeof( uint16_t ) )
96     {
97         pusTopOfStack = ( uint16_t * ) pxTopOfStack;
98         ulTemp = ( uint32_t ) pxCode;
99         *pusTopOfStack = ( uint16_t ) ulTemp;
100     }
101     else
102     {
103         /* Make room for a 20 bit value stored as a 32 bit value. */
104         pusTopOfStack = ( uint16_t * ) pxTopOfStack;
105         pusTopOfStack--;
106         pulTopOfStack = ( uint32_t * ) pusTopOfStack;
107         *pulTopOfStack = ( uint32_t ) pxCode;
108     }
109 
110     pusTopOfStack--;
111     *pusTopOfStack = portFLAGS_INT_ENABLED;
112     pusTopOfStack -= ( sizeof( StackType_t ) / 2 );
113 
114     /* From here on the size of stacked items depends on the memory model. */
115     pxTopOfStack = ( StackType_t * ) pusTopOfStack;
116 
117     /* Next the general purpose registers. */
118     #ifdef PRELOAD_REGISTER_VALUES
119         *pxTopOfStack = ( StackType_t ) 0xffff;
120         pxTopOfStack--;
121         *pxTopOfStack = ( StackType_t ) 0xeeee;
122         pxTopOfStack--;
123         *pxTopOfStack = ( StackType_t ) 0xdddd;
124         pxTopOfStack--;
125         *pxTopOfStack = ( StackType_t ) pvParameters;
126         pxTopOfStack--;
127         *pxTopOfStack = ( StackType_t ) 0xbbbb;
128         pxTopOfStack--;
129         *pxTopOfStack = ( StackType_t ) 0xaaaa;
130         pxTopOfStack--;
131         *pxTopOfStack = ( StackType_t ) 0x9999;
132         pxTopOfStack--;
133         *pxTopOfStack = ( StackType_t ) 0x8888;
134         pxTopOfStack--;
135         *pxTopOfStack = ( StackType_t ) 0x5555;
136         pxTopOfStack--;
137         *pxTopOfStack = ( StackType_t ) 0x6666;
138         pxTopOfStack--;
139         *pxTopOfStack = ( StackType_t ) 0x5555;
140         pxTopOfStack--;
141         *pxTopOfStack = ( StackType_t ) 0x4444;
142         pxTopOfStack--;
143     #else
144         pxTopOfStack -= 3;
145         *pxTopOfStack = ( StackType_t ) pvParameters;
146         pxTopOfStack -= 9;
147     #endif
148 
149     /* A variable is used to keep track of the critical section nesting.
150     This variable has to be stored as part of the task context and is
151     initially set to zero. */
152     *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
153 
154     /* Return a pointer to the top of the stack we have generated so this can
155     be stored in the task control block for the task. */
156     return pxTopOfStack;
157 }
158 /*-----------------------------------------------------------*/
159 
vPortEndScheduler(void)160 void vPortEndScheduler( void )
161 {
162     /* It is unlikely that the MSP430 port will get stopped.  If required simply
163     disable the tick interrupt here. */
164 }
165 /*-----------------------------------------------------------*/
166 
167 /*
168  * Hardware initialisation to generate the RTOS tick.
169  */
vPortSetupTimerInterrupt(void)170 void vPortSetupTimerInterrupt( void )
171 {
172     vApplicationSetupTimerInterrupt();
173 }
174 /*-----------------------------------------------------------*/
175 
176 #pragma vector=configTICK_VECTOR
vTickISREntry(void)177 interrupt void vTickISREntry( void )
178 {
179 extern void vPortTickISR( void );
180 
181     __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
182     #if configUSE_PREEMPTION == 1
183         extern void vPortPreemptiveTickISR( void );
184         vPortPreemptiveTickISR();
185     #else
186         extern void vPortCooperativeTickISR( void );
187         vPortCooperativeTickISR();
188     #endif
189 }
190 
191 
192