]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/MSP430X/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / IAR / MSP430X / port.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Scheduler includes. */\r
30 #include "FreeRTOS.h"\r
31 #include "task.h"\r
32 \r
33 /*-----------------------------------------------------------\r
34  * Implementation of functions defined in portable.h for the MSP430X port.\r
35  *----------------------------------------------------------*/\r
36 \r
37 /* Constants required for hardware setup.  The tick ISR runs off the ACLK,\r
38 not the MCLK. */\r
39 #define portACLK_FREQUENCY_HZ                   ( ( TickType_t ) 32768 )\r
40 #define portINITIAL_CRITICAL_NESTING    ( ( uint16_t ) 10 )\r
41 #define portFLAGS_INT_ENABLED                   ( ( StackType_t ) 0x08 )\r
42 \r
43 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
44 any details of its type. */\r
45 typedef void TCB_t;\r
46 extern volatile TCB_t * volatile pxCurrentTCB;\r
47 \r
48 /* Each task maintains a count of the critical section nesting depth.  Each\r
49 time a critical section is entered the count is incremented.  Each time a\r
50 critical section is exited the count is decremented - with interrupts only\r
51 being re-enabled if the count is zero.\r
52 \r
53 usCriticalNesting will get set to zero when the scheduler starts, but must\r
54 not be initialised to zero as this will cause problems during the startup\r
55 sequence. */\r
56 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
57 /*-----------------------------------------------------------*/\r
58 \r
59 \r
60 /*\r
61  * Sets up the periodic ISR used for the RTOS tick.  This uses timer 0, but\r
62  * could have alternatively used the watchdog timer or timer 1.\r
63  */\r
64 void vPortSetupTimerInterrupt( void );\r
65 /*-----------------------------------------------------------*/\r
66 \r
67 /*\r
68  * Initialise the stack of a task to look exactly as if a call to\r
69  * portSAVE_CONTEXT had been called.\r
70  *\r
71  * See the header file portable.h.\r
72  */\r
73 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
74 {\r
75 uint16_t *pusTopOfStack;\r
76 uint32_t *pulTopOfStack;\r
77 \r
78         /*\r
79                 Place a few bytes of known values on the bottom of the stack.\r
80                 This is just useful for debugging and can be included if required.\r
81         \r
82                 *pxTopOfStack = ( StackType_t ) 0x1111;\r
83                 pxTopOfStack--;\r
84                 *pxTopOfStack = ( StackType_t ) 0x2222;\r
85                 pxTopOfStack--;\r
86                 *pxTopOfStack = ( StackType_t ) 0x3333;\r
87         */\r
88 \r
89         /* StackType_t is either 16 bits or 32 bits depending on the data model.\r
90         Some stacked items do not change size depending on the data model so have\r
91         to be explicitly cast to the correct size so this function will work\r
92         whichever data model is being used. */\r
93         if( sizeof( StackType_t ) == sizeof( uint16_t ) )\r
94         {\r
95                 /* Make room for a 20 bit value stored as a 32 bit value. */\r
96                 pusTopOfStack = ( uint16_t * ) pxTopOfStack;\r
97                 pusTopOfStack--;\r
98                 pulTopOfStack = ( uint32_t * ) pusTopOfStack;\r
99         }\r
100         else\r
101         {\r
102                 pulTopOfStack = ( uint32_t * ) pxTopOfStack;\r
103         }\r
104         *pulTopOfStack = ( uint32_t ) pxCode;\r
105         \r
106         pusTopOfStack = ( uint16_t * ) pulTopOfStack;\r
107         pusTopOfStack--;\r
108         *pusTopOfStack = portFLAGS_INT_ENABLED;\r
109         pusTopOfStack -= ( sizeof( StackType_t ) / 2 );\r
110         \r
111         /* From here on the size of stacked items depends on the memory model. */\r
112         pxTopOfStack = ( StackType_t * ) pusTopOfStack;\r
113 \r
114         /* Next the general purpose registers. */\r
115         #ifdef PRELOAD_REGISTER_VALUES\r
116                 *pxTopOfStack = ( StackType_t ) 0xffff;\r
117                 pxTopOfStack--;\r
118                 *pxTopOfStack = ( StackType_t ) 0xeeee;\r
119                 pxTopOfStack--;\r
120                 *pxTopOfStack = ( StackType_t ) 0xdddd;\r
121                 pxTopOfStack--;\r
122                 *pxTopOfStack = ( StackType_t ) pvParameters;\r
123                 pxTopOfStack--;\r
124                 *pxTopOfStack = ( StackType_t ) 0xbbbb;\r
125                 pxTopOfStack--;\r
126                 *pxTopOfStack = ( StackType_t ) 0xaaaa;\r
127                 pxTopOfStack--;\r
128                 *pxTopOfStack = ( StackType_t ) 0x9999;\r
129                 pxTopOfStack--;\r
130                 *pxTopOfStack = ( StackType_t ) 0x8888;\r
131                 pxTopOfStack--; \r
132                 *pxTopOfStack = ( StackType_t ) 0x5555;\r
133                 pxTopOfStack--;\r
134                 *pxTopOfStack = ( StackType_t ) 0x6666;\r
135                 pxTopOfStack--;\r
136                 *pxTopOfStack = ( StackType_t ) 0x5555;\r
137                 pxTopOfStack--;\r
138                 *pxTopOfStack = ( StackType_t ) 0x4444;\r
139                 pxTopOfStack--;\r
140         #else\r
141                 pxTopOfStack -= 3;\r
142                 *pxTopOfStack = ( StackType_t ) pvParameters;\r
143                 pxTopOfStack -= 9;\r
144         #endif\r
145 \r
146 \r
147         /* A variable is used to keep track of the critical section nesting.\r
148         This variable has to be stored as part of the task context and is\r
149         initially set to zero. */\r
150         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;        \r
151 \r
152         /* Return a pointer to the top of the stack we have generated so this can\r
153         be stored in the task control block for the task. */\r
154         return pxTopOfStack;\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 void vPortEndScheduler( void )\r
159 {\r
160         /* It is unlikely that the MSP430 port will get stopped.  If required simply\r
161         disable the tick interrupt here. */\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 /*\r
166  * Hardware initialisation to generate the RTOS tick.\r
167  */\r
168 void vPortSetupTimerInterrupt( void )\r
169 {\r
170         vApplicationSetupTimerInterrupt();\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 #pragma vector=configTICK_VECTOR\r
175 __interrupt __raw void vTickISREntry( void )\r
176 {\r
177 extern void vPortTickISR( void );\r
178 \r
179         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
180         vPortTickISR();\r
181 }\r
182 \r
183         \r