2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Scheduler includes. */
\r
30 #include "FreeRTOS.h"
\r
34 /*-----------------------------------------------------------
\r
35 * Implementation of functions defined in portable.h for the H8S port.
\r
36 *----------------------------------------------------------*/
\r
39 /*-----------------------------------------------------------*/
\r
41 /* When the task starts interrupts should be enabled. */
\r
42 #define portINITIAL_CCR ( ( StackType_t ) 0x00 )
\r
44 /* Hardware specific constants used to generate the RTOS tick from the TPU. */
\r
45 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( uint8_t ) 0x20 )
\r
46 #define portCLOCK_DIV_64 ( ( uint8_t ) 0x03 )
\r
47 #define portCLOCK_DIV ( ( uint32_t ) 64 )
\r
48 #define portTGRA_INTERRUPT_ENABLE ( ( uint8_t ) 0x01 )
\r
49 #define portTIMER_CHANNEL ( ( uint8_t ) 0x02 )
\r
50 #define portMSTP13 ( ( uint16_t ) 0x2000 )
\r
53 * Setup TPU channel one for the RTOS tick at the requested frequency.
\r
55 static void prvSetupTimerInterrupt( void );
\r
58 * The ISR used by portYIELD(). This is installed as a trap handler.
\r
60 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
62 /*-----------------------------------------------------------*/
\r
65 * See header file for description.
\r
67 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
71 /* This requires an even address. */
\r
72 ulValue = ( uint32_t ) pxTopOfStack;
\r
75 pxTopOfStack = pxTopOfStack - 1;
\r
78 /* Place a few bytes of known values on the bottom of the stack.
\r
79 This is just useful for debugging. */
\r
81 *pxTopOfStack = 0xaa;
\r
83 *pxTopOfStack = 0xbb;
\r
85 *pxTopOfStack = 0xcc;
\r
87 *pxTopOfStack = 0xdd;
\r
89 /* The initial stack mimics an interrupt stack. First there is the program
\r
90 counter (24 bits). */
\r
91 ulValue = ( uint32_t ) pxCode;
\r
94 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
97 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
100 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
102 /* Followed by the CCR. */
\r
104 *pxTopOfStack = portINITIAL_CCR;
\r
106 /* Next all the general purpose registers - with the parameters being passed
\r
107 in ER0. The parameter order must match that used by the compiler when the
\r
108 "saveall" function attribute is used. */
\r
112 *pxTopOfStack = 0x66;
\r
114 *pxTopOfStack = 0x66;
\r
116 *pxTopOfStack = 0x66;
\r
118 *pxTopOfStack = 0x66;
\r
121 ulValue = ( uint32_t ) pvParameters;
\r
124 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
127 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
130 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
133 *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
\r
137 *pxTopOfStack = 0x11;
\r
139 *pxTopOfStack = 0x11;
\r
141 *pxTopOfStack = 0x11;
\r
143 *pxTopOfStack = 0x11;
\r
147 *pxTopOfStack = 0x22;
\r
149 *pxTopOfStack = 0x22;
\r
151 *pxTopOfStack = 0x22;
\r
153 *pxTopOfStack = 0x22;
\r
157 *pxTopOfStack = 0x33;
\r
159 *pxTopOfStack = 0x33;
\r
161 *pxTopOfStack = 0x33;
\r
163 *pxTopOfStack = 0x33;
\r
167 *pxTopOfStack = 0x44;
\r
169 *pxTopOfStack = 0x44;
\r
171 *pxTopOfStack = 0x44;
\r
173 *pxTopOfStack = 0x44;
\r
177 *pxTopOfStack = 0x55;
\r
179 *pxTopOfStack = 0x55;
\r
181 *pxTopOfStack = 0x55;
\r
183 *pxTopOfStack = 0x55;
\r
185 return pxTopOfStack;
\r
187 /*-----------------------------------------------------------*/
\r
189 BaseType_t xPortStartScheduler( void )
\r
191 extern void * pxCurrentTCB;
\r
193 /* Setup the hardware to generate the tick. */
\r
194 prvSetupTimerInterrupt();
\r
196 /* Restore the context of the first task that is going to run. This
\r
197 mirrors the function epilogue code generated by the compiler when the
\r
198 "saveall" function attribute is used. */
\r
200 "MOV.L @_pxCurrentTCB, ER6 \n\t"
\r
201 "MOV.L @ER6, ER7 \n\t"
\r
202 "LDM.L @SP+, (ER4-ER5) \n\t"
\r
203 "LDM.L @SP+, (ER0-ER3) \n\t"
\r
204 "MOV.L @ER7+, ER6 \n\t"
\r
208 ( void ) pxCurrentTCB;
\r
210 /* Should not get here. */
\r
213 /*-----------------------------------------------------------*/
\r
215 void vPortEndScheduler( void )
\r
217 /* It is unlikely that the h8 port will get stopped. */
\r
219 /*-----------------------------------------------------------*/
\r
222 * Manual context switch. This is a trap handler. The "saveall" function
\r
223 * attribute is used so the context is saved by the compiler prologue. All
\r
224 * we have to do is save the stack pointer.
\r
226 void vPortYield( void )
\r
228 portSAVE_STACK_POINTER();
\r
229 vTaskSwitchContext();
\r
230 portRESTORE_STACK_POINTER();
\r
232 /*-----------------------------------------------------------*/
\r
235 * The interrupt handler installed for the RTOS tick depends on whether the
\r
236 * preemptive or cooperative scheduler is being used.
\r
238 #if( configUSE_PREEMPTION == 1 )
\r
241 * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().
\r
242 * The function prologue saves the context so all we have to do is save
\r
243 * the stack pointer.
\r
245 void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
246 void vTickISR( void )
\r
248 portSAVE_STACK_POINTER();
\r
250 if( xTaskIncrementTick() != pdFALSE )
\r
252 vTaskSwitchContext();
\r
255 /* Clear the interrupt. */
\r
258 portRESTORE_STACK_POINTER();
\r
264 * The cooperative scheduler is being used so all we have to do is
\r
265 * periodically increment the tick. This can just be a normal ISR and
\r
266 * the "saveall" attribute is not required.
\r
268 void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );
\r
269 void vTickISR( void )
\r
271 xTaskIncrementTick();
\r
273 /* Clear the interrupt. */
\r
278 /*-----------------------------------------------------------*/
\r
281 * Setup timer 1 compare match to generate a tick interrupt.
\r
283 static void prvSetupTimerInterrupt( void )
\r
285 const uint32_t ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;
\r
287 /* Turn the module on. */
\r
288 MSTPCR &= ~portMSTP13;
\r
290 /* Configure timer 1. */
\r
291 TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;
\r
293 /* Configure the compare match value for a tick of configTICK_RATE_HZ. */
\r
294 TGR1A = ulCompareMatch;
\r
296 /* Start the timer and enable the interrupt - we can do this here as
\r
297 interrupts are globally disabled when this function is called. */
\r
298 TIER1 |= portTGRA_INTERRUPT_ENABLE;
\r
299 TSTR |= portTIMER_CHANNEL;
\r
301 /*-----------------------------------------------------------*/
\r