2 FreeRTOS.org V5.1.1 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
50 /* Scheduler includes. */
\r
51 #include "FreeRTOS.h"
\r
55 /*-----------------------------------------------------------
\r
56 * Implementation of functions defined in portable.h for the H8S port.
\r
57 *----------------------------------------------------------*/
\r
60 /*-----------------------------------------------------------*/
\r
62 /* When the task starts interrupts should be enabled. */
\r
63 #define portINITIAL_CCR ( ( portSTACK_TYPE ) 0x00 )
\r
65 /* Hardware specific constants used to generate the RTOS tick from the TPU. */
\r
66 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( unsigned portCHAR ) 0x20 )
\r
67 #define portCLOCK_DIV_64 ( ( unsigned portCHAR ) 0x03 )
\r
68 #define portCLOCK_DIV ( ( unsigned portLONG ) 64 )
\r
69 #define portTGRA_INTERRUPT_ENABLE ( ( unsigned portCHAR ) 0x01 )
\r
70 #define portTIMER_CHANNEL ( ( unsigned portCHAR ) 0x02 )
\r
71 #define portMSTP13 ( ( unsigned portSHORT ) 0x2000 )
\r
74 * Setup TPU channel one for the RTOS tick at the requested frequency.
\r
76 static void prvSetupTimerInterrupt( void );
\r
79 * The ISR used by portYIELD(). This is installed as a trap handler.
\r
81 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
83 /*-----------------------------------------------------------*/
\r
86 * See header file for description.
\r
88 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
90 unsigned portLONG ulValue;
\r
92 /* This requires an even address. */
\r
93 ulValue = ( unsigned portLONG ) pxTopOfStack;
\r
96 pxTopOfStack = pxTopOfStack - 1;
\r
99 /* Place a few bytes of known values on the bottom of the stack.
\r
100 This is just useful for debugging. */
\r
102 *pxTopOfStack = 0xaa;
\r
104 *pxTopOfStack = 0xbb;
\r
106 *pxTopOfStack = 0xcc;
\r
108 *pxTopOfStack = 0xdd;
\r
110 /* The initial stack mimics an interrupt stack. First there is the program
\r
111 counter (24 bits). */
\r
112 ulValue = ( unsigned portLONG ) pxCode;
\r
115 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
118 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
121 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
123 /* Followed by the CCR. */
\r
125 *pxTopOfStack = portINITIAL_CCR;
\r
127 /* Next all the general purpose registers - with the parameters being passed
\r
128 in ER0. The parameter order must match that used by the compiler when the
\r
129 "saveall" function attribute is used. */
\r
133 *pxTopOfStack = 0x66;
\r
135 *pxTopOfStack = 0x66;
\r
137 *pxTopOfStack = 0x66;
\r
139 *pxTopOfStack = 0x66;
\r
142 ulValue = ( unsigned portLONG ) pvParameters;
\r
145 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
148 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
151 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
154 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
158 *pxTopOfStack = 0x11;
\r
160 *pxTopOfStack = 0x11;
\r
162 *pxTopOfStack = 0x11;
\r
164 *pxTopOfStack = 0x11;
\r
168 *pxTopOfStack = 0x22;
\r
170 *pxTopOfStack = 0x22;
\r
172 *pxTopOfStack = 0x22;
\r
174 *pxTopOfStack = 0x22;
\r
178 *pxTopOfStack = 0x33;
\r
180 *pxTopOfStack = 0x33;
\r
182 *pxTopOfStack = 0x33;
\r
184 *pxTopOfStack = 0x33;
\r
188 *pxTopOfStack = 0x44;
\r
190 *pxTopOfStack = 0x44;
\r
192 *pxTopOfStack = 0x44;
\r
194 *pxTopOfStack = 0x44;
\r
198 *pxTopOfStack = 0x55;
\r
200 *pxTopOfStack = 0x55;
\r
202 *pxTopOfStack = 0x55;
\r
204 *pxTopOfStack = 0x55;
\r
206 return pxTopOfStack;
\r
208 /*-----------------------------------------------------------*/
\r
210 portBASE_TYPE xPortStartScheduler( void )
\r
212 extern void * pxCurrentTCB;
\r
214 /* Setup the hardware to generate the tick. */
\r
215 prvSetupTimerInterrupt();
\r
217 /* Restore the context of the first task that is going to run. This
\r
218 mirrors the function epilogue code generated by the compiler when the
\r
219 "saveall" function attribute is used. */
\r
221 "MOV.L @_pxCurrentTCB, ER6 \n\t"
\r
222 "MOV.L @ER6, ER7 \n\t"
\r
223 "LDM.L @SP+, (ER4-ER5) \n\t"
\r
224 "LDM.L @SP+, (ER0-ER3) \n\t"
\r
225 "MOV.L @ER7+, ER6 \n\t"
\r
229 ( void ) pxCurrentTCB;
\r
231 /* Should not get here. */
\r
234 /*-----------------------------------------------------------*/
\r
236 void vPortEndScheduler( void )
\r
238 /* It is unlikely that the h8 port will get stopped. */
\r
240 /*-----------------------------------------------------------*/
\r
243 * Manual context switch. This is a trap handler. The "saveall" function
\r
244 * attribute is used so the context is saved by the compiler prologue. All
\r
245 * we have to do is save the stack pointer.
\r
247 void vPortYield( void )
\r
249 portSAVE_STACK_POINTER();
\r
250 vTaskSwitchContext();
\r
251 portRESTORE_STACK_POINTER();
\r
253 /*-----------------------------------------------------------*/
\r
256 * The interrupt handler installed for the RTOS tick depends on whether the
\r
257 * preemptive or cooperative scheduler is being used.
\r
259 #if( configUSE_PREEMPTION == 1 )
\r
262 * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().
\r
263 * The function prologue saves the context so all we have to do is save
\r
264 * the stack pointer.
\r
266 void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
267 void vTickISR( void )
\r
269 portSAVE_STACK_POINTER();
\r
271 vTaskIncrementTick();
\r
272 vTaskSwitchContext();
\r
274 /* Clear the interrupt. */
\r
277 portRESTORE_STACK_POINTER();
\r
283 * The cooperative scheduler is being used so all we have to do is
\r
284 * periodically increment the tick. This can just be a normal ISR and
\r
285 * the "saveall" attribute is not required.
\r
287 void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );
\r
288 void vTickISR( void )
\r
290 vTaskIncrementTick();
\r
292 /* Clear the interrupt. */
\r
297 /*-----------------------------------------------------------*/
\r
300 * Setup timer 1 compare match to generate a tick interrupt.
\r
302 static void prvSetupTimerInterrupt( void )
\r
304 const unsigned portLONG ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;
\r
306 /* Turn the module on. */
\r
307 MSTPCR &= ~portMSTP13;
\r
309 /* Configure timer 1. */
\r
310 TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;
\r
312 /* Configure the compare match value for a tick of configTICK_RATE_HZ. */
\r
313 TGR1A = ulCompareMatch;
\r
315 /* Start the timer and enable the interrupt - we can do this here as
\r
316 interrupts are globally disabled when this function is called. */
\r
317 TIER1 |= portTGRA_INTERRUPT_ENABLE;
\r
318 TSTR |= portTIMER_CHANNEL;
\r
320 /*-----------------------------------------------------------*/
\r