Compare commits

..

1 Commits

Author SHA1 Message Date
Thomas Klaehn
519594600d gpio driver 2019-07-14 11:28:10 +02:00
3 changed files with 26 additions and 99 deletions

27
.vscode/launch.json vendored
View File

@ -1,27 +0,0 @@
{
// 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",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

View File

@ -41,24 +41,6 @@ static int ftdi_open(void)
return EXIT_SUCCESS;
}
static int gpio_read_mask(const struct gpio *gpio, unsigned char *mask)
{
int res;
if(NULL == gpio || NULL == mask) {
return EXIT_FAILURE;
}
res = ftdi_read_data(ftdi_obj.ftdi, mask, 1);
if(res < 0) {
syslog(LOG_ERR, "unable to readfrom ftdi device: %d (%s)\n", res,
ftdi_get_error_string(ftdi_obj.ftdi));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int gpio_open(const struct gpio *gpio)
{
int res;
@ -80,7 +62,6 @@ int gpio_open(const struct gpio *gpio)
}
ftdi_obj.bit_mask |= (unsigned char)gpio->pin;
syslog(LOG_DEBUG, "bitmask: 0x%02x\n", ftdi_obj.bit_mask);
res = ftdi_set_bitmode(ftdi_obj.ftdi, ftdi_obj.bit_mask, BITMODE_BITBANG);
if(res < 0) {
syslog(LOG_ERR, "unable to set bit bang mode: %d (%s)\n", res,
@ -122,8 +103,8 @@ int gpio_read(const struct gpio *gpio, unsigned int *value)
return EXIT_FAILURE;
}
res = gpio_read_mask(gpio, &buf);
if(res != EXIT_SUCCESS) {
res = ftdi_read_data(ftdi_obj.ftdi, &buf, 1);
if(res < 0) {
syslog(LOG_ERR, "unable to readfrom ftdi device: %d (%s)\n", res,
ftdi_get_error_string(ftdi_obj.ftdi));
return EXIT_FAILURE;
@ -141,27 +122,27 @@ int gpio_read(const struct gpio *gpio, unsigned int *value)
int gpio_write(const struct gpio *gpio, unsigned int value)
{
int res;
unsigned int current_state;
unsigned char buf;
if(NULL == gpio) {
return EXIT_FAILURE;
}
res = gpio_read_mask(gpio, &buf);
res = gpio_read(gpio, &current_state);
if(res != EXIT_SUCCESS) {
syslog(LOG_ERR, "write failed for 0x%02x, error %d (%s)\n", buf, res,
syslog(LOG_ERR, "write failed for 0x%x, error %d (%s)\n", value, res,
ftdi_get_error_string(ftdi_obj.ftdi));
return EXIT_FAILURE;
}
syslog(LOG_DEBUG, "current state: 0x%02x\n", buf);
buf = (unsigned char)current_state;
if(value == 0) {
buf &= ~(unsigned char)(gpio->pin);
}
else {
buf |= (unsigned char)(gpio->pin);
}
syslog(LOG_DEBUG, "new state: 0x%02x\n", buf);
res = ftdi_write_data(ftdi_obj.ftdi, &buf, 1);
if(res < 0) {

View File

@ -5,66 +5,39 @@
#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 const struct gpio gpio_1 = {
.pin = 0x08, /* CTS (brown wire on FTDI cable) */
};
static const struct gpio gpio_2 = {
.pin = 0x01, /* TX (orange wire on FTDI cable) */
static const struct gpio gpio = {
.pin = 0x08,
};
int main(int argc, char *argv[])
{
int res, cnt;
int res;
unsigned int value;
openlog("ftdi_gpio", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
res = gpio_open(&gpio_1);
res |= gpio_open(&gpio_2);
res = gpio_open(&gpio);
if(res != EXIT_SUCCESS) {
syslog(LOG_ERR, "Unable to open gpio\n");
syslog(LOG_ERR, "Unable to open gpio pin %u\n", gpio.pin);
return res;
}
for(cnt = 0; cnt < 10; cnt++) {
value = cnt & 0x01;
res = gpio_write(&gpio_1, value);
if(cnt % 2) {
res |= gpio_write(&gpio_2, value);
}
if(res != EXIT_SUCCESS) {
syslog(LOG_ERR, "Unable to write to gpio\n");
gpio_close(&gpio_1);
gpio_close(&gpio_2);
return res;
}
res = gpio_read(&gpio_1, &value);
if(res != EXIT_SUCCESS) {
syslog(LOG_ERR, "Unable to read from gpio pin %u\n", gpio_1.pin);
gpio_close(&gpio_1);
return res;
}
printf("read 1 value: 0x%02x\n", value);
if(cnt % 2) {
res = gpio_read(&gpio_2, &value);
if(res != EXIT_SUCCESS) {
syslog(LOG_ERR, "Unable to read from gpio pin %u\n", gpio_2.pin);
gpio_close(&gpio_2);
return res;
}
printf("read 2 value: 0x%02x\n", value);
}
sleep(1);
res = gpio_write(&gpio, 0x00);
if(res != EXIT_SUCCESS) {
syslog(LOG_ERR, "Unable to write to gpio pin %u\n", gpio.pin);
gpio_close(&gpio);
return res;
}
gpio_close(&gpio_1);
res = gpio_read(&gpio, &value);
if(res != EXIT_SUCCESS) {
syslog(LOG_ERR, "Unable to read from gpio pin %u\n", gpio.pin);
gpio_close(&gpio);
return res;
}
printf("read value: 0x%02x\n", value);
gpio_close(&gpio);
return EXIT_SUCCESS;
}