Skip to content

Commit e7e987a

Browse files
authored
Merge pull request #119 from JesseMcL/pr
Add GPIO pullup configurations and fix PWM Segfault on kernel 4.1+
2 parents 1b04cdf + 6d4ada1 commit e7e987a

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
classifiers = classifiers,
4141
packages = find_packages(),
4242
py_modules = ['Adafruit_I2C'],
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),
43+
ext_modules = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/c_pinmux.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
4444
Extension('Adafruit_BBIO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/c_pinmux.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),
4545
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),
4646
Extension('Adafruit_BBIO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'], define_macros=kernel41),

source/c_pinmux.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,25 @@ BBIO_err set_pin_mode(const char *key, const char *mode)
1010
// char ocp_dir[] defined in common.h
1111
char path[60]; // "/sys/devices/platform/ocp/ocp:P#_##_pinmux/state"
1212
char pinmux_dir[20]; // "ocp:P#_##_pinmux"
13+
char pin[6]; //"P#_##"
1314
FILE *f = NULL;
15+
16+
if (strlen(key) == 4) // Key P#_# format, must inject '0' to be P#_0#
17+
snprintf(pin, sizeof(pin), "%.3s0%c", key,key[3]);
18+
else //copy string
19+
snprintf(pin, sizeof(pin), "%s", key);
1420

1521
#ifndef BBBVERSION41
1622
BBIO_err err;
17-
err = build_path("/sys/devices", "ocp", ocp_dir, sizeof(ocp_dir));
23+
err = build_path("/sys/devices/platform", "ocp", ocp_dir, sizeof(ocp_dir));
1824
if (err != BBIO_OK) {
1925
return err;
2026
}
2127
#else
2228
strncpy(ocp_dir, "/sys/devices/platform/ocp", sizeof(ocp_dir));
2329
#endif
2430

25-
snprintf(pinmux_dir, sizeof(pinmux_dir), "ocp:%s_pinmux", key);
31+
snprintf(pinmux_dir, sizeof(pinmux_dir), "ocp:%s_pinmux", pin);
2632
snprintf(path, sizeof(path), "%s/%s/state", ocp_dir, pinmux_dir);
2733

2834
f = fopen(path, "w");

source/c_pwm.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -566,22 +566,7 @@ BBIO_err pwm_disable(const char *key)
566566
struct pwm_exp *pwm, *temp, *prev_pwm = NULL;
567567
BBIO_err err;
568568

569-
#ifdef BBBVERSION41
570-
char buffer[2];
571-
size_t len;
572-
pwm = lookup_exported_pwm(key);
573-
574-
// Disable the PWM
575-
lseek(pwm->enable_fd, 0, SEEK_SET);
576-
len = snprintf(buffer, sizeof(buffer), "0");
577-
if (write(pwm->enable_fd, buffer, len) < 0) {
578-
return BBIO_SYSFS;
579-
}
580-
581-
// Unexport the PWM
582-
// TODO later
583-
584-
#else
569+
#ifndef BBBVERSION41
585570
char fragment[18];
586571
snprintf(fragment, sizeof(fragment), "bone_pwm_%s", key);
587572
err = unload_device_tree(fragment);
@@ -595,6 +580,22 @@ BBIO_err pwm_disable(const char *key)
595580
{
596581
if (strcmp(pwm->key, key) == 0)
597582
{
583+
584+
#ifdef BBBVERSION41
585+
char buffer[2];
586+
size_t len;
587+
588+
// Disable the PWM
589+
lseek(pwm->enable_fd, 0, SEEK_SET);
590+
len = snprintf(buffer, sizeof(buffer), "0");
591+
if (write(pwm->enable_fd, buffer, len) < 0) {
592+
return BBIO_SYSFS;
593+
}
594+
595+
// Unexport the PWM
596+
// TODO later
597+
#endif
598+
598599
//close the fd
599600
close(pwm->period_fd);
600601
close(pwm->duty_fd);

source/py_gpio.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SOFTWARE.
3232
#include "constants.h"
3333
#include "common.h"
3434
#include "event_gpio.h"
35+
#include "c_pinmux.h"
3536

3637
static int gpio_warnings = 1;
3738

@@ -111,7 +112,12 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar
111112
if (direction == OUTPUT) {
112113
gpio_set_value(gpio, initial);
113114
} else {
114-
gpio_set_value(gpio, pud);
115+
if (pud == PUD_DOWN)
116+
set_pin_mode(channel, "gpio_pd");
117+
else if (pud == PUD_UP)
118+
set_pin_mode(channel, "gpio_pu");
119+
else
120+
set_pin_mode(channel, "gpio");
115121
}
116122

117123
gpio_direction[gpio] = direction;

0 commit comments

Comments
 (0)