2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /* Scheduler includes. */
\r
66 #include "FreeRTOS.h"
\r
69 /* The critical nesting value is initialised to a non zero value to ensure
\r
70 interrupts don't accidentally become enabled before the scheduler is started. */
\r
71 #define portINITIAL_CRITICAL_NESTING ( ( unsigned short ) 10 )
\r
73 /* Initial PSW value allocated to a newly created task.
\r
75 * ||||||||-------------- Fill byte
\r
76 * |||||||--------------- Carry Flag cleared
\r
77 * |||||----------------- In-service priority Flags set to low level
\r
78 * ||||------------------ Register bank Select 0 Flag cleared
\r
79 * |||------------------- Auxiliary Carry Flag cleared
\r
80 * ||-------------------- Register bank Select 1 Flag cleared
\r
81 * |--------------------- Zero Flag set
\r
82 * ---------------------- Global Interrupt Flag set (enabled)
\r
84 #define portPSW ( 0xc6UL )
\r
86 /* The address of the pxCurrentTCB variable, but don't know or need to know its
\r
88 typedef void tskTCB;
\r
89 extern volatile tskTCB * volatile pxCurrentTCB;
\r
91 /* Each task maintains a count of the critical section nesting depth. Each time
\r
92 a critical section is entered the count is incremented. Each time a critical
\r
93 section is exited the count is decremented - with interrupts only being
\r
94 re-enabled if the count is zero.
\r
96 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
97 not be initialised to zero as that could cause problems during the startup
\r
99 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
101 /*-----------------------------------------------------------*/
\r
104 * Sets up the periodic ISR used for the RTOS tick using the interval timer.
\r
105 * The application writer can define configSETUP_TICK_INTERRUPT() (in
\r
106 * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
\r
107 * in place of prvSetupTimerInterrupt().
\r
109 static void prvSetupTimerInterrupt( void );
\r
110 #ifndef configSETUP_TICK_INTERRUPT
\r
111 /* The user has not provided their own tick interrupt configuration so use
\r
112 the definition in this file (which uses the interval timer). */
\r
113 #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
\r
114 #endif /* configSETUP_TICK_INTERRUPT */
\r
117 * Defined in portasm.s87, this function starts the scheduler by loading the
\r
118 * context of the first task to run.
\r
120 extern void vPortStartFirstTask( void );
\r
122 /*-----------------------------------------------------------*/
\r
125 * Initialise the stack of a task to look exactly as if a call to
\r
126 * portSAVE_CONTEXT had been called.
\r
128 * See the header file portable.h.
\r
130 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
132 unsigned long *pulLocal;
\r
134 #if __DATA_MODEL__ == __DATA_MODEL_FAR__
\r
136 /* Parameters are passed in on the stack, and written using a 32bit value
\r
137 hence a space is left for the second two bytes. */
\r
140 /* Write in the parameter value. */
\r
141 pulLocal = ( unsigned long * ) pxTopOfStack;
\r
142 *pulLocal = ( unsigned long ) pvParameters;
\r
145 /* These values are just spacers. The return address of the function
\r
146 would normally be written here. */
\r
147 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;
\r
149 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;
\r
152 /* The start address / PSW value is also written in as a 32bit value,
\r
153 so leave a space for the second two bytes. */
\r
156 /* Task function start address combined with the PSW. */
\r
157 pulLocal = ( unsigned long * ) pxTopOfStack;
\r
158 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );
\r
161 /* An initial value for the AX register. */
\r
162 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;
\r
167 /* Task function address is written to the stack first. As it is
\r
168 written as a 32bit value a space is left on the stack for the second
\r
172 /* Task function start address combined with the PSW. */
\r
173 pulLocal = ( unsigned long * ) pxTopOfStack;
\r
174 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );
\r
177 /* The parameter is passed in AX. */
\r
178 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
183 /* An initial value for the HL register. */
\r
184 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;
\r
187 /* CS and ES registers. */
\r
188 *pxTopOfStack = ( portSTACK_TYPE ) 0x0F00;
\r
191 /* The remaining general purpose registers DE and BC */
\r
192 *pxTopOfStack = ( portSTACK_TYPE ) 0xDEDE;
\r
194 *pxTopOfStack = ( portSTACK_TYPE ) 0xBCBC;
\r
197 /* Finally the critical section nesting count is set to zero when the task
\r
199 *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;
\r
201 /* Return a pointer to the top of the stack that has been generated so it
\r
202 can be stored in the task control block for the task. */
\r
203 return pxTopOfStack;
\r
205 /*-----------------------------------------------------------*/
\r
207 portBASE_TYPE xPortStartScheduler( void )
\r
209 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
210 this function is called. */
\r
211 configSETUP_TICK_INTERRUPT();
\r
213 /* Restore the context of the first task that is going to run. */
\r
214 vPortStartFirstTask();
\r
216 /* Execution should not reach here as the tasks are now running!
\r
217 prvSetupTimerInterrupt() is called here to prevent the compiler outputting
\r
218 a warning about a statically declared function not being referenced in the
\r
219 case that the application writer has provided their own tick interrupt
\r
220 configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
\r
221 their own routine will be called in place of prvSetupTimerInterrupt()). */
\r
222 prvSetupTimerInterrupt();
\r
225 /*-----------------------------------------------------------*/
\r
227 void vPortEndScheduler( void )
\r
229 /* It is unlikely that the RL78 port will get stopped. */
\r
231 /*-----------------------------------------------------------*/
\r
233 static void prvSetupTimerInterrupt( void )
\r
235 const unsigned short usClockHz = 15000UL; /* Internal clock. */
\r
236 const unsigned short usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;
\r
238 /* Use the internal 15K clock. */
\r
239 OSMC = ( unsigned char ) 0x16;
\r
243 /* Supply the interval timer clock. */
\r
244 RTCEN = ( unsigned char ) 1U;
\r
246 /* Disable INTIT interrupt. */
\r
247 ITMK = ( unsigned char ) 1;
\r
249 /* Disable ITMC operation. */
\r
250 ITMC = ( unsigned char ) 0x0000;
\r
252 /* Clear INIT interrupt. */
\r
253 ITIF = ( unsigned char ) 0;
\r
255 /* Set interval and enable interrupt operation. */
\r
256 ITMC = usCompareMatch | 0x8000U;
\r
258 /* Enable INTIT interrupt. */
\r
259 ITMK = ( unsigned char ) 0;
\r
265 /* Supply the interval timer clock. */
\r
266 TMKAEN = ( unsigned char ) 1U;
\r
268 /* Disable INTIT interrupt. */
\r
269 TMKAMK = ( unsigned char ) 1;
\r
271 /* Disable ITMC operation. */
\r
272 ITMC = ( unsigned char ) 0x0000;
\r
274 /* Clear INIT interrupt. */
\r
275 TMKAIF = ( unsigned char ) 0;
\r
277 /* Set interval and enable interrupt operation. */
\r
278 ITMC = usCompareMatch | 0x8000U;
\r
280 /* Enable INTIT interrupt. */
\r
281 TMKAMK = ( unsigned char ) 0;
\r
285 /*-----------------------------------------------------------*/
\r