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