1 /*
2  * FreeRTOS Kernel V11.1.0
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,
74                                      TaskFunction_t pxCode,
75                                      void * pvParameters )
76 {
77     /* R0 is not included as it is the stack pointer. */
78 
79     *pxTopOfStack = 0x00;
80     pxTopOfStack--;
81     *pxTopOfStack = portINITIAL_PSW;
82     pxTopOfStack--;
83     *pxTopOfStack = ( StackType_t ) pxCode;
84 
85     /* When debugging it can be useful if every register is set to a known
86      * value.  Otherwise code space can be saved by just setting the registers
87      * that need to be set. */
88     #ifdef USE_FULL_REGISTER_INITIALISATION
89     {
90         pxTopOfStack--;
91         *pxTopOfStack = 0xffffffff; /* r15. */
92         pxTopOfStack--;
93         *pxTopOfStack = 0xeeeeeeee;
94         pxTopOfStack--;
95         *pxTopOfStack = 0xdddddddd;
96         pxTopOfStack--;
97         *pxTopOfStack = 0xcccccccc;
98         pxTopOfStack--;
99         *pxTopOfStack = 0xbbbbbbbb;
100         pxTopOfStack--;
101         *pxTopOfStack = 0xaaaaaaaa;
102         pxTopOfStack--;
103         *pxTopOfStack = 0x99999999;
104         pxTopOfStack--;
105         *pxTopOfStack = 0x88888888;
106         pxTopOfStack--;
107         *pxTopOfStack = 0x77777777;
108         pxTopOfStack--;
109         *pxTopOfStack = 0x66666666;
110         pxTopOfStack--;
111         *pxTopOfStack = 0x55555555;
112         pxTopOfStack--;
113         *pxTopOfStack = 0x44444444;
114         pxTopOfStack--;
115         *pxTopOfStack = 0x33333333;
116         pxTopOfStack--;
117         *pxTopOfStack = 0x22222222;
118         pxTopOfStack--;
119     }
120     #else /* ifdef USE_FULL_REGISTER_INITIALISATION */
121     {
122         pxTopOfStack -= 15;
123     }
124     #endif /* ifdef USE_FULL_REGISTER_INITIALISATION */
125 
126     *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
127     pxTopOfStack--;
128     *pxTopOfStack = portINITIAL_FPSW;
129     pxTopOfStack--;
130     *pxTopOfStack = 0x11111111; /* Accumulator 0. */
131     pxTopOfStack--;
132     *pxTopOfStack = 0x22222222; /* Accumulator 0. */
133     pxTopOfStack--;
134     *pxTopOfStack = 0x33333333; /* Accumulator 0. */
135     pxTopOfStack--;
136     *pxTopOfStack = 0x44444444; /* Accumulator 1. */
137     pxTopOfStack--;
138     *pxTopOfStack = 0x55555555; /* Accumulator 1. */
139     pxTopOfStack--;
140     *pxTopOfStack = 0x66666666; /* Accumulator 1. */
141 
142     return pxTopOfStack;
143 }
144 /*-----------------------------------------------------------*/
145 
xPortStartScheduler(void)146 BaseType_t xPortStartScheduler( void )
147 {
148     extern void vApplicationSetupTimerInterrupt( void );
149 
150     /* Use pxCurrentTCB just so it does not get optimised away. */
151     if( pxCurrentTCB != NULL )
152     {
153         /* Call an application function to set up the timer that will generate the
154          * tick interrupt.  This way the application can decide which peripheral to
155          * use.  A demo application is provided to show a suitable example. */
156         vApplicationSetupTimerInterrupt();
157 
158         /* Enable the software interrupt. */
159         _IEN( _ICU_SWINT ) = 1;
160 
161         /* Ensure the software interrupt is clear. */
162         _IR( _ICU_SWINT ) = 0;
163 
164         /* Ensure the software interrupt is set to the kernel priority. */
165         _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
166 
167         /* Start the first task. */
168         prvStartFirstTask();
169     }
170 
171     /* Should not get here. */
172     return pdFAIL;
173 }
174 /*-----------------------------------------------------------*/
175 
176 #pragma vector = configTICK_VECTOR
vTickISR(void)177 __interrupt void vTickISR( void )
178 {
179     /* Re-enable interrupts. */
180     __enable_interrupt();
181 
182     /* Increment the tick, and perform any processing the new tick value
183      * necessitates. */
184     __set_interrupt_level( configMAX_SYSCALL_INTERRUPT_PRIORITY );
185     {
186         if( xTaskIncrementTick() != pdFALSE )
187         {
188             taskYIELD();
189         }
190     }
191     __set_interrupt_level( configKERNEL_INTERRUPT_PRIORITY );
192 }
193 /*-----------------------------------------------------------*/
194 
vPortEndScheduler(void)195 void vPortEndScheduler( void )
196 {
197     /* Not implemented in ports where there is nothing to return to.
198      * Artificially force an assert. */
199     configASSERT( pxCurrentTCB == NULL );
200 }
201 /*-----------------------------------------------------------*/
202