2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* Scheduler includes. */
\r
67 #include "FreeRTOS.h"
\r
70 /* The critical nesting value is initialised to a non zero value to ensure
\r
71 interrupts don't accidentally become enabled before the scheduler is started. */
\r
72 #define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
\r
74 /* Initial PSW value allocated to a newly created task.
\r
76 * ||||||||-------------- Fill byte
\r
77 * |||||||--------------- Carry Flag cleared
\r
78 * |||||----------------- In-service priority Flags set to low level
\r
79 * ||||------------------ Register bank Select 0 Flag cleared
\r
80 * |||------------------- Auxiliary Carry Flag cleared
\r
81 * ||-------------------- Register bank Select 1 Flag cleared
\r
82 * |--------------------- Zero Flag set
\r
83 * ---------------------- Global Interrupt Flag set (enabled)
\r
85 #define portPSW ( 0xc6UL )
\r
87 /* The address of the pxCurrentTCB variable, but don't know or need to know its
\r
90 extern volatile TCB_t * volatile pxCurrentTCB;
\r
92 /* Each task maintains a count of the critical section nesting depth. Each time
\r
93 a critical section is entered the count is incremented. Each time a critical
\r
94 section is exited the count is decremented - with interrupts only being
\r
95 re-enabled if the count is zero.
\r
97 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
98 not be initialised to zero as that could cause problems during the startup
\r
100 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
102 /*-----------------------------------------------------------*/
\r
105 * Sets up the periodic ISR used for the RTOS tick using the interval timer.
\r
106 * The application writer can define configSETUP_TICK_INTERRUPT() (in
\r
107 * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
\r
108 * in place of prvSetupTimerInterrupt().
\r
110 static void prvSetupTimerInterrupt( void );
\r
111 #ifndef configSETUP_TICK_INTERRUPT
\r
112 /* The user has not provided their own tick interrupt configuration so use
\r
113 the definition in this file (which uses the interval timer). */
\r
114 #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
\r
115 #endif /* configSETUP_TICK_INTERRUPT */
\r
118 * Defined in portasm.s87, this function starts the scheduler by loading the
\r
119 * context of the first task to run.
\r
121 extern void vPortStartFirstTask( void );
\r
123 /*-----------------------------------------------------------*/
\r
126 * Initialise the stack of a task to look exactly as if a call to
\r
127 * portSAVE_CONTEXT had been called.
\r
129 * See the header file portable.h.
\r
131 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
133 uint32_t *pulLocal;
\r
135 #if __DATA_MODEL__ == __DATA_MODEL_FAR__
\r
137 /* Parameters are passed in on the stack, and written using a 32bit value
\r
138 hence a space is left for the second two bytes. */
\r
141 /* Write in the parameter value. */
\r
142 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
143 *pulLocal = ( uint32_t ) pvParameters;
\r
146 /* These values are just spacers. The return address of the function
\r
147 would normally be written here. */
\r
148 *pxTopOfStack = ( StackType_t ) 0xcdcd;
\r
150 *pxTopOfStack = ( StackType_t ) 0xcdcd;
\r
153 /* The start address / PSW value is also written in as a 32bit value,
\r
154 so leave a space for the second two bytes. */
\r
157 /* Task function start address combined with the PSW. */
\r
158 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
159 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
\r
162 /* An initial value for the AX register. */
\r
163 *pxTopOfStack = ( StackType_t ) 0x1111;
\r
168 /* Task function address is written to the stack first. As it is
\r
169 written as a 32bit value a space is left on the stack for the second
\r
173 /* Task function start address combined with the PSW. */
\r
174 pulLocal = ( uint32_t * ) pxTopOfStack;
\r
175 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
\r
178 /* The parameter is passed in AX. */
\r
179 *pxTopOfStack = ( StackType_t ) pvParameters;
\r
184 /* An initial value for the HL register. */
\r
185 *pxTopOfStack = ( StackType_t ) 0x2222;
\r
188 /* CS and ES registers. */
\r
189 *pxTopOfStack = ( StackType_t ) 0x0F00;
\r
192 /* The remaining general purpose registers DE and BC */
\r
193 *pxTopOfStack = ( StackType_t ) 0xDEDE;
\r
195 *pxTopOfStack = ( StackType_t ) 0xBCBC;
\r
198 /* Finally the critical section nesting count is set to zero when the task
\r
200 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
\r
202 /* Return a pointer to the top of the stack that has been generated so it
\r
203 can be stored in the task control block for the task. */
\r
204 return pxTopOfStack;
\r
206 /*-----------------------------------------------------------*/
\r
208 BaseType_t xPortStartScheduler( void )
\r
210 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
211 this function is called. */
\r
212 configSETUP_TICK_INTERRUPT();
\r
214 /* Restore the context of the first task that is going to run. */
\r
215 vPortStartFirstTask();
\r
217 /* Execution should not reach here as the tasks are now running!
\r
218 prvSetupTimerInterrupt() is called here to prevent the compiler outputting
\r
219 a warning about a statically declared function not being referenced in the
\r
220 case that the application writer has provided their own tick interrupt
\r
221 configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
\r
222 their own routine will be called in place of prvSetupTimerInterrupt()). */
\r
223 prvSetupTimerInterrupt();
\r
226 /*-----------------------------------------------------------*/
\r
228 void vPortEndScheduler( void )
\r
230 /* It is unlikely that the RL78 port will get stopped. */
\r
232 /*-----------------------------------------------------------*/
\r
234 static void prvSetupTimerInterrupt( void )
\r
236 const uint16_t usClockHz = 15000UL; /* Internal clock. */
\r
237 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;
\r
239 /* Use the internal 15K clock. */
\r
240 OSMC = ( uint8_t ) 0x16;
\r
244 /* Supply the interval timer clock. */
\r
245 RTCEN = ( uint8_t ) 1U;
\r
247 /* Disable INTIT interrupt. */
\r
248 ITMK = ( uint8_t ) 1;
\r
250 /* Disable ITMC operation. */
\r
251 ITMC = ( uint8_t ) 0x0000;
\r
253 /* Clear INIT interrupt. */
\r
254 ITIF = ( uint8_t ) 0;
\r
256 /* Set interval and enable interrupt operation. */
\r
257 ITMC = usCompareMatch | 0x8000U;
\r
259 /* Enable INTIT interrupt. */
\r
260 ITMK = ( uint8_t ) 0;
\r
266 /* Supply the interval timer clock. */
\r
267 TMKAEN = ( uint8_t ) 1U;
\r
269 /* Disable INTIT interrupt. */
\r
270 TMKAMK = ( uint8_t ) 1;
\r
272 /* Disable ITMC operation. */
\r
273 ITMC = ( uint8_t ) 0x0000;
\r
275 /* Clear INIT interrupt. */
\r
276 TMKAIF = ( uint8_t ) 0;
\r
278 /* Set interval and enable interrupt operation. */
\r
279 ITMC = usCompareMatch | 0x8000U;
\r
281 /* Enable INTIT interrupt. */
\r
282 TMKAMK = ( uint8_t ) 0;
\r
286 /*-----------------------------------------------------------*/
\r