Compare commits

..

2 Commits

Author SHA1 Message Date
Thomas Klaehn
98bd5dc4d3 Debug config for vs code ide 2019-07-14 12:20:35 +02:00
Thomas Klaehn
8a897d5668 gpio driver 2019-07-14 12:20:16 +02:00
3 changed files with 98 additions and 25 deletions

27
.vscode/launch.json vendored Normal file
View File

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

View File

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