Quality RTOS & Embedded Software

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


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

Posted by Toan Pham on June 5, 2009

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.
char *tmp ;
tmp = malloc(10);
if(tmp != NULL){

} */

int main( void )
/* Setup the ports. */

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

/* Setup lwIP. */

/* 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. */

/* 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. */
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;
strcat( cDynamicPage,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).


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

Posted by Toan Pham on June 5, 2009

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) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists