[En-Nut-Discussion] USART _read Problem

Jason Walton walton.jason at gmail.com
Tue Jan 9 21:02:38 CET 2007


Hello Everyone,

My application includes a thread created in the main thread whose sole
purpose is to _read() from UART1 utilizing the devUsartAvr1 Device.  I've
set the Receive Timeout to 150ms via the _ioctl function and the appropriate
UART_SETREADTIMEOUT Flag; however, the thread does not return from the
_read() call until ~60-65 seconds later in the event of "No Data".  I've
also read the timeout from the uart using the UART_GETREADTIMEOUT Flag and
it has been set to 150ms.  I can only assume that there is an uninitialized
timeout (i.e. 0xFFFF ~= 65535ms) somewhere, but I don't see any other
timeouts to init.  Attached is the source for the thread.  Does anyone see
something I may have missed.

//=======================================
struct
{
    FILE* File_Handle;
    unsigned char* glob_buff;
    unsigned char* inbuf;
    int glob_buff_bytecount;
    int glob_bytes_this_read;
    HANDLE glob_data_ready;
    unsigned long baud;
    unsigned char data_bits;
    unsigned char parity;
    unsigned char stop_bits;
    unsigned short read_timeout;

}UART1_PARAMS;
//===================================
THREAD(Uart1_Rx_Thread, arg)
{

    int return_value;
    int byte_count;

    NutRegisterDevice(&devUsartAvr1,0,0);

    UART1_PARAMS.glob_buff = (unsigned char*) NutHeapAllocClear( 350 );
    UART1_PARAMS.inbuf = (unsigned char*) NutHeapAllocClear( 128 );

    UART1_PARAMS.File_Handle = fopen("uart1", "r+b");
    UART1_PARAMS.data_bits     = 8;
    UART1_PARAMS.stop_bits    = 1;
    UART1_PARAMS.parity = 0;
    UART1_PARAMS.enable = 1;

    UART1_PARAMS.baud = 115200;
    UART1_PARAMS.read_timeout = 150;

    _ioctl(_fileno(UART1_PARAMS.File_Handle), UART_SETDATABITS,
&UART1_PARAMS.data_bits);     //Set the data bits to 8
    _ioctl(_fileno(UART1_PARAMS.File_Handle), UART_SETPARITY,
&UART1_PARAMS.parity);     //Set the Parity to None
    _ioctl(_fileno(UART1_PARAMS.File_Handle), UART_SETSTOPBITS,
&UART1_PARAMS.stop_bits);     //Set Stop Bits to 1

    _ioctl(_fileno(UART1_PARAMS.File_Handle), UART_SETSPEED,
&UART1_PARAMS.baud); //Set the Baud Rate
    _ioctl(_fileno(UART1_PARAMS.File_Handle), UART_SETREADTIMEOUT,
&UART1_PARAMS.read_timeout); //Set The Receive Timeout

    NutThreadSetPriority(100);

    for(;;)
    {
            byte_count =
_read(_fileno(UART1_PARAMS.File_Handle),&UART1_PARAMS.inbuf[0],128);

            if( byte_count <= 0 )
                continue;
            else
            {
               ...Other Code

             }

    }

Thanks in advance,

Jason



More information about the En-Nut-Discussion mailing list