| 1 |
572 |
jeremybenn |
/*
|
| 2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
| 3 |
|
|
|
| 4 |
|
|
***************************************************************************
|
| 5 |
|
|
* *
|
| 6 |
|
|
* If you are: *
|
| 7 |
|
|
* *
|
| 8 |
|
|
* + New to FreeRTOS, *
|
| 9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
| 10 |
|
|
* + Looking for basic training, *
|
| 11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
| 12 |
|
|
* *
|
| 13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
| 14 |
|
|
* *
|
| 15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
| 16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
| 17 |
|
|
* *
|
| 18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
| 19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
| 20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
| 21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
| 22 |
|
|
* *
|
| 23 |
|
|
***************************************************************************
|
| 24 |
|
|
|
| 25 |
|
|
This file is part of the FreeRTOS distribution.
|
| 26 |
|
|
|
| 27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
| 28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
| 29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
| 30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
| 31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
| 32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
| 33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
| 34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
| 36 |
|
|
more details. You should have received a copy of the GNU General Public
|
| 37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
| 38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
| 39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
| 40 |
|
|
FreeRTOS WEB site.
|
| 41 |
|
|
|
| 42 |
|
|
1 tab == 4 spaces!
|
| 43 |
|
|
|
| 44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
| 45 |
|
|
contact details.
|
| 46 |
|
|
|
| 47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
| 48 |
|
|
critical systems.
|
| 49 |
|
|
|
| 50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
| 51 |
|
|
licensing and training services.
|
| 52 |
|
|
*/
|
| 53 |
|
|
|
| 54 |
|
|
/* Standard includes. */
|
| 55 |
|
|
#include <stdlib.h>
|
| 56 |
|
|
|
| 57 |
|
|
/* Scheduler includes. */
|
| 58 |
|
|
#include "FreeRTOS.h"
|
| 59 |
|
|
#include "task.h"
|
| 60 |
|
|
|
| 61 |
|
|
/* The critical nesting value is initialised to a non zero value to ensure
|
| 62 |
|
|
interrupts don't accidentally become enabled before the scheduler is started. */
|
| 63 |
|
|
#define portINITIAL_CRITICAL_NESTING (( unsigned short ) 10)
|
| 64 |
|
|
|
| 65 |
|
|
/* Initial PSW value allocated to a newly created task.
|
| 66 |
|
|
* 1100011000000000
|
| 67 |
|
|
* ||||||||-------------- Fill byte
|
| 68 |
|
|
* |||||||--------------- Carry Flag cleared
|
| 69 |
|
|
* |||||----------------- In-service priority Flags set to low level
|
| 70 |
|
|
* ||||------------------ Register bank Select 0 Flag cleared
|
| 71 |
|
|
* |||------------------- Auxiliary Carry Flag cleared
|
| 72 |
|
|
* ||-------------------- Register bank Select 1 Flag cleared
|
| 73 |
|
|
* |--------------------- Zero Flag set
|
| 74 |
|
|
* ---------------------- Global Interrupt Flag set (enabled)
|
| 75 |
|
|
*/
|
| 76 |
|
|
#define portPSW (0xc6UL)
|
| 77 |
|
|
|
| 78 |
|
|
/* We require the address of the pxCurrentTCB variable, but don't want to know
|
| 79 |
|
|
any details of its type. */
|
| 80 |
|
|
typedef void tskTCB;
|
| 81 |
|
|
extern volatile tskTCB * volatile pxCurrentTCB;
|
| 82 |
|
|
|
| 83 |
|
|
/* Most ports implement critical sections by placing the interrupt flags on
|
| 84 |
|
|
the stack before disabling interrupts. Exiting the critical section is then
|
| 85 |
|
|
simply a case of popping the flags from the stack. As 78K0 IAR does not use
|
| 86 |
|
|
a frame pointer this cannot be done as modifying the stack will clobber all
|
| 87 |
|
|
the stack variables. Instead each task maintains a count of the critical
|
| 88 |
|
|
section nesting depth. Each time a critical section is entered the count is
|
| 89 |
|
|
incremented. Each time a critical section is left the count is decremented -
|
| 90 |
|
|
with interrupts only being re-enabled if the count is zero.
|
| 91 |
|
|
|
| 92 |
|
|
usCriticalNesting will get set to zero when the scheduler starts, but must
|
| 93 |
|
|
not be initialised to zero as this will cause problems during the startup
|
| 94 |
|
|
sequence. */
|
| 95 |
|
|
volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;
|
| 96 |
|
|
/*-----------------------------------------------------------*/
|
| 97 |
|
|
|
| 98 |
|
|
/*
|
| 99 |
|
|
* Sets up the periodic ISR used for the RTOS tick.
|
| 100 |
|
|
*/
|
| 101 |
|
|
static void prvSetupTimerInterrupt( void );
|
| 102 |
|
|
/*-----------------------------------------------------------*/
|
| 103 |
|
|
|
| 104 |
|
|
/*
|
| 105 |
|
|
* Initialise the stack of a task to look exactly as if a call to
|
| 106 |
|
|
* portSAVE_CONTEXT had been called.
|
| 107 |
|
|
*
|
| 108 |
|
|
* See the header file portable.h.
|
| 109 |
|
|
*/
|
| 110 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
| 111 |
|
|
{
|
| 112 |
|
|
unsigned long *pulLocal;
|
| 113 |
|
|
|
| 114 |
|
|
#if configMEMORY_MODE == 1
|
| 115 |
|
|
{
|
| 116 |
|
|
/* Parameters are passed in on the stack, and written using a 32bit value
|
| 117 |
|
|
hence a space is left for the second two bytes. */
|
| 118 |
|
|
pxTopOfStack--;
|
| 119 |
|
|
|
| 120 |
|
|
/* Write in the parameter value. */
|
| 121 |
|
|
pulLocal = ( unsigned long * ) pxTopOfStack;
|
| 122 |
|
|
*pulLocal = ( unsigned long ) pvParameters;
|
| 123 |
|
|
pxTopOfStack--;
|
| 124 |
|
|
|
| 125 |
|
|
/* These values are just spacers. The return address of the function
|
| 126 |
|
|
would normally be written here. */
|
| 127 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;
|
| 128 |
|
|
pxTopOfStack--;
|
| 129 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;
|
| 130 |
|
|
pxTopOfStack--;
|
| 131 |
|
|
|
| 132 |
|
|
/* The start address / PSW value is also written in as a 32bit value,
|
| 133 |
|
|
so leave a space for the second two bytes. */
|
| 134 |
|
|
pxTopOfStack--;
|
| 135 |
|
|
|
| 136 |
|
|
/* Task function start address combined with the PSW. */
|
| 137 |
|
|
pulLocal = ( unsigned long * ) pxTopOfStack;
|
| 138 |
|
|
*pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );
|
| 139 |
|
|
pxTopOfStack--;
|
| 140 |
|
|
|
| 141 |
|
|
/* An initial value for the AX register. */
|
| 142 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x1111;
|
| 143 |
|
|
pxTopOfStack--;
|
| 144 |
|
|
}
|
| 145 |
|
|
#else
|
| 146 |
|
|
{
|
| 147 |
|
|
/* Task function address is written to the stack first. As it is
|
| 148 |
|
|
written as a 32bit value a space is left on the stack for the second
|
| 149 |
|
|
two bytes. */
|
| 150 |
|
|
pxTopOfStack--;
|
| 151 |
|
|
|
| 152 |
|
|
/* Task function start address combined with the PSW. */
|
| 153 |
|
|
pulLocal = ( unsigned long * ) pxTopOfStack;
|
| 154 |
|
|
*pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );
|
| 155 |
|
|
pxTopOfStack--;
|
| 156 |
|
|
|
| 157 |
|
|
/* The parameter is passed in AX. */
|
| 158 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
|
| 159 |
|
|
pxTopOfStack--;
|
| 160 |
|
|
}
|
| 161 |
|
|
#endif
|
| 162 |
|
|
|
| 163 |
|
|
/* An initial value for the HL register. */
|
| 164 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x2222;
|
| 165 |
|
|
pxTopOfStack--;
|
| 166 |
|
|
|
| 167 |
|
|
/* CS and ES registers. */
|
| 168 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x0F00;
|
| 169 |
|
|
pxTopOfStack--;
|
| 170 |
|
|
|
| 171 |
|
|
/* Finally the remaining general purpose registers DE and BC */
|
| 172 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xDEDE;
|
| 173 |
|
|
pxTopOfStack--;
|
| 174 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xBCBC;
|
| 175 |
|
|
pxTopOfStack--;
|
| 176 |
|
|
|
| 177 |
|
|
/* Finally the critical section nesting count is set to zero when the task
|
| 178 |
|
|
first starts. */
|
| 179 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;
|
| 180 |
|
|
|
| 181 |
|
|
/* Return a pointer to the top of the stack we have generated so this can
|
| 182 |
|
|
be stored in the task control block for the task. */
|
| 183 |
|
|
return pxTopOfStack;
|
| 184 |
|
|
}
|
| 185 |
|
|
/*-----------------------------------------------------------*/
|
| 186 |
|
|
|
| 187 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
| 188 |
|
|
{
|
| 189 |
|
|
/* Setup the hardware to generate the tick. Interrupts are disabled when
|
| 190 |
|
|
this function is called. */
|
| 191 |
|
|
prvSetupTimerInterrupt();
|
| 192 |
|
|
|
| 193 |
|
|
/* Restore the context of the first task that is going to run. */
|
| 194 |
|
|
vPortStart();
|
| 195 |
|
|
|
| 196 |
|
|
/* Should not get here as the tasks are now running! */
|
| 197 |
|
|
return pdTRUE;
|
| 198 |
|
|
}
|
| 199 |
|
|
/*-----------------------------------------------------------*/
|
| 200 |
|
|
|
| 201 |
|
|
void vPortEndScheduler( void )
|
| 202 |
|
|
{
|
| 203 |
|
|
/* It is unlikely that the 78K0R port will get stopped. If required simply
|
| 204 |
|
|
disable the tick interrupt here. */
|
| 205 |
|
|
}
|
| 206 |
|
|
/*-----------------------------------------------------------*/
|
| 207 |
|
|
|
| 208 |
|
|
static void prvSetupTimerInterrupt( void )
|
| 209 |
|
|
{
|
| 210 |
|
|
/* Setup channel 5 of the TAU to generate the tick interrupt. */
|
| 211 |
|
|
|
| 212 |
|
|
/* First the Timer Array Unit has to be enabled. */
|
| 213 |
|
|
TAU0EN = 1;
|
| 214 |
|
|
|
| 215 |
|
|
/* To configure the Timer Array Unit all Channels have to first be stopped. */
|
| 216 |
|
|
TT0 = 0xff;
|
| 217 |
|
|
|
| 218 |
|
|
/* Interrupt of Timer Array Unit Channel 5 is disabled to set the interrupt
|
| 219 |
|
|
priority. */
|
| 220 |
|
|
TMMK05 = 1;
|
| 221 |
|
|
|
| 222 |
|
|
/* Clear Timer Array Unit Channel 5 interrupt flag. */
|
| 223 |
|
|
TMIF05 = 0;
|
| 224 |
|
|
|
| 225 |
|
|
/* Set Timer Array Unit Channel 5 interrupt priority */
|
| 226 |
|
|
TMPR005 = 0;
|
| 227 |
|
|
TMPR105 = 0;
|
| 228 |
|
|
|
| 229 |
|
|
/* Set Timer Array Unit Channel 5 Mode as interval timer. */
|
| 230 |
|
|
TMR05 = 0x0000;
|
| 231 |
|
|
|
| 232 |
|
|
/* Set the compare match value according to the tick rate we want. */
|
| 233 |
|
|
TDR05 = ( portTickType ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
|
| 234 |
|
|
|
| 235 |
|
|
/* Set Timer Array Unit Channel 5 output mode */
|
| 236 |
|
|
TOM0 &= ~0x0020;
|
| 237 |
|
|
|
| 238 |
|
|
/* Set Timer Array Unit Channel 5 output level */
|
| 239 |
|
|
TOL0 &= ~0x0020;
|
| 240 |
|
|
|
| 241 |
|
|
/* Set Timer Array Unit Channel 5 output enable */
|
| 242 |
|
|
TOE0 &= ~0x0020;
|
| 243 |
|
|
|
| 244 |
|
|
/* Interrupt of Timer Array Unit Channel 5 enabled */
|
| 245 |
|
|
TMMK05 = 0;
|
| 246 |
|
|
|
| 247 |
|
|
/* Start Timer Array Unit Channel 5.*/
|
| 248 |
|
|
TS0 |= 0x0020;
|
| 249 |
|
|
}
|
| 250 |
|
|
/*-----------------------------------------------------------*/
|
| 251 |
|
|
|