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