Convert to library

This commit is contained in:
Thomas Klaehn 2019-07-17 00:08:17 +02:00
parent 8f5d0dd7f8
commit 1d358b1086
4 changed files with 39 additions and 98 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
bin/
lib/
obj/
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,8 @@
CROSS_COMPILE ?=
TARGET_FILE ?= ftdi_gpio
STATIC_LIB_FILE ?= libftdi_gpio.a
DYNAMIC_LIB_FILE ?= libftdi_gpio.so
CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)cpp
@ -10,24 +12,28 @@ GENHTML = genhtml
SRC_DIR = src
OBJ_DIR = obj
LIB_DIR = lib
BIN_DIR = bin
COVERAGE_DIR = coverage
UNIT_TEST_SRC_DIR = test/unit
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 += /usr/include/libftdi1
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" ""
INCLUDES += test/inc
C_FLAGS += --coverage
L_FLAGS += --coverage
else
LIBS := ftdi1
endif
L_FLAGS += -shared -Wl,-soname,$(notdir $(DYNAMIC_LIB))
C_FLAGS += -fpic
C_FLAGS += -O0 -g -Wall -Wextra -Werror
CPP_FLAGS += $(addprefix -I, $(INCLUDES))
CHECK_FLAGS = --enable=all --template=gcc --error-exitcode=1 --suppress=missingIncludeSystem
@ -40,13 +46,30 @@ UNIT_TEST_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst $(UNIT_TEST_SRC_D
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)
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
.PHONY: all
all: $(TARGET)
.PHONY: all install clean
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
coverage: _cov_genhtml
@ -80,11 +103,15 @@ check: $(C_SRCS)
$(UNIT_TEST_TARGET): $(UNIT_TEST_OBJS) $(THIS_MAKEFILE)
@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)
@mkdir -p $(BIN_DIR)
$(CC) $(C_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@
$(STATIC_LIB): $(OBJS) $(THIS_MAKEFILE)
@mkdir -p $(LIB_DIR)
$(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
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
@ -104,13 +131,6 @@ $(UNIT_TEST_OBJ_DIR)/%.o: $(UNIT_TEST_SRC_DIR)/%.c $(UNIT_TEST_OBJ_DIR)/%.d
@mkdir -p $(UNIT_TEST_OBJ_DIR)
$(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)
-include $(patsubst %.o,%.d,$(OBJS))
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;
}