xQueueCreate

Hello, I’m relatively new to FREERTOS so bear with me please. My target is the NXP Kinitic K64 series (formerly Freescale) bord.I created a FREERTOS project with the Kernel and libraraies and build it Created 2 priodic taske and evrything seems to be running well. For the next step I’m trying to create a QUE for passing messeges between tasks so I created a task (this is taken from the freertos example): void vATask( void *pvParameters ) { struct AMessage *pxMessage; uint32_t * test_ptr;
// Create a queue capable of containing 10 pointers to AMessage structures.
// These should be passed by pointer as they contain a lot of data.
xQueue = xQueueCreate( 10, sizeof( unsigned int*  ) );
if( xQueue == 0 )
{
    // Failed to create the queue.
}



// ...



// Send a pointer to a struct AMessage object.  Don't block if the
// queue is already full.
pxMessage = & xMessage;
test_ptr= &test_var;
xQueueSend( xQueue, ( void * )/* &pxMessage*/&test_ptr, ( TickType_t ) 0 );



// ... Rest of task code.
} When I step through the code ,It hangs in xQueueCreate(). It hits configASSERT( xReturn ); and just hangs there. Don’t know what is going on. I appriciate your help. Thanks, Koorosh Hajiani

xQueueCreate

Hi Koorosh, it means that xReturn is NULL. And this means that xQueueCreate() was not able to allocte the queue. Step with your debugger through xQueueCreate() and you should see what is failing. I think your problem is /* Allocate the new queue structure and storage area. */ pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); and that pvPortMalloc() returns NULL because you don’t have enough heap allocated. I suggest you allocate more heap with increasing configTOTALHEAPSIZE in FreeRTOSConfig.h I hope this helps, Erich

xQueueCreate

Hi Erich, Many thanks you for your response. You’re correct, It does crash here: pxNewQueue = ( Queuet * ) pvPortMalloc( sizeof( Queuet ) ); if( pxNewQueue != NULL ) Which is part of xQueueGenericCreate( const UBaseTypet uxQueueLength, const UBaseTypet uxItemSize, const uint8_t ucQueueType ) ** function which is the macro definition of ** xQueueCreate. I tried to change the heap size from 0x8000 to 0xA000

define configTOTALHEAPSIZE ((size_t)(/*0x8000*/ 0xA000)) /* size of heap in bytes */

It is not helping. I looked at the linker file and there is 0x4000 bytes allocated on the heap. I am not sure if the freertos definition of the heap size override the linker one but I changed the configTOTALHEAPSIZE to 0x4000 to match that in the linker file and it did not help either. I’m allocating only 10 bytes and 0x8000 seems sufficient for this. Something is not right. Any ideas? Thanks, Koorosh Hajiani

xQueueCreate

The FreeRTOS heap is separate to the heap allocated by the linker – and FreeRTOS doesn’t touch the heap allocated by the linker unless you are using heap_3 (see http://www.freertos.org/a00111.html for an explanation of the different heap options). Unless you code is calling malloc() somewhere then you can set the size of the heap allocated by the linker to 0 (or 4 as 0 is not always a valid value) as it is not needed. You application code can always call pvPortMalloc() instead of malloc() too – which will also allow you to set the linker allocated heap to 0. If you are interested there is a V9 release candidate on SourceForge currently that allows you to allocate this memory statically too http://www.freertos.org/FreeRTOS-V9.html Regards.

xQueueCreate

Hi Thank you for thr response. I changed heap3 to heap1 and it seems to have done the trick. However no my code is generating a trap. As I said I’m kind of new to freertos and I’m certain there are lots of mistake in the code. My code is trying to creat 4 tasks : void taskone(taskparamt param); void tasktwo(taskparamt param); void vATask(taskparamt param); void vADifferentTask(taskparamt param); taskone and tasktwo are periodic: void taskone(taskparam_t param) { int i = 0; PRINTF(“This is Task Onennr”); PRINTF(“Press SW2 to Toggle Blue LED…nnr”); while(1) {
    vTaskDelay( 500 );
    // Poll (sw2) if GPIO_INTERRUPT is not set
    if(/*GPIO_DRV_ReadPinInput(kGpioSW2) == 0*/1)
    {
        //OSA_TimeDelay(200);
        GPIO_DRV_TogglePinOutput(BOARD_GPIO_LED_GREEN);
    }
    PRINTF("%xrn",*test_rx_ptr);
}
} void tasktwo(taskparam_t param) { //int i = 0; PRINTF(“This is Task Twonnr”); PRINTF(“Press SW2 to Toggle Blue LED…nnr”); while(1) {
    vTaskDelay( 700 );
    // Poll (sw2) if GPIO_INTERRUPT is not set
    if(GPIO_DRV_ReadPinInput(kGpioSW2) == 0)
    {
        //OSA_TimeDelay(200);
        GPIO_DRV_TogglePinOutput(BOARD_GPIO_LED_GREEN);
    }

    test_var++;
}
} vATask is a task that creates a queue and post a value to it void vATask( void *pvParameters ) { struct AMessage *pxMessage; uint32_t * test_ptr;
// Create a queue capable of containing 10 pointers to AMessage structures.
// These should be passed by pointer as they contain a lot of data.
xQueue = xQueueCreate( 16, sizeof( unsigned int  ) );
if( xQueue == 0 )
{
    // Failed to create the queue.
}



// ...



// Send a pointer to a struct AMessage object.  Don't block if the
// queue is already full.
pxMessage = & xMessage;
test_ptr= &test_var;
xQueueSend( xQueue, ( void * )/* &pxMessage*/&test_ptr, ( TickType_t ) 0 );



// ... Rest of task code.
} vADifferentTask is a task that read the queue: void vADifferentTask( void *pvParameters ) { struct AMessage *pxRxedMessage; //uint32_t test_rx_ptr;
 if( xQueue != 0 )
 {
     // Receive a message on the created queue.  Block for 10 ticks if a
     // message is not immediately available.
     if( xQueueReceive( xQueue,/* &( pxRxedMessage )*/&test_rx_ptr, ( TickType_t ) 10 ) )
     {
         // pcRxedMessage now points to the struct AMessage variable posted
         // by vATask.
     }
 }
and here are the tasks creation OSATaskCreate(tasktwo,”tasktwo”,TASKONESTACKSIZE,NULL,6u,NULL,false,&tasktwotaskhandler); OSATaskCreate(vATask,”vATask”,TASKONESTACKSIZE,NULL,5u,NULL,false,&vATasktaskhandler); OSATaskCreate(vADifferentTask,”vADifferentTask”,TASKONESTACKSIZE,NULL,5u,NULL,false,&vADifferentTasktask_handler);
//create task
 OSA_TaskCreate(task_one,
        (uint8_t *)"task_one",
        TASK_ONE_STACK_SIZE,
        NULL,
        TASK_ONE_PRIO,
        (task_param_t)0,
        false,
        &task_one_task_handler);
what happens is it goes through thevATask and as soon as it returns it hits an eception which I believe is the HARD FAULT eception as my targer is an ARM CORTEX M4. Sorry about the long post , but I need help.In the mean time I try to figure what I’m doing wrong. Thanks, Koorosh Hajiani

xQueueCreate

Futther debugging ,reveals the following: HEAP1 is selected. From OSSTART() leads to xPortStartScheduler(void) and that calls **vPortStartFirstTask() ** and that is shown below: ~~~~

if (configCOMPILER==configCOMPILERARMGCC)

void vPortStartFirstTask(void) { __asm volatile ( ” ldr r0, =0xE000ED08 n” /* Use the NVIC offset register to locate the stack. / ” ldr r0, [r0] n” / load address of vector table / ” ldr r0, [r0] n” / load first entry of vector table which is the reset stack pointer / ” msr msp, r0 n” / Set the msp back to the start of the stack. / ” cpsie i n” / Globally enable interrupts. / ” svc 0 n” / System call to start first task. */ ” nop n” ); }

endif

~~~~ now at this point the stack points to a portion of RAM which is not allocated for the stack by the linker .eventhough the first task that creats a Queue and post a variable to it is called and correctly does what is supposed to do , however the return address is corrupted and the Arm triggeres a trap(Hard Fault) in this case. Why is Stack pointer changing by vPortStartFirstTas? Thanks, Koorosh Hajiani

xQueueCreate

Every task has a unique stack allocated from the heap so the stack pointer is changed each time the running task is changed.