xref: /Kernel-v10.6.2/portable/IAR/MSP430/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 /* Scheduler includes. */
30 #include "FreeRTOS.h"
31 #include "task.h"
32 
33 /*-----------------------------------------------------------
34  * Implementation of functions defined in portable.h for the MSP430 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     /*
76         Place a few bytes of known values on the bottom of the stack.
77         This is just useful for debugging and can be included if required.
78 
79         *pxTopOfStack = ( StackType_t ) 0x1111;
80         pxTopOfStack--;
81         *pxTopOfStack = ( StackType_t ) 0x2222;
82         pxTopOfStack--;
83         *pxTopOfStack = ( StackType_t ) 0x3333;
84         pxTopOfStack--;
85     */
86 
87     /* The msp430 automatically pushes the PC then SR onto the stack before
88     executing an ISR.  We want the stack to look just as if this has happened
89     so place a pointer to the start of the task on the stack first - followed
90     by the flags we want the task to use when it starts up. */
91     *pxTopOfStack = ( StackType_t ) pxCode;
92     pxTopOfStack--;
93     *pxTopOfStack = portFLAGS_INT_ENABLED;
94     pxTopOfStack--;
95 
96     /* Next the general purpose registers. */
97     *pxTopOfStack = ( StackType_t ) 0x4444;
98     pxTopOfStack--;
99     *pxTopOfStack = ( StackType_t ) 0x5555;
100     pxTopOfStack--;
101     *pxTopOfStack = ( StackType_t ) 0x6666;
102     pxTopOfStack--;
103     *pxTopOfStack = ( StackType_t ) 0x7777;
104     pxTopOfStack--;
105     *pxTopOfStack = ( StackType_t ) 0x8888;
106     pxTopOfStack--;
107     *pxTopOfStack = ( StackType_t ) 0x9999;
108     pxTopOfStack--;
109     *pxTopOfStack = ( StackType_t ) 0xaaaa;
110     pxTopOfStack--;
111     *pxTopOfStack = ( StackType_t ) 0xbbbb;
112     pxTopOfStack--;
113 
114     /* When the task starts is will expect to find the function parameter in
115     R15. */
116     *pxTopOfStack = ( StackType_t ) pvParameters;
117     pxTopOfStack--;
118 
119     *pxTopOfStack = ( StackType_t ) 0xdddd;
120     pxTopOfStack--;
121     *pxTopOfStack = ( StackType_t ) 0xeeee;
122     pxTopOfStack--;
123     *pxTopOfStack = ( StackType_t ) 0xffff;
124     pxTopOfStack--;
125 
126     /* A variable is used to keep track of the critical section nesting.
127     This variable has to be stored as part of the task context and is
128     initially set to zero. */
129     *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
130 
131     /* Return a pointer to the top of the stack we have generated so this can
132     be stored in the task control block for the task. */
133     return pxTopOfStack;
134 }
135 /*-----------------------------------------------------------*/
136 
vPortEndScheduler(void)137 void vPortEndScheduler( void )
138 {
139     /* It is unlikely that the MSP430 port will get stopped.  If required simply
140     disable the tick interrupt here. */
141 }
142 /*-----------------------------------------------------------*/
143 
144 /*
145  * Hardware initialisation to generate the RTOS tick.  This uses timer 0
146  * but could alternatively use the watchdog timer or timer 1.
147  */
vPortSetupTimerInterrupt(void)148 void vPortSetupTimerInterrupt( void )
149 {
150     /* Ensure the timer is stopped. */
151     TACTL = 0;
152 
153     /* Run the timer of the ACLK. */
154     TACTL = TASSEL_1;
155 
156     /* Clear everything to start with. */
157     TACTL |= TACLR;
158 
159     /* Set the compare match value according to the tick rate we want. */
160     TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
161 
162     /* Enable the interrupts. */
163     TACCTL0 = CCIE;
164 
165     /* Start up clean. */
166     TACTL |= TACLR;
167 
168     /* Up mode. */
169     TACTL |= MC_1;
170 }
171 /*-----------------------------------------------------------*/
172 
173 
174 
175