2 * FreeRTOS Kernel V10.6.2
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 /* Each task maintains a count of the critical section nesting depth. Each time
51 a critical section is entered the count is incremented. Each time a critical
52 section is exited the count is decremented - with interrupts only being
53 re-enabled if the count is zero.
55 usCriticalNesting will get set to zero when the scheduler starts, but must
56 not be initialised to zero as that could cause problems during the startup
58 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
60 /*-----------------------------------------------------------*/
63 * Sets up the periodic ISR used for the RTOS tick.
65 __attribute__((weak)) void vApplicationSetupTimerInterrupt( void );
68 * Starts the scheduler by loading the context of the first task to run.
69 * (defined in portasm.S).
71 extern void vPortStartFirstTask( void );
73 /*-----------------------------------------------------------*/
76 * Initialise the stack of a task to look exactly as if a call to
77 * portSAVE_CONTEXT had been called.
79 * See the header file portable.h.
81 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
85 /* Stack type and pointers to the stack type are both 2 bytes. */
87 /* Parameters are passed in on the stack, and written using a 32bit value
88 hence a space is left for the second two bytes. */
91 /* Write in the parameter value. */
92 pulLocal = ( uint32_t * ) pxTopOfStack;
93 *pulLocal = ( StackType_t ) pvParameters;
96 /* The return address, leaving space for the first two bytes of the
99 pulLocal = ( uint32_t * ) pxTopOfStack;
100 *pulLocal = ( uint32_t ) 0;
103 /* The start address / PSW value is also written in as a 32bit value,
104 so leave a space for the second two bytes. */
107 /* Task function start address combined with the PSW. */
108 pulLocal = ( uint32_t * ) pxTopOfStack;
109 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
112 /* An initial value for the AX register. */
113 *pxTopOfStack = ( StackType_t ) 0x1111;
116 /* An initial value for the HL register. */
117 *pxTopOfStack = ( StackType_t ) 0x2222;
120 /* CS and ES registers. */
121 *pxTopOfStack = ( StackType_t ) 0x0F00;
124 /* The remaining general purpose registers bank 0 (DE and BC) and the other
125 two register banks...register bank 3 is dedicated for use by interrupts so
126 is not saved as part of the task context. */
129 /* Finally the critical section nesting count is set to zero when the task
131 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
133 /* Return a pointer to the top of the stack that has beene generated so it
134 can be stored in the task control block for the task. */
137 /*-----------------------------------------------------------*/
139 portBASE_TYPE xPortStartScheduler( void )
141 /* Setup the hardware to generate the tick. Interrupts are disabled when
142 this function is called. */
143 vApplicationSetupTimerInterrupt();
145 /* Restore the context of the first task that is going to run. */
146 vPortStartFirstTask();
148 /* Execution should not reach here as the tasks are now running! */
151 /*-----------------------------------------------------------*/
153 void vPortEndScheduler( void )
155 /* It is unlikely that the RL78 port will get stopped. */
157 /*-----------------------------------------------------------*/
159 __attribute__((weak)) void vApplicationSetupTimerInterrupt( void )
161 const uint16_t usClockHz = 15000UL; /* Internal clock. */
162 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;
164 /* Use the internal 15K clock. */
165 OSMC = ( unsigned char ) 0x16;
169 /* Supply the interval timer clock. */
170 RTCEN = ( unsigned char ) 1U;
172 /* Disable INTIT interrupt. */
173 ITMK = ( unsigned char ) 1;
175 /* Disable ITMC operation. */
176 ITMC = ( unsigned char ) 0x0000;
178 /* Clear INIT interrupt. */
179 ITIF = ( unsigned char ) 0;
181 /* Set interval and enable interrupt operation. */
182 ITMC = usCompareMatch | 0x8000U;
184 /* Enable INTIT interrupt. */
185 ITMK = ( unsigned char ) 0;
191 /* Supply the interval timer clock. */
192 TMKAEN = ( unsigned char ) 1U;
194 /* Disable INTIT interrupt. */
195 TMKAMK = ( unsigned char ) 1;
197 /* Disable ITMC operation. */
198 ITMC = ( unsigned char ) 0x0000;
200 /* Clear INIT interrupt. */
201 TMKAIF = ( unsigned char ) 0;
203 /* Set interval and enable interrupt operation. */
204 ITMC = usCompareMatch | 0x8000U;
206 /* Enable INTIT interrupt. */
207 TMKAMK = ( unsigned char ) 0;
211 /*-----------------------------------------------------------*/