Also we will need to get the libftdi devel/libftdi if you don’t have it already.
12
# cd /usr/ports/devel/libftdi# make install clean
The documentation is very helpful when working with libftdi API in C++: libftdi docs.
When you connect the FT232R breakout board via USB, you should be able to see your device as a virtual serial port in my case: /dev/cuaU0 and it was identified as a ugen1.2 device.
123
% dmesg | tail
ugen1.2: < FTDI > at usbus1
uftdi0: < FT232R USB UART > on usbus1
Next let’s try the LED blinker example by Phil at Hack a Day.
I made some changes to the C++ code to be compatible with the latest libftdi 0.20
We will need an LED and a 330 Ohm resistor. Connect the Anode of LED (long lead +ve) to CTS pin on FTDI breakout, and the Cathode (short lead -ve) to the resistor, and the other lead of the resistor will be connected to GND.
Put the following code in a file named hello_ftdi.c
/* hello_ftdi.c: flash LED connected between CTS and GND.This example uses the libftdi API.Minimal error checking; written for brevity, not durability. */#include <stdio.h>#include <ftdi.h>#define LED 0x08 /* CTS */intmain(){unsignedcharc=0;structftdi_contextftdic;/* Initialize context for subsequent function calls */ftdi_init(&ftdic);/* Open FTDI device based on FT232R vendor & product IDs */if(ftdi_usb_open(&ftdic,0x0403,0x6001)<0){puts("Can't open device");return1;}/* Enable bitbang mode with a single output line */ftdi_set_bitmode(&ftdic,LED,BITMODE_BITBANG);/* Endless loop: invert LED state, write output, pause 1 second */for(;;){c^=LED;ftdi_write_data(&ftdic,&c,1);sleep(1);}}
To compile the above code we use the following command:
For now you can have a look on Phil’s post on Hack a Day he explains the code pretty well, the code above is modified and works well with libftdi 0.20, also I’ll post here the PWM LED chaser code in that other article it’s written using D2XX API instead. So I’ll post below a modified version to work with libftdi API.
The hardware setup includes 4 LEDs and 4 330 Ohm resistors, I happened to use an LED bar that I had laying around.
/* pwmchase.c: 8-bit PWM on 4 LEDs using FTDI cable or breakout.This example uses the libftdi API.Minimal error checking; written for brevity, not durability. */#include <stdio.h>#include <string.h>#include <math.h>#include <ftdi.h>#define LED1 0x08 /* CTS */#define LED2 0x01 /* TXD */#define LED3 0x02 /* RXD */#define LED4 0x14 /* RTS + DTR */intmain(){inti,n;unsignedchardata[255*256];structftdi_contextftdic;/* 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 context for subsequent function calls */ftdi_init(&ftdic);/* Open FTDI device based on FT232R vendor & product IDs */if(ftdi_usb_open(&ftdic,0x0403,0x6001)<0){puts("Can't open device");return1;}/* Initialize, open device, set bitbang mode w/5 outputs */ftdi_set_bitmode(&ftdic,LED1|LED2|LED3|LED4,BITMODE_BITBANG);ftdi_set_baudrate(&ftdic,9600);/* Actually 9600 * 16 *//* Endless loop: dump precomputed PWM data to the device */for(;;)ftdi_write_data(&ftdic,data,sizeof(data));}