2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Scheduler includes. */
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 )
37 /* Initial PSW value allocated to a newly created task.
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)
48 #define portPSW ( 0xc6UL )
50 /* The address of the pxCurrentTCB variable, but don't know or need to know its
53 extern volatile TCB_t * volatile pxCurrentTCB;
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.
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
63 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
65 /*-----------------------------------------------------------*/
68 * Sets up the periodic ISR used for the RTOS tick.
70 extern void vApplicationSetupTimerInterrupt( void );
73 * Starts the scheduler by loading the context of the first Task to run.
74 * (implemented in portasm.s).
76 extern void vPortStartFirstTask( void );
79 * Used to catch tasks that attempt to return from their implementing function.
81 static void prvTaskExitError( void );
83 /*-----------------------------------------------------------*/
86 * Initialise the stack of a task to look exactly as if a call to
87 * portSAVE_CONTEXT had been called.
89 * See the header file portable.h.
91 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
92 TaskFunction_t pxCode,
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. */
101 #if __DATA_MODEL__ == __DATA_MODEL_FAR__
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. */
108 pulLocal = ( uint32_t * ) pxTopOfStack;
109 *pulLocal = ( uint32_t ) pvParameters;
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. */
116 pulLocal = ( uint32_t * ) pxTopOfStack;
117 *pulLocal = ( uint32_t ) prvTaskExitError;
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. */
123 pulLocal = ( uint32_t * ) pxTopOfStack;
124 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
127 /* An initial value for the AX register. */
128 *pxTopOfStack = ( StackType_t ) 0x1111;
131 #else /* if __DATA_MODEL__ == __DATA_MODEL_FAR__ */
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. */
137 pulLocal = ( uint32_t * ) pxTopOfStack;
138 *pulLocal = ( uint32_t ) prvTaskExitError;
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. */
145 /* Task function start address combined with the PSW. */
146 pulLocal = ( uint32_t * ) pxTopOfStack;
147 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
150 /* The parameter is passed in AX. */
151 *pxTopOfStack = ( StackType_t ) pvParameters;
154 #endif /* if __DATA_MODEL__ == __DATA_MODEL_FAR__ */
156 /* An initial value for the HL register. */
157 *pxTopOfStack = ( StackType_t ) 0x2222;
160 /* CS and ES registers. */
161 *pxTopOfStack = ( StackType_t ) 0x0F00;
164 /* The remaining general purpose registers DE and BC */
165 *pxTopOfStack = ( StackType_t ) 0xDEDE;
167 *pxTopOfStack = ( StackType_t ) 0xBCBC;
170 /* Finally the critical section nesting count is set to zero when the task
172 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
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. */
178 /*-----------------------------------------------------------*/
180 static void prvTaskExitError( void )
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 ).
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();
195 /*-----------------------------------------------------------*/
197 BaseType_t xPortStartScheduler( void )
199 /* Setup the hardware to generate the tick. Interrupts are disabled when
200 * this function is called. */
201 vApplicationSetupTimerInterrupt();
203 /* Restore the context of the first task that is going to run. */
204 vPortStartFirstTask();
206 /* Execution should not reach here as the tasks are now running! */
209 /*-----------------------------------------------------------*/
211 void vPortEndScheduler( void )
213 /* It is unlikely that the RL78 port will get stopped. */
215 /*-----------------------------------------------------------*/