Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bit-banging with USB to Serial UART

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!

like image 389
Forivin Avatar asked Oct 31 '22 20:10

Forivin


1 Answers

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.

like image 124
David Grayson Avatar answered Nov 04 '22 23:11

David Grayson