nobody wrote on Tuesday, January 25, 2005:
Hi again,
I’ve actually put the code here in case somebody can help me. I’ve added an extra task Task0 wich is equivalent to Task1.
The main code:
**************************************************************
*
* main.c
*
* File containing the main() function.
*
* Task1 is a low priority task. Task2 is a high priority task.
* ISR is tied to Timer2 which ticks over at fc/(256*256).
* FreeRTOS system tick is based on Timer0 which ticks over at
* fc/(256*8). The value of fc is 8MHz generally. The mcu is
* the ATmega128.
*
* The ISR sends a message consisting of a single incrementing
* portCHAR value to Task2 using vQueueSendFromISR(). Task2
* blocks on receiving a message from ISR using cQueueReceive().
* Task1 blocks for 10 ticks using vTaskDelay().
*
* Both tasks, when running, write a character value to the
* global static array, pcCharArray[]. Task1 writes the character
* ‘-’ whereas Task2 writes whatever the ISR sent it via the
* message queue; the value is ‘A’,‘B’,…,‘Z’ and repeating.
*
*
*
* Revision (do not edit):
*
* $Id$
*
**************************************************************/
/*-INCLUDES---------------------------------------------------*/
#include <stdlib.h>
#include "projdefs.h"
#include "portable.h"
#include "task.h"
#include "queue.h"
#include "ISR.h"
/*-DEFINES----------------------------------------------------*/
#define TASK0_PRIORITY ( tskIDLE_PRIORITY )
#define TASK1_PRIORITY ( tskIDLE_PRIORITY )
#define TASK2_PRIORITY ( tskIDLE_PRIORITY + ( unsigned portCHAR ) 1 )
#define TASK_STACK_SIZE portMINIMAL_STACK_SIZE
#define BLOCK_TIME ( ( portTickType ) 250 )
#define CHARARRAY_LENGTH 200
/*-TYPEDEFS---------------------------------------------------*/
/* Parameters for Task2. */
typedef struct
{
xQueueHandle xQueue;
portTickType xBlockTime;
} xTask2Parameters;
/*-GLOBALS----------------------------------------------------*/
static portCHAR pcCharArray[ CHARARRAY_LENGTH ];
static portSHORT cArrayIndex = 0;
/*-PROTOTYPES-------------------------------------------------*/
/* Create a specific queue. */
xQueueHandle pxCreateTask2Queue( void );
/* Task0 code. */
void vTask0Code( void * pvParameters );
/* Task1 code. */
void vTask1Code( void * pvParameters );
/* Task2 code. */
void vTask2Code( void * pvParameters );
/* Function to log a character in an array. */
void vLogCharacter( portCHAR cCharacter );
/*-IMPLEMENTATIONS--------------------------------------------*/
portSHORT main( void )
{
xTask2Parameters *pxParameters2;
/* A handle to the queue shared by Task2 and ISR. */
xQueueHandle xTask2Queue;
/* Get the queues for the tasks. */
xTask2Queue = pxCreateTask2Queue();
/* Initialize parameters for Task1 - NO PARAMETERS. */
/* Initialize parameters for Task2. */
pxParameters2 = ( xTask2Parameters * ) pvPortMalloc( sizeof( xTask2Parameters ) );
pxParameters2->xQueue = xTask2Queue;
pxParameters2->xBlockTime = BLOCK_TIME;
/* Create Task0 - LOW PRIORITY. */
sTaskCreate( vTask0Code, "TASK0", TASK_STACK_SIZE, NULL, TASK0_PRIORITY, NULL );
/* Create Task1 - LOW PRIORITY. */
sTaskCreate( vTask1Code, "TASK1", TASK_STACK_SIZE, NULL, TASK1_PRIORITY, NULL );
/* Create Task2 - HIGH PRIORITY. */
//sTaskCreate( vTask2Code, "TASK2", TASK_STACK_SIZE, pxParameters2, TASK2_PRIORITY, NULL );
/* Set up ISRs, give it the handle to the queue. */
vInitializeISR( xTask2Queue );
/* Start the scheduler. */
vTaskStartScheduler( portUSE_PREEMPTION );
return 0;
}
void vTask0Code( void * pvParameters )
{
for (;
{
/* Block for some ticks then log a ‘|’ character. */
vTaskDelay( ( portTickType ) 10 );
vLogCharacter( ‘|’ );
}
}
void vTask1Code( void * pvParameters )
{
for (;
{
/* Block for some ticks then log a ‘-’ character. */
vTaskDelay( ( portTickType ) 5 );
vLogCharacter( ‘-’ );
}
}
void vTask2Code( void * pvParameters )
{
unsigned portCHAR ucMessage;
xTask2Parameters * pxParameters = ( xTask2Parameters * ) pvParameters;
for (;
{
/* Block on message available from ISR, and log the received character. */
if( cQueueReceive( pxParameters->xQueue, &ucMessage, pxParameters->xBlockTime ) == pdPASS )
{
vLogCharacter( ucMessage );
}
}
}
xQueueHandle pxCreateTask2Queue( void )
{
xQueueHandle xQueue;
const unsigned portCHAR ucQueueSize = 1;
/* Create the queue. */
xQueue = xQueueCreate( ucQueueSize, ( unsigned portCHAR ) sizeof ( portCHAR ) );
/* Check that the queue was create. */
if ( xQueue == 0 )
{
/* Queue not created. Should call error handler. */
}
return xQueue;
}
void vLogCharacter( portCHAR cCharacter )
{
pcCharArray[ cArrayIndex ] = cCharacter;
++cArrayIndex;
/* The array is a circular buffer. */
if ( cArrayIndex > CHARARRAY_LENGTH )
{
cArrayIndex = 0;
}
}
The ISR code:
*************************************
**************************************************************
*
* ISR.c
*
* A brief description
*
*
* Revision (do not edit):
*
* $Id$
*
**************************************************************/
#include "ISR.h"
#include <avr/signal.h>
static xQueueHandle pxQueueHandle;
static portCHAR ucMessage;
void vInitializeISR( xQueueHandle xQueue )
{
pxQueueHandle = xQueue;
ucMessage = ‘A’;
}
/* This ISR kicks off when the 8-bit Timer2 overflows. */
SIGNAL( SIG_OVERFLOW2 )
{
portCHAR cTaskWokenByPost = pdFALSE;
/* The message is a portCHAR and it cycles from ‘A’ to ‘Z’. */
if ( ucMessage > ‘Z’ )
{
ucMessage = ‘A’;
}
/* Send the current message to Task2. */
cTaskWokenByPost = cQueueSendFromISR( pxQueueHandle, &ucMessage, cTaskWokenByPost );
/* Go to the next letter in the alphabet. */
++ucMessage;
/* If a higher priority task is woken by this message then yield the current task. */
if ( cTaskWokenByPost )
{
taskYIELD();
}
}