]> begriffs open source - freertos/blob - portable/CodeWarrior/HCS12/port.c
Add SMP in the License Header (#402)
[freertos] / portable / CodeWarrior / HCS12 / port.c
1 /*
2  * FreeRTOS SMP Kernel V202110.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /* Scheduler includes. */
29 #include "FreeRTOS.h"
30 #include "task.h"
31
32
33 /*-----------------------------------------------------------
34  * Implementation of functions defined in portable.h for the HCS12 port.
35  *----------------------------------------------------------*/
36
37
38 /*
39  * Configure a timer to generate the RTOS tick at the frequency specified 
40  * within FreeRTOSConfig.h.
41  */
42 static void prvSetupTimerInterrupt( void );
43
44 /* Interrupt service routines have to be in non-banked memory - as does the
45 scheduler startup function. */
46 #pragma CODE_SEG __NEAR_SEG NON_BANKED
47
48         /* Manual context switch function.  This is the SWI ISR. */
49         void interrupt vPortYield( void );
50
51         /* Tick context switch function.  This is the timer ISR. */
52         void interrupt vPortTickInterrupt( void );
53         
54         /* Simply called by xPortStartScheduler().  xPortStartScheduler() does not
55         start the scheduler directly because the header file containing the 
56         xPortStartScheduler() prototype is part of the common kernel code, and 
57         therefore cannot use the CODE_SEG pragma. */
58         static BaseType_t xBankedStartScheduler( void );
59
60 #pragma CODE_SEG DEFAULT
61
62 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the 
63 critical section should not be left (i.e. interrupts should not be re-enabled)
64 until the nesting depth reaches 0.  This variable simply tracks the nesting 
65 depth.  Each task maintains it's own critical nesting depth variable so 
66 uxCriticalNesting is saved and restored from the task stack during a context
67 switch. */
68 volatile UBaseType_t uxCriticalNesting = 0xff;
69
70 /*-----------------------------------------------------------*/
71
72 /* 
73  * See header file for description. 
74  */
75 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
76 {
77         /* 
78                 Place a few bytes of known values on the bottom of the stack.
79                 This can be uncommented to provide useful stack markers when debugging.
80
81                 *pxTopOfStack = ( StackType_t ) 0x11;
82                 pxTopOfStack--;
83                 *pxTopOfStack = ( StackType_t ) 0x22;
84                 pxTopOfStack--;
85                 *pxTopOfStack = ( StackType_t ) 0x33;
86                 pxTopOfStack--;
87         */
88
89
90
91         /* Setup the initial stack of the task.  The stack is set exactly as 
92         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as
93         expected by the HCS12 RTI instruction. */
94
95
96         /* The address of the task function is placed in the stack byte at a time. */
97         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 1 );
98         pxTopOfStack--;
99         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 0 );
100         pxTopOfStack--;
101
102         /* Next are all the registers that form part of the task context. */
103
104         /* Y register */
105         *pxTopOfStack = ( StackType_t ) 0xff;
106         pxTopOfStack--;
107         *pxTopOfStack = ( StackType_t ) 0xee;
108         pxTopOfStack--;
109
110         /* X register */
111         *pxTopOfStack = ( StackType_t ) 0xdd;
112         pxTopOfStack--;
113         *pxTopOfStack = ( StackType_t ) 0xcc;
114         pxTopOfStack--;
115  
116         /* A register contains parameter high byte. */
117         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 0 );
118         pxTopOfStack--;
119
120         /* B register contains parameter low byte. */
121         *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 1 );
122         pxTopOfStack--;
123
124         /* CCR: Note that when the task starts interrupts will be enabled since
125         "I" bit of CCR is cleared */
126         *pxTopOfStack = ( StackType_t ) 0x00;
127         pxTopOfStack--;
128         
129         #ifdef BANKED_MODEL
130                 /* The page of the task. */
131                 *pxTopOfStack = ( StackType_t ) ( ( int ) pxCode );
132                 pxTopOfStack--;
133         #endif
134         
135         /* Finally the critical nesting depth is initialised with 0 (not within
136         a critical section). */
137         *pxTopOfStack = ( StackType_t ) 0x00;
138
139         return pxTopOfStack;
140 }
141 /*-----------------------------------------------------------*/
142
143 void vPortEndScheduler( void )
144 {
145         /* It is unlikely that the HCS12 port will get stopped. */
146 }
147 /*-----------------------------------------------------------*/
148
149 static void prvSetupTimerInterrupt( void )
150 {
151         TickTimer_SetFreqHz( configTICK_RATE_HZ );
152         TickTimer_Enable();
153 }
154 /*-----------------------------------------------------------*/
155
156 BaseType_t xPortStartScheduler( void )
157 {
158         /* xPortStartScheduler() does not start the scheduler directly because 
159         the header file containing the xPortStartScheduler() prototype is part 
160         of the common kernel code, and therefore cannot use the CODE_SEG pragma. 
161         Instead it simply calls the locally defined xBankedStartScheduler() - 
162         which does use the CODE_SEG pragma. */
163
164         return xBankedStartScheduler();
165 }
166 /*-----------------------------------------------------------*/
167
168 #pragma CODE_SEG __NEAR_SEG NON_BANKED
169
170 static BaseType_t xBankedStartScheduler( void )
171 {
172         /* Configure the timer that will generate the RTOS tick.  Interrupts are
173         disabled when this function is called. */
174         prvSetupTimerInterrupt();
175
176         /* Restore the context of the first task. */
177         portRESTORE_CONTEXT();
178
179         /* Simulate the end of an interrupt to start the scheduler off. */
180         __asm( "rti" );
181
182         /* Should not get here! */
183         return pdFALSE;
184 }
185 /*-----------------------------------------------------------*/
186
187 /*
188  * Context switch functions.  These are both interrupt service routines.
189  */
190
191 /*
192  * Manual context switch forced by calling portYIELD().  This is the SWI
193  * handler.
194  */
195 void interrupt vPortYield( void )
196 {
197         portSAVE_CONTEXT();
198         vTaskSwitchContext();
199         portRESTORE_CONTEXT();
200 }
201 /*-----------------------------------------------------------*/
202
203 /*
204  * RTOS tick interrupt service routine.  If the cooperative scheduler is 
205  * being used then this simply increments the tick count.  If the 
206  * preemptive scheduler is being used a context switch can occur.
207  */
208 void interrupt vPortTickInterrupt( void )
209 {
210         #if configUSE_PREEMPTION == 1
211         {
212                 /* A context switch might happen so save the context. */
213                 portSAVE_CONTEXT();
214
215                 /* Increment the tick ... */
216                 if( xTaskIncrementTick() != pdFALSE )
217                 {
218                         vTaskSwitchContext();
219                 }
220
221                 TFLG1 = 1;                                                                 
222
223                 /* Restore the context of a task - which may be a different task
224                 to that interrupted. */
225                 portRESTORE_CONTEXT();  
226         }
227         #else
228         {
229                 xTaskIncrementTick();
230                 TFLG1 = 1;
231         }
232         #endif
233 }
234
235 #pragma CODE_SEG DEFAULT
236
237