]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/CodeWarrior/HCS12/port.c
Update version numbers ready for release.
[freertos] / FreeRTOS / Source / portable / CodeWarrior / HCS12 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.1.1\r
3  * Copyright (C) 2018 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 /* Scheduler includes. */\r
29 #include "FreeRTOS.h"\r
30 #include "task.h"\r
31 \r
32 \r
33 /*-----------------------------------------------------------\r
34  * Implementation of functions defined in portable.h for the HCS12 port.\r
35  *----------------------------------------------------------*/\r
36 \r
37 \r
38 /*\r
39  * Configure a timer to generate the RTOS tick at the frequency specified \r
40  * within FreeRTOSConfig.h.\r
41  */\r
42 static void prvSetupTimerInterrupt( void );\r
43 \r
44 /* Interrupt service routines have to be in non-banked memory - as does the\r
45 scheduler startup function. */\r
46 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
47 \r
48         /* Manual context switch function.  This is the SWI ISR. */\r
49         void interrupt vPortYield( void );\r
50 \r
51         /* Tick context switch function.  This is the timer ISR. */\r
52         void interrupt vPortTickInterrupt( void );\r
53         \r
54         /* Simply called by xPortStartScheduler().  xPortStartScheduler() does not\r
55         start the scheduler directly because the header file containing the \r
56         xPortStartScheduler() prototype is part of the common kernel code, and \r
57         therefore cannot use the CODE_SEG pragma. */\r
58         static BaseType_t xBankedStartScheduler( void );\r
59 \r
60 #pragma CODE_SEG DEFAULT\r
61 \r
62 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
63 critical section should not be left (i.e. interrupts should not be re-enabled)\r
64 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
65 depth.  Each task maintains it's own critical nesting depth variable so \r
66 uxCriticalNesting is saved and restored from the task stack during a context\r
67 switch. */\r
68 volatile UBaseType_t uxCriticalNesting = 0xff;\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /* \r
73  * See header file for description. \r
74  */\r
75 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
76 {\r
77         /* \r
78                 Place a few bytes of known values on the bottom of the stack.\r
79                 This can be uncommented to provide useful stack markers when debugging.\r
80 \r
81                 *pxTopOfStack = ( StackType_t ) 0x11;\r
82                 pxTopOfStack--;\r
83                 *pxTopOfStack = ( StackType_t ) 0x22;\r
84                 pxTopOfStack--;\r
85                 *pxTopOfStack = ( StackType_t ) 0x33;\r
86                 pxTopOfStack--;\r
87         */\r
88 \r
89 \r
90 \r
91         /* Setup the initial stack of the task.  The stack is set exactly as \r
92         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
93         expected by the HCS12 RTI instruction. */\r
94 \r
95 \r
96         /* The address of the task function is placed in the stack byte at a time. */\r
97         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 1 );\r
98         pxTopOfStack--;\r
99         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 0 );\r
100         pxTopOfStack--;\r
101 \r
102         /* Next are all the registers that form part of the task context. */\r
103 \r
104         /* Y register */\r
105         *pxTopOfStack = ( StackType_t ) 0xff;\r
106         pxTopOfStack--;\r
107         *pxTopOfStack = ( StackType_t ) 0xee;\r
108         pxTopOfStack--;\r
109 \r
110         /* X register */\r
111         *pxTopOfStack = ( StackType_t ) 0xdd;\r
112         pxTopOfStack--;\r
113         *pxTopOfStack = ( StackType_t ) 0xcc;\r
114         pxTopOfStack--;\r
115  \r
116         /* A register contains parameter high byte. */\r
117         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 0 );\r
118         pxTopOfStack--;\r
119 \r
120         /* B register contains parameter low byte. */\r
121         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 1 );\r
122         pxTopOfStack--;\r
123 \r
124         /* CCR: Note that when the task starts interrupts will be enabled since\r
125         "I" bit of CCR is cleared */\r
126         *pxTopOfStack = ( StackType_t ) 0x00;\r
127         pxTopOfStack--;\r
128         \r
129         #ifdef BANKED_MODEL\r
130                 /* The page of the task. */\r
131                 *pxTopOfStack = ( StackType_t ) ( ( int ) pxCode );\r
132                 pxTopOfStack--;\r
133         #endif\r
134         \r
135         /* Finally the critical nesting depth is initialised with 0 (not within\r
136         a critical section). */\r
137         *pxTopOfStack = ( StackType_t ) 0x00;\r
138 \r
139         return pxTopOfStack;\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 void vPortEndScheduler( void )\r
144 {\r
145         /* It is unlikely that the HCS12 port will get stopped. */\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 static void prvSetupTimerInterrupt( void )\r
150 {\r
151         TickTimer_SetFreqHz( configTICK_RATE_HZ );\r
152         TickTimer_Enable();\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 BaseType_t xPortStartScheduler( void )\r
157 {\r
158         /* xPortStartScheduler() does not start the scheduler directly because \r
159         the header file containing the xPortStartScheduler() prototype is part \r
160         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
161         Instead it simply calls the locally defined xBankedStartScheduler() - \r
162         which does use the CODE_SEG pragma. */\r
163 \r
164         return xBankedStartScheduler();\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
169 \r
170 static BaseType_t xBankedStartScheduler( void )\r
171 {\r
172         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
173         disabled when this function is called. */\r
174         prvSetupTimerInterrupt();\r
175 \r
176         /* Restore the context of the first task. */\r
177         portRESTORE_CONTEXT();\r
178 \r
179         /* Simulate the end of an interrupt to start the scheduler off. */\r
180         __asm( "rti" );\r
181 \r
182         /* Should not get here! */\r
183         return pdFALSE;\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 /*\r
188  * Context switch functions.  These are both interrupt service routines.\r
189  */\r
190 \r
191 /*\r
192  * Manual context switch forced by calling portYIELD().  This is the SWI\r
193  * handler.\r
194  */\r
195 void interrupt vPortYield( void )\r
196 {\r
197         portSAVE_CONTEXT();\r
198         vTaskSwitchContext();\r
199         portRESTORE_CONTEXT();\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /*\r
204  * RTOS tick interrupt service routine.  If the cooperative scheduler is \r
205  * being used then this simply increments the tick count.  If the \r
206  * preemptive scheduler is being used a context switch can occur.\r
207  */\r
208 void interrupt vPortTickInterrupt( void )\r
209 {\r
210         #if configUSE_PREEMPTION == 1\r
211         {\r
212                 /* A context switch might happen so save the context. */\r
213                 portSAVE_CONTEXT();\r
214 \r
215                 /* Increment the tick ... */\r
216                 if( xTaskIncrementTick() != pdFALSE )\r
217                 {\r
218                         vTaskSwitchContext();\r
219                 }\r
220 \r
221                 TFLG1 = 1;                                                                 \r
222 \r
223                 /* Restore the context of a task - which may be a different task\r
224                 to that interrupted. */\r
225                 portRESTORE_CONTEXT();  \r
226         }\r
227         #else\r
228         {\r
229                 xTaskIncrementTick();\r
230                 TFLG1 = 1;\r
231         }\r
232         #endif\r
233 }\r
234 \r
235 #pragma CODE_SEG DEFAULT\r
236 \r
237 \r