AnsweredAssumed Answered

CW MobileGT - Blocking Serial Read() Call Doesn't Cause Task To Block?

Question asked by Tim Hutchinson on Aug 22, 2011
Latest reply on Aug 24, 2011 by Tim Hutchinson

Hi All, I have a task created to receive data on a serial port that calls read() in an endless loop and stores any received data in a buffer for later processing. The problem is, even though the read() call is blocking, when it blocks while waiting for data, the task that called it doesn't block and return me to the main task.

 

 

Here's the task list template with the two tasks defined

TASK_TEMPLATE_STRUCT MQX_template_list[] = {   /* Task number, Entry point, Stack, Pri, String,    Auto? */  {MAIN_TASK,     Main_task,   3000,  10,  "main",    MQX_AUTO_START_TASK},  {SERIALRX_TASK, MAIN_SerialRxTask, 3000, 9, "serx", 0},  {            0,         0,      0,   0,          0,0}};

 

Here, the main task creates a task to receive serial data (then goes on to waste time in a loop)

void Main_task(uint_32 initial_data)    {    _task_id tiNewTaskID;    uint_32 iWasteTime;    tiNewTaskID = _task_create(0, SERIALRX_TASK, 0);    if(tiNewTaskID == MQX_NULL_TASK_ID)        _time_delay(1);    iWasteTime = 0;    while(1)        {        iWasteTime++;        if(iWasteTime >= 100)            iWasteTime = 0;        }    _mqx_exit(0);    }

 

  

The serialRx task runs, and read() blocks until data is received but MQX doesn't ever return back to the main task (main and this task are the only 2 tasks). I've done a similar thing with RTCS with blocking calls in a task to read IP data and when the recv() function is blocked waiting for data, MQX blocks the task to let other tasks run, any idea why not here? I've tried various priority levels for the two tasks (doesn't seem to make a difference).

void MAIN_SerialRxTask(uint_32 initial_data)    {    int_32 iCount;    uint_32 baud=57600;    unsigned char data_buffer[2048];    FILE_PTR serial_fd =  fopen("ttyj:", IO_SERIAL_RAW_IO);    ioctl(serial_fd, IO_IOCTL_SERIAL_SET_BAUD, &baud);    while(1)        {        //Function blocks but doesn't cause MQX to return to main task        iCount = read(serial_fd, data_buffer, 1);        if(iCount == IO_ERROR)    //Error check            break;        //Write the recvd data back out the same port        write(serial_fd, data_buffer, iCount);           }    fclose(serial_fd);    return;    }

 

 

I had thought that when a task called a blocking function (and that function blocked) that the task would block until the function was ready to go again. Do I have that wrong? Is there something specific about the read() function that won't let the task block? Perhaps there's a better way of setting up a task to receive incoming serial data that will be processed in another task? The serial port is operating in polled (verses interrupt) mode and it works properly, I just can't figure out why the task doesn't block when the read() function does.

 

The code is running on MQX 3.7 and the Tower MPC5125 board also using the Tower Serial Module. Thanks in advance for any suggestions or advice.

 

~Tim

 

 

 

 

 

 

 

 

 

 

 

Outcomes