This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
FreeRTOS-Kernel/Demo/MB96350_Softune_Dice_Kit/DiceTask.c

186 lines
6.4 KiB
C
Raw Normal View History

2009-01-30 22:06:37 +08:00
/*
FreeRTOS.org V5.1.1 - Copyright (C) 2003-2008 Richard Barry.
This file is part of the FreeRTOS.org distribution.
FreeRTOS.org is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FreeRTOS.org is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FreeRTOS.org; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A special exception to the GPL can be applied should you wish to distribute
a combined work that includes FreeRTOS.org, without being obliged to provide
the source code for any proprietary components. See the licensing section
of http://www.FreeRTOS.org for full details of how and when the exception
can be applied.
***************************************************************************
***************************************************************************
* *
* SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
* and even write all or part of your application on your behalf. *
* See http://www.OpenRTOS.com for details of the services we provide to *
* expedite your project. *
* *
***************************************************************************
***************************************************************************
Please ensure to read the configuration and relevant port sections of the
online documentation.
http://www.FreeRTOS.org - Documentation, latest information, license and
contact details.
http://www.SafeRTOS.com - A version that is certified for use in safety
critical systems.
http://www.OpenRTOS.com - Commercial support, development, porting,
licensing and training services.
*/
#include "FreeRTOS.h"
#include "task.h"
2009-01-31 18:31:21 +08:00
#include "semphr.h"
2009-01-30 22:06:37 +08:00
2009-01-31 20:56:33 +08:00
#define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms ( 20 )
#define diceRUN_TIME ( 2000 / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )
2009-01-30 22:06:37 +08:00
2009-01-31 00:28:08 +08:00
#define diceEND_DELAY ( 5000 / portTICK_RATE_MS )
#define dice7SEG_Value( x ) *( pucDisplayOutput[ x ] )
2009-01-31 18:31:21 +08:00
#define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )
2009-01-30 22:06:37 +08:00
static const char cDisplaySegments[ 2 ][ 11 ] =
{
{ 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 },
{ 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f }
};
2009-01-31 18:31:21 +08:00
static xSemaphoreHandle xSemaphores[ 2 ] = { 0 };
2009-01-31 00:28:08 +08:00
extern volatile unsigned char *pucDisplayOutput[ 2 ];
2009-01-30 22:06:37 +08:00
/*-----------------------------------------------------------*/
void vDiceTask( void *pvParameters )
{
unsigned char ucDiceValue, ucIndex;
2009-01-31 20:56:33 +08:00
unsigned long ulDiceRunTime;
extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
2009-01-30 22:06:37 +08:00
2009-01-31 20:56:33 +08:00
/* Two instances of this task are created so the task parameter is used
to pass in an index that allows this task to know which file scope variables
it should use. Cast this index into a usable type. */
2009-01-30 22:06:37 +08:00
ucIndex = ( unsigned char ) pvParameters;
2009-01-31 20:56:33 +08:00
/* A binary semaphore is used to signal button push events. Create the
semaphore before it is used. */
2009-01-31 18:31:21 +08:00
vSemaphoreCreateBinary( xSemaphores[ ucIndex ] );
2009-01-31 20:56:33 +08:00
/* Make sure the semaphore starts in the wanted state - no button pushes
pending. This call will just clear any button pushes that are latched.
Passing in 0 as the block time means the call will not wait for any further
button pushes. */
prvButtonHit( ucIndex, 0 );
/* Seed the random number generator. */
srand( ( unsigned char ) diceRUN_TIME );
2009-01-30 22:06:37 +08:00
for( ;; )
{
2009-01-31 20:56:33 +08:00
/* Wait for a button push. This task will enter the Blocked state
(will not run again) until after a button has been pushed. */
prvButtonHit( ucIndex, portMAX_DELAY );
/* The next line will only execute after a button has been pushed -
initialise the variable used to shake the dice. */
ulDiceRunTime = diceRUN_TIME;;
/* Suspend the flash tasks so this task has exclusive access to the
display. */
vSuspendFlashTasks( ucIndex, pdTRUE );
while( ulDiceRunTime > 0 )
{
ulDiceRunTime--;
/* Generate and display a random number. */
ucDiceValue = rand() % 6 + 1;
dice7SEG_Value( ucIndex ) = ( dice7SEG_Value( ucIndex ) | 0xf7 ) & cDisplaySegments[ ucIndex ][ ucDiceValue ];
/* Block/sleep for a very short time before generating the next
random number. */
vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms / portTICK_RATE_MS );
}
/* Wait for a short time before resuming (un-suspending) the flash
task. The flash tasks are only restarted if a button is not pushed
during this delay - if a button is pushed then the dice are shaken
again.
First...clear any button pushes that are already pending. Again a
block time of zero is used so the function does not wait for any
pushes. */
prvButtonHit( ucIndex, 0 );
/* Second...peek the semaphore. This task will block/sleep until a
button is pushed again, but because the peek function is used a
button being pushed will unblock the task but remain pending. */
if( xQueuePeek( xSemaphores[ ucIndex ], NULL, diceEND_DELAY ) == pdFALSE )
2009-01-30 22:06:37 +08:00
{
2009-01-31 20:56:33 +08:00
*pucDisplayOutput[ ucIndex ] = 0xff;
vSuspendFlashTasks( ucIndex, pdFALSE );
2009-01-30 22:06:37 +08:00
}
}
}
/*-----------------------------------------------------------*/
2009-01-31 18:31:21 +08:00
__interrupt void vExternalInt8Handler( void )
2009-01-30 22:06:37 +08:00
{
2009-01-31 18:31:21 +08:00
short sHigherPriorityTaskWoken = pdFALSE;
/* Reset the interrupt. */
EIRR1_ER8 = 0;
xSemaphoreGiveFromISR( xSemaphores[ 0 ], &sHigherPriorityTaskWoken );
if( sHigherPriorityTaskWoken != pdFALSE )
2009-01-30 22:06:37 +08:00
{
2009-01-31 18:31:21 +08:00
portYIELD_FROM_ISR();
2009-01-30 22:06:37 +08:00
}
2009-01-31 18:31:21 +08:00
}
/*-----------------------------------------------------------*/
__interrupt void vExternalInt9Handler( void )
{
short sHigherPriorityTaskWoken = pdFALSE;
/* Reset the interrupt. */
EIRR1_ER9 = 0;
xSemaphoreGiveFromISR( xSemaphores[ 1 ], &sHigherPriorityTaskWoken );
if( sHigherPriorityTaskWoken != pdFALSE )
2009-01-30 22:06:37 +08:00
{
2009-01-31 18:31:21 +08:00
portYIELD_FROM_ISR();
2009-01-30 22:06:37 +08:00
}
}
2009-01-31 00:28:08 +08:00
2009-01-30 22:06:37 +08:00