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 /* 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, TaskFunction_t pxCode, void *pvParameters )
92 {
93 uint32_t *pulLocal;
94
95 /* With large code and large data sizeof( StackType_t ) == 2, and
96 sizeof( StackType_t * ) == 4. With small code and small data
97 sizeof( StackType_t ) == 2 and sizeof( StackType_t * ) == 2. */
98
99 #if __DATA_MODEL__ == __DATA_MODEL_FAR__
100 {
101 /* Far pointer parameters are passed using the A:DE registers (24-bit).
102 Although they are stored in memory as a 32-bit value. Hence decrement
103 the stack pointer, so 2 bytes are left for the contents of A, before
104 storing the pvParameters value. */
105 pxTopOfStack--;
106 pulLocal = ( uint32_t * ) pxTopOfStack;
107 *pulLocal = ( uint32_t ) pvParameters;
108 pxTopOfStack--;
109
110 /* The return address is a 32-bit value. So decrement the stack pointer
111 in order to make extra room needed to store the correct value. See the
112 comments above the prvTaskExitError() prototype at the top of this file. */
113 pxTopOfStack--;
114 pulLocal = ( uint32_t * ) pxTopOfStack;
115 *pulLocal = ( uint32_t ) prvTaskExitError;
116 pxTopOfStack--;
117
118 /* The task function start address combined with the PSW is also stored
119 as a 32-bit value. So leave a space for the second two bytes. */
120 pxTopOfStack--;
121 pulLocal = ( uint32_t * ) pxTopOfStack;
122 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
123 pxTopOfStack--;
124
125 /* An initial value for the AX register. */
126 *pxTopOfStack = ( StackType_t ) 0x1111;
127 pxTopOfStack--;
128 }
129 #else
130 {
131 /* The return address, leaving space for the first two bytes of the
132 32-bit value. See the comments above the prvTaskExitError() prototype
133 at the top of this file. */
134 pxTopOfStack--;
135 pulLocal = ( uint32_t * ) pxTopOfStack;
136 *pulLocal = ( uint32_t ) prvTaskExitError;
137 pxTopOfStack--;
138
139 /* Task function. Again as it is written as a 32-bit value a space is
140 left on the stack for the second two bytes. */
141 pxTopOfStack--;
142
143 /* Task function start address combined with the PSW. */
144 pulLocal = ( uint32_t * ) pxTopOfStack;
145 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
146 pxTopOfStack--;
147
148 /* The parameter is passed in AX. */
149 *pxTopOfStack = ( StackType_t ) pvParameters;
150 pxTopOfStack--;
151 }
152 #endif
153
154 /* An initial value for the HL register. */
155 *pxTopOfStack = ( StackType_t ) 0x2222;
156 pxTopOfStack--;
157
158 /* CS and ES registers. */
159 *pxTopOfStack = ( StackType_t ) 0x0F00;
160 pxTopOfStack--;
161
162 /* The remaining general purpose registers DE and BC */
163 *pxTopOfStack = ( StackType_t ) 0xDEDE;
164 pxTopOfStack--;
165 *pxTopOfStack = ( StackType_t ) 0xBCBC;
166 pxTopOfStack--;
167
168 /* Finally the critical section nesting count is set to zero when the task
169 first starts. */
170 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
171
172 /* Return a pointer to the top of the stack that has been generated so
173 it can be stored in the task control block for the task. */
174 return pxTopOfStack;
175 }
176 /*-----------------------------------------------------------*/
177
prvTaskExitError(void)178 static void prvTaskExitError( void )
179 {
180 /* A function that implements a task must not exit or attempt to return to
181 its caller as there is nothing to return to. If a task wants to exit it
182 should instead call vTaskDelete( NULL ).
183
184 Artificially force an assert() to be triggered if configASSERT() is
185 defined, then stop here so application writers can catch the error. */
186 configASSERT( usCriticalNesting == ~0U );
187 portDISABLE_INTERRUPTS();
188 for( ;; );
189 }
190 /*-----------------------------------------------------------*/
191
xPortStartScheduler(void)192 BaseType_t xPortStartScheduler( void )
193 {
194 /* Setup the hardware to generate the tick. Interrupts are disabled when
195 this function is called. */
196 vApplicationSetupTimerInterrupt();
197
198 /* Restore the context of the first task that is going to run. */
199 vPortStartFirstTask();
200
201 /* Execution should not reach here as the tasks are now running! */
202 return pdTRUE;
203 }
204 /*-----------------------------------------------------------*/
205
vPortEndScheduler(void)206 void vPortEndScheduler( void )
207 {
208 /* It is unlikely that the RL78 port will get stopped. */
209 }
210 /*-----------------------------------------------------------*/
211