libftdi Archives

Subject: Re: C++ flush() Never Flushes RX

From: Eric Schott <eric@xxxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 16 Jan 2017 16:51:55 -0500
On Mon, Jan 16, 2017 at 2:46PM, Eric Schott <eric@xxxxxxxxxxxxxx> wrote


The two additional changes (below) make for a better solution.

> The file ftdi.hpp contains
>      enum Direction
>      {
>          Input,
>          Output
>      };
>  
> The file ftdi.cpp contains
>   int Context::flush(int mask)
>   {
>       int ret = 1;
> 
>       if (mask & Input)
>           ret &= ftdi_usb_purge_rx_buffer(d->ftdi);
>       if (mask & Output)
>          ret &= ftdi_usb_purge_tx_buffer(d->ftdi);
>
>      return ret;
>   }

The "&=" is inappropriate as a failure with the rx flush would be
masked if the tx flush was successful.  A better fix is to use the
additional purge function in libftdi.c like:

int Context::flush(int mask)
{
    switch (mask)
    {

    case Input:
        return ftdi_usb_purge_rx_buffer(d->ftdi);

    case Output:
        return ftdi_usb_purge_tx_buffer(d->ftdi);

    case Input | Output:
        return ftdi_usb_purge_buffers(d->ftdi);

    case 0:
        return 0;

    default:
        d->ftdi->error_str = "Wrong arguments";
        return -101;
    }
}

> Since the enumeration assigns the value "0" to Input, the input queue is
> never flushed.
> The enumeration should be something like
> 
> The file ftdi.hpp contains
>      enum Direction
>      {
>          Input  = 0x1,
>          Output = 0x2
>      };

To allow previously compiled programs to work with a corrected shared
library, the enumeration should be

    enum Direction
    {
        Input  = 0x2,
        Output = 0x1
    };


                 Old API             New API
                 ----------------    -----------------
Arg as coded     Val  Behavior       Val  Behavior
--------------   ---  -----------    ---  ------------
<zero>           0x0  No action      0x0  No action
Input            0x0  No action      0x2  Rx Flushed
Output           0x1  Out Flushed    0x1  Tx Flushed
Input | Output   0x1  Out Flushed    0x3  Both Flushed


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread