]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/CodeWarrior/HCS12/port.c
Update MSP432 projects to use updated driver library files.
[freertos] / FreeRTOS / Source / portable / CodeWarrior / HCS12 / port.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 \r
74 \r
75 /*-----------------------------------------------------------\r
76  * Implementation of functions defined in portable.h for the HCS12 port.\r
77  *----------------------------------------------------------*/\r
78 \r
79 \r
80 /*\r
81  * Configure a timer to generate the RTOS tick at the frequency specified \r
82  * within FreeRTOSConfig.h.\r
83  */\r
84 static void prvSetupTimerInterrupt( void );\r
85 \r
86 /* Interrupt service routines have to be in non-banked memory - as does the\r
87 scheduler startup function. */\r
88 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
89 \r
90         /* Manual context switch function.  This is the SWI ISR. */\r
91         void interrupt vPortYield( void );\r
92 \r
93         /* Tick context switch function.  This is the timer ISR. */\r
94         void interrupt vPortTickInterrupt( void );\r
95         \r
96         /* Simply called by xPortStartScheduler().  xPortStartScheduler() does not\r
97         start the scheduler directly because the header file containing the \r
98         xPortStartScheduler() prototype is part of the common kernel code, and \r
99         therefore cannot use the CODE_SEG pragma. */\r
100         static BaseType_t xBankedStartScheduler( void );\r
101 \r
102 #pragma CODE_SEG DEFAULT\r
103 \r
104 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
105 critical section should not be left (i.e. interrupts should not be re-enabled)\r
106 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
107 depth.  Each task maintains it's own critical nesting depth variable so \r
108 uxCriticalNesting is saved and restored from the task stack during a context\r
109 switch. */\r
110 volatile UBaseType_t uxCriticalNesting = 0xff;\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /* \r
115  * See header file for description. \r
116  */\r
117 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
118 {\r
119         /* \r
120                 Place a few bytes of known values on the bottom of the stack.\r
121                 This can be uncommented to provide useful stack markers when debugging.\r
122 \r
123                 *pxTopOfStack = ( StackType_t ) 0x11;\r
124                 pxTopOfStack--;\r
125                 *pxTopOfStack = ( StackType_t ) 0x22;\r
126                 pxTopOfStack--;\r
127                 *pxTopOfStack = ( StackType_t ) 0x33;\r
128                 pxTopOfStack--;\r
129         */\r
130 \r
131 \r
132 \r
133         /* Setup the initial stack of the task.  The stack is set exactly as \r
134         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
135         expected by the HCS12 RTI instruction. */\r
136 \r
137 \r
138         /* The address of the task function is placed in the stack byte at a time. */\r
139         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 1 );\r
140         pxTopOfStack--;\r
141         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 0 );\r
142         pxTopOfStack--;\r
143 \r
144         /* Next are all the registers that form part of the task context. */\r
145 \r
146         /* Y register */\r
147         *pxTopOfStack = ( StackType_t ) 0xff;\r
148         pxTopOfStack--;\r
149         *pxTopOfStack = ( StackType_t ) 0xee;\r
150         pxTopOfStack--;\r
151 \r
152         /* X register */\r
153         *pxTopOfStack = ( StackType_t ) 0xdd;\r
154         pxTopOfStack--;\r
155         *pxTopOfStack = ( StackType_t ) 0xcc;\r
156         pxTopOfStack--;\r
157  \r
158         /* A register contains parameter high byte. */\r
159         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 0 );\r
160         pxTopOfStack--;\r
161 \r
162         /* B register contains parameter low byte. */\r
163         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 1 );\r
164         pxTopOfStack--;\r
165 \r
166         /* CCR: Note that when the task starts interrupts will be enabled since\r
167         "I" bit of CCR is cleared */\r
168         *pxTopOfStack = ( StackType_t ) 0x00;\r
169         pxTopOfStack--;\r
170         \r
171         #ifdef BANKED_MODEL\r
172                 /* The page of the task. */\r
173                 *pxTopOfStack = ( StackType_t ) ( ( int ) pxCode );\r
174                 pxTopOfStack--;\r
175         #endif\r
176         \r
177         /* Finally the critical nesting depth is initialised with 0 (not within\r
178         a critical section). */\r
179         *pxTopOfStack = ( StackType_t ) 0x00;\r
180 \r
181         return pxTopOfStack;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void vPortEndScheduler( void )\r
186 {\r
187         /* It is unlikely that the HCS12 port will get stopped. */\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvSetupTimerInterrupt( void )\r
192 {\r
193         TickTimer_SetFreqHz( configTICK_RATE_HZ );\r
194         TickTimer_Enable();\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 BaseType_t xPortStartScheduler( void )\r
199 {\r
200         /* xPortStartScheduler() does not start the scheduler directly because \r
201         the header file containing the xPortStartScheduler() prototype is part \r
202         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
203         Instead it simply calls the locally defined xBankedStartScheduler() - \r
204         which does use the CODE_SEG pragma. */\r
205 \r
206         return xBankedStartScheduler();\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
211 \r
212 static BaseType_t xBankedStartScheduler( void )\r
213 {\r
214         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
215         disabled when this function is called. */\r
216         prvSetupTimerInterrupt();\r
217 \r
218         /* Restore the context of the first task. */\r
219         portRESTORE_CONTEXT();\r
220 \r
221         /* Simulate the end of an interrupt to start the scheduler off. */\r
222         __asm( "rti" );\r
223 \r
224         /* Should not get here! */\r
225         return pdFALSE;\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 /*\r
230  * Context switch functions.  These are both interrupt service routines.\r
231  */\r
232 \r
233 /*\r
234  * Manual context switch forced by calling portYIELD().  This is the SWI\r
235  * handler.\r
236  */\r
237 void interrupt vPortYield( void )\r
238 {\r
239         portSAVE_CONTEXT();\r
240         vTaskSwitchContext();\r
241         portRESTORE_CONTEXT();\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 /*\r
246  * RTOS tick interrupt service routine.  If the cooperative scheduler is \r
247  * being used then this simply increments the tick count.  If the \r
248  * preemptive scheduler is being used a context switch can occur.\r
249  */\r
250 void interrupt vPortTickInterrupt( void )\r
251 {\r
252         #if configUSE_PREEMPTION == 1\r
253         {\r
254                 /* A context switch might happen so save the context. */\r
255                 portSAVE_CONTEXT();\r
256 \r
257                 /* Increment the tick ... */\r
258                 if( xTaskIncrementTick() != pdFALSE )\r
259                 {\r
260                         vTaskSwitchContext();\r
261                 }\r
262 \r
263                 TFLG1 = 1;                                                                 \r
264 \r
265                 /* Restore the context of a task - which may be a different task\r
266                 to that interrupted. */\r
267                 portRESTORE_CONTEXT();  \r
268         }\r
269         #else\r
270         {\r
271                 xTaskIncrementTick();\r
272                 TFLG1 = 1;\r
273         }\r
274         #endif\r
275 }\r
276 \r
277 #pragma CODE_SEG DEFAULT\r
278 \r
279 \r