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 * 11000110
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 /* 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.
54 *
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
57 * sequence. */
58 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
59
60 /*-----------------------------------------------------------*/
61
62 /*
63 * Sets up the periodic ISR used for the RTOS tick.
64 */
65 __attribute__( ( weak ) ) void vApplicationSetupTimerInterrupt( void );
66
67 /*
68 * Starts the scheduler by loading the context of the first task to run.
69 * (defined in portasm.S).
70 */
71 extern void vPortStartFirstTask( void );
72
73 /*-----------------------------------------------------------*/
74
75 /*
76 * Initialise the stack of a task to look exactly as if a call to
77 * portSAVE_CONTEXT had been called.
78 *
79 * See the header file portable.h.
80 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)81 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
82 TaskFunction_t pxCode,
83 void * pvParameters )
84 {
85 uint32_t * pulLocal;
86
87 /* Stack type and pointers to the stack type are both 2 bytes. */
88
89 /* Parameters are passed in on the stack, and written using a 32bit value
90 * hence a space is left for the second two bytes. */
91 pxTopOfStack--;
92
93 /* Write in the parameter value. */
94 pulLocal = ( uint32_t * ) pxTopOfStack;
95 *pulLocal = ( StackType_t ) pvParameters;
96 pxTopOfStack--;
97
98 /* The return address, leaving space for the first two bytes of the
99 * 32-bit value. */
100 pxTopOfStack--;
101 pulLocal = ( uint32_t * ) pxTopOfStack;
102 *pulLocal = ( uint32_t ) 0;
103 pxTopOfStack--;
104
105 /* The start address / PSW value is also written in as a 32bit value,
106 * so leave a space for the second two bytes. */
107 pxTopOfStack--;
108
109 /* Task function start address combined with the PSW. */
110 pulLocal = ( uint32_t * ) pxTopOfStack;
111 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
112 pxTopOfStack--;
113
114 /* An initial value for the AX register. */
115 *pxTopOfStack = ( StackType_t ) 0x1111;
116 pxTopOfStack--;
117
118 /* An initial value for the HL register. */
119 *pxTopOfStack = ( StackType_t ) 0x2222;
120 pxTopOfStack--;
121
122 /* CS and ES registers. */
123 *pxTopOfStack = ( StackType_t ) 0x0F00;
124 pxTopOfStack--;
125
126 /* The remaining general purpose registers bank 0 (DE and BC) and the other
127 * two register banks...register bank 3 is dedicated for use by interrupts so
128 * is not saved as part of the task context. */
129 pxTopOfStack -= 10;
130
131 /* Finally the critical section nesting count is set to zero when the task
132 * first starts. */
133 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
134
135 /* Return a pointer to the top of the stack that has been generated so it
136 * can be stored in the task control block for the task. */
137 return pxTopOfStack;
138 }
139 /*-----------------------------------------------------------*/
140
xPortStartScheduler(void)141 BaseType_t xPortStartScheduler( void )
142 {
143 /* Setup the hardware to generate the tick. Interrupts are disabled when
144 * this function is called. */
145 vApplicationSetupTimerInterrupt();
146
147 /* Restore the context of the first task that is going to run. */
148 vPortStartFirstTask();
149
150 /* Execution should not reach here as the tasks are now running! */
151 return pdTRUE;
152 }
153 /*-----------------------------------------------------------*/
154
vPortEndScheduler(void)155 void vPortEndScheduler( void )
156 {
157 /* It is unlikely that the RL78 port will get stopped. */
158 }
159 /*-----------------------------------------------------------*/
160
vApplicationSetupTimerInterrupt(void)161 __attribute__( ( weak ) ) void vApplicationSetupTimerInterrupt( void )
162 {
163 const uint16_t usClockHz = 15000UL; /* Internal clock. */
164 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;
165
166 /* Use the internal 15K clock. */
167 OSMC = ( unsigned char ) 0x16;
168
169 #ifdef RTCEN
170 {
171 /* Supply the interval timer clock. */
172 RTCEN = ( unsigned char ) 1U;
173
174 /* Disable INTIT interrupt. */
175 ITMK = ( unsigned char ) 1;
176
177 /* Disable ITMC operation. */
178 ITMC = ( unsigned char ) 0x0000;
179
180 /* Clear INIT interrupt. */
181 ITIF = ( unsigned char ) 0;
182
183 /* Set interval and enable interrupt operation. */
184 ITMC = usCompareMatch | 0x8000U;
185
186 /* Enable INTIT interrupt. */
187 ITMK = ( unsigned char ) 0;
188 }
189 #endif /* ifdef RTCEN */
190
191 #ifdef TMKAEN
192 {
193 /* Supply the interval timer clock. */
194 TMKAEN = ( unsigned char ) 1U;
195
196 /* Disable INTIT interrupt. */
197 TMKAMK = ( unsigned char ) 1;
198
199 /* Disable ITMC operation. */
200 ITMC = ( unsigned char ) 0x0000;
201
202 /* Clear INIT interrupt. */
203 TMKAIF = ( unsigned char ) 0;
204
205 /* Set interval and enable interrupt operation. */
206 ITMC = usCompareMatch | 0x8000U;
207
208 /* Enable INTIT interrupt. */
209 TMKAMK = ( unsigned char ) 0;
210 }
211 #endif /* ifdef TMKAEN */
212 }
213 /*-----------------------------------------------------------*/
214