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 /* Scheduler includes. */
30 #include "FreeRTOS.h"
31 #include "task.h"
32
33 /* The critical nesting value is initialised to a non zero value to ensure
34 * interrupts don't accidentally become enabled before the scheduler is started. */
35 #define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
36
37 /* Initial PSW value allocated to a newly created task.
38 * 1100011000000000
39 * ||||||||-------------- Fill byte
40 * |||||||--------------- Carry Flag cleared
41 * |||||----------------- In-service priority Flags set to low level
42 * ||||------------------ Register bank Select 0 Flag cleared
43 * |||------------------- Auxiliary Carry Flag cleared
44 * ||-------------------- Register bank Select 1 Flag cleared
45 * |--------------------- Zero Flag set
46 * ---------------------- Global Interrupt Flag set (enabled)
47 */
48 #define portPSW ( 0xc6UL )
49
50 /* The address of the pxCurrentTCB variable, but don't know or need to know its
51 * type. */
52 typedef void TCB_t;
53 extern volatile TCB_t * volatile pxCurrentTCB;
54
55 /* Each task maintains a count of the critical section nesting depth. Each time
56 * a critical section is entered the count is incremented. Each time a critical
57 * section is exited the count is decremented - with interrupts only being
58 * re-enabled if the count is zero.
59 *
60 * usCriticalNesting will get set to zero when the scheduler starts, but must
61 * not be initialised to zero as that could cause problems during the startup
62 * sequence. */
63 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
64
65 /*-----------------------------------------------------------*/
66
67 /*
68 * Sets up the periodic ISR used for the RTOS tick.
69 */
70 extern void vApplicationSetupTimerInterrupt( void );
71
72 /*
73 * Starts the scheduler by loading the context of the first Task to run.
74 * (implemented in portasm.s).
75 */
76 extern void vPortStartFirstTask( void );
77
78 /*
79 * Used to catch tasks that attempt to return from their implementing function.
80 */
81 static void prvTaskExitError( void );
82
83 /*-----------------------------------------------------------*/
84
85 /*
86 * Initialise the stack of a task to look exactly as if a call to
87 * portSAVE_CONTEXT had been called.
88 *
89 * See the header file portable.h.
90 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)91 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
92 TaskFunction_t pxCode,
93 void * pvParameters )
94 {
95 uint32_t * pulLocal;
96
97 /* With large code and large data sizeof( StackType_t ) == 2, and
98 * sizeof( StackType_t * ) == 4. With small code and small data
99 * sizeof( StackType_t ) == 2 and sizeof( StackType_t * ) == 2. */
100
101 #if __DATA_MODEL__ == __DATA_MODEL_FAR__
102 {
103 /* Far pointer parameters are passed using the A:DE registers (24-bit).
104 * Although they are stored in memory as a 32-bit value. Hence decrement
105 * the stack pointer, so 2 bytes are left for the contents of A, before
106 * storing the pvParameters value. */
107 pxTopOfStack--;
108 pulLocal = ( uint32_t * ) pxTopOfStack;
109 *pulLocal = ( uint32_t ) pvParameters;
110 pxTopOfStack--;
111
112 /* The return address is a 32-bit value. So decrement the stack pointer
113 * in order to make extra room needed to store the correct value. See the
114 * comments above the prvTaskExitError() prototype at the top of this file. */
115 pxTopOfStack--;
116 pulLocal = ( uint32_t * ) pxTopOfStack;
117 *pulLocal = ( uint32_t ) prvTaskExitError;
118 pxTopOfStack--;
119
120 /* The task function start address combined with the PSW is also stored
121 * as a 32-bit value. So leave a space for the second two bytes. */
122 pxTopOfStack--;
123 pulLocal = ( uint32_t * ) pxTopOfStack;
124 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
125 pxTopOfStack--;
126
127 /* An initial value for the AX register. */
128 *pxTopOfStack = ( StackType_t ) 0x1111;
129 pxTopOfStack--;
130 }
131 #else /* if __DATA_MODEL__ == __DATA_MODEL_FAR__ */
132 {
133 /* The return address, leaving space for the first two bytes of the
134 * 32-bit value. See the comments above the prvTaskExitError() prototype
135 * at the top of this file. */
136 pxTopOfStack--;
137 pulLocal = ( uint32_t * ) pxTopOfStack;
138 *pulLocal = ( uint32_t ) prvTaskExitError;
139 pxTopOfStack--;
140
141 /* Task function. Again as it is written as a 32-bit value a space is
142 * left on the stack for the second two bytes. */
143 pxTopOfStack--;
144
145 /* Task function start address combined with the PSW. */
146 pulLocal = ( uint32_t * ) pxTopOfStack;
147 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
148 pxTopOfStack--;
149
150 /* The parameter is passed in AX. */
151 *pxTopOfStack = ( StackType_t ) pvParameters;
152 pxTopOfStack--;
153 }
154 #endif /* if __DATA_MODEL__ == __DATA_MODEL_FAR__ */
155
156 /* An initial value for the HL register. */
157 *pxTopOfStack = ( StackType_t ) 0x2222;
158 pxTopOfStack--;
159
160 /* CS and ES registers. */
161 *pxTopOfStack = ( StackType_t ) 0x0F00;
162 pxTopOfStack--;
163
164 /* The remaining general purpose registers DE and BC */
165 *pxTopOfStack = ( StackType_t ) 0xDEDE;
166 pxTopOfStack--;
167 *pxTopOfStack = ( StackType_t ) 0xBCBC;
168 pxTopOfStack--;
169
170 /* Finally the critical section nesting count is set to zero when the task
171 * first starts. */
172 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
173
174 /* Return a pointer to the top of the stack that has been generated so
175 * it can be stored in the task control block for the task. */
176 return pxTopOfStack;
177 }
178 /*-----------------------------------------------------------*/
179
prvTaskExitError(void)180 static void prvTaskExitError( void )
181 {
182 /* A function that implements a task must not exit or attempt to return to
183 * its caller as there is nothing to return to. If a task wants to exit it
184 * should instead call vTaskDelete( NULL ).
185 *
186 * Artificially force an assert() to be triggered if configASSERT() is
187 * defined, then stop here so application writers can catch the error. */
188 configASSERT( usCriticalNesting == ~0U );
189 portDISABLE_INTERRUPTS();
190
191 for( ; ; )
192 {
193 }
194 }
195 /*-----------------------------------------------------------*/
196
xPortStartScheduler(void)197 BaseType_t xPortStartScheduler( void )
198 {
199 /* Setup the hardware to generate the tick. Interrupts are disabled when
200 * this function is called. */
201 vApplicationSetupTimerInterrupt();
202
203 /* Restore the context of the first task that is going to run. */
204 vPortStartFirstTask();
205
206 /* Execution should not reach here as the tasks are now running! */
207 return pdTRUE;
208 }
209 /*-----------------------------------------------------------*/
210
vPortEndScheduler(void)211 void vPortEndScheduler( void )
212 {
213 /* It is unlikely that the RL78 port will get stopped. */
214 }
215 /*-----------------------------------------------------------*/
216