softport wrote on Monday, 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
{
TickType_t xTimeStamp;
uint32_t ulValue;
} Example_t;
// Serial port messages
#define serialMAX_MSG_LEN ( 50 )
char cBuffer[serialMAX_MSG_LEN];
char sNewLine[5] = "\r\n";
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 MX_FREERTOS_Init(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)
{
Example_t xData;
uint32_t ulNewValue = 0;
for(;;){
xData.ulValue = ulNewValue;
ulNewValue++;
xData.xTimeStamp = xTaskGetTickCount();
xQueueOverwrite( xMailbox, &xData );
osDelay(500);
}
}
void vReadMailbox(void const * argument)
{
Example_t pxData;
TickType_t 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 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);
}
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();
}