Regarding creating queue and I2C communication(using polling) inside main()

I am creating a queue using xQueueCreate(). Before that i am initializing I2C communication.
For eg :-

  1. I have taken the instance of queue handle from queue.h (typedef struct QueueDefinition * QueueHandle_t)
    My Handle declaration :- EXTERN QueueHandle_t appState_Queue in sys_include header file
  2. I am using the appState_Queue inside main but before that i am initializing I2C communication using polling. In this case the queue is not getting created the xQueueCreate() returns NULL.
  3. For eg :-
    int main(void)
{
	/*
		All Pheripheral Initialization 
	*/
	
	i2cInit();
	
	SYSTEM_State STATE_CMD_t; //System state is a typedef struct contain system future state and current state 
	appState_Queue = NULL;
	appState_Queue = xQueueCreate(10, sizeof(STATE_CMD_t));
	
	if(appState_Queue != NULL)
	{
		/* 
			Task are created here.....
			
			eg :- xTaskCreate(xTaskCreate(vUartCommunicationTask, "Uart 
                                Communication", 500, NULL, 1, NULL);
			
			Create task and add it to the list of tasks that are ready to run
			
		*/
		vTaskStartScheduler(); 	
	}
	for(;;);
}
  1. When I pause the debugging I found the control gets in xQueueGiveFromISR() and get
    stuck. But here xQueueGiveFromISR() is not even used.

So how i will be able to use the xQueueCreate inside the main with i2CInit (using Polling).
I have tried ways which i was able to get from forum.

Thanks
Prince Tripathi

Did you verify that by stepping through the code? If yes, you are running out of heap and you need to increase heap size. Which FreeRTOS heap are you using?

Do not declare any variable on the main stack if you want them to be available after the scheduler has started.

Do you have configASSERT defined and stack and malloc failure checking enabled?

Thanks.

Hi Gaurav,

  1. I have verified xQueueCreate() by stepping through the code and checked my heap size which is #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 20 * 1024 ) ). So heap is not running out

  2. SYSTEM_state STATE_CMD_T variable is only used to give size of the queue. Which i have checked declaring it apart from main stack it still no affect.

  3. Yes I have defined configASSERT() with stack and malloc failure.

I can’t create queue before pheripheral initialization and also I2CInit() might have some affect over Queue Creation?

You should be able to create queue before and that should not be a problem.

What is the callstack when this happens? Does that give any clue?

You should be able to create queue before and that should not be a problem.
:- I have tried creating queue before peripherals initialization now the queue is getting created and returning the handle correctly. Does FreeRTOS recommend that ?

What is the callstack when this happens? Does that give any clue?
:- I tried using call stack. But i was not able to find any clue.

Thanks

The order of creation should not have any effect unless your peripheral initialization does something weird. My guess is that the problem is somewhere else which is hard to guess without taking a look at the code.

Thanks.

The only problem i was facing was after I2CInit() queue was not getting created. But if i create my queue before that then it was working. I think the problem is with I2C will look into that.

Thanks
Prince