Bitbang speed/latency

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Bitbang speed/latency

Peter Smith
Hi,

I'm using the Device::FTDI Perl wrapper for libftdi to communicate
with an FT230X, which in turn is driving 16 cascaded shift registers.
The communication speed is decent, but I'm hoping for more if
possible.

Here's my code:

use Device::FTDI;

##  0x08 CTS - LATCH
##  0x01 TX - DATA
##  0x02 RX - CLOCK

$dev = Device::FTDI->new('vendor' => 0x0403, 'product' => 0x6015) ||
print $!;
$dev->set_bitmode(0xff, 0x01);

   for (1...128) {
        $dev->write_data(0x01);
        $dev->write_data(0x03);
   }

   $dev->write_data(0x08);
   $dev->write_data(0x00);

I appreciate most of you will be using the libftdi C library, but
Device::FTDI is little more than a wrapper, and hopefully the code
will make sense to non-Perl programmers.

Anyway, the code works fine but it consistently takes around 250ms to
run, which means around 1ms per write. That's just about OK for me
needs, but faster would be better, as it means quarter of a second to
set every bit on my 128 bit shift register monster.

I'm trying to figure out where the delays are occuring. I've
experimented with setting the baud rate, latency timer and write chunk
size, but none of these seemed to have any impact.

I'm running Linux on x86, and the FT chip seems to be using USB 2.

Cheers,
Peter Smith






--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Bitbang speed/latency

Jim Paris
Peter Smith wrote:

> Hi,
>
> I'm using the Device::FTDI Perl wrapper for libftdi to communicate
> with an FT230X, which in turn is driving 16 cascaded shift registers.
> The communication speed is decent, but I'm hoping for more if
> possible.
>
> Here's my code:
>
> use Device::FTDI;
>
> ##  0x08 CTS - LATCH
> ##  0x01 TX - DATA
> ##  0x02 RX - CLOCK
>
> $dev = Device::FTDI->new('vendor' => 0x0403, 'product' => 0x6015) ||
> print $!;
> $dev->set_bitmode(0xff, 0x01);
>
>    for (1...128) {
>         $dev->write_data(0x01);
>         $dev->write_data(0x03);
>    }
>
>    $dev->write_data(0x08);
>    $dev->write_data(0x00);

Try something like this (untested):

$data = '';
for (1...128) {
    $data .= chr(0x01);
    $data .= chr(0x03);
}
$data .= chr(0x08);
$data .= chr(0x00);
$dev->write_data($data);

Jim

>
> I appreciate most of you will be using the libftdi C library, but
> Device::FTDI is little more than a wrapper, and hopefully the code
> will make sense to non-Perl programmers.
>
> Anyway, the code works fine but it consistently takes around 250ms to
> run, which means around 1ms per write. That's just about OK for me
> needs, but faster would be better, as it means quarter of a second to
> set every bit on my 128 bit shift register monster.
>
> I'm trying to figure out where the delays are occuring. I've
> experimented with setting the baud rate, latency timer and write chunk
> size, but none of these seemed to have any impact.
>
> I'm running Linux on x86, and the FT chip seems to be using USB 2.
>
> Cheers,
> Peter Smith
>
>
>
>
>
>
> --
> libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
> To unsubscribe send a mail to [hidden email]  

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Bitbang speed/latency

Peter Smith
> Peter Smith wrote:
>> Hi,
>>
>> I'm using the Device::FTDI Perl wrapper for libftdi to communicate
>> with an FT230X, which in turn is driving 16 cascaded shift
>> registers.
>> The communication speed is decent, but I'm hoping for more if
>> possible.
>>
>> Here's my code:
>>
>> use Device::FTDI;
>>
>> ##  0x08 CTS - LATCH
>> ##  0x01 TX - DATA
>> ##  0x02 RX - CLOCK
>>
>> $dev = Device::FTDI->new('vendor' => 0x0403, 'product' => 0x6015) ||
>> print $!;
>> $dev->set_bitmode(0xff, 0x01);
>>
>>    for (1...128) {
>>         $dev->write_data(0x01);
>>         $dev->write_data(0x03);
>>    }
>>
>>    $dev->write_data(0x08);
>>    $dev->write_data(0x00);
>
> Try something like this (untested):
>
> $data = '';
> for (1...128) {
>     $data .= chr(0x01);
>     $data .= chr(0x03);
> }
> $data .= chr(0x08);
> $data .= chr(0x00);
> $dev->write_data($data);
>
> Jim
>

Hi Jim,

It's actually one of your MicroFTX breakout boards that I'm using
(cheers - they arrived yesterday).

I've tried your code, but no joy - I suspect because it's now going
too fast. Because the FTX is talking to shift registers I need to set
my data pin high/low, then pulse the clock, then set the data pin,
then pulse the clock etc etc. Previously I had some usleep statements
in my code to sleep for a few ms between writes, but because of the
latency they're not needed. The clock pulses need to be a minimum of
40ns.

Thanks,
Pete




--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Bitbang speed/latency

Rogier Wolff
On Sun, Mar 08, 2015 at 01:21:44AM -0000, Peter Smith wrote:

> > Peter Smith wrote:
> >> Hi,
> >>
> >> I'm using the Device::FTDI Perl wrapper for libftdi to communicate
> >> with an FT230X, which in turn is driving 16 cascaded shift
> >> registers.
> >> The communication speed is decent, but I'm hoping for more if
> >> possible.
> >>
> >> Here's my code:
> >>
> >> use Device::FTDI;
> >>
> >> ##  0x08 CTS - LATCH
> >> ##  0x01 TX - DATA
> >> ##  0x02 RX - CLOCK
> >>
> >> $dev = Device::FTDI->new('vendor' => 0x0403, 'product' => 0x6015) ||
> >> print $!;
> >> $dev->set_bitmode(0xff, 0x01);
> >>
> >>    for (1...128) {
> >>         $dev->write_data(0x01);
> >>         $dev->write_data(0x03);
> >>    }
> >>
> >>    $dev->write_data(0x08);
> >>    $dev->write_data(0x00);
> >
> > Try something like this (untested):
> >
> > $data = '';
> > for (1...128) {
> >     $data .= chr(0x01);
> >     $data .= chr(0x03);
> > }
> > $data .= chr(0x08);
> > $data .= chr(0x00);
> > $dev->write_data($data);
> >
> > Jim
> >
>
> Hi Jim,
>
> It's actually one of your MicroFTX breakout boards that I'm using
> (cheers - they arrived yesterday).
>
> I've tried your code, but no joy - I suspect because it's now going
> too fast. Because the FTX is talking to shift registers I need to set
> my data pin high/low, then pulse the clock, then set the data pin,
> then pulse the clock etc etc. Previously I had some usleep statements
> in my code to sleep for a few ms between writes, but because of the
> latency they're not needed. The clock pulses need to be a minimum of
> 40ns.

The new code should work. Try setting the baud rate. All FTDI chips use
this in "bitbang" mode to pace the outputs.

That said, as far as I can see your current code is shifting all
"ones" on the data-bit D0. When actually doing data you need to
prepare the data on the right clock edge. So

   if (databit[i]) {
         $data .= chr (0x01);
         $data .= chr (0x03);
   } else {
         $data .= chr (0x00);
         $data .= chr (0x02);
   }

inside the loop may need to become:

   if (databit[i]) {
         $data .= chr (0x03);
         $data .= chr (0x01);
   } else {
         $data .= chr (0x02);
         $data .= chr (0x00);
   }

with "half" an iteration moved to BEFORE the loop and half
AFTER the loop.

        Roger.

--
** [hidden email] ** http://www.BitWizard.nl/ ** +31-15-2600998 **
**    Delftechpark 26 2628 XH  Delft, The Netherlands. KVK: 27239233    **
*-- BitWizard writes Linux device drivers for any device you may have! --*
The plan was simple, like my brother-in-law Phil. But unlike
Phil, this plan just might work.

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Bitbang speed/latency

Peter Smith

> The new code should work. Try setting the baud rate. All FTDI chips
> use
> this in "bitbang" mode to pace the outputs.

Bingo, that was it. I was thinking that the baud rate setting
controlled the transfer rate over the USB link. But of course it's for
the FTDI chip's output. I've set a lower baud rate and it's working
great now - thanks both.


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Loading...