2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\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
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
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\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
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
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
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\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
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
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
66 /* Scheduler includes. */
\r
67 #include "FreeRTOS.h"
\r
71 /*-----------------------------------------------------------
\r
72 * Implementation of functions defined in portable.h for the H8S port.
\r
73 *----------------------------------------------------------*/
\r
76 /*-----------------------------------------------------------*/
\r
78 /* When the task starts interrupts should be enabled. */
\r
79 #define portINITIAL_CCR ( ( StackType_t ) 0x00 )
\r
81 /* Hardware specific constants used to generate the RTOS tick from the TPU. */
\r
82 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( uint8_t ) 0x20 )
\r
83 #define portCLOCK_DIV_64 ( ( uint8_t ) 0x03 )
\r
84 #define portCLOCK_DIV ( ( uint32_t ) 64 )
\r
85 #define portTGRA_INTERRUPT_ENABLE ( ( uint8_t ) 0x01 )
\r
86 #define portTIMER_CHANNEL ( ( uint8_t ) 0x02 )
\r
87 #define portMSTP13 ( ( uint16_t ) 0x2000 )
\r
90 * Setup TPU channel one for the RTOS tick at the requested frequency.
\r
92 static void prvSetupTimerInterrupt( void );
\r
95 * The ISR used by portYIELD(). This is installed as a trap handler.
\r
97 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
99 /*-----------------------------------------------------------*/
\r
102 * See header file for description.
\r
104 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
108 /* This requires an even address. */
\r
109 ulValue = ( uint32_t ) pxTopOfStack;
\r
110 if( ulValue & 1UL )
\r
112 pxTopOfStack = pxTopOfStack - 1;
\r
115 /* Place a few bytes of known values on the bottom of the stack.
\r
116 This is just useful for debugging. */
\r
118 *pxTopOfStack = 0xaa;
\r
120 *pxTopOfStack = 0xbb;
\r
122 *pxTopOfStack = 0xcc;
\r
124 *pxTopOfStack = 0xdd;
\r
126 /* The initial stack mimics an interrupt stack. First there is the program
\r
127 counter (24 bits). */
\r
128 ulValue = ( uint32_t ) pxCode;
\r
131 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
134 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
137 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
139 /* Followed by the CCR. */
\r
141 *pxTopOfStack = portINITIAL_CCR;
\r
143 /* Next all the general purpose registers - with the parameters being passed
\r
144 in ER0. The parameter order must match that used by the compiler when the
\r
145 "saveall" function attribute is used. */
\r
149 *pxTopOfStack = 0x66;
\r
151 *pxTopOfStack = 0x66;
\r
153 *pxTopOfStack = 0x66;
\r
155 *pxTopOfStack = 0x66;
\r
158 ulValue = ( uint32_t ) pvParameters;
\r
161 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
164 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
167 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
170 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
174 *pxTopOfStack = 0x11;
\r
176 *pxTopOfStack = 0x11;
\r
178 *pxTopOfStack = 0x11;
\r
180 *pxTopOfStack = 0x11;
\r
184 *pxTopOfStack = 0x22;
\r
186 *pxTopOfStack = 0x22;
\r
188 *pxTopOfStack = 0x22;
\r
190 *pxTopOfStack = 0x22;
\r
194 *pxTopOfStack = 0x33;
\r
196 *pxTopOfStack = 0x33;
\r
198 *pxTopOfStack = 0x33;
\r
200 *pxTopOfStack = 0x33;
\r
204 *pxTopOfStack = 0x44;
\r
206 *pxTopOfStack = 0x44;
\r
208 *pxTopOfStack = 0x44;
\r
210 *pxTopOfStack = 0x44;
\r
214 *pxTopOfStack = 0x55;
\r
216 *pxTopOfStack = 0x55;
\r
218 *pxTopOfStack = 0x55;
\r
220 *pxTopOfStack = 0x55;
\r
222 return pxTopOfStack;
\r
224 /*-----------------------------------------------------------*/
\r
226 BaseType_t xPortStartScheduler( void )
\r
228 extern void * pxCurrentTCB;
\r
230 /* Setup the hardware to generate the tick. */
\r
231 prvSetupTimerInterrupt();
\r
233 /* Restore the context of the first task that is going to run. This
\r
234 mirrors the function epilogue code generated by the compiler when the
\r
235 "saveall" function attribute is used. */
\r
237 "MOV.L @_pxCurrentTCB, ER6 \n\t"
\r
238 "MOV.L @ER6, ER7 \n\t"
\r
239 "LDM.L @SP+, (ER4-ER5) \n\t"
\r
240 "LDM.L @SP+, (ER0-ER3) \n\t"
\r
241 "MOV.L @ER7+, ER6 \n\t"
\r
245 ( void ) pxCurrentTCB;
\r
247 /* Should not get here. */
\r
250 /*-----------------------------------------------------------*/
\r
252 void vPortEndScheduler( void )
\r
254 /* It is unlikely that the h8 port will get stopped. */
\r
256 /*-----------------------------------------------------------*/
\r
259 * Manual context switch. This is a trap handler. The "saveall" function
\r
260 * attribute is used so the context is saved by the compiler prologue. All
\r
261 * we have to do is save the stack pointer.
\r
263 void vPortYield( void )
\r
265 portSAVE_STACK_POINTER();
\r
266 vTaskSwitchContext();
\r
267 portRESTORE_STACK_POINTER();
\r
269 /*-----------------------------------------------------------*/
\r
272 * The interrupt handler installed for the RTOS tick depends on whether the
\r
273 * preemptive or cooperative scheduler is being used.
\r
275 #if( configUSE_PREEMPTION == 1 )
\r
278 * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().
\r
279 * The function prologue saves the context so all we have to do is save
\r
280 * the stack pointer.
\r
282 void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
283 void vTickISR( void )
\r
285 portSAVE_STACK_POINTER();
\r
287 if( xTaskIncrementTick() != pdFALSE )
\r
289 vTaskSwitchContext();
\r
292 /* Clear the interrupt. */
\r
295 portRESTORE_STACK_POINTER();
\r
301 * The cooperative scheduler is being used so all we have to do is
\r
302 * periodically increment the tick. This can just be a normal ISR and
\r
303 * the "saveall" attribute is not required.
\r
305 void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );
\r
306 void vTickISR( void )
\r
308 xTaskIncrementTick();
\r
310 /* Clear the interrupt. */
\r
315 /*-----------------------------------------------------------*/
\r
318 * Setup timer 1 compare match to generate a tick interrupt.
\r
320 static void prvSetupTimerInterrupt( void )
\r
322 const uint32_t ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;
\r
324 /* Turn the module on. */
\r
325 MSTPCR &= ~portMSTP13;
\r
327 /* Configure timer 1. */
\r
328 TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;
\r
330 /* Configure the compare match value for a tick of configTICK_RATE_HZ. */
\r
331 TGR1A = ulCompareMatch;
\r
333 /* Start the timer and enable the interrupt - we can do this here as
\r
334 interrupts are globally disabled when this function is called. */
\r
335 TIER1 |= portTGRA_INTERRUPT_ENABLE;
\r
336 TSTR |= portTIMER_CHANNEL;
\r
338 /*-----------------------------------------------------------*/
\r