]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/HCS12/port.c
Add xEventGroupClearBitsFromISR() and xEventGroupGetBitsFromISR() functions.
[freertos] / FreeRTOS / Source / portable / GCC / HCS12 / port.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* GCC/HCS12 port by Jefferson L Smith, 2005 */\r
67 \r
68 /* Scheduler includes. */\r
69 #include "FreeRTOS.h"\r
70 #include "task.h"\r
71 \r
72 /* Port includes */\r
73 #include <sys/ports_def.h>\r
74 \r
75 /*-----------------------------------------------------------\r
76  * Implementation of functions defined in portable.h for the HCS12 port.\r
77  *----------------------------------------------------------*/\r
78 \r
79 \r
80 /*\r
81  * Configure a timer to generate the RTOS tick at the frequency specified \r
82  * within FreeRTOSConfig.h.\r
83  */\r
84 static void prvSetupTimerInterrupt( void );\r
85 \r
86 /* NOTE: Interrupt service routines must be in non-banked memory - as does the\r
87 scheduler startup function. */\r
88 #define ATTR_NEAR       __attribute__((near))\r
89 \r
90 /* Manual context switch function.  This is the SWI ISR. */\r
91 // __attribute__((interrupt))\r
92 void ATTR_NEAR vPortYield( void );\r
93 \r
94 /* Tick context switch function.  This is the timer ISR. */\r
95 // __attribute__((interrupt))\r
96 void ATTR_NEAR vPortTickInterrupt( void );\r
97 \r
98 /* Function in non-banked memory which actually switches to first task. */\r
99 BaseType_t ATTR_NEAR xStartSchedulerNear( void );\r
100 \r
101 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
102 critical section should not be left (i.e. interrupts should not be re-enabled)\r
103 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
104 depth.  Each task maintains it's own critical nesting depth variable so \r
105 uxCriticalNesting is saved and restored from the task stack during a context\r
106 switch. */\r
107 volatile UBaseType_t uxCriticalNesting = 0x80;  // un-initialized\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* \r
112  * See header file for description. \r
113  */\r
114 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
115 {\r
116 \r
117 \r
118         /* Setup the initial stack of the task.  The stack is set exactly as \r
119         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
120         expected by the HCS12 RTI instruction. */\r
121 \r
122 \r
123         /* The address of the task function is placed in the stack byte at a time. */\r
124         *pxTopOfStack   = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 1 );\r
125         *--pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 0 );\r
126 \r
127         /* Next are all the registers that form part of the task context. */\r
128 \r
129         /* Y register */\r
130         *--pxTopOfStack = ( StackType_t ) 0xff;\r
131         *--pxTopOfStack = ( StackType_t ) 0xee;\r
132 \r
133         /* X register */\r
134         *--pxTopOfStack = ( StackType_t ) 0xdd;\r
135         *--pxTopOfStack = ( StackType_t ) 0xcc;\r
136  \r
137         /* A register contains parameter high byte. */\r
138         *--pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 0 );\r
139 \r
140         /* B register contains parameter low byte. */\r
141         *--pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 1 );\r
142 \r
143         /* CCR: Note that when the task starts interrupts will be enabled since\r
144         "I" bit of CCR is cleared */\r
145         *--pxTopOfStack = ( StackType_t ) 0x80;         // keeps Stop disabled (MCU default)\r
146         \r
147         /* tmp softregs used by GCC. Values right now don't     matter. */\r
148         __asm("\n\\r
149                 movw _.frame, 2,-%0                                                     \n\\r
150                 movw _.tmp, 2,-%0                                                       \n\\r
151                 movw _.z, 2,-%0                                                         \n\\r
152                 movw _.xy, 2,-%0                                                        \n\\r
153                 ;movw _.d2, 2,-%0                                                       \n\\r
154                 ;movw _.d1, 2,-%0                                                       \n\\r
155         ": "=A"(pxTopOfStack) : "0"(pxTopOfStack) );\r
156 \r
157         #ifdef BANKED_MODEL\r
158                 /* The page of the task. */\r
159                 *--pxTopOfStack = 0x30;      // can only directly start in PPAGE 0x30\r
160         #endif\r
161         \r
162         /* The critical nesting depth is initialised with 0 (meaning not in\r
163         a critical section). */\r
164         *--pxTopOfStack = ( StackType_t ) 0x00;\r
165 \r
166 \r
167         return pxTopOfStack;\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 void vPortEndScheduler( void )\r
172 {\r
173         /* It is unlikely that the HCS12 port will get stopped. */\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 static void prvSetupTimerInterrupt( void )\r
178 {\r
179         /* Enable hardware RTI timer */\r
180         /* Ignores configTICK_RATE_HZ */\r
181         RTICTL = 0x50;                  // 16 MHz xtal: 976.56 Hz, 1024mS \r
182         CRGINT |= 0x80;                 // RTIE\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 BaseType_t xPortStartScheduler( void )\r
187 {\r
188         /* xPortStartScheduler() does not start the scheduler directly because \r
189         the header file containing the xPortStartScheduler() prototype is part \r
190         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
191         Instead it simply calls the locally defined xNearStartScheduler() - \r
192         which does use the CODE_SEG pragma. */\r
193 \r
194         int16_t register d;\r
195         __asm ("jmp  xStartSchedulerNear                ; will never return": "=d"(d));\r
196         return d;\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 BaseType_t xStartSchedulerNear( void )\r
201 {\r
202         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
203         disabled when this function is called. */\r
204         prvSetupTimerInterrupt();\r
205 \r
206         /* Restore the context of the first task. */\r
207         portRESTORE_CONTEXT();\r
208 \r
209         portISR_TAIL();\r
210 \r
211         /* Should not get here! */\r
212         return pdFALSE;\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 /*\r
217  * Context switch functions.  These are interrupt service routines.\r
218  */\r
219 \r
220 /*\r
221  * Manual context switch forced by calling portYIELD().  This is the SWI\r
222  * handler.\r
223  */\r
224 void vPortYield( void )\r
225 {\r
226         portISR_HEAD();\r
227         /* NOTE: This is the trap routine (swi) although not defined as a trap.\r
228            It will fill the stack the same way as an ISR in order to mix preemtion\r
229            and cooperative yield. */\r
230 \r
231         portSAVE_CONTEXT();\r
232         vTaskSwitchContext();\r
233         portRESTORE_CONTEXT();\r
234 \r
235         portISR_TAIL();\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 /*\r
240  * RTOS tick interrupt service routine.  If the cooperative scheduler is \r
241  * being used then this simply increments the tick count.  If the \r
242  * preemptive scheduler is being used a context switch can occur.\r
243  */\r
244 void vPortTickInterrupt( void )\r
245 {\r
246         portISR_HEAD();\r
247 \r
248         /* Clear tick timer flag */\r
249         CRGFLG = 0x80;\r
250 \r
251         #if configUSE_PREEMPTION == 1\r
252         {\r
253                 /* A context switch might happen so save the context. */\r
254                 portSAVE_CONTEXT();\r
255 \r
256                 /* Increment the tick ... */\r
257                 if( xTaskIncrementTick() != pdFALSE )\r
258                 {\r
259                         /* A context switch is necessary. */\r
260                         vTaskSwitchContext();\r
261                 }\r
262 \r
263                 /* Restore the context of a task - which may be a different task\r
264                 to that interrupted. */\r
265                 portRESTORE_CONTEXT();\r
266         }\r
267         #else\r
268         {\r
269                 xTaskIncrementTick();\r
270         }\r
271         #endif\r
272 \r
273         portISR_TAIL();\r
274 }\r
275 \r