2 * FreeRTOS Kernel V10.1.0
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Scheduler includes. */
\r
29 #include "FreeRTOS.h"
\r
32 /* The critical nesting value is initialised to a non zero value to ensure
\r
33 interrupts don't accidentally become enabled before the scheduler is started. */
\r
34 #define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
\r
36 /* Initial PSW value allocated to a newly created task.
\r
38 * ||||||||-------------- Fill byte
\r
39 * |||||||--------------- Carry Flag cleared
\r
40 * |||||----------------- In-service priority Flags set to low level
\r
41 * ||||------------------ Register bank Select 0 Flag cleared
\r
42 * |||------------------- Auxiliary Carry Flag cleared
\r
43 * ||-------------------- Register bank Select 1 Flag cleared
\r
44 * |--------------------- Zero Flag set
\r
45 * ---------------------- Global Interrupt Flag set (enabled)
\r
47 #define portPSW ( 0xc6UL )
\r
49 /* The address of the pxCurrentTCB variable, but don't know or need to know its
\r
52 extern volatile TCB_t * volatile pxCurrentTCB;
\r
54 /* Each task maintains a count of the critical section nesting depth. Each time
\r
55 a critical section is entered the count is incremented. Each time a critical
\r
56 section is exited the count is decremented - with interrupts only being
\r
57 re-enabled if the count is zero.
\r
59 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
60 not be initialised to zero as that could cause problems during the startup
\r
62 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
64 /*-----------------------------------------------------------*/
\r
67 * Sets up the periodic ISR used for the RTOS tick using the interval timer.
\r
68 * The application writer can define configSETUP_TICK_INTERRUPT() (in
\r
69 * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
\r
70 * in place of prvSetupTimerInterrupt().
\r
72 static void prvSetupTimerInterrupt( void );
\r
73 #ifndef configSETUP_TICK_INTERRUPT
\r
74 /* The user has not provided their own tick interrupt configuration so use
\r
75 the definition in this file (which uses the interval timer). */
\r
76 #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
\r
77 #endif /* configSETUP_TICK_INTERRUPT */
\r
80 * Defined in portasm.s87, this function starts the scheduler by loading the
\r
81 * context of the first task to run.
\r
83 extern void vPortStartFirstTask( void );
\r
86 * Used to catch tasks that attempt to return from their implementing function.
\r
88 static void prvTaskExitError( void );
\r
90 /*-----------------------------------------------------------*/
\r
93 * Initialise the stack of a task to look exactly as if a call to
\r
94 * portSAVE_CONTEXT had been called.
\r
96 * See the header file portable.h.
\r
98 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
100 uint32_t *pulLocal;
\r
102 /* With large code and large data sizeof( StackType_t ) == 2, and
\r
103 sizeof( StackType_t * ) == 4. With small code and small data
\r
104 sizeof( StackType_t ) == 2 and sizeof( StackType_t * ) == 2. */
\r
106 #if __DATA_MODEL__ == __DATA_MODEL_FAR__
\r
108 /* Parameters are passed in on the stack, and written using a 32-bit value
\r
109 hence a space is left for the second two bytes. */
\r
112 /* Write in the parameter value. */
\r
113 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
114 *pulLocal = ( uint32_t ) pvParameters;
\r
117 /* The return address, leaving space for the first two bytes of the
\r
118 32-bit value. See the comments above the prvTaskExitError() prototype
\r
119 at the top of this file. */
\r
121 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
122 *pulLocal = ( uint32_t ) prvTaskExitError;
\r
125 /* The start address / PSW value is also written in as a 32-bit value,
\r
126 so leave a space for the second two bytes. */
\r
129 /* Task function start address combined with the PSW. */
\r
130 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
131 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
\r
134 /* An initial value for the AX register. */
\r
135 *pxTopOfStack = ( StackType_t ) 0x1111;
\r
140 /* The return address, leaving space for the first two bytes of the
\r
141 32-bit value. See the comments above the prvTaskExitError() prototype
\r
142 at the top of this file. */
\r
144 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
145 *pulLocal = ( uint32_t ) prvTaskExitError;
\r
148 /* Task function. Again as it is written as a 32-bit value a space is
\r
149 left on the stack for the second two bytes. */
\r
152 /* Task function start address combined with the PSW. */
\r
153 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
154 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
\r
157 /* The parameter is passed in AX. */
\r
158 *pxTopOfStack = ( StackType_t ) pvParameters;
\r
163 /* An initial value for the HL register. */
\r
164 *pxTopOfStack = ( StackType_t ) 0x2222;
\r
167 /* CS and ES registers. */
\r
168 *pxTopOfStack = ( StackType_t ) 0x0F00;
\r
171 /* The remaining general purpose registers DE and BC */
\r
172 *pxTopOfStack = ( StackType_t ) 0xDEDE;
\r
174 *pxTopOfStack = ( StackType_t ) 0xBCBC;
\r
177 /* Finally the critical section nesting count is set to zero when the task
\r
179 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
\r
181 /* Return a pointer to the top of the stack that has been generated so it
\r
182 can be stored in the task control block for the task. */
\r
183 return pxTopOfStack;
\r
185 /*-----------------------------------------------------------*/
\r
187 static void prvTaskExitError( void )
\r
189 /* A function that implements a task must not exit or attempt to return to
\r
190 its caller as there is nothing to return to. If a task wants to exit it
\r
191 should instead call vTaskDelete( NULL ).
\r
193 Artificially force an assert() to be triggered if configASSERT() is
\r
194 defined, then stop here so application writers can catch the error. */
\r
195 configASSERT( usCriticalNesting == ~0U );
\r
196 portDISABLE_INTERRUPTS();
\r
199 /*-----------------------------------------------------------*/
\r
201 BaseType_t xPortStartScheduler( void )
\r
203 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
204 this function is called. */
\r
205 configSETUP_TICK_INTERRUPT();
\r
207 /* Restore the context of the first task that is going to run. */
\r
208 vPortStartFirstTask();
\r
210 /* Execution should not reach here as the tasks are now running!
\r
211 prvSetupTimerInterrupt() is called here to prevent the compiler outputting
\r
212 a warning about a statically declared function not being referenced in the
\r
213 case that the application writer has provided their own tick interrupt
\r
214 configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
\r
215 their own routine will be called in place of prvSetupTimerInterrupt()). */
\r
216 prvSetupTimerInterrupt();
\r
219 /*-----------------------------------------------------------*/
\r
221 void vPortEndScheduler( void )
\r
223 /* It is unlikely that the RL78 port will get stopped. */
\r
225 /*-----------------------------------------------------------*/
\r
227 static void prvSetupTimerInterrupt( void )
\r
229 const uint16_t usClockHz = 15000UL; /* Internal clock. */
\r
230 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;
\r
232 /* Use the internal 15K clock. */
\r
233 OSMC = ( uint8_t ) 0x16;
\r
237 /* Supply the interval timer clock. */
\r
238 RTCEN = ( uint8_t ) 1U;
\r
240 /* Disable INTIT interrupt. */
\r
241 ITMK = ( uint8_t ) 1;
\r
243 /* Disable ITMC operation. */
\r
244 ITMC = ( uint8_t ) 0x0000;
\r
246 /* Clear INIT interrupt. */
\r
247 ITIF = ( uint8_t ) 0;
\r
249 /* Set interval and enable interrupt operation. */
\r
250 ITMC = usCompareMatch | 0x8000U;
\r
252 /* Enable INTIT interrupt. */
\r
253 ITMK = ( uint8_t ) 0;
\r
259 /* Supply the interval timer clock. */
\r
260 TMKAEN = ( uint8_t ) 1U;
\r
262 /* Disable INTIT interrupt. */
\r
263 TMKAMK = ( uint8_t ) 1;
\r
265 /* Disable ITMC operation. */
\r
266 ITMC = ( uint8_t ) 0x0000;
\r
268 /* Clear INIT interrupt. */
\r
269 TMKAIF = ( uint8_t ) 0;
\r
271 /* Set interval and enable interrupt operation. */
\r
272 ITMC = usCompareMatch | 0x8000U;
\r
274 /* Enable INTIT interrupt. */
\r
275 TMKAMK = ( uint8_t ) 0;
\r
279 /*-----------------------------------------------------------*/
\r