robinus wrote on Thursday, March 12, 2015:
As there are only 3 space inside the queue declared,
I think it was supposed to print only 3 “sent.” messages at the very beginning. But it is showing 4 “sent.” messages, which I cannot not figure out. Could you give me some clear advice on it?
Thanks.
Console Output
sent.
sent.
sent.
sent.
From Sender 2 = 200
sent.
From Sender 1 = 100
sent.
From Sender 2 = 200
Source Code
include “FreeRTOS.h”
include “task.h”
include “queue.h”
include “basic_io.h”
define mainSENDER_1 1
define mainSENDER_2 2
static void vSenderTask( void pvParameters );
static void vReceiverTask( void pvParameters );
xQueueHandle xQueue;
typedef struct
{
unsigned short ucValue;
unsigned char ucSource;
} xData;
xData xStructsToSend[ 2 ] =
{
{ 100, mainSENDER_1 }, / Used by Sender1. /
{ 200, mainSENDER_2 } / Used by Sender2. /
};
int main( void ){
xQueue = xQueueCreate( 3, sizeof( xData ) );
if( xQueue != NULL ){
xTaskCreate( vSenderTask, "Sender1", 240, ( void * ) &( xStructsToSend[ 0 ] ), 2, NULL );
xTaskCreate( vSenderTask, "Sender2", 240, ( void * ) &( xStructsToSend[ 1 ] ), 2, NULL );
/* Create the task that will read from the queue. The task is created with
priority 1, so below the priority of the sender tasks. */
xTaskCreate( vReceiverTask, "Receiver", 240, NULL, 1, NULL );
/* Start the scheduler so the created tasks start executing. */
vTaskStartScheduler();
}
else{
vPrintString( “Queue not created.\n” );
}
for( ;; );
return 0;
}
/-----------------------------------------------------------/
static void vSenderTask( void *pvParameters ){
portBASE_TYPE xStatus;
const portTickType xTicksToWait = 100 / portTICK_RATE_MS;
for( ;; ){
xStatus = xQueueSendToBack( xQueue, pvParameters, xTicksToWait );
if( xStatus != pdPASS ){
vPrintString( "Could not send to the queue.\n" );
}
else if( xStatus == errQUEUE_FULL )
{
vPrintString( "Queue is full.\n" );
}
else if( xStatus == pdPASS ){
//vPrintString(*pvParameters);
vPrintString(" sent.\n");
}
taskYIELD();
}
}
/-----------------------------------------------------------/
static void vReceiverTask( void *pvParameters ){
xData xReceivedStructure;
portBASE_TYPE xStatus;
for( ;; ){
if( uxQueueMessagesWaiting( xQueue ) != 3 ){
vPrintString( "Queue should have been full!\n" );
}
xStatus = xQueueReceive( xQueue, &xReceivedStructure, 0 );
if( xStatus == pdPASS ){
if( xReceivedStructure.ucSource == mainSENDER_1 ){
vPrintStringAndNumber( "From Sender 1 = ", xReceivedStructure.ucValue );
vPrintString("\n");
}
else
{
vPrintStringAndNumber( "From Sender 2 = ", xReceivedStructure.ucValue );
vPrintString("\n");
}
}
else
{
vPrintString( "Could not receive from the queue.\n" );
}
}
}
/-----------------------------------------------------------/
void vApplicationMallocFailedHook( void ){
for( ;; );
}
/-----------------------------------------------------------/
void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char pcTaskName ){
for( ;; );
}
/-----------------------------------------------------------/
void vApplicationIdleHook( void ){
}
/-----------------------------------------------------------/
void vApplicationTickHook( void ){
}