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