1 /*
2 * FreeRTOS Kernel V10.6.2
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the SH2A port.
31 *----------------------------------------------------------*/
32
33 /* Scheduler includes. */
34 #include "FreeRTOS.h"
35 #include "task.h"
36
37 /* Library includes. */
38 #include "string.h"
39
40 /* Hardware specifics. */
41 #include <machine.h>
42
43 /*-----------------------------------------------------------*/
44
45 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
46 PSW is set with U and I set, and PM and IPL clear. */
47 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
48 #define portINITIAL_FPSW ( ( StackType_t ) 0x00000100 )
49
50 /*-----------------------------------------------------------*/
51
52 /*
53 * Function to start the first task executing - written in asm code as direct
54 * access to registers is required.
55 */
56 extern void prvStartFirstTask( void );
57
58 /*
59 * The tick ISR handler. The peripheral used is configured by the application
60 * via a hook/callback function.
61 */
62 __interrupt void vTickISR( void );
63
64 /*-----------------------------------------------------------*/
65
66 extern void *pxCurrentTCB;
67
68 /*-----------------------------------------------------------*/
69
70 /*
71 * See header file for description.
72 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)73 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
74 {
75 /* R0 is not included as it is the stack pointer. */
76
77 *pxTopOfStack = 0x00;
78 pxTopOfStack--;
79 *pxTopOfStack = portINITIAL_PSW;
80 pxTopOfStack--;
81 *pxTopOfStack = ( StackType_t ) pxCode;
82
83 /* When debugging it can be useful if every register is set to a known
84 value. Otherwise code space can be saved by just setting the registers
85 that need to be set. */
86 #ifdef USE_FULL_REGISTER_INITIALISATION
87 {
88 pxTopOfStack--;
89 *pxTopOfStack = 0xffffffff; /* r15. */
90 pxTopOfStack--;
91 *pxTopOfStack = 0xeeeeeeee;
92 pxTopOfStack--;
93 *pxTopOfStack = 0xdddddddd;
94 pxTopOfStack--;
95 *pxTopOfStack = 0xcccccccc;
96 pxTopOfStack--;
97 *pxTopOfStack = 0xbbbbbbbb;
98 pxTopOfStack--;
99 *pxTopOfStack = 0xaaaaaaaa;
100 pxTopOfStack--;
101 *pxTopOfStack = 0x99999999;
102 pxTopOfStack--;
103 *pxTopOfStack = 0x88888888;
104 pxTopOfStack--;
105 *pxTopOfStack = 0x77777777;
106 pxTopOfStack--;
107 *pxTopOfStack = 0x66666666;
108 pxTopOfStack--;
109 *pxTopOfStack = 0x55555555;
110 pxTopOfStack--;
111 *pxTopOfStack = 0x44444444;
112 pxTopOfStack--;
113 *pxTopOfStack = 0x33333333;
114 pxTopOfStack--;
115 *pxTopOfStack = 0x22222222;
116 pxTopOfStack--;
117 }
118 #else
119 {
120 pxTopOfStack -= 15;
121 }
122 #endif
123
124 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
125 pxTopOfStack--;
126 *pxTopOfStack = portINITIAL_FPSW;
127 pxTopOfStack--;
128 *pxTopOfStack = 0x11111111; /* Accumulator 0. */
129 pxTopOfStack--;
130 *pxTopOfStack = 0x22222222; /* Accumulator 0. */
131 pxTopOfStack--;
132 *pxTopOfStack = 0x33333333; /* Accumulator 0. */
133 pxTopOfStack--;
134 *pxTopOfStack = 0x44444444; /* Accumulator 1. */
135 pxTopOfStack--;
136 *pxTopOfStack = 0x55555555; /* Accumulator 1. */
137 pxTopOfStack--;
138 *pxTopOfStack = 0x66666666; /* Accumulator 1. */
139
140 return pxTopOfStack;
141 }
142 /*-----------------------------------------------------------*/
143
xPortStartScheduler(void)144 BaseType_t xPortStartScheduler( void )
145 {
146 extern void vApplicationSetupTimerInterrupt( void );
147
148 /* Use pxCurrentTCB just so it does not get optimised away. */
149 if( pxCurrentTCB != NULL )
150 {
151 /* Call an application function to set up the timer that will generate the
152 tick interrupt. This way the application can decide which peripheral to
153 use. A demo application is provided to show a suitable example. */
154 vApplicationSetupTimerInterrupt();
155
156 /* Enable the software interrupt. */
157 _IEN( _ICU_SWINT ) = 1;
158
159 /* Ensure the software interrupt is clear. */
160 _IR( _ICU_SWINT ) = 0;
161
162 /* Ensure the software interrupt is set to the kernel priority. */
163 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
164
165 /* Start the first task. */
166 prvStartFirstTask();
167 }
168
169 /* Should not get here. */
170 return pdFAIL;
171 }
172 /*-----------------------------------------------------------*/
173
174 #pragma vector = configTICK_VECTOR
vTickISR(void)175 __interrupt void vTickISR( void )
176 {
177 /* Re-enable interrupts. */
178 __enable_interrupt();
179
180 /* Increment the tick, and perform any processing the new tick value
181 necessitates. */
182 __set_interrupt_level( configMAX_SYSCALL_INTERRUPT_PRIORITY );
183 {
184 if( xTaskIncrementTick() != pdFALSE )
185 {
186 taskYIELD();
187 }
188 }
189 __set_interrupt_level( configKERNEL_INTERRUPT_PRIORITY );
190 }
191 /*-----------------------------------------------------------*/
192
vPortEndScheduler(void)193 void vPortEndScheduler( void )
194 {
195 /* Not implemented in ports where there is nothing to return to.
196 Artificially force an assert. */
197 configASSERT( pxCurrentTCB == NULL );
198 }
199 /*-----------------------------------------------------------*/
200