Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


xQueuePeek disregards portMAX_DELAY, runs continuously. xQueueReceive works fine.

Posted by softport on June 26, 2017

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 */ MXGPIOInit(); MXUSART1UART_Init();

/* Call init function for freertos objects (in freertos.c) */ MXFREERTOSInit();

/* 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;


	xData.ulValue = ulNewValue;

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



void vReadMailbox(void const * argument) { Examplet pxData; TickTypet xPreviousTimeStamp = 0;

pxData.ulValue = 0;
pxData.xTimeStamp = 0;

/* Infinite loop */

	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);
		vSendString( "No new data\r\n");
		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);


void vSendStringAndNumber( const char *pcString, unsigned long ulValue){

	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);



xQueuePeek disregards portMAX_DELAY, runs continuously. xQueueReceive works fine.

Posted by richarddamon on June 26, 2017

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.

Posted by softport on June 26, 2017

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

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner