xQueuePeek disregards portMAX_DELAY, runs continuously. xQueueReceive works fine.

Hello, I have been working with FreeRtos for the last couple of weeks. I have read ‘Mastering the FreeRTOS Real Time Kernel’ up to the end of chapter 4, that deals with queues, and also have ‘The FreeRTOS Reference Manual 9.0.0’ manual. I’ve been going through the first manual, following the examples and modifying as needed, to work with CubeMX, setup for STM32F4 controller. My IDE is System Workbench for STM32. This is the first problem I have not been able to solve: xQueuePeek is disregarding portMAX_DELAY, making the task run continuously. There are no errors or warnings before/after compiling. If I replace xQueuePeek with xQueueReceive(with exact same arguments), everything works fine. The sender task uses xQueueOverwrite(), and osDelay(500). Sending and receiving tasks have the same priority 1. When the receiver uses xQueueReceive(), my output is updated every 0.5 seconds as expected, however when I replace it with xQueuePeek(), the output scrolls continuosly. I am using the rs232 port and connecting with Putty. I know some FreeRtos functions need special settings in FreeRTOSConfig.h, but I did not see any info on this regarding xQueuePeek, at least nothing more than xQueueReceive would need. Am I missing something? Thanks in advance ~~~ // Example adapted from: // Mastering the FreeRTOS Real Time Kernel (#161204) // Section 4.7 ‘Using a Queue to Create a Mailbox’ // Hardware configuration done using STM32CubeMX // Processor: STM32F407VE

include “FreeRTOS.h”

include “task.h”

include “cmsis_os.h”

include “stm32f4xx_hal.h”

include “usart.h”

include “string.h” // memset, used to clear cBuffer

include “stdio.h” // snprntf

// Mailbox handle QueueHandle_t xMailbox; // Mailbox message typedef struct xExampleStructure { TickTypet xTimeStamp; uint32t ulValue; } Example_t; // Serial port messages

define serialMAXMSGLEN ( 50 )

char cBuffer[serialMAXMSGLEN]; char sNewLine[5] = “rn”; int main(void) { /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); /* Call init function for freertos objects (in freertos.c) */ MX_FREERTOS_Init(); /* Start scheduler */ osKernelStart(); /* We should never get here as control is now taken by the scheduler */ while (1){ } } void MXFREERTOSInit(void) {
xTaskCreate( (TaskFunction_t)vUpdateMailbox, "updateMailbox", 128, NULL, 1, NULL );
xTaskCreate( (TaskFunction_t)vReadMailbox, "readMailbox", 128, NULL, 1, NULL );

xMailbox = xQueueCreate( 1, sizeof( Example_t ) );
} void vUpdateMailbox(void const * argument) { Examplet xData; uint32t ulNewValue = 0;
for(;;){

    xData.ulValue = ulNewValue;
    ulNewValue++;

    xData.xTimeStamp = xTaskGetTickCount();
    xQueueOverwrite( xMailbox, &xData );

    osDelay(500);
}
} void vReadMailbox(void const * argument) { Examplet pxData; TickTypet xPreviousTimeStamp = 0;
pxData.ulValue = 0;
pxData.xTimeStamp = 0;

/* Infinite loop */
for(;;){

    xPreviousTimeStamp = pxData.xTimeStamp;

    xQueuePeek( xMailbox, &pxData, portMAX_DELAY ); // Does not wait
    //xQueueReceive(xMailbox, &pxData, portMAX_DELAY); // This works

    if( pxData.xTimeStamp > xPreviousTimeStamp ){
        vSendStringAndNumber( "Data received: ", pxData.ulValue);
        vSendStringAndNumber( "Timestamp: ", pxData.xTimeStamp);
    }
    else{
        vSendString( "No new datarn");
        vSendStringAndNumber( "Current data: ", pxData.ulValue);
        vSendStringAndNumber( "Timestamp: ", pxData.xTimeStamp);
    }
}
} // Adapted from basic_io.c void vSendString( const char *pcString ) { vTaskSuspendAll(); { memset(cBuffer, 0, sizeof(cBuffer)); // string.h snprintf(cBuffer, sizeof(cBuffer), “%s”, pcString); // stdio.h
    HAL_UART_Transmit(&huart1, (uint8_t *)cBuffer, sizeof(cBuffer), 1000);
}
xTaskResumeAll();
} void vSendStringAndNumber( const char *pcString, unsigned long ulValue){
vTaskSuspendAll();
{
    memset(cBuffer, 0, sizeof(cBuffer)); // string.h
    snprintf(cBuffer, sizeof(cBuffer), "%s%lu", pcString, ulValue);
    strcat(cBuffer, (char *)sNewLine);

    HAL_UART_Transmit(&huart1, (uint8_t *)cBuffer, sizeof(cBuffer), 1000);
}
xTaskResumeAll();
} ~~~

xQueuePeek disregards portMAX_DELAY, runs continuously. xQueueReceive works fine.

Oncd the peek has detected an item on the queue, your proogram doesn’t do anything later to remove it, so it wii continue to be there. The difference between a peek and a receive is that peek leaves the data on the queue, so something later can take it off.

xQueuePeek disregards portMAX_DELAY, runs continuously. xQueueReceive works fine.

Oh dear, that is so obvious, I’m a bit embarassed. Thank you.