Skip to content

Instantly share code, notes, and snippets.

@nonchip
Created January 8, 2017 19:17
Show Gist options
  • Save nonchip/f40e73cd1b08dabecd2c391fe68273a1 to your computer and use it in GitHub Desktop.
Save nonchip/f40e73cd1b08dabecd2c391fe68273a1 to your computer and use it in GitHub Desktop.
libftdi bitbanged clocksource
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
//#include <ftd2xx.h>
#include <errno.h>
//#include <asm/errno.h>
#include <libftdi1/ftdi.h>
#include <time.h>
#include "pins.h"
#define PIN PIN_TX
int main(int argc, char const *argv[])
{
if(argc < 2){
printf("Usage: %s <frequency>\n", argv[0]);
printf("Bitbangs a clock signal of <frequency> Hz (>1Hz <1GHz) on GND<TXD<VCC\n");
return EINVAL;
}
unsigned long freq = strtoul(argv[1],NULL,0);
if(freq == 0 || errno==ERANGE){
printf("Could not parse frequency: %s\n", argv[1]);
return EINVAL;
}
struct timespec sleeptime;
sleeptime.tv_sec=0;
sleeptime.tv_nsec = 500000000L / freq;
unsigned char buf=0;
struct ftdi_context ftdic;
ftdi_init(&ftdic);
if(ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0) {
printf("Can't open device\n");
return ENODEV;
}
if(ftdi_set_bitmode(&ftdic, PIN,BITMODE_BITBANG)<0) {
printf("Can't set bitbang mode\n");
return EIO;
}
for(;;) {
buf ^= PIN;
ftdi_write_data(&ftdic, &buf, 1);
//putchar('.');
nanosleep(&sleeptime,NULL);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment