Created
June 7, 2018 18:57
-
-
Save tnishinaga/13389c9fca6d33257b99fc6ac59a5138 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author Toshifumi NISHINAGA < tnishinaga.dev AT gmail DOT com > | |
// License: GPLv2 | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <ftdi.h> | |
static inline void ftdi_bitmode_fail(struct ftdi_context *ftdi, int f) | |
{ | |
fprintf(stderr, "ftdi_set_bitmode failed, error %d (%s)\n", f, ftdi_get_error_string(ftdi)); | |
ftdi_usb_close(ftdi); | |
ftdi_free(ftdi); | |
exit(-1); | |
} | |
int main(void) | |
{ | |
struct ftdi_context *ftdi; | |
int f; | |
ftdi = ftdi_new(); | |
if (ftdi == 0) { | |
fprintf(stderr, "ftdi new failed\n"); | |
return EXIT_FAILURE; | |
} | |
// open ft230x | |
f = ftdi_usb_open(ftdi, 0x0403, 0x6015); | |
if (f < 0 && f != -5) { | |
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi)); | |
ftdi_free(ftdi); | |
exit(-1); | |
} | |
printf("ftdi open succeeded: %d\n",f); | |
for(int i = 0; i < 10; i++) { | |
// set bitmask | |
// all output, all high | |
printf("Set high\n"); | |
f = ftdi_set_bitmode(ftdi, 0xff, BITMODE_CBUS); | |
if (f < 0){ | |
ftdi_bitmode_fail(ftdi, f); | |
} | |
sleep(1); | |
// all output, all low | |
printf("Set low\n"); | |
f = ftdi_set_bitmode(ftdi, 0xf0, BITMODE_CBUS); | |
if (f < 0){ | |
ftdi_bitmode_fail(ftdi, f); | |
} | |
sleep(1); | |
} | |
printf("finish\n"); | |
ftdi_disable_bitbang(ftdi); | |
ftdi_usb_close(ftdi); | |
ftdi_free(ftdi); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
gcc -o cbus_led_blink cbus_led_blink.c -std=c11 | |
./cbus_led_blink |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment