Compare commits

...

1 Commits

Author SHA1 Message Date
Thomas Klaehn
0067d6ea97 Convert to library 2019-07-17 00:08:17 +02:00
4 changed files with 39 additions and 101 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
bin/ lib/
obj/ obj/
coverage/ coverage/

View File

@ -1,17 +0,0 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/libftdi1"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}

View File

@ -1,6 +1,7 @@
CROSS_COMPILE ?= CROSS_COMPILE ?=
TARGET_FILE ?= ftdi_gpio STATIC_LIB_FILE ?= libftdi_gpio.a
DYNAMIC_LIB_FILE ?= libftdi_gpio.so
CC = $(CROSS_COMPILE)gcc CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)cpp CPP = $(CROSS_COMPILE)cpp
@ -10,24 +11,27 @@ GENHTML = genhtml
SRC_DIR = src SRC_DIR = src
OBJ_DIR = obj OBJ_DIR = obj
BIN_DIR = bin LIB_DIR = lib
COVERAGE_DIR = coverage COVERAGE_DIR = coverage
UNIT_TEST_SRC_DIR = test/unit UNIT_TEST_SRC_DIR = test/unit
UNIT_TEST_OBJ_DIR = $(OBJ_DIR)/$(UNIT_TEST_SRC_DIR) UNIT_TEST_OBJ_DIR = $(OBJ_DIR)/$(UNIT_TEST_SRC_DIR)
PREFIX ?= /usr
LIB_INSTALL_DIR ?= $(PREFIX)/lib/ftdi_gpio
INC_INSTALL_DIR ?= $(PREFIX)/include/ftdi_gpio
INCLUDES := inc INCLUDES := inc
INCLUDES += /usr/include/libftdi1 INCLUDES += /usr/include/libftdi1
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" "" ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" ""
INCLUDES += test/inc INCLUDES += test/inc
C_FLAGS += --coverage C_FLAGS += --coverage
L_FLAGS += --coverage L_FLAGS += --coverage
else
LIBS := ftdi1
endif endif
L_FLAGS += -shared -Wl,-soname,$(notdir $(DYNAMIC_LIB))
C_FLAGS += -fpic
C_FLAGS += -O0 -g -Wall -Wextra -Werror C_FLAGS += -O0 -g -Wall -Wextra -Werror
CPP_FLAGS += $(addprefix -I, $(INCLUDES)) CPP_FLAGS += $(addprefix -I, $(INCLUDES))
CHECK_FLAGS = --enable=all --template=gcc --error-exitcode=1 --suppress=missingIncludeSystem CHECK_FLAGS = --enable=all --template=gcc --error-exitcode=1 --suppress=missingIncludeSystem
@ -40,13 +44,30 @@ UNIT_TEST_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst $(UNIT_TEST_SRC_D
OBJS = $(C_OBJS) OBJS = $(C_OBJS)
TARGET = $(BIN_DIR)/$(TARGET_FILE) STATIC_LIB = $(LIB_DIR)/$(STATIC_LIB_FILE)
DYNAMIC_LIB = $(LIB_DIR)/$(DYNAMIC_LIB_FILE)
UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE) UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE)
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST)) THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
.PHONY: all .PHONY: all install clean
all: $(TARGET) all: $(STATIC_LIB) $(DYNAMIC_LIB)
install: all
install -d $(LIB_INSTALL_DIR)
install -d $(INC_INSTALL_DIR)
install -m 0644 $(STATIC_LIB) $(LIB_INSTALL_DIR)
install -m 0777 $(DYNAMIC_LIB) $(LIB_INSTALL_DIR)
install -m 0644 inc/* $(INC_INSTALL_DIR)
clean:
rm -f $(STATIC_LIB) $(DYNAMIC_LIB)
rm -f $(OBJS) $(patsubst %.o,%.d,$(OBJS))
rm -f $(UNIT_TEST_OBJS) $(patsubst %.o,%.d,$(UNIT_TEST_OBJS)) $(UNIT_TEST_TARGET)
rm -f $(UNIT_TEST_OBJ_DIR)/*.gcda
rm -f $(UNIT_TEST_OBJ_DIR)/*.gcno
rm -f $(OBJ_DIR)/*.gcda $(OBJ_DIR)/*.gcno
rm -fr $(COVERAGE_DIR)
.PHONY: coverage .PHONY: coverage
coverage: _cov_genhtml coverage: _cov_genhtml
@ -80,11 +101,15 @@ check: $(C_SRCS)
$(UNIT_TEST_TARGET): $(UNIT_TEST_OBJS) $(THIS_MAKEFILE) $(UNIT_TEST_TARGET): $(UNIT_TEST_OBJS) $(THIS_MAKEFILE)
@mkdir -p $(BIN_DIR)/$(UNIT_TEST_SRC_DIR) @mkdir -p $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)
$(CC) $(C_FLAGS) $(LD_FLAGS) $(UNIT_TEST_OBJS) $(addprefix -l,$(LIBS)) -o $@ $(CC) $(C_FLAGS) $(LD_FLAGS) $(UNIT_TEST_OBJS) -o $@
$(TARGET): $(OBJS) $(THIS_MAKEFILE) $(STATIC_LIB): $(OBJS) $(THIS_MAKEFILE)
@mkdir -p $(BIN_DIR) @mkdir -p $(LIB_DIR)
$(CC) $(C_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@ $(AR) rcs $(STATIC_LIB) $(OBJS)
$(DYNAMIC_LIB): $(OBJS) $(THIS_MAKEFILE)
@mkdir -p $(LIB_DIR)
$(CC) $(L_FLAGS) -o $(DYNAMIC_LIB) $(OBJS)
.PRECIOUS: $(OBJ_DIR)/%.d .PRECIOUS: $(OBJ_DIR)/%.d
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c $(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
@ -104,13 +129,6 @@ $(UNIT_TEST_OBJ_DIR)/%.o: $(UNIT_TEST_SRC_DIR)/%.c $(UNIT_TEST_OBJ_DIR)/%.d
@mkdir -p $(UNIT_TEST_OBJ_DIR) @mkdir -p $(UNIT_TEST_OBJ_DIR)
$(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@ $(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@
.PHONY: clean
clean:
rm -f $(OBJS) $(patsubst %.o,%.d,$(OBJS)) $(TARGET)
rm -f $(UNIT_TEST_OBJS) $(patsubst %.o,%.d,$(UNIT_TEST_OBJS)) $(UNIT_TEST_TARGET)
rm -f $(OBJ_DIR)/*.gcda $(OBJ_DIR)/*.gcno
rm -fr $(COVERAGE_DIR)
ifneq ($(MAKECMDGOALS),clean) ifneq ($(MAKECMDGOALS),clean)
-include $(patsubst %.o,%.d,$(OBJS)) -include $(patsubst %.o,%.d,$(OBJS))
endif endif

View File

@ -1,63 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <stdbool.h>
#include <ftdi_dev.h>
#include <gpio.h>
/*
#define GPIO1 0x08 // TX (brown)
#define GPIO2 0x01 // TX (orange)
#define GPIO3 0x02 // RX (yellow)
#define GPIO4 0x14 // RTS (green on FTDI) + DTR (on SparkFun breakout)
*/
static struct ftdi_dev ftdi_obj = {
.ftdi = NULL,
.is_open = false,
.vendor_id = 0x0403,
.product_id = 0x6001,
.bit_mask = 0,
.status_mask = 0,
};
static const struct gpio gpio_1 = {
.pin = 0x08, /* CTS (brown wire on FTDI cable) */
.ftdi_dev = &ftdi_obj,
};
static const struct gpio gpio_2 = {
.pin = 0x01, /* TX (orange wire on FTDI cable) */
.ftdi_dev = &ftdi_obj,
};
int main(void)
{
int res, cnt;
unsigned int value;
openlog("ftdi_gpio", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
res = gpio_open(&gpio_1);
res |= gpio_open(&gpio_2);
if(res != EXIT_SUCCESS) {
syslog(LOG_ERR, "Unable to open gpio\n");
return res;
}
gpio_write(&gpio_1, 1);
gpio_write(&gpio_2, 0);
for(cnt = 0; cnt < 10; cnt++) {
gpio_toggle(&gpio_1);
gpio_toggle(&gpio_2);
gpio_read(&gpio_1, &value);
printf("Gpio1: %u\n", value);
gpio_read(&gpio_2, &value);
printf("Gpio2: %u\n", value);
sleep(1);
}
gpio_close(&gpio_1);
gpio_close(&gpio_2);
return EXIT_SUCCESS;
}