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