2 FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* Scheduler includes. */
\r
71 #include "FreeRTOS.h"
\r
75 /*-----------------------------------------------------------
\r
76 * Implementation of functions defined in portable.h for the H8S port.
\r
77 *----------------------------------------------------------*/
\r
80 /*-----------------------------------------------------------*/
\r
82 /* When the task starts interrupts should be enabled. */
\r
83 #define portINITIAL_CCR ( ( StackType_t ) 0x00 )
\r
85 /* Hardware specific constants used to generate the RTOS tick from the TPU. */
\r
86 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( uint8_t ) 0x20 )
\r
87 #define portCLOCK_DIV_64 ( ( uint8_t ) 0x03 )
\r
88 #define portCLOCK_DIV ( ( uint32_t ) 64 )
\r
89 #define portTGRA_INTERRUPT_ENABLE ( ( uint8_t ) 0x01 )
\r
90 #define portTIMER_CHANNEL ( ( uint8_t ) 0x02 )
\r
91 #define portMSTP13 ( ( uint16_t ) 0x2000 )
\r
94 * Setup TPU channel one for the RTOS tick at the requested frequency.
\r
96 static void prvSetupTimerInterrupt( void );
\r
99 * The ISR used by portYIELD(). This is installed as a trap handler.
\r
101 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
103 /*-----------------------------------------------------------*/
\r
106 * See header file for description.
\r
108 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
112 /* This requires an even address. */
\r
113 ulValue = ( uint32_t ) pxTopOfStack;
\r
114 if( ulValue & 1UL )
\r
116 pxTopOfStack = pxTopOfStack - 1;
\r
119 /* Place a few bytes of known values on the bottom of the stack.
\r
120 This is just useful for debugging. */
\r
122 *pxTopOfStack = 0xaa;
\r
124 *pxTopOfStack = 0xbb;
\r
126 *pxTopOfStack = 0xcc;
\r
128 *pxTopOfStack = 0xdd;
\r
130 /* The initial stack mimics an interrupt stack. First there is the program
\r
131 counter (24 bits). */
\r
132 ulValue = ( uint32_t ) pxCode;
\r
135 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
138 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
141 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
143 /* Followed by the CCR. */
\r
145 *pxTopOfStack = portINITIAL_CCR;
\r
147 /* Next all the general purpose registers - with the parameters being passed
\r
148 in ER0. The parameter order must match that used by the compiler when the
\r
149 "saveall" function attribute is used. */
\r
153 *pxTopOfStack = 0x66;
\r
155 *pxTopOfStack = 0x66;
\r
157 *pxTopOfStack = 0x66;
\r
159 *pxTopOfStack = 0x66;
\r
162 ulValue = ( uint32_t ) pvParameters;
\r
165 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
168 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
171 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
174 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
178 *pxTopOfStack = 0x11;
\r
180 *pxTopOfStack = 0x11;
\r
182 *pxTopOfStack = 0x11;
\r
184 *pxTopOfStack = 0x11;
\r
188 *pxTopOfStack = 0x22;
\r
190 *pxTopOfStack = 0x22;
\r
192 *pxTopOfStack = 0x22;
\r
194 *pxTopOfStack = 0x22;
\r
198 *pxTopOfStack = 0x33;
\r
200 *pxTopOfStack = 0x33;
\r
202 *pxTopOfStack = 0x33;
\r
204 *pxTopOfStack = 0x33;
\r
208 *pxTopOfStack = 0x44;
\r
210 *pxTopOfStack = 0x44;
\r
212 *pxTopOfStack = 0x44;
\r
214 *pxTopOfStack = 0x44;
\r
218 *pxTopOfStack = 0x55;
\r
220 *pxTopOfStack = 0x55;
\r
222 *pxTopOfStack = 0x55;
\r
224 *pxTopOfStack = 0x55;
\r
226 return pxTopOfStack;
\r
228 /*-----------------------------------------------------------*/
\r
230 BaseType_t xPortStartScheduler( void )
\r
232 extern void * pxCurrentTCB;
\r
234 /* Setup the hardware to generate the tick. */
\r
235 prvSetupTimerInterrupt();
\r
237 /* Restore the context of the first task that is going to run. This
\r
238 mirrors the function epilogue code generated by the compiler when the
\r
239 "saveall" function attribute is used. */
\r
241 "MOV.L @_pxCurrentTCB, ER6 \n\t"
\r
242 "MOV.L @ER6, ER7 \n\t"
\r
243 "LDM.L @SP+, (ER4-ER5) \n\t"
\r
244 "LDM.L @SP+, (ER0-ER3) \n\t"
\r
245 "MOV.L @ER7+, ER6 \n\t"
\r
249 ( void ) pxCurrentTCB;
\r
251 /* Should not get here. */
\r
254 /*-----------------------------------------------------------*/
\r
256 void vPortEndScheduler( void )
\r
258 /* It is unlikely that the h8 port will get stopped. */
\r
260 /*-----------------------------------------------------------*/
\r
263 * Manual context switch. This is a trap handler. The "saveall" function
\r
264 * attribute is used so the context is saved by the compiler prologue. All
\r
265 * we have to do is save the stack pointer.
\r
267 void vPortYield( void )
\r
269 portSAVE_STACK_POINTER();
\r
270 vTaskSwitchContext();
\r
271 portRESTORE_STACK_POINTER();
\r
273 /*-----------------------------------------------------------*/
\r
276 * The interrupt handler installed for the RTOS tick depends on whether the
\r
277 * preemptive or cooperative scheduler is being used.
\r
279 #if( configUSE_PREEMPTION == 1 )
\r
282 * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().
\r
283 * The function prologue saves the context so all we have to do is save
\r
284 * the stack pointer.
\r
286 void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
287 void vTickISR( void )
\r
289 portSAVE_STACK_POINTER();
\r
291 if( xTaskIncrementTick() != pdFALSE )
\r
293 vTaskSwitchContext();
\r
296 /* Clear the interrupt. */
\r
299 portRESTORE_STACK_POINTER();
\r
305 * The cooperative scheduler is being used so all we have to do is
\r
306 * periodically increment the tick. This can just be a normal ISR and
\r
307 * the "saveall" attribute is not required.
\r
309 void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );
\r
310 void vTickISR( void )
\r
312 xTaskIncrementTick();
\r
314 /* Clear the interrupt. */
\r
319 /*-----------------------------------------------------------*/
\r
322 * Setup timer 1 compare match to generate a tick interrupt.
\r
324 static void prvSetupTimerInterrupt( void )
\r
326 const uint32_t ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;
\r
328 /* Turn the module on. */
\r
329 MSTPCR &= ~portMSTP13;
\r
331 /* Configure timer 1. */
\r
332 TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;
\r
334 /* Configure the compare match value for a tick of configTICK_RATE_HZ. */
\r
335 TGR1A = ulCompareMatch;
\r
337 /* Start the timer and enable the interrupt - we can do this here as
\r
338 interrupts are globally disabled when this function is called. */
\r
339 TIER1 |= portTGRA_INTERRUPT_ENABLE;
\r
340 TSTR |= portTIMER_CHANNEL;
\r
342 /*-----------------------------------------------------------*/
\r