Skip to content

Commit a916340

Browse files
committed
Merge pull request #96 from PeteLawler/master
Make BBIO 4.1+ kernel friendly
2 parents 2eaf223 + dacf24a commit a916340

File tree

4 files changed

+84
-20
lines changed

4 files changed

+84
-20
lines changed

setup.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
66
pass
77

88
import distribute_setup
9+
import sys
10+
import platform
911
distribute_setup.use_setuptools()
1012
from setuptools import setup, Extension, find_packages
1113

14+
kernel = platform.release()
15+
16+
if kernel >= '4.1.0':
17+
kernel41 = [('BBBVERSION41', None)]
18+
else:
19+
kernel41 = None
20+
1221
classifiers = ['Development Status :: 3 - Alpha',
1322
'Operating System :: POSIX :: Linux',
1423
'License :: OSI Approved :: MIT License',
@@ -31,8 +40,9 @@
3140
classifiers = classifiers,
3241
packages = find_packages(),
3342
py_modules = ['Adafruit_I2C'],
34-
ext_modules = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
35-
Extension('Adafruit_BBIO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
36-
Extension('Adafruit_BBIO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
37-
Extension('Adafruit_BBIO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
38-
Extension('Adafruit_BBIO.UART', ['source/py_uart.c', 'source/c_uart.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'])])
43+
ext_modules = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
44+
Extension('Adafruit_BBIO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
45+
Extension('Adafruit_BBIO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
46+
Extension('Adafruit_BBIO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
47+
Extension('Adafruit_BBIO.UART', ['source/py_uart.c', 'source/c_uart.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41)] )
48+

source/c_adc.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,75 @@ SOFTWARE.
3030
#include "c_adc.h"
3131
#include "common.h"
3232

33-
char adc_prefix_dir[40];
33+
#ifdef BBBVERSION41
34+
char adc_prefix_dir[49];
35+
#else
36+
char adc_prefix_dir[40];
37+
#endif
3438

3539
int adc_initialized = 0;
3640

3741
int initialize_adc(void)
3842
{
43+
#ifdef BBBVERSION41
44+
char test_path[49];
45+
#else
3946
char test_path[40];
47+
#endif
4048
FILE *fh;
4149
if (adc_initialized) {
4250
return 1;
4351
}
4452

53+
#ifdef BBBVERSION41
54+
if (load_device_tree("BB-ADC")) {
55+
strncat(adc_prefix_dir, "/sys/bus/iio/devices/iio:device0/in_voltage", sizeof(adc_prefix_dir));
56+
snprintf(test_path, sizeof(test_path), "%s%d_raw", adc_prefix_dir, 1);
57+
fh = fopen(test_path, "r");
58+
59+
if (!fh) {
60+
puts("wiiii");
61+
return 0;
62+
}
63+
fclose(fh);
64+
65+
adc_initialized = 1;
66+
return 1;
67+
}
68+
#else
4569
if (load_device_tree("cape-bone-iio")) {
4670
build_path("/sys/devices", "ocp.", ocp_dir, sizeof(ocp_dir));
4771
build_path(ocp_dir, "helper.", adc_prefix_dir, sizeof(adc_prefix_dir));
4872
strncat(adc_prefix_dir, "/AIN", sizeof(adc_prefix_dir));
49-
50-
// Test that the directory has an AIN entry (found correct devicetree)
5173
snprintf(test_path, sizeof(test_path), "%s%d", adc_prefix_dir, 0);
52-
5374
fh = fopen(test_path, "r");
5475

5576
if (!fh) {
56-
return 0;
77+
return 0;
5778
}
5879
fclose(fh);
5980

6081
adc_initialized = 1;
6182
return 1;
6283
}
84+
#endif
6385

6486
return 0;
6587
}
6688

6789
int read_value(unsigned int ain, float *value)
6890
{
6991
FILE * fh;
92+
#ifdef BBBVERSION41
93+
char ain_path[49];
94+
snprintf(ain_path, sizeof(ain_path), "%s%d_raw", adc_prefix_dir, ain);
95+
#else
7096
char ain_path[40];
97+
snprintf(ain_path, sizeof(ain_path), "%s%d", adc_prefix_dir, ain);
98+
#endif
99+
71100
int err, try_count=0;
72101
int read_successful;
73-
snprintf(ain_path, sizeof(ain_path), "%s%d", adc_prefix_dir, ain);
74102

75103
read_successful = 0;
76104

@@ -106,5 +134,9 @@ int adc_setup()
106134

107135
void adc_cleanup(void)
108136
{
137+
#ifdef BBBVERSION41
138+
unload_device_tree("BB-ADC");
139+
#else
109140
unload_device_tree("cape-bone-iio");
141+
#endif
110142
}

source/common.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Copyright (c) 2013 Adafruit
33
44
Original RPi.GPIO Author Ben Croston
55
Modified for BBIO Author Justin Cooper
6+
Modified for 4.1+ kernels by Grizmio
7+
Unified for 3.8 and 4.1+ kernels by Peter Lawler <relwalretep@gmail.com>
68
79
This file incorporates work covered by the following copyright and
810
permission notice, all modified code adopts the original license:
@@ -33,6 +35,11 @@ SOFTWARE.
3335
#include <time.h>
3436
#include "common.h"
3537

38+
#include <linux/version.h>
39+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,1,0)
40+
#define BBBVERSION41
41+
#endif
42+
3643
int setup_error = 0;
3744
int module_setup = 0;
3845

@@ -361,10 +368,16 @@ int get_spi_bus_path_number(unsigned int spi)
361368
int load_device_tree(const char *name)
362369
{
363370
FILE *file = NULL;
364-
char slots[40];
371+
#ifdef BBBVERSION41
372+
char slots[41];
373+
snprintf(ctrl_dir, sizeof(ctrl_dir), "/sys/devices/platform/bone_capemgr");
374+
#else
375+
char slots[40];
376+
build_path("/sys/devices", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
377+
#endif
378+
365379
char line[256];
366380

367-
build_path("/sys/devices", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
368381
snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);
369382

370383
file = fopen(slots, "r+");
@@ -394,12 +407,16 @@ int load_device_tree(const char *name)
394407
int unload_device_tree(const char *name)
395408
{
396409
FILE *file = NULL;
410+
#ifdef BBBVERSION41
411+
char slots[41];
412+
snprintf(ctrl_dir, sizeof(ctrl_dir), "/sys/devices/platform/bone_capemgr");
413+
snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);
414+
#else
397415
char slots[40];
398-
char line[256];
399-
char *slot_line;
400-
401416
build_path("/sys/devices", "bone_capemgr", ctrl_dir, sizeof(ctrl_dir));
402-
snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);
417+
#endif
418+
char line[256];
419+
char *slot_line;
403420

404421
file = fopen(slots, "r+");
405422
if (!file) {

source/common.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ SOFTWARE.
3939
int gpio_mode;
4040
int gpio_direction[120];
4141

42-
char ctrl_dir[35];
43-
char ocp_dir[25];
44-
42+
#ifdef BBBVERSION41
43+
char ctrl_dir[43];
44+
char ocp_dir[33];
45+
#else
46+
char ctrl_dir[35];
47+
char ocp_dir[25];
48+
#endif
49+
4550
int get_gpio_number(const char *key, unsigned int *gpio);
4651
int get_pwm_key(const char *input, char *key);
4752
int get_adc_ain(const char *key, unsigned int *ain);

0 commit comments

Comments
 (0)