Skip to content

Instantly share code, notes, and snippets.

@madpilot
Created January 16, 2017 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madpilot/e055a66aa4afc15dceff1b44d754cf26 to your computer and use it in GitHub Desktop.
Save madpilot/e055a66aa4afc15dceff1b44d754cf26 to your computer and use it in GitHub Desktop.
Bit-bang me some binary
/* 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>
#include <time.h>
#define CTS 0x08 /* CTS (brown wire on FTDI cable) */
int main()
{
unsigned char c = 0;
struct ftdi_context ftdic;
unsigned long cpuMask;
cpuMask = 2; // bind to cpu 1
/* 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");
return 1;
}
/* Enable bitbang mode with a single output line */
ftdi_enable_bitbang(&ftdic, CTS);
struct timespec request;
unsigned char train[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1,1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 1 ,1, 1, 1, 1 ,1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
unsigned long len = sizeof(train) / sizeof(train[0]);
unsigned index = 0;
unsigned max = len;
printf("Number of bits: %i\n", max);
for(;;) {
request.tv_sec = 0;
request.tv_nsec = 1800000L;
//c = train[index++];
c = train[index++] == 0 ? CTS : 0;
ftdi_write_data(&ftdic, &c, 1);
while(nanosleep(&request, &request) == -1) {
continue;
}
if(index > max) {
index = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment