2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* GCC/HCS12 port by Jefferson L Smith, 2005 */
31 /* Scheduler includes. */
36 #include <sys/ports_def.h>
38 /*-----------------------------------------------------------
39 * Implementation of functions defined in portable.h for the HCS12 port.
40 *----------------------------------------------------------*/
44 * Configure a timer to generate the RTOS tick at the frequency specified
45 * within FreeRTOSConfig.h.
47 static void prvSetupTimerInterrupt( void );
49 /* NOTE: Interrupt service routines must be in non-banked memory - as does the
50 scheduler startup function. */
51 #define ATTR_NEAR __attribute__((near))
53 /* Manual context switch function. This is the SWI ISR. */
54 // __attribute__((interrupt))
55 void ATTR_NEAR vPortYield( void );
57 /* Tick context switch function. This is the timer ISR. */
58 // __attribute__((interrupt))
59 void ATTR_NEAR vPortTickInterrupt( void );
61 /* Function in non-banked memory which actually switches to first task. */
62 BaseType_t ATTR_NEAR xStartSchedulerNear( void );
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
70 volatile UBaseType_t uxCriticalNesting = 0x80; // un-initialized
72 /*-----------------------------------------------------------*/
75 * See header file for description.
77 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
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. */
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 );
90 /* Next are all the registers that form part of the task context. */
93 *--pxTopOfStack = ( StackType_t ) 0xff;
94 *--pxTopOfStack = ( StackType_t ) 0xee;
97 *--pxTopOfStack = ( StackType_t ) 0xdd;
98 *--pxTopOfStack = ( StackType_t ) 0xcc;
100 /* A register contains parameter high byte. */
101 *--pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 0 );
103 /* B register contains parameter low byte. */
104 *--pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 1 );
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)
110 /* tmp softregs used by GCC. Values right now don't matter. */
112 movw _.frame, 2,-%0 \n\
113 movw _.tmp, 2,-%0 \n\
116 ;movw _.d2, 2,-%0 \n\
117 ;movw _.d1, 2,-%0 \n\
118 ": "=A"(pxTopOfStack) : "0"(pxTopOfStack) );
121 /* The page of the task. */
122 *--pxTopOfStack = 0x30; // can only directly start in PPAGE 0x30
125 /* The critical nesting depth is initialised with 0 (meaning not in
126 a critical section). */
127 *--pxTopOfStack = ( StackType_t ) 0x00;
132 /*-----------------------------------------------------------*/
134 void vPortEndScheduler( void )
136 /* It is unlikely that the HCS12 port will get stopped. */
138 /*-----------------------------------------------------------*/
140 static void prvSetupTimerInterrupt( void )
142 /* Enable hardware RTI timer */
143 /* Ignores configTICK_RATE_HZ */
144 RTICTL = 0x50; // 16 MHz xtal: 976.56 Hz, 1024mS
145 CRGINT |= 0x80; // RTIE
147 /*-----------------------------------------------------------*/
149 BaseType_t xPortStartScheduler( void )
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. */
158 __asm ("jmp xStartSchedulerNear ; will never return": "=d"(d));
161 /*-----------------------------------------------------------*/
163 BaseType_t xStartSchedulerNear( void )
165 /* Configure the timer that will generate the RTOS tick. Interrupts are
166 disabled when this function is called. */
167 prvSetupTimerInterrupt();
169 /* Restore the context of the first task. */
170 portRESTORE_CONTEXT();
174 /* Should not get here! */
177 /*-----------------------------------------------------------*/
180 * Context switch functions. These are interrupt service routines.
184 * Manual context switch forced by calling portYIELD(). This is the SWI
187 void vPortYield( void )
190 /* NOTE: This is the trap routine (swi) although not defined as a trap.
191 It will fill the stack the same way as an ISR in order to mix preemtion
192 and cooperative yield. */
195 vTaskSwitchContext();
196 portRESTORE_CONTEXT();
200 /*-----------------------------------------------------------*/
203 * RTOS tick interrupt service routine. If the cooperative scheduler is
204 * being used then this simply increments the tick count. If the
205 * preemptive scheduler is being used a context switch can occur.
207 void vPortTickInterrupt( void )
211 /* Clear tick timer flag */
214 #if configUSE_PREEMPTION == 1
216 /* A context switch might happen so save the context. */
219 /* Increment the tick ... */
220 if( xTaskIncrementTick() != pdFALSE )
222 /* A context switch is necessary. */
223 vTaskSwitchContext();
226 /* Restore the context of a task - which may be a different task
227 to that interrupted. */
228 portRESTORE_CONTEXT();
232 xTaskIncrementTick();