5

I just bought the UM232R USB Serial UART Development Module which uses a FT232RL chip to emulate a UART-like interface over USB.
I actually just bought this complicated module for a very simple purpose: to trigger a very simple LED circuit that I built myself. So all I want is to "bit-bang" the first bit-bangable pin "CB0" (pin 23) [see page 8/9 in the datasheet] of the module. Using C++ or AHK (or maybe Python, even though I don't really know it), it doesn't really matter. And it needs to run on Windows.

What I've tried so far:
I found a nice tutorial on how to bit-bang FTDI devices. But first of all I installed the VCP driver or to be more accurate the "setup executable" on the very right of the table. This installed not only the VCP driver but also the D2XX driver. Then I downloaded the D2XX driver as a zip (the one for windows).

Okay, then:

  • I created a new Visual C++ project (Win32 Console Application with precompiled header).
  • I extracted the D2XX driver in the project folder.
  • I added the ftd2xx.h header file to the project.
  • I took this piece of code from the mentioned tutorial and modified it to this:

(I actually just added windows.h, stdafx.h and modified to #include <ftd2xx.h> #include "ftd2xx.h")

/* 8-bit PWM on 4 LEDs using FTDI cable or breakout.
   This example uses the D2XX API.
   Minimal error checking; written for brevity, not durability. */

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "ftd2xx.h"

#define LED1 0x08  /* CTS (brown wire on FTDI cable) */
#define LED2 0x01  /* TX  (orange) */
#define LED3 0x02  /* RX  (yellow) */
#define LED4 0x14  /* RTS (green on FTDI) + DTR (on SparkFun breakout) */

int _tmain(int argc, _TCHAR* argv[])
{
    int i,n;
    unsigned char data[255 * 256];
    FT_HANDLE handle;
    DWORD bytes;

    /* Generate data for a single PWM 'throb' cycle */
    memset(data, 0, sizeof(data));
    for(i=1; i<128; i++) {
        /* Apply gamma correction to PWM brightness */
        n = (int)(pow((double)i / 127.0, 2.5) * 255.0);
        memset(&data[i * 255], LED1, n);         /* Ramp up */
        memset(&data[(256 - i) * 255], LED1, n); /* Ramp down */
    }   

    /* Copy data from first LED to others, offset as appropriate */
    n = sizeof(data) / 4;
    for(i=0; i<sizeof(data); i++)
    {
        if(data[i] & LED1) {
            data[(i + n    ) % sizeof(data)] |= LED2;
            data[(i + n * 2) % sizeof(data)] |= LED3;
            data[(i + n * 3) % sizeof(data)] |= LED4;
        }
    }   

    /* Initialize, open device, set bitbang mode w/5 outputs */
    if(FT_Open(0, &handle) != FT_OK) {
        puts("Can't open device");
        return 1;
    }
    FT_SetBitMode(handle, LED1 | LED2 | LED3 | LED4, 1);
    FT_SetBaudRate(handle, 9600);  /* Actually 9600 * 16 */

    /* Endless loop: dump precomputed PWM data to the device */
    for(;;) FT_Write(handle, &data, (DWORD)sizeof(data), &bytes);

    return 0;
}

(If I'm not mistaken, this example program should trigger every (or most of the) bit-bangable pins on my device.) But when I tried to build it, I got some weird linker errors:

1>------ Build started: Project: FTDI-Project, Configuration: Debug Win32 ------
1>  FTDI-Project.cpp
1>FTDI-Project.obj : error LNK2019: unresolved external symbol __imp__FT_Write@16 referenced in function _wmain
1>FTDI-Project.obj : error LNK2019: unresolved external symbol __imp__FT_SetBaudRate@8 referenced in function _wmain
1>FTDI-Project.obj : error LNK2019: unresolved external symbol __imp__FT_SetBitMode@12 referenced in function _wmain
1>FTDI-Project.obj : error LNK2019: unresolved external symbol __imp__FT_Open@8 referenced in function _wmain
1>C:\Users\username\Documents\Visual Studio 2010\Projects\FTDI-Project\Debug\FTDI-Project.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

It all just seems incredibly complicated to me. I hope someone of you can help me with this. I would really appreciate it!

Forivin
  • 14,780
  • 27
  • 106
  • 199
  • you must post this question for electrical.stackexchange – Chani Jul 08 '14 at 15:19
  • 2
    @Wildling I don't know about *must*, but it's actually http://electronics.stackexchange.com – kenny Jul 08 '14 at 15:22
  • 1
    Hmm, but this is about the programming side of the project not about building a circuit or so. – Forivin Jul 08 '14 at 15:31
  • 1
    By the way, it's really nice that you gave some context for this question by telling us what hardware you have, what you are trying to do, a basic outline of what steps you followed, and what tutorial you are following. I think that's why you got upvoted so many times. – David Grayson Jul 09 '14 at 01:33

1 Answers1

2

Since you are getting unresolved reference errors for FTDI library functions, you should check the linker settings for your project and make sure you are somehow telling the linker where to find the FTDI library. There is probably a file provided by FTDI whose name ends in ".lib" and you need to add it to the "Additional Linker Inputs" list.

Also you seem to be confused about whether you are using the VCP driver or the D2xx driver for this device. You can't use both, and you should make sure you are using the D2xx driver by inspecting the device in the device manager or else you will get runtime errors down the road.

Also, please note that tutorial is 5 years old so you might have to refer to actual documentation from FTDI to get updated information.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • There actually are 4 lib files that came with the d2xx driver zip. They are all called "ftd2xx.lib". They are located in "\amd64", "\i386", "\static\amd64" and "\static\i386". Which one should I take? (I'm on Win8 x64 with an Intel i7 processor.) – Forivin Jul 08 '14 at 15:44
  • 1
    It depends how you are compiling your program. If you are making a 32-bit executable (which is what I would recommend because it will run on both 32-bit and 64-bit machines), use an i386 library. If you are making a 64-bit executable, use an amd64 library. I presume the "static" library doesn't depend on any FTDI DLLs so that would be a good one to try first, but it will increase the size of your executable. – David Grayson Jul 08 '14 at 17:11
  • I tried it with the static/i386 lib file and it didn't work (same errors), probably because i did something wrong... Then I tried it with the i386 directory which had multiple files in it (some dlls and sys files and the lib file) and added them to the project just like you have to do it with libraries like curl and it worked fine. :) – Forivin Jul 09 '14 at 13:49