gpio driver

This commit is contained in:
Thomas Klaehn 2019-07-13 23:22:43 +02:00
parent cc851fc929
commit 082277bf25
6 changed files with 109 additions and 18 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"stdbool.h": "c"
}
}

36
.vscode/tasks.json vendored
View File

@ -2,16 +2,16 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558 // See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format // for the documentation about the tasks.json format
"version": "2.0.0", "version": "2.0.0",
"type":"shell",
"command": "make",
"echoCommand": true,
"problemMatcher": {
"base": "gcc",
},
"tasks": [ "tasks": [
{ {
"label": "all", "label": "all",
"args": ["all"], "type":"shell",
"command": "make all -j4",
"problemMatcher": {
"base": "gcc",
"owner": "gcc",
"fileLocation":"absolute"
},
"group": { "group": {
"kind": "build", "kind": "build",
"isDefault": true "isDefault": true
@ -19,7 +19,23 @@
}, },
{ {
"label": "clean", "label": "clean",
"args": ["clean"], "type":"shell",
"command": "make clean -j4",
"problemMatcher": {
"base": "gcc",
"owner": "gcc",
"fileLocation":"absolute"
},
"group": {
"kind": "build",
"isDefault": true
}
} }
] ],
} "presentation": {
"focus": true,
"reveal": "always",
"panel": "shared",
"clear": true,
}
}

View File

@ -1,6 +0,0 @@
#ifndef __INC_DEF_H__
#define __INC_DEF_H__
#define HELLO "Hello world!"
#endif // __INC_DEF_H__

14
inc/gpio.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __GPIO_H__
#define __GPIO_H__
struct gpio {
unsigned int pin;
};
int gpio_open(const struct gpio *gpio);
int gpio_close(const struct gpio *gpio);
char gpio_read(const struct gpio *gpio);
void gpio_write(const struct gpio *gpio, char byte);
void gpio_toggle(const struct gpio *gpio);
#endif

64
src/gpio.c Normal file
View File

@ -0,0 +1,64 @@
#include <stdlib.h>
#include <syslog.h>
#include <stdbool.h>
#include <ftdi.h>
#include <gpio.h>
struct gpio_obj {
struct ftdi_context *ftdi;
bool is_open;
};
static struct gpio_obj gpio_obj = {
.ftdi = NULL,
.is_open = false,
};
int gpio_open(const struct gpio *gpio)
{
int res;
if((gpio_obj.ftdi = ftdi_new()) == 0) {
syslog(LOG_ERR, "ftdi_new failed\n");
return EXIT_FAILURE;
}
res = ftdi_usb_open(gpio_obj.ftdi, 0x0403, 0x6001);
if (res < 0 && res != -5) {
syslog(LOG_ERR, "unable to open ftdi device: %d (%s)\n", res,
ftdi_get_error_string(gpio_obj.ftdi));
ftdi_free(gpio_obj.ftdi);
return EXIT_FAILURE;
}
res = ftdi_set_bitmode(gpio_obj.ftdi, (char)gpio->pin, BITMODE_BITBANG);
if(res < 0) {
syslog(LOG_ERR, "unable to set bit bang mode: %d (%s)\n", res,
ftdi_get_error_string(gpio_obj.ftdi));
ftdi_free(gpio_obj.ftdi);
return EXIT_FAILURE;
}
gpio_obj.is_open = true;
return EXIT_SUCCESS;
}
int gpio_close(const struct gpio *gpio)
{
ftdi_usb_close(gpio_obj.ftdi);
ftdi_free(gpio_obj.ftdi);
return EXIT_SUCCESS;
}
char gpio_read(const struct gpio *gpio)
{
return 0;
}
void gpio_write(const struct gpio *gpio, char byte)
{
}
void gpio_toggle(const struct gpio *gpio)
{
}

View File

@ -3,8 +3,6 @@
#include <syslog.h> #include <syslog.h>
#include <ftdi.h> #include <ftdi.h>
#include <def.h>
#define LED 0x08 #define LED 0x08
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {