]> begriffs open source - freertos/blob - Source/portable/IAR/MSP430/port.c
Update to V5.4.2. See http://www.freertos.org/History.txt .
[freertos] / Source / portable / IAR / MSP430 / port.c
1 /*\r
2         FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /* Scheduler includes. */\r
49 #include "FreeRTOS.h"\r
50 #include "task.h"\r
51 \r
52 /*-----------------------------------------------------------\r
53  * Implementation of functions defined in portable.h for the MSP430 port.\r
54  *----------------------------------------------------------*/\r
55 \r
56 /* Constants required for hardware setup.  The tick ISR runs off the ACLK,\r
57 not the MCLK. */\r
58 #define portACLK_FREQUENCY_HZ                   ( ( portTickType ) 32768 )\r
59 #define portINITIAL_CRITICAL_NESTING    ( ( unsigned portSHORT ) 10 )\r
60 #define portFLAGS_INT_ENABLED                   ( ( portSTACK_TYPE ) 0x08 )\r
61 \r
62 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
63 any details of its type. */\r
64 typedef void tskTCB;\r
65 extern volatile tskTCB * volatile pxCurrentTCB;\r
66 \r
67 /* Each task maintains a count of the critical section nesting depth.  Each\r
68 time a critical section is entered the count is incremented.  Each time a\r
69 critical section is exited the count is decremented - with interrupts only\r
70 being re-enabled if the count is zero.\r
71 \r
72 usCriticalNesting will get set to zero when the scheduler starts, but must\r
73 not be initialised to zero as this will cause problems during the startup\r
74 sequence. */\r
75 volatile unsigned portSHORT usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
76 /*-----------------------------------------------------------*/\r
77 \r
78 \r
79 /*\r
80  * Sets up the periodic ISR used for the RTOS tick.  This uses timer 0, but\r
81  * could have alternatively used the watchdog timer or timer 1.\r
82  */\r
83 void vPortSetupTimerInterrupt( void );\r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /*\r
87  * Initialise the stack of a task to look exactly as if a call to\r
88  * portSAVE_CONTEXT had been called.\r
89  *\r
90  * See the header file portable.h.\r
91  */\r
92 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
93 {\r
94         /*\r
95                 Place a few bytes of known values on the bottom of the stack.\r
96                 This is just useful for debugging and can be included if required.\r
97 \r
98                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;\r
99                 pxTopOfStack--;\r
100                 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;\r
101                 pxTopOfStack--;\r
102                 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333;\r
103                 pxTopOfStack--;\r
104         */\r
105 \r
106         /* The msp430 automatically pushes the PC then SR onto the stack before\r
107         executing an ISR.  We want the stack to look just as if this has happened\r
108         so place a pointer to the start of the task on the stack first - followed\r
109         by the flags we want the task to use when it starts up. */\r
110         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;\r
111         pxTopOfStack--;\r
112         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
113         pxTopOfStack--;\r
114 \r
115         /* Next the general purpose registers. */\r
116         *pxTopOfStack = ( portSTACK_TYPE ) 0x4444;\r
117         pxTopOfStack--;\r
118         *pxTopOfStack = ( portSTACK_TYPE ) 0x5555;\r
119         pxTopOfStack--;\r
120         *pxTopOfStack = ( portSTACK_TYPE ) 0x6666;\r
121         pxTopOfStack--;\r
122         *pxTopOfStack = ( portSTACK_TYPE ) 0x7777;\r
123         pxTopOfStack--;\r
124         *pxTopOfStack = ( portSTACK_TYPE ) 0x8888;\r
125         pxTopOfStack--;\r
126         *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;\r
127         pxTopOfStack--;\r
128         *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaa;\r
129         pxTopOfStack--;\r
130         *pxTopOfStack = ( portSTACK_TYPE ) 0xbbbb;\r
131         pxTopOfStack--; \r
132         \r
133         /* When the task starts is will expect to find the function parameter in\r
134         R15. */\r
135         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
136         pxTopOfStack--;\r
137         \r
138         *pxTopOfStack = ( portSTACK_TYPE ) 0xdddd;\r
139         pxTopOfStack--;\r
140         *pxTopOfStack = ( portSTACK_TYPE ) 0xeeee;\r
141         pxTopOfStack--;\r
142         *pxTopOfStack = ( portSTACK_TYPE ) 0xffff;\r
143         pxTopOfStack--;\r
144 \r
145         /* A variable is used to keep track of the critical section nesting.\r
146         This variable has to be stored as part of the task context and is\r
147         initially set to zero. */\r
148         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;     \r
149 \r
150         /* Return a pointer to the top of the stack we have generated so this can\r
151         be stored in the task control block for the task. */\r
152         return pxTopOfStack;\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 void vPortEndScheduler( void )\r
157 {\r
158         /* It is unlikely that the MSP430 port will get stopped.  If required simply\r
159         disable the tick interrupt here. */\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /*\r
164  * Hardware initialisation to generate the RTOS tick.  This uses timer 0\r
165  * but could alternatively use the watchdog timer or timer 1.\r
166  */\r
167 void vPortSetupTimerInterrupt( void )\r
168 {\r
169         /* Ensure the timer is stopped. */\r
170         TACTL = 0;\r
171 \r
172         /* Run the timer of the ACLK. */\r
173         TACTL = TASSEL_1;\r
174 \r
175         /* Clear everything to start with. */\r
176         TACTL |= TACLR;\r
177 \r
178         /* Set the compare match value according to the tick rate we want. */\r
179         TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;\r
180 \r
181         /* Enable the interrupts. */\r
182         TACCTL0 = CCIE;\r
183 \r
184         /* Start up clean. */\r
185         TACTL |= TACLR;\r
186 \r
187         /* Up mode. */\r
188         TACTL |= MC_1;\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 \r
193         \r