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