Quality RTOS & Embedded Software

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




Loading

Queue/malloc/pointer...whats wrong this this?

Posted by Toan Pham on June 5, 2009
Hi

I am trying to get freertos queue/messaging to work.
As an experiment, i create a task that dynamicly allocate
memory, copy a string to this memory sector; and post to
a queue by pointer. The default webserver demo task reads
all the elements in this queue, output to the webpage, and
free the memory block which was allocated on the other task.

here is the code, please advise on what i did wrong. thank you.



char * Element;

xQueueHandle prvMainQueue;

static void vTaskTest(void *pvParameters)
{
/* The parameters are not used. */
( void ) pvParameters;


for( ;; )
{
// post a message every 3 sec.
vTaskDelay(3000/portTICK_RATE_MS);
char *tmp ;
tmp = malloc(10);
if(tmp != NULL){
strcpy(tmp,"test12345");
xQueueSend(prvMainQueue,&tmp,10);
}

}
} */



int main( void )
{
/* Setup the ports. */
prvSetupHardware();

prvMainQueue = xQueueCreate(10,sizeof(Element));

/* Setup lwIP. */
vlwIPInit();

/* Create the lwIP task. This uses the lwIP RTOS abstraction layer.*/
sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );

xTaskCreate(vTaskTest, ( signed portCHAR * ) "Test", configMINIMAL_STACK_SIZE * 2, NULL, 0, NULL );

/* Start the check task - which is defined in this file. */
xTaskCreate( vErrorChecks, ( signed portCHAR * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );

/* Finally, start the scheduler.

NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
The processor MUST be in supervisor mode when vTaskStartScheduler is
called. The demo applications included in the FreeRTOS.org download switch
to supervisor mode prior to main being called. If you are not using one of
these demo application projects then ensure Supervisor mode is used here. */
vTaskStartScheduler();

/* Should never get here! */
return 0;
}




static void vProcessConnection( struct netconn *pxNetCon )
{
static portCHAR cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];
struct netbuf *pxRxBuffer;
portCHAR *pcRxString;
unsigned portSHORT usLength;
static unsigned portLONG ulPageHits = 0;

/* We expect to immediately get data. */
pxRxBuffer = netconn_recv( pxNetCon );

if( pxRxBuffer != NULL )
{
/* Where is the data? */
netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );

/* Is this a GET? We don't handle anything else. */
if( !strncmp( pcRxString, "GET", 3 ) )
{
pcRxString = cDynamicPage;

/* Update the hit count. */
ulPageHits++;
sprintf( cPageHits, "%lu", ulPageHits );

/* Write out the HTTP OK header. */
netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );

/* Generate the dynamic page...

... First the page header. */
strcpy( cDynamicPage, webHTML_START );
/* ... Then the hit count... */
strcat( cDynamicPage, cPageHits );
strcat( cDynamicPage, "<p><pre>Task State Priority Stack#<br>************************************************<br>" );
/* ... Then the list of tasks and their status... */
vTaskList( ( signed portCHAR * ) cDynamicPage + strlen( cDynamicPage ) );

char *e;
while(xQueueReceive(prvMainQueue,&e,10)!=pdFAIL)
{
strcat( cDynamicPage,e);
free(e);
}

/* ... Finally the page footer. */
strcat( cDynamicPage, webHTML_END );

/* Write out the dynamically generated page. */
netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );
}

netbuf_delete( pxRxBuffer );
}

netconn_close( pxNetCon );
}

RE: Queue/malloc/pointer...whats wrong this t

Posted by Dave on June 5, 2009
I think you need to cut down the number of things you are doing at once. I cannot see anything wrong with the queue usage, but suspect you will see problems because you are blocking and waiting in the middle of processing a TCP frame which could cause strange timing effects at the TCP protocol level.

Try getting the queues working in a simpler example (without the WEB server) first.

RE: Queue/malloc/pointer...whats wrong this this?

Posted by Toan Pham on June 5, 2009


the code above output weird characters on the html webpage every three seconds,
so i assume that the webservice task is working properly.
one thing that can possibly go wrong would be the call to strcpy, which did not actually replicate the data.

i also tried a simplier example which output to a serial port instead of a webpage, and that too did not work.

please help.

i am stuck on this matter for days.


RE: Queue/malloc/pointer...whats wrong this t

Posted by Richard on June 5, 2009
I just tried this http://pastebin.com/m5594b168 and it worked as expected (real mode x86 OpenWatcom).

Regards.

RE: Queue/malloc/pointer...whats wrong this t

Posted by Toan Pham on June 5, 2009
richard,

Thank you for your help.

I changed malloc to pvportmalloc and free to vportfree and it worked.
I guess using the ported API is important, not the generic C API.

**I can't believe I struggled 2-3 days over something as simple as this.

thanks again.



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




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

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

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