Skip to content

Commit

Permalink
Soporte para adc y gpio ok
Browse files Browse the repository at this point in the history
TODO: PWM
  • Loading branch information
grizmio committed Feb 3, 2016
1 parent 93f3558 commit d9ad2b3
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 18 deletions.
113 changes: 113 additions & 0 deletions adafruit.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
diff --git a/source/c_adc.c b/source/c_adc.c
index e0c754c..5269407 100644
--- a/source/c_adc.c
+++ b/source/c_adc.c
@@ -30,29 +30,28 @@ SOFTWARE.
#include "c_adc.h"
#include "common.h"

-char adc_prefix_dir[40];
+char adc_prefix_dir[49];

int adc_initialized = 0;

int initialize_adc(void)
{
- char test_path[40];
+ char test_path[49];
FILE *fh;
if (adc_initialized) {
return 1;
}

- if (load_device_tree("cape-bone-iio")) {
- build_path("/sys/devices", "ocp.", ocp_dir, sizeof(ocp_dir));
- build_path(ocp_dir, "helper.", adc_prefix_dir, sizeof(adc_prefix_dir));
- strncat(adc_prefix_dir, "/AIN", sizeof(adc_prefix_dir));
-
+ if (load_device_tree("BB-ADC")) {
+ strncat(adc_prefix_dir, "/sys/bus/iio/devices/iio:device0/in_voltage", sizeof(adc_prefix_dir));
+
// Test that the directory has an AIN entry (found correct devicetree)
- snprintf(test_path, sizeof(test_path), "%s%d", adc_prefix_dir, 0);
+ snprintf(test_path, sizeof(test_path), "%s%d_raw", adc_prefix_dir, 1);

fh = fopen(test_path, "r");

if (!fh) {
+ puts("wiiii");
return 0;
}
fclose(fh);
@@ -67,10 +66,10 @@ int initialize_adc(void)
int read_value(unsigned int ain, float *value)
{
FILE * fh;
- char ain_path[40];
+ char ain_path[49];
int err, try_count=0;
int read_successful;
- snprintf(ain_path, sizeof(ain_path), "%s%d", adc_prefix_dir, ain);
+ snprintf(ain_path, sizeof(ain_path), "%s%d_raw", adc_prefix_dir, ain);

read_successful = 0;

@@ -106,5 +105,5 @@ int adc_setup()

void adc_cleanup(void)
{
- unload_device_tree("cape-bone-iio");
+ unload_device_tree("BB-ADC");
}
diff --git a/source/common.c b/source/common.c
index 8911a9c..4b07e7c 100644
--- a/source/common.c
+++ b/source/common.c
@@ -361,12 +361,14 @@ int get_spi_bus_path_number(unsigned int spi)
int load_device_tree(const char *name)
{
FILE *file = NULL;
- char slots[40];
+ char slots[41];
char line[256];

- build_path("/sys/devices", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
- snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);
+ // build_path("/sys/devices/platform", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
+ snprintf(ctrl_dir, sizeof(ctrl_dir), "/sys/devices/platform/bone_capemgr");

+ snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);
+
file = fopen(slots, "r+");
if (!file) {
PyErr_SetFromErrnoWithFilename(PyExc_IOError, slots);
@@ -394,11 +396,12 @@ int load_device_tree(const char *name)
int unload_device_tree(const char *name)
{
FILE *file = NULL;
- char slots[40];
+ char slots[41];
char line[256];
char *slot_line;

- build_path("/sys/devices", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
+ // build_path("/sys/devices/platform", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
+ snprintf(ctrl_dir, sizeof(ctrl_dir), "/sys/devices/platform/bone_capemgr");
snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);

file = fopen(slots, "r+");
diff --git a/source/common.h b/source/common.h
index 2600b40..a9a2c88 100644
--- a/source/common.h
+++ b/source/common.h
@@ -39,8 +39,8 @@ SOFTWARE.
int gpio_mode;
int gpio_direction[120];

-char ctrl_dir[35];
-char ocp_dir[25];
+char ctrl_dir[43];
+char ocp_dir[33];

int get_gpio_number(const char *key, unsigned int *gpio);
int get_pwm_key(const char *input, char *key);
21 changes: 10 additions & 11 deletions source/c_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,28 @@ SOFTWARE.
#include "c_adc.h"
#include "common.h"

char adc_prefix_dir[40];
char adc_prefix_dir[49];

int adc_initialized = 0;

int initialize_adc(void)
{
char test_path[40];
char test_path[49];
FILE *fh;
if (adc_initialized) {
return 1;
}

if (load_device_tree("cape-bone-iio")) {
build_path("/sys/devices", "ocp.", ocp_dir, sizeof(ocp_dir));
build_path(ocp_dir, "helper.", adc_prefix_dir, sizeof(adc_prefix_dir));
strncat(adc_prefix_dir, "/AIN", sizeof(adc_prefix_dir));

if (load_device_tree("BB-ADC")) {
strncat(adc_prefix_dir, "/sys/bus/iio/devices/iio:device0/in_voltage", sizeof(adc_prefix_dir));

// Test that the directory has an AIN entry (found correct devicetree)
snprintf(test_path, sizeof(test_path), "%s%d", adc_prefix_dir, 0);
snprintf(test_path, sizeof(test_path), "%s%d_raw", adc_prefix_dir, 1);

fh = fopen(test_path, "r");

if (!fh) {
puts("wiiii");
return 0;
}
fclose(fh);
Expand All @@ -67,10 +66,10 @@ int initialize_adc(void)
int read_value(unsigned int ain, float *value)
{
FILE * fh;
char ain_path[40];
char ain_path[49];
int err, try_count=0;
int read_successful;
snprintf(ain_path, sizeof(ain_path), "%s%d", adc_prefix_dir, ain);
snprintf(ain_path, sizeof(ain_path), "%s%d_raw", adc_prefix_dir, ain);

read_successful = 0;

Expand Down Expand Up @@ -106,5 +105,5 @@ int adc_setup()

void adc_cleanup(void)
{
unload_device_tree("cape-bone-iio");
unload_device_tree("BB-ADC");
}
13 changes: 8 additions & 5 deletions source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,14 @@ int get_spi_bus_path_number(unsigned int spi)
int load_device_tree(const char *name)
{
FILE *file = NULL;
char slots[40];
char slots[41];
char line[256];

build_path("/sys/devices", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);
// build_path("/sys/devices/platform", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
snprintf(ctrl_dir, sizeof(ctrl_dir), "/sys/devices/platform/bone_capemgr");

snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);

file = fopen(slots, "r+");
if (!file) {
PyErr_SetFromErrnoWithFilename(PyExc_IOError, slots);
Expand Down Expand Up @@ -394,11 +396,12 @@ int load_device_tree(const char *name)
int unload_device_tree(const char *name)
{
FILE *file = NULL;
char slots[40];
char slots[41];
char line[256];
char *slot_line;

build_path("/sys/devices", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
// build_path("/sys/devices/platform", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
snprintf(ctrl_dir, sizeof(ctrl_dir), "/sys/devices/platform/bone_capemgr");
snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);

file = fopen(slots, "r+");
Expand Down
4 changes: 2 additions & 2 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ SOFTWARE.
int gpio_mode;
int gpio_direction[120];

char ctrl_dir[35];
char ocp_dir[25];
char ctrl_dir[43];
char ocp_dir[33];

int get_gpio_number(const char *key, unsigned int *gpio);
int get_pwm_key(const char *input, char *key);
Expand Down

0 comments on commit d9ad2b3

Please sign in to comment.