2 FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* Scheduler includes. */
\r
55 #include "FreeRTOS.h"
\r
59 /*-----------------------------------------------------------
\r
60 * Implementation of functions defined in portable.h for the H8S port.
\r
61 *----------------------------------------------------------*/
\r
64 /*-----------------------------------------------------------*/
\r
66 /* When the task starts interrupts should be enabled. */
\r
67 #define portINITIAL_CCR ( ( portSTACK_TYPE ) 0x00 )
\r
69 /* Hardware specific constants used to generate the RTOS tick from the TPU. */
\r
70 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( unsigned char ) 0x20 )
\r
71 #define portCLOCK_DIV_64 ( ( unsigned char ) 0x03 )
\r
72 #define portCLOCK_DIV ( ( unsigned long ) 64 )
\r
73 #define portTGRA_INTERRUPT_ENABLE ( ( unsigned char ) 0x01 )
\r
74 #define portTIMER_CHANNEL ( ( unsigned char ) 0x02 )
\r
75 #define portMSTP13 ( ( unsigned short ) 0x2000 )
\r
78 * Setup TPU channel one for the RTOS tick at the requested frequency.
\r
80 static void prvSetupTimerInterrupt( void );
\r
83 * The ISR used by portYIELD(). This is installed as a trap handler.
\r
85 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
87 /*-----------------------------------------------------------*/
\r
90 * See header file for description.
\r
92 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
94 unsigned long ulValue;
\r
96 /* This requires an even address. */
\r
97 ulValue = ( unsigned long ) pxTopOfStack;
\r
100 pxTopOfStack = pxTopOfStack - 1;
\r
103 /* Place a few bytes of known values on the bottom of the stack.
\r
104 This is just useful for debugging. */
\r
106 *pxTopOfStack = 0xaa;
\r
108 *pxTopOfStack = 0xbb;
\r
110 *pxTopOfStack = 0xcc;
\r
112 *pxTopOfStack = 0xdd;
\r
114 /* The initial stack mimics an interrupt stack. First there is the program
\r
115 counter (24 bits). */
\r
116 ulValue = ( unsigned long ) pxCode;
\r
119 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
122 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
125 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
127 /* Followed by the CCR. */
\r
129 *pxTopOfStack = portINITIAL_CCR;
\r
131 /* Next all the general purpose registers - with the parameters being passed
\r
132 in ER0. The parameter order must match that used by the compiler when the
\r
133 "saveall" function attribute is used. */
\r
137 *pxTopOfStack = 0x66;
\r
139 *pxTopOfStack = 0x66;
\r
141 *pxTopOfStack = 0x66;
\r
143 *pxTopOfStack = 0x66;
\r
146 ulValue = ( unsigned long ) pvParameters;
\r
149 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
152 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
155 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
158 *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
\r
162 *pxTopOfStack = 0x11;
\r
164 *pxTopOfStack = 0x11;
\r
166 *pxTopOfStack = 0x11;
\r
168 *pxTopOfStack = 0x11;
\r
172 *pxTopOfStack = 0x22;
\r
174 *pxTopOfStack = 0x22;
\r
176 *pxTopOfStack = 0x22;
\r
178 *pxTopOfStack = 0x22;
\r
182 *pxTopOfStack = 0x33;
\r
184 *pxTopOfStack = 0x33;
\r
186 *pxTopOfStack = 0x33;
\r
188 *pxTopOfStack = 0x33;
\r
192 *pxTopOfStack = 0x44;
\r
194 *pxTopOfStack = 0x44;
\r
196 *pxTopOfStack = 0x44;
\r
198 *pxTopOfStack = 0x44;
\r
202 *pxTopOfStack = 0x55;
\r
204 *pxTopOfStack = 0x55;
\r
206 *pxTopOfStack = 0x55;
\r
208 *pxTopOfStack = 0x55;
\r
210 return pxTopOfStack;
\r
212 /*-----------------------------------------------------------*/
\r
214 portBASE_TYPE xPortStartScheduler( void )
\r
216 extern void * pxCurrentTCB;
\r
218 /* Setup the hardware to generate the tick. */
\r
219 prvSetupTimerInterrupt();
\r
221 /* Restore the context of the first task that is going to run. This
\r
222 mirrors the function epilogue code generated by the compiler when the
\r
223 "saveall" function attribute is used. */
\r
225 "MOV.L @_pxCurrentTCB, ER6 \n\t"
\r
226 "MOV.L @ER6, ER7 \n\t"
\r
227 "LDM.L @SP+, (ER4-ER5) \n\t"
\r
228 "LDM.L @SP+, (ER0-ER3) \n\t"
\r
229 "MOV.L @ER7+, ER6 \n\t"
\r
233 ( void ) pxCurrentTCB;
\r
235 /* Should not get here. */
\r
238 /*-----------------------------------------------------------*/
\r
240 void vPortEndScheduler( void )
\r
242 /* It is unlikely that the h8 port will get stopped. */
\r
244 /*-----------------------------------------------------------*/
\r
247 * Manual context switch. This is a trap handler. The "saveall" function
\r
248 * attribute is used so the context is saved by the compiler prologue. All
\r
249 * we have to do is save the stack pointer.
\r
251 void vPortYield( void )
\r
253 portSAVE_STACK_POINTER();
\r
254 vTaskSwitchContext();
\r
255 portRESTORE_STACK_POINTER();
\r
257 /*-----------------------------------------------------------*/
\r
260 * The interrupt handler installed for the RTOS tick depends on whether the
\r
261 * preemptive or cooperative scheduler is being used.
\r
263 #if( configUSE_PREEMPTION == 1 )
\r
266 * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().
\r
267 * The function prologue saves the context so all we have to do is save
\r
268 * the stack pointer.
\r
270 void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
271 void vTickISR( void )
\r
273 portSAVE_STACK_POINTER();
\r
275 vTaskIncrementTick();
\r
276 vTaskSwitchContext();
\r
278 /* Clear the interrupt. */
\r
281 portRESTORE_STACK_POINTER();
\r
287 * The cooperative scheduler is being used so all we have to do is
\r
288 * periodically increment the tick. This can just be a normal ISR and
\r
289 * the "saveall" attribute is not required.
\r
291 void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );
\r
292 void vTickISR( void )
\r
294 vTaskIncrementTick();
\r
296 /* Clear the interrupt. */
\r
301 /*-----------------------------------------------------------*/
\r
304 * Setup timer 1 compare match to generate a tick interrupt.
\r
306 static void prvSetupTimerInterrupt( void )
\r
308 const unsigned long ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;
\r
310 /* Turn the module on. */
\r
311 MSTPCR &= ~portMSTP13;
\r
313 /* Configure timer 1. */
\r
314 TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;
\r
316 /* Configure the compare match value for a tick of configTICK_RATE_HZ. */
\r
317 TGR1A = ulCompareMatch;
\r
319 /* Start the timer and enable the interrupt - we can do this here as
\r
320 interrupts are globally disabled when this function is called. */
\r
321 TIER1 |= portTGRA_INTERRUPT_ENABLE;
\r
322 TSTR |= portTIMER_CHANNEL;
\r
324 /*-----------------------------------------------------------*/
\r