Compare commits
No commits in common. "89ba05379f134b5ec37cc8db710568bf61b27d52" and "5aaccc39e54a9c97ad1bd52f1e4ddff4833a9306" have entirely different histories.
89ba05379f
...
5aaccc39e5
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,4 +2,3 @@ bin/
|
||||
lib/
|
||||
obj/
|
||||
coverage/
|
||||
|
||||
|
17
.vscode/c_cpp_properties.json
vendored
Normal file
17
.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"/usr/include/gpio_ftdi"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "/usr/bin/clang",
|
||||
"cStandard": "c11",
|
||||
"cppStandard": "c++17",
|
||||
"intelliSenseMode": "clang-x64"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
47
.vscode/launch.json
vendored
Normal file
47
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(gdb) Launch distance",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/bin/distance",
|
||||
"args": [],
|
||||
"stopAtEntry": true,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "(gdb) Launch unit test",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/bin/test/unit/distance",
|
||||
"args": [],
|
||||
"stopAtEntry": true,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
]
|
||||
}
|
115
.vscode/tasks.json
vendored
Normal file
115
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,115 @@
|
||||
{
|
||||
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||
// for the documentation about the tasks.json format
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"label": "all",
|
||||
"type":"shell",
|
||||
"command": "make all -j8",
|
||||
"problemMatcher": {
|
||||
"base": "$gcc",
|
||||
"owner": "gcc",
|
||||
"fileLocation": [
|
||||
"relative",
|
||||
"${workspaceFolder}"
|
||||
]
|
||||
},
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "check",
|
||||
"type":"shell",
|
||||
"command": "make check -j8",
|
||||
"problemMatcher": {
|
||||
"base": "$gcc",
|
||||
"owner": "gcc",
|
||||
"fileLocation": [
|
||||
"relative",
|
||||
"${workspaceFolder}"
|
||||
]
|
||||
},
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "clean",
|
||||
"type":"shell",
|
||||
"command": "make clean -j8",
|
||||
"problemMatcher": {
|
||||
"base": "$gcc",
|
||||
"owner": "gcc",
|
||||
"fileLocation": [
|
||||
"relative",
|
||||
"${workspaceFolder}"
|
||||
]
|
||||
},
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "build_unit_test",
|
||||
"type":"shell",
|
||||
"command": "make build_unit_test -j8",
|
||||
"problemMatcher": {
|
||||
"base": "$gcc",
|
||||
"owner": "gcc",
|
||||
"fileLocation": [
|
||||
"relative",
|
||||
"${workspaceFolder}"
|
||||
]
|
||||
},
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "exec_unit_test",
|
||||
"type":"shell",
|
||||
"command": "make exec_unit_test -j1",
|
||||
"problemMatcher": {
|
||||
"base": "$gcc",
|
||||
"owner": "gcc",
|
||||
"fileLocation": [
|
||||
"relative",
|
||||
"${workspaceFolder}"
|
||||
]
|
||||
},
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "coverage",
|
||||
"type":"shell",
|
||||
"command": "make coverage -j1",
|
||||
"problemMatcher": {
|
||||
"base": "$gcc",
|
||||
"owner": "gcc",
|
||||
"fileLocation": [
|
||||
"relative",
|
||||
"${workspaceFolder}"
|
||||
]
|
||||
},
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"presentation": {
|
||||
"focus": true,
|
||||
"reveal": "always",
|
||||
"panel": "shared",
|
||||
"clear": true,
|
||||
}
|
||||
}
|
13
Makefile
13
Makefile
@ -1,6 +1,6 @@
|
||||
CROSS_COMPILE ?=
|
||||
|
||||
TARGET_FILE ?= blink
|
||||
TARGET_FILE ?= distance
|
||||
|
||||
CC = $(CROSS_COMPILE)gcc
|
||||
CPP = $(CROSS_COMPILE)cpp
|
||||
@ -19,13 +19,13 @@ PREFIX ?= /usr
|
||||
BIN_INSTALL_DIR ?= $(PREFIX)/bin/$(TARGET_FILE)
|
||||
|
||||
INCLUDES := inc
|
||||
|
||||
LIBS = gpio_sys i2c_bb
|
||||
INCLUDES += /usr/include/libftdi1
|
||||
INCLUDES += /usr/include/gpio_ftdi
|
||||
|
||||
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" ""
|
||||
INCLUDES += test/inc
|
||||
C_FLAGS += --coverage
|
||||
LD_FLAGS += --coverage
|
||||
L_FLAGS += --coverage
|
||||
endif
|
||||
|
||||
C_FLAGS += -fpic
|
||||
@ -46,7 +46,7 @@ UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE)
|
||||
TARGET = $(BIN_DIR)/$(TARGET_FILE)
|
||||
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
|
||||
|
||||
.PHONY: all install uninstall clean distclean
|
||||
.PHONY: all install uninstall clean
|
||||
all: $(TARGET)
|
||||
|
||||
install: all
|
||||
@ -65,9 +65,6 @@ clean:
|
||||
rm -f $(OBJ_DIR)/*.gcda $(OBJ_DIR)/*.gcno
|
||||
rm -fr $(COVERAGE_DIR)
|
||||
|
||||
distclean:
|
||||
rm -rf $(OBJ_DIR) $(BIN_DIR) $(COVERAGE_DIR)
|
||||
|
||||
.PHONY: coverage
|
||||
coverage: _cov_genhtml
|
||||
$(eval COVERAGE:=$(shell grep 'lines' .coverage.tmp | egrep -o '[0-9]+.[0-9]+%'))
|
||||
|
90
src/main.c
90
src/main.c
@ -6,95 +6,9 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <libgpio_sys/gpio_sys.h>
|
||||
#include <libi2c_bb/i2c_bb.h>
|
||||
|
||||
#define ADS1115_ADDR 0x48
|
||||
#define VPS 6.144 / 32768.0
|
||||
|
||||
static struct gpio_sys gpio_sda = {
|
||||
.pin = 4,
|
||||
.direction = OUT,
|
||||
};
|
||||
|
||||
static struct gpio_sys gpio_scl = {
|
||||
.pin = 24,
|
||||
.direction = OUT,
|
||||
};
|
||||
|
||||
static struct i2c_bb i2c_obj = {
|
||||
.sda = &gpio_sda,
|
||||
.scl = &gpio_scl,
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int res;
|
||||
uint16_t value;
|
||||
float f_value;
|
||||
char w_buffer[3], r_buffer[2];
|
||||
printf("Hello world!\n");
|
||||
|
||||
openlog("distance_app", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
|
||||
printf("Trying to open I2C device.\n");
|
||||
|
||||
syslog(LOG_DEBUG, "Trying to open i2c_bb.\n");
|
||||
res = i2c_open(&i2c_obj);
|
||||
if(res != EXIT_SUCCESS) {
|
||||
syslog(LOG_ERR, "Unable to open i2c_bb.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
syslog(LOG_DEBUG, "i2c_bb successfully opened.\n");
|
||||
|
||||
w_buffer[0] = 0b00000001; // config register @ address 1
|
||||
w_buffer[1] = 0b11000001; // config value high byte
|
||||
w_buffer[2] = 0b10000111; // config value low byte
|
||||
|
||||
// begin conversation
|
||||
res = i2c_write(&i2c_obj, ADS1115_ADDR, w_buffer, 3);
|
||||
if(res != 3) {
|
||||
syslog(LOG_ERR, "Unable to write to ADS1115.\n");
|
||||
i2c_close(&i2c_obj);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// wait for conversation complete
|
||||
do {
|
||||
res = i2c_read(&i2c_obj, ADS1115_ADDR, w_buffer, 2);
|
||||
if(res != 2) {
|
||||
syslog(LOG_ERR, "Unable to read from ADS1115.\n");
|
||||
i2c_close(&i2c_obj);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} while((w_buffer[0] & 0x80) == 0);
|
||||
|
||||
// read conversation result
|
||||
r_buffer[0] = 0b00000000; /* conversation register at address 0b00000000 */
|
||||
res = i2c_write(&i2c_obj, ADS1115_ADDR, r_buffer, 1);
|
||||
if(res != 1) {
|
||||
syslog(LOG_ERR, "Unable to write to ADS1115.\n");
|
||||
i2c_close(&i2c_obj);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// read 2 bytes
|
||||
res = i2c_read(&i2c_obj, ADS1115_ADDR, r_buffer, 2);
|
||||
if(res != 2) {
|
||||
syslog(LOG_ERR, "Unable to read from ADS1115.\n");
|
||||
i2c_close(&i2c_obj);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
printf("Conversation done [0x%02x:%02x].\n", r_buffer[0], r_buffer[1]);
|
||||
i2c_close(&i2c_obj);
|
||||
|
||||
|
||||
value = r_buffer[0] << 8 | r_buffer[1];
|
||||
if(value & 0x8000) {
|
||||
value = 0;
|
||||
}
|
||||
f_value = value * VPS;
|
||||
|
||||
printf("Conversation result: %4.3fV (0x%04x).\n", f_value, value);
|
||||
|
||||
return res;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user