| 1 |
584 |
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 |
|
|
/**
|
| 55 |
|
|
* Defines the tasks and co-routines used to toggle the segments of the two
|
| 56 |
|
|
* seven segment displays, as described at the top of main.c
|
| 57 |
|
|
*/
|
| 58 |
|
|
|
| 59 |
|
|
|
| 60 |
|
|
#include <stdlib.h>
|
| 61 |
|
|
|
| 62 |
|
|
/* Scheduler include files. */
|
| 63 |
|
|
#include "FreeRTOS.h"
|
| 64 |
|
|
#include "task.h"
|
| 65 |
|
|
#include "croutine.h"
|
| 66 |
|
|
|
| 67 |
|
|
/* Demo program include files. */
|
| 68 |
|
|
#include "partest.h"
|
| 69 |
|
|
|
| 70 |
|
|
/*-----------------------------------------------------------*/
|
| 71 |
|
|
|
| 72 |
|
|
/* One task per segment of the left side display. */
|
| 73 |
|
|
#define ledNUM_OF_LED_TASKS ( 7 )
|
| 74 |
|
|
|
| 75 |
|
|
/* Each task toggles at a frequency that is a multiple of 333ms. */
|
| 76 |
|
|
#define ledFLASH_RATE_BASE ( ( portTickType ) 333 )
|
| 77 |
|
|
|
| 78 |
|
|
/* One co-routine per segment of the right hand display. */
|
| 79 |
|
|
#define ledNUM_OF_LED_CO_ROUTINES 7
|
| 80 |
|
|
|
| 81 |
|
|
/* All co-routines run at the same priority. */
|
| 82 |
|
|
#define ledCO_ROUTINE_PRIORITY 0
|
| 83 |
|
|
|
| 84 |
|
|
/*-----------------------------------------------------------*/
|
| 85 |
|
|
|
| 86 |
|
|
/* The task that is created 7 times. */
|
| 87 |
|
|
static void vLEDFlashTask( void *pvParameters );
|
| 88 |
|
|
|
| 89 |
|
|
/* The co-routine that is created 7 times. */
|
| 90 |
|
|
static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );
|
| 91 |
|
|
|
| 92 |
|
|
/* This task is created once, but itself creates 7 co-routines. */
|
| 93 |
|
|
static void vLEDCoRoutineControlTask( void *pvParameters );
|
| 94 |
|
|
|
| 95 |
|
|
/* Handles to each of the 7 tasks. Used so the tasks can be suspended
|
| 96 |
|
|
and resumed. */
|
| 97 |
|
|
static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
|
| 98 |
|
|
|
| 99 |
|
|
/* Handle to the task in which the co-routines run. Used so the
|
| 100 |
|
|
co-routines can be suspended and resumed. */
|
| 101 |
|
|
static xTaskHandle xCoroutineTask;
|
| 102 |
|
|
|
| 103 |
|
|
/*-----------------------------------------------------------*/
|
| 104 |
|
|
|
| 105 |
|
|
/**
|
| 106 |
|
|
* Creates the tasks and co-routines used to toggle the segments of the two
|
| 107 |
|
|
* seven segment displays, as described at the top of main.c
|
| 108 |
|
|
*/
|
| 109 |
|
|
void vCreateFlashTasksAndCoRoutines( void )
|
| 110 |
|
|
{
|
| 111 |
|
|
signed short sLEDTask;
|
| 112 |
|
|
|
| 113 |
|
|
/* Create the tasks that flash segments on the first LED. */
|
| 114 |
|
|
for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
|
| 115 |
|
|
{
|
| 116 |
|
|
/* Spawn the task. */
|
| 117 |
|
|
xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
|
| 118 |
|
|
}
|
| 119 |
|
|
|
| 120 |
|
|
/* Create the task in which the co-routines run. The co-routines themselves
|
| 121 |
|
|
are created within the task. */
|
| 122 |
|
|
xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
|
| 123 |
|
|
}
|
| 124 |
|
|
/*-----------------------------------------------------------*/
|
| 125 |
|
|
|
| 126 |
|
|
void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
|
| 127 |
|
|
{
|
| 128 |
|
|
short sLEDTask;
|
| 129 |
|
|
|
| 130 |
|
|
if( ucIndex == configLEFT_DISPLAY )
|
| 131 |
|
|
{
|
| 132 |
|
|
/* Suspend or resume the tasks that are toggling the segments of the
|
| 133 |
|
|
left side display. */
|
| 134 |
|
|
for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
|
| 135 |
|
|
{
|
| 136 |
|
|
if( xFlashTaskHandles[ sLEDTask ] != NULL )
|
| 137 |
|
|
{
|
| 138 |
|
|
if( sSuspendTasks == pdTRUE )
|
| 139 |
|
|
{
|
| 140 |
|
|
vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
|
| 141 |
|
|
}
|
| 142 |
|
|
else
|
| 143 |
|
|
{
|
| 144 |
|
|
vTaskResume( xFlashTaskHandles[ sLEDTask ] );
|
| 145 |
|
|
}
|
| 146 |
|
|
}
|
| 147 |
|
|
}
|
| 148 |
|
|
}
|
| 149 |
|
|
else
|
| 150 |
|
|
{
|
| 151 |
|
|
/* Suspend or resume the task in which the co-routines are running. The
|
| 152 |
|
|
co-routines toggle the segments of the right side display. */
|
| 153 |
|
|
if( sSuspendTasks == pdTRUE )
|
| 154 |
|
|
{
|
| 155 |
|
|
vTaskSuspend( xCoroutineTask );
|
| 156 |
|
|
}
|
| 157 |
|
|
else
|
| 158 |
|
|
{
|
| 159 |
|
|
vTaskResume( xCoroutineTask );
|
| 160 |
|
|
}
|
| 161 |
|
|
}
|
| 162 |
|
|
}
|
| 163 |
|
|
/*-----------------------------------------------------------*/
|
| 164 |
|
|
|
| 165 |
|
|
static void vLEDFlashTask( void * pvParameters )
|
| 166 |
|
|
{
|
| 167 |
|
|
portTickType xFlashRate, xLastFlashTime;
|
| 168 |
|
|
unsigned short usLED;
|
| 169 |
|
|
|
| 170 |
|
|
/* The LED to flash is passed in as the task parameter. */
|
| 171 |
|
|
usLED = ( unsigned short ) pvParameters;
|
| 172 |
|
|
|
| 173 |
|
|
/* Calculate the rate at which this task is going to toggle its LED. */
|
| 174 |
|
|
xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );
|
| 175 |
|
|
xFlashRate /= portTICK_RATE_MS;
|
| 176 |
|
|
|
| 177 |
|
|
/* We will turn the LED on and off again in the delay period, so each
|
| 178 |
|
|
delay is only half the total period. */
|
| 179 |
|
|
xFlashRate /= ( portTickType ) 2;
|
| 180 |
|
|
|
| 181 |
|
|
/* We need to initialise xLastFlashTime prior to the first call to
|
| 182 |
|
|
vTaskDelayUntil(). */
|
| 183 |
|
|
xLastFlashTime = xTaskGetTickCount();
|
| 184 |
|
|
|
| 185 |
|
|
for(;;)
|
| 186 |
|
|
{
|
| 187 |
|
|
/* Delay for half the flash period then turn the LED on. */
|
| 188 |
|
|
vTaskDelayUntil( &xLastFlashTime, xFlashRate );
|
| 189 |
|
|
vParTestToggleLED( usLED );
|
| 190 |
|
|
|
| 191 |
|
|
/* Delay for half the flash period then turn the LED off. */
|
| 192 |
|
|
vTaskDelayUntil( &xLastFlashTime, xFlashRate );
|
| 193 |
|
|
vParTestToggleLED( usLED );
|
| 194 |
|
|
}
|
| 195 |
|
|
}
|
| 196 |
|
|
/*-----------------------------------------------------------*/
|
| 197 |
|
|
|
| 198 |
|
|
static void vLEDCoRoutineControlTask( void *pvParameters )
|
| 199 |
|
|
{
|
| 200 |
|
|
unsigned short usCoroutine;
|
| 201 |
|
|
|
| 202 |
|
|
( void ) pvParameters;
|
| 203 |
|
|
|
| 204 |
|
|
/* Create the co-routines - one of each segment of the right side display. */
|
| 205 |
|
|
for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
|
| 206 |
|
|
{
|
| 207 |
|
|
xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
|
| 208 |
|
|
}
|
| 209 |
|
|
|
| 210 |
|
|
/* This task has nothing else to do except scheduler the co-routines it just
|
| 211 |
|
|
created. */
|
| 212 |
|
|
for( ;; )
|
| 213 |
|
|
{
|
| 214 |
|
|
vCoRoutineSchedule();
|
| 215 |
|
|
}
|
| 216 |
|
|
}
|
| 217 |
|
|
/*-----------------------------------------------------------*/
|
| 218 |
|
|
|
| 219 |
|
|
static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )
|
| 220 |
|
|
{
|
| 221 |
|
|
/* The usIndex parameter of the co-routine function is used as an index into
|
| 222 |
|
|
the xFlashRates array to obtain the delay period to use. */
|
| 223 |
|
|
static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,
|
| 224 |
|
|
300 / portTICK_RATE_MS,
|
| 225 |
|
|
450 / portTICK_RATE_MS,
|
| 226 |
|
|
600 / portTICK_RATE_MS,
|
| 227 |
|
|
750 / portTICK_RATE_MS,
|
| 228 |
|
|
900 / portTICK_RATE_MS,
|
| 229 |
|
|
1050 / portTICK_RATE_MS };
|
| 230 |
|
|
|
| 231 |
|
|
/* Co-routines MUST start with a call to crSTART. */
|
| 232 |
|
|
crSTART( xHandle );
|
| 233 |
|
|
|
| 234 |
|
|
for( ;; )
|
| 235 |
|
|
{
|
| 236 |
|
|
/* Toggle the LED. An offset of 8 is used to skip over the segments of
|
| 237 |
|
|
the left side display which use the low numbers. */
|
| 238 |
|
|
vParTestToggleLED( usIndex + 8 );
|
| 239 |
|
|
|
| 240 |
|
|
/* Delay until it is time to toggle the segment that this co-routine is
|
| 241 |
|
|
controlling again. */
|
| 242 |
|
|
crDELAY( xHandle, xFlashRates[ usIndex ] );
|
| 243 |
|
|
}
|
| 244 |
|
|
|
| 245 |
|
|
/* Co-routines MUST end with a call to crEND. */
|
| 246 |
|
|
crEND();
|
| 247 |
|
|
}
|
| 248 |
|
|
/*-----------------------------------------------------------*/
|
| 249 |
|
|
|
| 250 |
|
|
|