2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Scheduler includes. */
\r
30 #include "FreeRTOS.h"
\r
33 /* The critical nesting value is initialised to a non zero value to ensure
\r
34 interrupts don't accidentally become enabled before the scheduler is started. */
\r
35 #define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
\r
37 /* Initial PSW value allocated to a newly created task.
\r
39 * ||||||||-------------- Fill byte
\r
40 * |||||||--------------- Carry Flag cleared
\r
41 * |||||----------------- In-service priority Flags set to low level
\r
42 * ||||------------------ Register bank Select 0 Flag cleared
\r
43 * |||------------------- Auxiliary Carry Flag cleared
\r
44 * ||-------------------- Register bank Select 1 Flag cleared
\r
45 * |--------------------- Zero Flag set
\r
46 * ---------------------- Global Interrupt Flag set (enabled)
\r
48 #define portPSW ( 0xc6UL )
\r
50 /* The address of the pxCurrentTCB variable, but don't know or need to know its
\r
53 extern volatile TCB_t * volatile pxCurrentTCB;
\r
55 /* Each task maintains a count of the critical section nesting depth. Each time
\r
56 a critical section is entered the count is incremented. Each time a critical
\r
57 section is exited the count is decremented - with interrupts only being
\r
58 re-enabled if the count is zero.
\r
60 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
61 not be initialised to zero as that could cause problems during the startup
\r
63 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
65 /*-----------------------------------------------------------*/
\r
68 * Sets up the periodic ISR used for the RTOS tick using the interval timer.
\r
69 * The application writer can define configSETUP_TICK_INTERRUPT() (in
\r
70 * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
\r
71 * in place of prvSetupTimerInterrupt().
\r
73 static void prvSetupTimerInterrupt( void );
\r
74 #ifndef configSETUP_TICK_INTERRUPT
\r
75 /* The user has not provided their own tick interrupt configuration so use
\r
76 the definition in this file (which uses the interval timer). */
\r
77 #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
\r
78 #endif /* configSETUP_TICK_INTERRUPT */
\r
81 * Defined in portasm.s87, this function starts the scheduler by loading the
\r
82 * context of the first task to run.
\r
84 extern void vPortStartFirstTask( void );
\r
87 * Used to catch tasks that attempt to return from their implementing function.
\r
89 static void prvTaskExitError( void );
\r
91 /*-----------------------------------------------------------*/
\r
94 * Initialise the stack of a task to look exactly as if a call to
\r
95 * portSAVE_CONTEXT had been called.
\r
97 * See the header file portable.h.
\r
99 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
101 uint32_t *pulLocal;
\r
103 /* With large code and large data sizeof( StackType_t ) == 2, and
\r
104 sizeof( StackType_t * ) == 4. With small code and small data
\r
105 sizeof( StackType_t ) == 2 and sizeof( StackType_t * ) == 2. */
\r
107 #if __DATA_MODEL__ == __DATA_MODEL_FAR__
\r
109 /* Parameters are passed in on the stack, and written using a 32-bit value
\r
110 hence a space is left for the second two bytes. */
\r
113 /* Write in the parameter value. */
\r
114 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
115 *pulLocal = ( uint32_t ) pvParameters;
\r
118 /* The return address, leaving space for the first two bytes of the
\r
119 32-bit value. See the comments above the prvTaskExitError() prototype
\r
120 at the top of this file. */
\r
122 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
123 *pulLocal = ( uint32_t ) prvTaskExitError;
\r
126 /* The start address / PSW value is also written in as a 32-bit value,
\r
127 so leave a space for the second two bytes. */
\r
130 /* Task function start address combined with the PSW. */
\r
131 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
132 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
\r
135 /* An initial value for the AX register. */
\r
136 *pxTopOfStack = ( StackType_t ) 0x1111;
\r
141 /* The return address, leaving space for the first two bytes of the
\r
142 32-bit value. See the comments above the prvTaskExitError() prototype
\r
143 at the top of this file. */
\r
145 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
146 *pulLocal = ( uint32_t ) prvTaskExitError;
\r
149 /* Task function. Again as it is written as a 32-bit value a space is
\r
150 left on the stack for the second two bytes. */
\r
153 /* Task function start address combined with the PSW. */
\r
154 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
155 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
\r
158 /* The parameter is passed in AX. */
\r
159 *pxTopOfStack = ( StackType_t ) pvParameters;
\r
164 /* An initial value for the HL register. */
\r
165 *pxTopOfStack = ( StackType_t ) 0x2222;
\r
168 /* CS and ES registers. */
\r
169 *pxTopOfStack = ( StackType_t ) 0x0F00;
\r
172 /* The remaining general purpose registers DE and BC */
\r
173 *pxTopOfStack = ( StackType_t ) 0xDEDE;
\r
175 *pxTopOfStack = ( StackType_t ) 0xBCBC;
\r
178 /* Finally the critical section nesting count is set to zero when the task
\r
180 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
\r
182 /* Return a pointer to the top of the stack that has been generated so it
\r
183 can be stored in the task control block for the task. */
\r
184 return pxTopOfStack;
\r
186 /*-----------------------------------------------------------*/
\r
188 static void prvTaskExitError( void )
\r
190 /* A function that implements a task must not exit or attempt to return to
\r
191 its caller as there is nothing to return to. If a task wants to exit it
\r
192 should instead call vTaskDelete( NULL ).
\r
194 Artificially force an assert() to be triggered if configASSERT() is
\r
195 defined, then stop here so application writers can catch the error. */
\r
196 configASSERT( usCriticalNesting == ~0U );
\r
197 portDISABLE_INTERRUPTS();
\r
200 /*-----------------------------------------------------------*/
\r
202 BaseType_t xPortStartScheduler( void )
\r
204 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
205 this function is called. */
\r
206 configSETUP_TICK_INTERRUPT();
\r
208 /* Restore the context of the first task that is going to run. */
\r
209 vPortStartFirstTask();
\r
211 /* Execution should not reach here as the tasks are now running!
\r
212 prvSetupTimerInterrupt() is called here to prevent the compiler outputting
\r
213 a warning about a statically declared function not being referenced in the
\r
214 case that the application writer has provided their own tick interrupt
\r
215 configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
\r
216 their own routine will be called in place of prvSetupTimerInterrupt()). */
\r
217 prvSetupTimerInterrupt();
\r
220 /*-----------------------------------------------------------*/
\r
222 void vPortEndScheduler( void )
\r
224 /* It is unlikely that the RL78 port will get stopped. */
\r
226 /*-----------------------------------------------------------*/
\r
228 static void prvSetupTimerInterrupt( void )
\r
230 const uint16_t usClockHz = 15000UL; /* Internal clock. */
\r
231 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;
\r
233 /* Use the internal 15K clock. */
\r
234 OSMC = ( uint8_t ) 0x16;
\r
238 /* Supply the interval timer clock. */
\r
239 RTCEN = ( uint8_t ) 1U;
\r
241 /* Disable INTIT interrupt. */
\r
242 ITMK = ( uint8_t ) 1;
\r
244 /* Disable ITMC operation. */
\r
245 ITMC = ( uint8_t ) 0x0000;
\r
247 /* Clear INIT interrupt. */
\r
248 ITIF = ( uint8_t ) 0;
\r
250 /* Set interval and enable interrupt operation. */
\r
251 ITMC = usCompareMatch | 0x8000U;
\r
253 /* Enable INTIT interrupt. */
\r
254 ITMK = ( uint8_t ) 0;
\r
260 /* Supply the interval timer clock. */
\r
261 TMKAEN = ( uint8_t ) 1U;
\r
263 /* Disable INTIT interrupt. */
\r
264 TMKAMK = ( uint8_t ) 1;
\r
266 /* Disable ITMC operation. */
\r
267 ITMC = ( uint8_t ) 0x0000;
\r
269 /* Clear INIT interrupt. */
\r
270 TMKAIF = ( uint8_t ) 0;
\r
272 /* Set interval and enable interrupt operation. */
\r
273 ITMC = usCompareMatch | 0x8000U;
\r
275 /* Enable INTIT interrupt. */
\r
276 TMKAMK = ( uint8_t ) 0;
\r
280 /*-----------------------------------------------------------*/
\r