tpham3783 wrote on Friday, June 05, 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 );
}