From 79859f2d9d2aaa75282e8eb8fc6a4de625a27959 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Sat, 13 Jul 2019 19:17:18 +0200 Subject: [PATCH] short ftdi bit bang test --- Makefile | 7 ++++++- src/main.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 48e6595..f76d4f5 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,11 @@ BIN_DIR = bin INCLUDES := inc +#Alpine Linux ftdi include path +INCLUDES += /usr/include/libftdi1 +LIBS := ftdi + +LD_FLAGS := C_FLAGS := -O0 -g CPP_FLAGS := $(addprefix -I, $(INCLUDES)) @@ -27,7 +32,7 @@ all: $(TARGET) $(TARGET): $(OBJS) $(THIS_MAKEFILE) @mkdir -p $(BIN_DIR) - $(CC) $(LD_FLAGS) $(OBJS) $(LIBS) -o $@ + $(CC) $(C_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@ .PRECIOUS: $(OBJ_DIR)/%.d $(OBJ_DIR)/%.d: $(SRC_DIR)/%.c diff --git a/src/main.c b/src/main.c index 6af738d..de5decd 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,56 @@ -#include +#include +#include #include +#define LED 0x08 int main(int argc, char *argv[]) { - printf("%s\n", HELLO); + struct ftdi_context *ftdi; + int f, retval, cnt; + unsigned char buf[1]; - return 0; + openlog("ftdi_gpio", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); + setlogmask(LOG_DEBUG); + + if ((ftdi = ftdi_new()) == 0) { + syslog(LOG_ERR, "ftdi_new failed\n"); + return EXIT_FAILURE; + } + + f = ftdi_usb_open(ftdi, 0x0403, 0x6001); + + if (f < 0 && f != -5) { + syslog(LOG_ERR, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi)); + retval = 1; + goto done; + } + + f = ftdi_set_bitmode(ftdi, LED, BITMODE_BITBANG); + if(f < 0) { + syslog(LOG_ERR, "unable to set bit bang mode: %d (%s)\n", f, ftdi_get_error_string(ftdi)); + retval = 1; + goto done; + } + + buf[0] = 0; + for(cnt = 0; cnt < 10; cnt++) { + buf[0] ^= LED, + f = ftdi_write_data(ftdi, buf, 1); + if(f < 0) { + syslog(LOG_ERR, "write failed for 0x%x, error %d (%s)\n",buf[0], f, ftdi_get_error_string(ftdi)); + cnt = 10; + } else { + syslog(LOG_DEBUG, "set LED to: %u\n", (unsigned int)buf[0]); + } + + sleep(1); + } + + ftdi_usb_close(ftdi); +done: + ftdi_free(ftdi); + closelog(); + + return retval; }