Using Queue for 2 tasks as sender and one as receiver task

Hi,
I am working on Queues. I have two tasks that sends different messages. And one receiver task that receive messages from the 2 sender tasks. My problem I identified is, one of the sender task (LED_Task_1) gets blocked in unknown state. I checked with the debugger by pausing it and the control goes somewhere to xQueueGenericSend function
and configASSERT( pxQueue ) and it doesn’t comes out of this. . I have attached the screen shot at the end of the post. Please go through it. I also attached the code.

#include “main.h”
#include “cmsis_os.h”
#include<stdio.h>
#include<stdint.h>
#include “FreeRTOSConfig.h”
#include “task.h”
#include “queue.h”

/* Definitions for DefaultTask_1 <em>/
osThreadId_t DefaultTask_1Handle;
const osThreadAttr_t DefaultTask_1_attributes = {
.name = “DefaultTask_1”,
.priority = (osPriority_t) osPriorityNormal,
.stack_size = 1000
};
/</em> Definitions for Serial_Task_2 <em>/
osThreadId_t Serial_Task_2Handle;
const osThreadAttr_t Serial_Task_2_attributes = {
.name = “Serial_Task_2”,
.priority = (osPriority_t) osPriorityNormal1,
.stack_size = 1000
};
/</em> Definitions for GPS_Task_03 <em>/
osThreadId_t GPS_Task_03Handle;
const osThreadAttr_t GPS_Task_03_attributes = {
.name = “GPS_Task_03”,
.priority = (osPriority_t) osPriorityNormal,
.stack_size = 1000
};
/</em> Definitions for xQueue1 <em>/
osMessageQueueId_t xQueue1Handle;
const osMessageQueueAttr_t xQueue1_attributes = {
.name = “xQueue1”
};
/</em> Definitions for xQueue2 <em>/
osMessageQueueId_t xQueue2Handle;
const osMessageQueueAttr_t xQueue2_attributes = {
.name = “xQueue2”
};
/</em> USER CODE BEGIN PV <em>/
/</em> Declare two variables of type QueueHandle_t. Both queues are added to the same queue set. <em>/
static QueueHandle_t xQueue1 = NULL, xQueue2 = NULL;
/</em> Declare a variable of type QueueSetHandle_t. This is the queue set to which the two queues are added. */
static QueueSetHandle_t xQueueSet = NULL;

/* USER CODE END PV */

int main(void)
{
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */


/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();


/* USER CODE END 2 <em>/
/</em> Init scheduler */
osKernelInitialize();

/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */

/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */

/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */
/* USER CODE END RTOS_TIMERS */

/* Create the queue(s) */
/* creation of xQueue1 */
xQueue1Handle = osMessageQueueNew (16, sizeof(uint8_t), &xQueue1_attributes);

/* creation of xQueue2 */
xQueue2Handle = osMessageQueueNew (16, sizeof(uint8_t), &xQueue2_attributes);


/* Create the queue set. Two queues will be added to the set, each of which can
	contain 1 item, so the maximum number of queue handles the queue set will ever
	have to hold at one time is 2 (2 queues multiplied by 1 item per queue). */
xQueueSet = xQueueCreateSet( 1 * 2 );

/* Add the two queues to the set. */
xQueueAddToSet( xQueue1Handle, xQueueSet );
xQueueAddToSet( xQueue2Handle, xQueueSet );

/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */

/* Create the thread(s) */
/* creation of DefaultTask_1 */
DefaultTask_1Handle = osThreadNew(LED_Task_1, NULL, &DefaultTask_1_attributes);

/* creation of Serial_Task_2 */
Serial_Task_2Handle = osThreadNew(Serial_Receive_2, NULL, &Serial_Task_2_attributes);

/* creation of GPS_Task_03 */
GPS_Task_03Handle = osThreadNew(GPS_Task_3, NULL, &GPS_Task_03_attributes);

/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */

/* Start scheduler */
osKernelStart();

/* We should never get here as control is now taken by the scheduler */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
	/* USER CODE END WHILE */

	/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */

}
void LED_Task_1(void <em>argument)
{
/</em> USER CODE BEGIN 5 */
const TickType_t xBlockTime = pdMS_TO_TICKS( 100 );
const char * const pcMessage = “Message from LED_Task_1 \r\n”;


/* Infinite loop */
for(;;)
{
	HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
	/* osDelay(100); */
	/* Block for 100ms. */
	vTaskDelay(xBlockTime);

	/* Send this task's string to xQueue1 */
	xQueueSend( xQueue1, &pcMessage, 0 );
}

osDelay(1);
/* USER CODE END 5 */
}

/* USER CODE BEGIN Header_Serial_Receive_2 <em>/
/</em> *

* @brief Function implementing the Serial_Task_2 thread.
* @param argument: Not used
* @retval None
<em>/
/</em> USER CODE END Header_Serial_Receive_2 */
void Serial_Receive_2(void <em>argument)
{
/</em> USER CODE BEGIN Serial_Receive_2 */
// HAL_UART_Receive_IT(&huart3, (uint8_t *)&pc, 1); //Restart receive interruptQueueHandle_t xQueueThatContainsData;
char *pcReceivedString;/* Infinite loop <em>/
for(;:wink:
{
/</em> Block on the queue set to wait for one of the queues in the set to contain data.
Cast the QueueSetMemberHandle_t value returned from xQueueSelectFromSet() to a
QueueHandle_t, as it is known all the members of the set are queues (the queue set
does not contain any semaphores). <em>/
xQueueThatContainsData = ( QueueHandle_t ) xQueueSelectFromSet( xQueueSet,portMAX_DELAY );
/</em> An indefinite block time was used when reading from the queue set, so
xQueueSelectFromSet() will not have returned unless one of the queues in the set
contained data, and xQueueThatContainsData cannot be NULL. Read from the queue. It
is not necessary to specify a block time because it is known the queue contains
data. The block time is set to 0. */
xQueueReceive( xQueueThatContainsData, &pcReceivedString, 0 );

/* HAL_UART_Transmit_IT(&huart2, (uint8_t *)pcReceivedString,strlen((const char* )pcReceivedString)); //Restart Transmit Interrupt
while (HAL_UART_GetState(&huart2) == HAL_UART_STATE_BUSY_TX ||
HAL_UART_GetState(&huart2) == HAL_UART_STATE_BUSY_TX_RX); */
HAL_UART_Transmit(&huart2, (uint8_t *)pcReceivedString,strlen((const char* )pcReceivedString) , 100);

}
/* USER CODE END Serial_Receive_2 */
osDelay(1);


}

/* USER CODE BEGIN Header_GPS_Task_3 <em>/
/</em> *

* @brief Function implementing the GPS_Task_03 thread.
* @param argument: Not used
* @retval None
<em>/
/</em> USER CODE END Header_GPS_Task_3 */
void GPS_Task_3(void <em>argument)
{
/</em> USER CODE BEGIN GPS_Task_3 */
const TickType_t xBlockTime = pdMS_TO_TICKS( 200 );
const char * const pcMessage = “Message from GPS_Task_3\r\n”;/* Infinite loop <em>/
for(;:wink:
{
/</em> Block for 200ms. */
vTaskDelay(xBlockTime);


 /* Send this task's string to xQueue2 */
 xQueueSend( xQueue2, &pcMessage, 0 );


}
/* USER CODE END GPS_Task_3 */
osDelay(1);
}

Please let me know any mistake I have done in the code.

But I pause the debugger the control goes to the xQueueGenericSend function

and configASSERT( pxQueue ) and it doesn’t comes out of this.

Regards,
Kumar

Maybe I’m wrong because the posted code wasn’t quoted correctly… But you are creating queues for uint8_t items, but try to send char* items (text) and try to receive the (char*)text into a char variable and use it as text buffer e.g. using strlen. This is broken and can‘t work.
You should get more familiar with how queues work e.g. (re-)reading the API documentation xQueueSend.

1 Like

The assert is telling you the queue handle is null. Looking at the code I see that task is using a handle called xQueue1, but I cannot see where you set xQueue1 to a value. If you do set it to a value, then make sure that is not done after the task tries to use it, and that nothing is overwriting it (maybe accidentally because of a bug somewhere).

[I edited the original post to try and make the source code easier to read, but it is still difficult due to lack of indentation.]

Thanks for the reply.

Regards,
Kumar