title: Using FTDI D2XX drivers with Python from Raspberry Pi on raspbian soft-float
body: <p>We have a USB device controlled by FTDI's D2XX drivers. It's currently controlled from a Windows machine via a Python interface and as a fun project I tried moving the control to a Raspberry Pi (about 1/10th the cost of a PC, not including the OS cost).</p>
  
  <p>There were many hurdles to clear, but after a few weeks I finally found all the answers and got it working. The answers were spread across several forums so as a thank you to the Stack&nbsp;Overflow community I thought I'd consolidate them here.</p>
  
  <p>First, the project required:</p>
  
  <ul>
  <li>An operating system: I picked up the latest <a href="https://en.wikipedia.org/wiki/Debian#Release_timeline" rel="nofollow noreferrer">"Wheezy"</a> Raspbian (hard-float 2012-12-16) from the <a href="http://www.raspberrypi.org/downloads" rel="nofollow noreferrer">Raspberry Pi webpage</a>. This included Python.</li>
  <li>The <a href="http://www.ftdichip.com/Drivers/D2XX.htm" rel="nofollow noreferrer">FTDI D2XX drivers</a> to talk to our FTDI device.</li>
  <li>A Python interface to the D2XX driver. I used <a href="http://bleyer.org/pyusb/" rel="nofollow noreferrer">PyUSB</a></li>
  <li>Our Python script</li>
  </ul>
  
  <p>I downloaded the Wheezy distribution and used Win32DiskImager to write to a 4&nbsp;GB SD card. The Raspberry Pi booted with no problems. I then unpacked the D2XX library (libftd2xx.so) and installed it into <code>/usr/local/lib</code>.</p>
  
  <p>PyUSB (1.6) currently is tested only against Windows, but they provide the source code. It's pretty straightforward to compile a copy for Raspberry Pi. Basically, modify setup.py to link to the libftd2xx.so library (no need to copy it). Also edit d2xx/_d2xx.c to comment out the routines with no Linux implementation (currently ftobj_Rescan, ftobj_Reload ftobj_GetComPortNumber). Copy WinTypes.h and ftd2xx.h from the FTDI D2XX driver download (in the release) directory into ftdi-win32 and run <code>python setup.py install</code> which will compile and install the Python module.</p>
  
  <p>Once all that was done I wrote a simple Python script to talk to the FTDI chip. Note you need to run via sudo.</p>
  
  <pre><code>import d2xx
  jd = d2xx.open(0)
  pd = jd.eeRead()
  print pd
  </code></pre>
  
  <p>The d2xx module could not seem to find the libftd2xx.so file. So, I tweaked the setup.py script to link to the static copy of the library, libftd2xx.a. Voila, I had my first clue of the problem: The D2XX library was built using soft-float, and my Wheezy distribution was configured to use floating point registers. That is, the gcc on my system generated code that was binary incompatible with the D2XX libraries and would not allow them to be linked to.</p>
  
  <p>To fix this I downloaded the <a href="http://www.raspberrypi.org/downloads" rel="nofollow noreferrer">soft-float debian "wheezy" distribution</a> (2012-08-08) and wrote to the 4&nbsp;GB SD card. This time the image would not boot. After looking around I found this <a href="https://raspberrypi.stackexchange.com/questions/2140/cant-boot-wheezy">helpful answer</a>. In short, there's a problem with the boot image for the soft-float so that for some Raspberry Pi boards, it won't boot. The solution is to replace the start.elf file on the soft-float distribution with one that does work e.g. a copy from the hard-float Raspbian image.  Fortunately, the SD card has two partitions: a FAT one and an ext3(?) one. The boot image is on the FAT partition, so it was trivial to pop the hard-float SD card into a Windows box, copy the start.elf file, pop in the soft-float SD card and update its start.elf with the hard-float one. After that, the Raspberry Pi booted no problem.</p>
  
  <p>After installing FTDI's D2XX drivers and building a d2xx Python module from PyUSB, I tried the test script again. Again it failed. The d2xx module could read the libftd2xx.so library no problem, but for some reason could just not talk to the device.</p>