]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/MSP430F449/port.c
Update version number in readiness for V10.2.0 release.
[freertos] / FreeRTOS / Source / portable / GCC / MSP430F449 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29         Changes from V2.5.2\r
30                 \r
31         + usCriticalNesting now has a volatile qualifier.\r
32 */\r
33 \r
34 /* Standard includes. */\r
35 #include <stdlib.h>\r
36 #include <signal.h>\r
37 \r
38 /* Scheduler includes. */\r
39 #include "FreeRTOS.h"\r
40 #include "task.h"\r
41 \r
42 /*-----------------------------------------------------------\r
43  * Implementation of functions defined in portable.h for the MSP430 port.\r
44  *----------------------------------------------------------*/\r
45 \r
46 /* Constants required for hardware setup.  The tick ISR runs off the ACLK, \r
47 not the MCLK. */\r
48 #define portACLK_FREQUENCY_HZ                   ( ( TickType_t ) 32768 )\r
49 #define portINITIAL_CRITICAL_NESTING    ( ( uint16_t ) 10 )\r
50 #define portFLAGS_INT_ENABLED   ( ( StackType_t ) 0x08 )\r
51 \r
52 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
53 any details of its type. */\r
54 typedef void TCB_t;\r
55 extern volatile TCB_t * volatile pxCurrentTCB;\r
56 \r
57 /* Most ports implement critical sections by placing the interrupt flags on\r
58 the stack before disabling interrupts.  Exiting the critical section is then\r
59 simply a case of popping the flags from the stack.  As mspgcc does not use\r
60 a frame pointer this cannot be done as modifying the stack will clobber all\r
61 the stack variables.  Instead each task maintains a count of the critical\r
62 section nesting depth.  Each time a critical section is entered the count is\r
63 incremented.  Each time a critical section is left the count is decremented -\r
64 with interrupts only being re-enabled if the count is zero.\r
65 \r
66 usCriticalNesting will get set to zero when the scheduler starts, but must\r
67 not be initialised to zero as this will cause problems during the startup\r
68 sequence. */\r
69 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /* \r
73  * Macro to save a task context to the task stack.  This simply pushes all the \r
74  * general purpose msp430 registers onto the stack, followed by the \r
75  * usCriticalNesting value used by the task.  Finally the resultant stack \r
76  * pointer value is saved into the task control block so it can be retrieved \r
77  * the next time the task executes.\r
78  */\r
79 #define portSAVE_CONTEXT()                                                                      \\r
80         asm volatile (  "push   r4                                              \n\t"   \\r
81                                         "push   r5                                              \n\t"   \\r
82                                         "push   r6                                              \n\t"   \\r
83                                         "push   r7                                              \n\t"   \\r
84                                         "push   r8                                              \n\t"   \\r
85                                         "push   r9                                              \n\t"   \\r
86                                         "push   r10                                             \n\t"   \\r
87                                         "push   r11                                             \n\t"   \\r
88                                         "push   r12                                             \n\t"   \\r
89                                         "push   r13                                             \n\t"   \\r
90                                         "push   r14                                             \n\t"   \\r
91                                         "push   r15                                             \n\t"   \\r
92                                         "mov.w  usCriticalNesting, r14  \n\t"   \\r
93                                         "push   r14                                             \n\t"   \\r
94                                         "mov.w  pxCurrentTCB, r12               \n\t"   \\r
95                                         "mov.w  r1, @r12                                \n\t"   \\r
96                                 );\r
97 \r
98 /* \r
99  * Macro to restore a task context from the task stack.  This is effectively\r
100  * the reverse of portSAVE_CONTEXT().  First the stack pointer value is\r
101  * loaded from the task control block.  Next the value for usCriticalNesting\r
102  * used by the task is retrieved from the stack - followed by the value of all\r
103  * the general purpose msp430 registers.\r
104  *\r
105  * The bic instruction ensures there are no low power bits set in the status\r
106  * register that is about to be popped from the stack.\r
107  */\r
108 #define portRESTORE_CONTEXT()                                                           \\r
109         asm volatile (  "mov.w  pxCurrentTCB, r12               \n\t"   \\r
110                                         "mov.w  @r12, r1                                \n\t"   \\r
111                                         "pop    r15                                             \n\t"   \\r
112                                         "mov.w  r15, usCriticalNesting  \n\t"   \\r
113                                         "pop    r15                                             \n\t"   \\r
114                                         "pop    r14                                             \n\t"   \\r
115                                         "pop    r13                                             \n\t"   \\r
116                                         "pop    r12                                             \n\t"   \\r
117                                         "pop    r11                                             \n\t"   \\r
118                                         "pop    r10                                             \n\t"   \\r
119                                         "pop    r9                                              \n\t"   \\r
120                                         "pop    r8                                              \n\t"   \\r
121                                         "pop    r7                                              \n\t"   \\r
122                                         "pop    r6                                              \n\t"   \\r
123                                         "pop    r5                                              \n\t"   \\r
124                                         "pop    r4                                              \n\t"   \\r
125                                         "bic    #(0xf0),0(r1)                   \n\t"   \\r
126                                         "reti                                                   \n\t"   \\r
127                                 );\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /*\r
131  * Sets up the periodic ISR used for the RTOS tick.  This uses timer 0, but\r
132  * could have alternatively used the watchdog timer or timer 1.\r
133  */\r
134 static void prvSetupTimerInterrupt( void );\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /* \r
138  * Initialise the stack of a task to look exactly as if a call to \r
139  * portSAVE_CONTEXT had been called.\r
140  * \r
141  * See the header file portable.h.\r
142  */\r
143 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
144 {\r
145         /* \r
146                 Place a few bytes of known values on the bottom of the stack. \r
147                 This is just useful for debugging and can be included if required.\r
148 \r
149                 *pxTopOfStack = ( StackType_t ) 0x1111;\r
150                 pxTopOfStack--;\r
151                 *pxTopOfStack = ( StackType_t ) 0x2222;\r
152                 pxTopOfStack--;\r
153                 *pxTopOfStack = ( StackType_t ) 0x3333;\r
154                 pxTopOfStack--; \r
155         */\r
156 \r
157         /* The msp430 automatically pushes the PC then SR onto the stack before \r
158         executing an ISR.  We want the stack to look just as if this has happened\r
159         so place a pointer to the start of the task on the stack first - followed\r
160         by the flags we want the task to use when it starts up. */\r
161         *pxTopOfStack = ( StackType_t ) pxCode;\r
162         pxTopOfStack--;\r
163         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
164         pxTopOfStack--;\r
165 \r
166         /* Next the general purpose registers. */\r
167         *pxTopOfStack = ( StackType_t ) 0x4444;\r
168         pxTopOfStack--;\r
169         *pxTopOfStack = ( StackType_t ) 0x5555;\r
170         pxTopOfStack--;\r
171         *pxTopOfStack = ( StackType_t ) 0x6666;\r
172         pxTopOfStack--;\r
173         *pxTopOfStack = ( StackType_t ) 0x7777;\r
174         pxTopOfStack--;\r
175         *pxTopOfStack = ( StackType_t ) 0x8888;\r
176         pxTopOfStack--;\r
177         *pxTopOfStack = ( StackType_t ) 0x9999;\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = ( StackType_t ) 0xaaaa;\r
180         pxTopOfStack--;\r
181         *pxTopOfStack = ( StackType_t ) 0xbbbb;\r
182         pxTopOfStack--;\r
183         *pxTopOfStack = ( StackType_t ) 0xcccc;\r
184         pxTopOfStack--;\r
185         *pxTopOfStack = ( StackType_t ) 0xdddd;\r
186         pxTopOfStack--;\r
187         *pxTopOfStack = ( StackType_t ) 0xeeee;\r
188         pxTopOfStack--;\r
189 \r
190         /* When the task starts is will expect to find the function parameter in\r
191         R15. */\r
192         *pxTopOfStack = ( StackType_t ) pvParameters;\r
193         pxTopOfStack--;\r
194 \r
195         /* The code generated by the mspgcc compiler does not maintain separate\r
196         stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
197         use the stack as per other ports.  Instead a variable is used to keep\r
198         track of the critical section nesting.  This variable has to be stored\r
199         as part of the task context and is initially set to zero. */\r
200         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;        \r
201 \r
202         /* Return a pointer to the top of the stack we have generated so this can\r
203         be stored in the task control block for the task. */\r
204         return pxTopOfStack;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 BaseType_t xPortStartScheduler( void )\r
209 {\r
210         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
211         this function is called. */\r
212         prvSetupTimerInterrupt();\r
213 \r
214         /* Restore the context of the first task that is going to run. */\r
215         portRESTORE_CONTEXT();\r
216 \r
217         /* Should not get here as the tasks are now running! */\r
218         return pdTRUE;\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 void vPortEndScheduler( void )\r
223 {\r
224         /* It is unlikely that the MSP430 port will get stopped.  If required simply\r
225         disable the tick interrupt here. */\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 /*\r
230  * Manual context switch called by portYIELD or taskYIELD.  \r
231  *\r
232  * The first thing we do is save the registers so we can use a naked attribute.\r
233  */\r
234 void vPortYield( void ) __attribute__ ( ( naked ) );\r
235 void vPortYield( void )\r
236 {\r
237         /* We want the stack of the task being saved to look exactly as if the task\r
238         was saved during a pre-emptive RTOS tick ISR.  Before calling an ISR the \r
239         msp430 places the status register onto the stack.  As this is a function \r
240         call and not an ISR we have to do this manually. */\r
241         asm volatile ( "push    r2" );\r
242         _DINT();\r
243 \r
244         /* Save the context of the current task. */\r
245         portSAVE_CONTEXT();\r
246 \r
247         /* Switch to the highest priority task that is ready to run. */\r
248         vTaskSwitchContext();\r
249 \r
250         /* Restore the context of the new task. */\r
251         portRESTORE_CONTEXT();\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 /*\r
256  * Hardware initialisation to generate the RTOS tick.  This uses timer 0\r
257  * but could alternatively use the watchdog timer or timer 1. \r
258  */\r
259 static void prvSetupTimerInterrupt( void )\r
260 {\r
261         /* Ensure the timer is stopped. */\r
262         TACTL = 0;\r
263 \r
264         /* Run the timer of the ACLK. */\r
265         TACTL = TASSEL_1;\r
266 \r
267         /* Clear everything to start with. */\r
268         TACTL |= TACLR;\r
269 \r
270         /* Set the compare match value according to the tick rate we want. */\r
271         TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;\r
272 \r
273         /* Enable the interrupts. */\r
274         TACCTL0 = CCIE;\r
275 \r
276         /* Start up clean. */\r
277         TACTL |= TACLR;\r
278 \r
279         /* Up mode. */\r
280         TACTL |= MC_1;\r
281 }\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 /* \r
285  * The interrupt service routine used depends on whether the pre-emptive\r
286  * scheduler is being used or not.\r
287  */\r
288 \r
289 #if configUSE_PREEMPTION == 1\r
290 \r
291         /*\r
292          * Tick ISR for preemptive scheduler.  We can use a naked attribute as\r
293          * the context is saved at the start of vPortYieldFromTick().  The tick\r
294          * count is incremented after the context is saved.\r
295          */\r
296         interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );\r
297         interrupt (TIMERA0_VECTOR) prvTickISR( void )\r
298         {\r
299                 /* Save the context of the interrupted task. */\r
300                 portSAVE_CONTEXT();\r
301 \r
302                 /* Increment the tick count then switch to the highest priority task\r
303                 that is ready to run. */\r
304                 if( xTaskIncrementTick() != pdFALSE )\r
305                 {\r
306                         vTaskSwitchContext();\r
307                 }\r
308 \r
309                 /* Restore the context of the new task. */\r
310                 portRESTORE_CONTEXT();\r
311         }\r
312 \r
313 #else\r
314 \r
315         /*\r
316          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
317          * tick count.  We don't need to switch context, this can only be done by\r
318          * manual calls to taskYIELD();\r
319          */\r
320         interrupt (TIMERA0_VECTOR) prvTickISR( void );\r
321         interrupt (TIMERA0_VECTOR) prvTickISR( void )\r
322         {\r
323                 xTaskIncrementTick();\r
324         }\r
325 #endif\r
326 \r
327 \r
328         \r