|
|
@ -47,41 +47,64 @@
|
|
|
|
licensing and training services.
|
|
|
|
licensing and training services.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Defines the 'dice' tasks as described at the top of main.c
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Kernel includes. */
|
|
|
|
#include "FreeRTOS.h"
|
|
|
|
#include "FreeRTOS.h"
|
|
|
|
#include "task.h"
|
|
|
|
#include "task.h"
|
|
|
|
#include "semphr.h"
|
|
|
|
#include "semphr.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms ( 20 )
|
|
|
|
/* Delays used within the dice functionality. All delays are defined in milliseconds. */
|
|
|
|
#define diceRUN_TIME ( 2000 / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )
|
|
|
|
#define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms ( 20 / portTICK_RATE_MS )
|
|
|
|
|
|
|
|
#define diceSHAKE_TIME ( ( 2000 / portTICK_RATE_MS ) / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )
|
|
|
|
|
|
|
|
#define diceSHORT_PAUSE_BEFORE_SHAKE ( 1000 / portTICK_RATE_MS )
|
|
|
|
#define diceEND_DELAY ( 5000 / portTICK_RATE_MS )
|
|
|
|
#define diceDELAY_WHILE_DISPLAYING_RESULT ( 5000 / portTICK_RATE_MS )
|
|
|
|
|
|
|
|
|
|
|
|
#define dice7SEG_Value( x ) *( pucDisplayOutput[ x ] )
|
|
|
|
/* Macro to access the display ports. */
|
|
|
|
|
|
|
|
#define dice7SEG_Value( x ) ( *( pucDisplayOutput[ x ] ) )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Checks the semaphore use to communicate button push events. A block time
|
|
|
|
|
|
|
|
can be specified - this is the time to wait for a button push to occur should
|
|
|
|
|
|
|
|
one have not already occurred. */
|
|
|
|
#define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )
|
|
|
|
#define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Defines the outputs required for each digit on the display. */
|
|
|
|
static const char cDisplaySegments[ 2 ][ 11 ] =
|
|
|
|
static const char cDisplaySegments[ 2 ][ 11 ] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
{ 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 },
|
|
|
|
{ 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 }, /* Left display. */
|
|
|
|
{ 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f }
|
|
|
|
{ 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f } /* Right display. */
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* The semaphores used to communicate button push events between the button
|
|
|
|
|
|
|
|
input interrupt handlers and the dice tasks. Two dice tasks are created so two
|
|
|
|
|
|
|
|
semaphores are required. */
|
|
|
|
static xSemaphoreHandle xSemaphores[ 2 ] = { 0 };
|
|
|
|
static xSemaphoreHandle xSemaphores[ 2 ] = { 0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Defines the ports used to write to the display. This variable is defined in
|
|
|
|
|
|
|
|
partest.c, which contains the LED set/clear/toggle functions. */
|
|
|
|
extern volatile unsigned char *pucDisplayOutput[ 2 ];
|
|
|
|
extern volatile unsigned char *pucDisplayOutput[ 2 ];
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Defines the 'dice' tasks as described at the top of main.c
|
|
|
|
|
|
|
|
*/
|
|
|
|
void vDiceTask( void *pvParameters )
|
|
|
|
void vDiceTask( void *pvParameters )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
unsigned char ucDiceValue, ucIndex;
|
|
|
|
unsigned char ucDiceValue, ucIndex;
|
|
|
|
unsigned long ulDiceRunTime;
|
|
|
|
unsigned long ulDiceRunTime;
|
|
|
|
extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
|
|
|
|
extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Two instances of this task are created so the task parameter is used
|
|
|
|
/* 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
|
|
|
|
to pass in a constant that indicates whether this task is controlling
|
|
|
|
it should use. Cast this index into a usable type. */
|
|
|
|
the left side or right side display. The constant is used as an index
|
|
|
|
|
|
|
|
into the arrays defined at file scope within this file. */
|
|
|
|
ucIndex = ( unsigned char ) pvParameters;
|
|
|
|
ucIndex = ( unsigned char ) pvParameters;
|
|
|
|
|
|
|
|
|
|
|
|
/* A binary semaphore is used to signal button push events. Create the
|
|
|
|
/* A binary semaphore is used to signal button push events. Create the
|
|
|
@ -91,12 +114,18 @@ extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
|
|
|
|
/* Make sure the semaphore starts in the wanted state - no button pushes
|
|
|
|
/* Make sure the semaphore starts in the wanted state - no button pushes
|
|
|
|
pending. This call will just clear any button pushes that are latched.
|
|
|
|
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
|
|
|
|
Passing in 0 as the block time means the call will not wait for any further
|
|
|
|
button pushes. */
|
|
|
|
button pushes but instead return immediately. */
|
|
|
|
prvButtonHit( ucIndex, 0 );
|
|
|
|
prvButtonHit( ucIndex, 0 );
|
|
|
|
|
|
|
|
|
|
|
|
/* Seed the random number generator. */
|
|
|
|
/* Seed the random number generator. */
|
|
|
|
srand( ( unsigned char ) diceRUN_TIME );
|
|
|
|
srand( ( unsigned char ) diceSHAKE_TIME );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Start the task proper. A loop will be performed each time a button is
|
|
|
|
|
|
|
|
pushed. The task will remain in the blocked state (sleeping) until a
|
|
|
|
|
|
|
|
button is pushed. */
|
|
|
|
for( ;; )
|
|
|
|
for( ;; )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/* Wait for a button push. This task will enter the Blocked state
|
|
|
|
/* Wait for a button push. This task will enter the Blocked state
|
|
|
@ -104,13 +133,21 @@ extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
|
|
|
|
prvButtonHit( ucIndex, portMAX_DELAY );
|
|
|
|
prvButtonHit( ucIndex, portMAX_DELAY );
|
|
|
|
|
|
|
|
|
|
|
|
/* The next line will only execute after a button has been pushed -
|
|
|
|
/* The next line will only execute after a button has been pushed -
|
|
|
|
initialise the variable used to shake the dice. */
|
|
|
|
initialise the variable used to control the time the dice is shaken
|
|
|
|
ulDiceRunTime = diceRUN_TIME;;
|
|
|
|
for. */
|
|
|
|
|
|
|
|
ulDiceRunTime = diceSHAKE_TIME;
|
|
|
|
|
|
|
|
|
|
|
|
/* Suspend the flash tasks so this task has exclusive access to the
|
|
|
|
/* Suspend the flash tasks so this task has exclusive access to the
|
|
|
|
display. */
|
|
|
|
display. */
|
|
|
|
vSuspendFlashTasks( ucIndex, pdTRUE );
|
|
|
|
vSuspendFlashTasks( ucIndex, pdTRUE );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Clear the display and pause for a short time, before starting to
|
|
|
|
|
|
|
|
shake. */
|
|
|
|
|
|
|
|
*pucDisplayOutput[ ucIndex ] = 0xff;
|
|
|
|
|
|
|
|
vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Keep generating and displaying random numbers until the shake time
|
|
|
|
|
|
|
|
expires. */
|
|
|
|
while( ulDiceRunTime > 0 )
|
|
|
|
while( ulDiceRunTime > 0 )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ulDiceRunTime--;
|
|
|
|
ulDiceRunTime--;
|
|
|
@ -121,31 +158,33 @@ extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
|
|
|
|
|
|
|
|
|
|
|
|
/* Block/sleep for a very short time before generating the next
|
|
|
|
/* Block/sleep for a very short time before generating the next
|
|
|
|
random number. */
|
|
|
|
random number. */
|
|
|
|
vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms / portTICK_RATE_MS );
|
|
|
|
vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_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
|
|
|
|
/* Clear any button pushes that are pending because a button bounced, or
|
|
|
|
pushes. */
|
|
|
|
was pressed while the dice were shaking. Again a block time of zero is
|
|
|
|
|
|
|
|
used so the function does not wait for any pushes but instead returns
|
|
|
|
|
|
|
|
immediately. */
|
|
|
|
prvButtonHit( ucIndex, 0 );
|
|
|
|
prvButtonHit( ucIndex, 0 );
|
|
|
|
|
|
|
|
|
|
|
|
/* Second...peek the semaphore. This task will block/sleep until a
|
|
|
|
/* Delay for a short while to display the dice shake result. Use a queue
|
|
|
|
button is pushed again, but because the peek function is used a
|
|
|
|
peek here instead of a vTaskDelay() allows the delay to be interrupted by
|
|
|
|
button being pushed will unblock the task but remain pending. */
|
|
|
|
a button push. If a button is pressed xQueuePeek() will return but the
|
|
|
|
if( xQueuePeek( xSemaphores[ ucIndex ], NULL, diceEND_DELAY ) == pdFALSE )
|
|
|
|
button push will remain pending to be read again at the top of this for
|
|
|
|
{
|
|
|
|
loop. It is safe to uses a queue function on a semaphore handle as
|
|
|
|
*pucDisplayOutput[ ucIndex ] = 0xff;
|
|
|
|
semaphores are implemented as macros that uses queues, so the two are
|
|
|
|
vSuspendFlashTasks( ucIndex, pdFALSE );
|
|
|
|
basically the same thing. */
|
|
|
|
}
|
|
|
|
xQueuePeek( xSemaphores[ ucIndex ], NULL, diceDELAY_WHILE_DISPLAYING_RESULT );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Clear the display then resume the tasks or co-routines that were using
|
|
|
|
|
|
|
|
the segments of the display. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Handler for the SW2 button push interrupt. */
|
|
|
|
__interrupt void vExternalInt8Handler( void )
|
|
|
|
__interrupt void vExternalInt8Handler( void )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
short sHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
short sHigherPriorityTaskWoken = pdFALSE;
|
|
|
@ -153,8 +192,18 @@ short sHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
/* Reset the interrupt. */
|
|
|
|
/* Reset the interrupt. */
|
|
|
|
EIRR1_ER8 = 0;
|
|
|
|
EIRR1_ER8 = 0;
|
|
|
|
|
|
|
|
|
|
|
|
xSemaphoreGiveFromISR( xSemaphores[ 0 ], &sHigherPriorityTaskWoken );
|
|
|
|
/* Check the semaphore has been created before attempting to use it. */
|
|
|
|
|
|
|
|
if( xSemaphores[ configLEFT_DISPLAY ] != NULL )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Send a message via the semaphore to the dice task that controls the
|
|
|
|
|
|
|
|
left side display. This will unblock the task if it is blocked waiting
|
|
|
|
|
|
|
|
for a button push. */
|
|
|
|
|
|
|
|
xSemaphoreGiveFromISR( xSemaphores[ configLEFT_DISPLAY ], &sHigherPriorityTaskWoken );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* If sending the semaphore unblocked a task, and the unblocked task has a
|
|
|
|
|
|
|
|
priority that is higher than the currently running task, then force a context
|
|
|
|
|
|
|
|
switch. */
|
|
|
|
if( sHigherPriorityTaskWoken != pdFALSE )
|
|
|
|
if( sHigherPriorityTaskWoken != pdFALSE )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
portYIELD_FROM_ISR();
|
|
|
|
portYIELD_FROM_ISR();
|
|
|
@ -162,6 +211,7 @@ short sHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* As per vExternalInt8Handler(), but for SW3 and the right side display. */
|
|
|
|
__interrupt void vExternalInt9Handler( void )
|
|
|
|
__interrupt void vExternalInt9Handler( void )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
short sHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
short sHigherPriorityTaskWoken = pdFALSE;
|
|
|
@ -169,7 +219,10 @@ short sHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
/* Reset the interrupt. */
|
|
|
|
/* Reset the interrupt. */
|
|
|
|
EIRR1_ER9 = 0;
|
|
|
|
EIRR1_ER9 = 0;
|
|
|
|
|
|
|
|
|
|
|
|
xSemaphoreGiveFromISR( xSemaphores[ 1 ], &sHigherPriorityTaskWoken );
|
|
|
|
if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( sHigherPriorityTaskWoken != pdFALSE )
|
|
|
|
if( sHigherPriorityTaskWoken != pdFALSE )
|
|
|
|
{
|
|
|
|
{
|
|
|
|