Skip to content

Commit

Permalink
Make friends between 3.8 and 4.1+ kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
plawler committed Apr 29, 2016
1 parent d9ad2b3 commit d16654d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
44 changes: 39 additions & 5 deletions source/c_adc.c
Expand Up @@ -30,43 +30,73 @@ SOFTWARE.
#include "c_adc.h"
#include "common.h"

char adc_prefix_dir[49];
#ifdef BBBVERSION41
char adc_prefix_dir[49];
#else
char adc_prefix_dir[40];
#endif

int adc_initialized = 0;

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

#ifdef BBBVERSION41
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_raw", adc_prefix_dir, 1);

fh = fopen(test_path, "r");

if (!fh) {
puts("wiiii");
return 0;
return 0;
}
fclose(fh);

adc_initialized = 1;
return 1;
}
#else
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));
snprintf(test_path, sizeof(test_path), "%s%d", adc_prefix_dir, 0);
fh = fopen(test_path, "r");

if (!fh) {
return 0;
}
fclose(fh);

adc_initialized = 1;
return 1;
}
#endif

return 0;
}

int read_value(unsigned int ain, float *value)
{
FILE * fh;
#ifdef BBBVERSION41
char ain_path[49];
snprintf(ain_path, sizeof(ain_path), "%s%d_raw", adc_prefix_dir, ain);
#else
char ain_path[40];
snprintf(ain_path, sizeof(ain_path), "%s%d", adc_prefix_dir, ain);
#endif

int err, try_count=0;
int read_successful;
snprintf(ain_path, sizeof(ain_path), "%s%d_raw", adc_prefix_dir, ain);
Expand Down Expand Up @@ -105,5 +135,9 @@ int adc_setup()

void adc_cleanup(void)
{
#ifdef BBBVERSION41
unload_device_tree("BB-ADC");
#else
unload_device_tree("cape-bone-iio");
#endif
}
25 changes: 21 additions & 4 deletions source/common.c
Expand Up @@ -33,6 +33,11 @@ SOFTWARE.
#include <time.h>
#include "common.h"

#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,1,0)
#define BBBVERSION41
#endif

int setup_error = 0;
int module_setup = 0;

Expand Down Expand Up @@ -361,7 +366,16 @@ int get_spi_bus_path_number(unsigned int spi)
int load_device_tree(const char *name)
{
FILE *file = NULL;
#ifdef BBBVERSION41
char slots[41];
snprintf(ctrl_dir, sizeof(ctrl_dir), "/sys/devices/platform/bone_capemgr");
snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);
#else
char slots[40];
build_path("/sys/devices", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);
#endif

char line[256];

// build_path("/sys/devices/platform", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
Expand Down Expand Up @@ -396,13 +410,16 @@ int load_device_tree(const char *name)
int unload_device_tree(const char *name)
{
FILE *file = NULL;
#ifdef BBBVERSION41
char slots[41];
char line[256];
char *slot_line;

// 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);
#else
char slots[40];
build_path("/sys/devices", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
#endif
char line[256];
char *slot_line;

file = fopen(slots, "r+");
if (!file) {
Expand Down
8 changes: 7 additions & 1 deletion source/common.h
Expand Up @@ -37,7 +37,13 @@ SOFTWARE.
#define FILENAME_BUFFER_SIZE 128

int gpio_mode;
int gpio_direction[120];
#ifdef BBBVERSION41
char ctrl_dir[43];
char ocp_dir[33];
#else
char ctrl_dir[35];
char ocp_dir[25];
#endif

char ctrl_dir[43];
char ocp_dir[33];
Expand Down

3 comments on commit d16654d

@SimplicityGuy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Any plans to do a PR on this work into Adafruit's original repo? If so, I'll back out my PR that fix this in a slightly different way (w/o the macros, so my fix addresses this during runtime). I like your fix better though.

@PeteLawler
Copy link
Owner

@PeteLawler PeteLawler commented on d16654d May 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
I'm currently in two minds about whether to do a PR into Adafruit. Reading around their forums, it'd seem they've got someone to work on this themselves, so I don't particularly want to step on their toes. I did draw attention to this change to someone on their forums who seems BBB knowledgable and figured I'd give them a few weeks to check it out and see what they think before deciding what to do next.
One thing's recently come to mind is that the kernel version check is very lazy and could probably be tightened, I wouldn't be keen on doing a PR without fixing that a little first.
Also, looking over the code again, it'd seem I got an edit wrong from my test environment (which isn't connected to the Internet thus I did a bit of a manual hack change... I'll def have to check out the common.h change as that really doesn't look right)

@PeteLawler
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cleaned up the diff some more and submitted a PR. Thanks for poking me about this, Simplicity. It would've sat in the bottom drawer if it had not been for you πŸ‘

Please sign in to comment.