1 /*
2  * FreeRTOS Kernel V11.1.0
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 /* GCC/HCS12 port by Jefferson L Smith, 2005 */
30 
31 /* Scheduler includes. */
32 #include "FreeRTOS.h"
33 #include "task.h"
34 
35 /* Port includes */
36 #include <sys/ports_def.h>
37 
38 /*-----------------------------------------------------------
39 * Implementation of functions defined in portable.h for the HCS12 port.
40 *----------------------------------------------------------*/
41 
42 
43 /*
44  * Configure a timer to generate the RTOS tick at the frequency specified
45  * within FreeRTOSConfig.h.
46  */
47 static void prvSetupTimerInterrupt( void );
48 
49 /* NOTE: Interrupt service routines must be in non-banked memory - as does the
50  * scheduler startup function. */
51 #define ATTR_NEAR    __attribute__( ( near ) )
52 
53 /* Manual context switch function.  This is the SWI ISR. */
54 /* __attribute__((interrupt)) */
55 void ATTR_NEAR vPortYield( void );
56 
57 /* Tick context switch function.  This is the timer ISR. */
58 /* __attribute__((interrupt)) */
59 void ATTR_NEAR vPortTickInterrupt( void );
60 
61 /* Function in non-banked memory which actually switches to first task. */
62 BaseType_t ATTR_NEAR xStartSchedulerNear( void );
63 
64 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the
65  * critical section should not be left (i.e. interrupts should not be re-enabled)
66  * until the nesting depth reaches 0.  This variable simply tracks the nesting
67  * depth.  Each task maintains it's own critical nesting depth variable so
68  * uxCriticalNesting is saved and restored from the task stack during a context
69  * switch. */
70 volatile UBaseType_t uxCriticalNesting = 0x80; /* un-initialized */
71 
72 /*-----------------------------------------------------------*/
73 
74 /*
75  * See header file for description.
76  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)77 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
78                                      TaskFunction_t pxCode,
79                                      void * pvParameters )
80 {
81     /* Setup the initial stack of the task.  The stack is set exactly as
82      * expected by the portRESTORE_CONTEXT() macro.  In this case the stack as
83      * expected by the HCS12 RTI instruction. */
84 
85 
86     /* The address of the task function is placed in the stack byte at a time. */
87     *pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pxCode ) ) + 1 );
88     *--pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pxCode ) ) + 0 );
89 
90     /* Next are all the registers that form part of the task context. */
91 
92     /* Y register */
93     *--pxTopOfStack = ( StackType_t ) 0xff;
94     *--pxTopOfStack = ( StackType_t ) 0xee;
95 
96     /* X register */
97     *--pxTopOfStack = ( StackType_t ) 0xdd;
98     *--pxTopOfStack = ( StackType_t ) 0xcc;
99 
100     /* A register contains parameter high byte. */
101     *--pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pvParameters ) ) + 0 );
102 
103     /* B register contains parameter low byte. */
104     *--pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pvParameters ) ) + 1 );
105 
106     /* CCR: Note that when the task starts interrupts will be enabled since
107      * "I" bit of CCR is cleared */
108     *--pxTopOfStack = ( StackType_t ) 0x80; /* keeps Stop disabled (MCU default) */
109 
110     /* tmp softregs used by GCC. Values right now don't matter. */
111     __asm( "\n\
112         movw _.frame, 2,-%0                         \n\
113         movw _.tmp, 2,-%0                           \n\
114         movw _.z, 2,-%0                             \n\
115         movw _.xy, 2,-%0                            \n\
116         ;movw _.d2, 2,-%0                           \n\
117         ;movw _.d1, 2,-%0                           \n\
118     " : "=A" ( pxTopOfStack ) : "0" ( pxTopOfStack ) );
119 
120     #ifdef BANKED_MODEL
121         /* The page of the task. */
122         *--pxTopOfStack = 0x30; /* can only directly start in PPAGE 0x30 */
123     #endif
124 
125     /* The critical nesting depth is initialised with 0 (meaning not in
126      * a critical section). */
127     *--pxTopOfStack = ( StackType_t ) 0x00;
128 
129 
130     return pxTopOfStack;
131 }
132 /*-----------------------------------------------------------*/
133 
vPortEndScheduler(void)134 void vPortEndScheduler( void )
135 {
136     /* It is unlikely that the HCS12 port will get stopped. */
137 }
138 /*-----------------------------------------------------------*/
139 
prvSetupTimerInterrupt(void)140 static void prvSetupTimerInterrupt( void )
141 {
142     /* Enable hardware RTI timer */
143     /* Ignores configTICK_RATE_HZ */
144     RTICTL = 0x50;  /* 16 MHz xtal: 976.56 Hz, 1024mS */
145     CRGINT |= 0x80; /* RTIE */
146 }
147 /*-----------------------------------------------------------*/
148 
xPortStartScheduler(void)149 BaseType_t xPortStartScheduler( void )
150 {
151     /* xPortStartScheduler() does not start the scheduler directly because
152      * the header file containing the xPortStartScheduler() prototype is part
153      * of the common kernel code, and therefore cannot use the CODE_SEG pragma.
154      * Instead it simply calls the locally defined xNearStartScheduler() -
155      * which does use the CODE_SEG pragma. */
156 
157     int16_t register d;
158 
159     __asm( "jmp  xStartSchedulerNear        ; will never return" : "=d" ( d ) );
160     return d;
161 }
162 /*-----------------------------------------------------------*/
163 
xStartSchedulerNear(void)164 BaseType_t xStartSchedulerNear( void )
165 {
166     /* Configure the timer that will generate the RTOS tick.  Interrupts are
167      * disabled when this function is called. */
168     prvSetupTimerInterrupt();
169 
170     /* Restore the context of the first task. */
171     portRESTORE_CONTEXT();
172 
173     portISR_TAIL();
174 
175     /* Should not get here! */
176     return pdFALSE;
177 }
178 /*-----------------------------------------------------------*/
179 
180 /*
181  * Context switch functions.  These are interrupt service routines.
182  */
183 
184 /*
185  * Manual context switch forced by calling portYIELD().  This is the SWI
186  * handler.
187  */
vPortYield(void)188 void vPortYield( void )
189 {
190     portISR_HEAD();
191 
192     /* NOTE: This is the trap routine (swi) although not defined as a trap.
193      * It will fill the stack the same way as an ISR in order to mix preemtion
194      * and cooperative yield. */
195 
196     portSAVE_CONTEXT();
197     vTaskSwitchContext();
198     portRESTORE_CONTEXT();
199 
200     portISR_TAIL();
201 }
202 /*-----------------------------------------------------------*/
203 
204 /*
205  * RTOS tick interrupt service routine.  If the cooperative scheduler is
206  * being used then this simply increments the tick count.  If the
207  * preemptive scheduler is being used a context switch can occur.
208  */
vPortTickInterrupt(void)209 void vPortTickInterrupt( void )
210 {
211     portISR_HEAD();
212 
213     /* Clear tick timer flag */
214     CRGFLG = 0x80;
215 
216     #if configUSE_PREEMPTION == 1
217     {
218         /* A context switch might happen so save the context. */
219         portSAVE_CONTEXT();
220 
221         /* Increment the tick ... */
222         if( xTaskIncrementTick() != pdFALSE )
223         {
224             /* A context switch is necessary. */
225             vTaskSwitchContext();
226         }
227 
228         /* Restore the context of a task - which may be a different task
229          * to that interrupted. */
230         portRESTORE_CONTEXT();
231     }
232     #else /* if configUSE_PREEMPTION == 1 */
233     {
234         xTaskIncrementTick();
235     }
236     #endif /* if configUSE_PREEMPTION == 1 */
237 
238     portISR_TAIL();
239 }
240