Skip to content

Commit 095a9c1

Browse files
authored
Merge pull request #184 from adafruit/pwm-issue170
Fix polarity error in PWM.setup() for Linux 4.9
2 parents f194320 + d6bf8fd commit 095a9c1

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

source/c_pwm.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ BBIO_err initialize_pwm(void)
113113
return BBIO_OK;
114114
}
115115

116-
syslog(LOG_INFO, "initialize_pwm: OK");
116+
syslog(LOG_INFO, "Adafruit_BBIO: initialize_pwm: OK");
117117
return BBIO_OK;
118118
}
119119

@@ -558,7 +558,7 @@ BBIO_err pwm_setup(const char *key, __attribute__ ((unused)) float duty, __attri
558558

559559
BBIO_err pwm_start(const char *key, float duty, float freq, int polarity)
560560
{
561-
syslog(LOG_DEBUG, "pwm_start: %s, %f, %f, %i", key, duty, freq, polarity);
561+
syslog(LOG_DEBUG, "Adafruit_BBIO: pwm_start: %s, %f, %f, %i", key, duty, freq, polarity);
562562

563563
BBIO_err err;
564564
char buffer[20];
@@ -581,13 +581,6 @@ BBIO_err pwm_start(const char *key, float duty, float freq, int polarity)
581581
return BBIO_GEN;
582582
}
583583

584-
//FIXME: polarity set broken in v4.9.x/v4.14.x
585-
// err = pwm_set_polarity(key, polarity);
586-
// if (err != BBIO_OK) {
587-
// syslog(LOG_ERR, "pwm_start: %s couldn't set polarity: %i", key, err);
588-
// return err;
589-
// }
590-
591584
// Read out current period_ns from the file, in order for it to behave
592585
// properly
593586
memset(buffer, 0, sizeof(buffer)); // Initialize buffer
@@ -626,23 +619,33 @@ BBIO_err pwm_start(const char *key, float duty, float freq, int polarity)
626619

627620
// Initialize pwm->duty to avoid weirdness
628621
pwm->duty = duty;
622+
syslog(LOG_DEBUG, "Adafruit_BBIO: pwm_start: call pwm_set_frequency(key=%s freq=%f)", key, freq);
629623
err = pwm_set_frequency(key, freq);
630624
if (err != BBIO_OK) {
631625
syslog(LOG_ERR, "Adafruit_BBIO: pwm_start: %s couldn't set duty frequency: %i", key, err);
632626
return err;
633627
}
634628

629+
syslog(LOG_DEBUG, "Adafruit_BBIO: pwm_start: call pwm_set_duty_cycle(key=%s duty=%f)", key, duty);
635630
err = pwm_set_duty_cycle(key, duty);
636631
if (err != BBIO_OK) {
637632
syslog(LOG_ERR, "Adafruit_BBIO: pwm_start: %s couldn't set duty cycle: %i", key, err);
638633
return err;
639634
}
640635

636+
syslog(LOG_DEBUG, "Adafruit_BBIO: pwm_start: call pwm_set_polarity(key=%s polarity=%d)", key, polarity);
637+
err = pwm_set_polarity(key, polarity);
638+
if (err != BBIO_OK) {
639+
syslog(LOG_ERR, "Adafruit_BBIO: pwm_start: %s couldn't set polarity: %i", key, err);
640+
return err;
641+
}
642+
641643
#ifdef BBBVERSION41 // Enable the PWM (don't think it's necessary before 4.1+)
642644
if (pwm == NULL) {
643645
return BBIO_GEN;
644646
}
645647
len = snprintf(buffer, sizeof(buffer), "1");
648+
syslog(LOG_DEBUG, "Adafruit_BBIO: pwm_start: write(pwm->enable_fd, buffer=%s, len=%d)", buffer, len);
646649
lseek(pwm->enable_fd, 0, SEEK_SET);
647650
if (write(pwm->enable_fd, buffer, len) < 0) {
648651
syslog(LOG_ERR, "Adafruit_BBIO: pwm_start: %s couldn't write enable: %i-%s",

source/event_gpio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ int open_value_file(unsigned int gpio)
238238
// }
239239

240240
if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0) {
241-
syslog(LOG_ERR, "gpio open_value_file: %u couldn't open '%s': %i-%s",
241+
syslog(LOG_ERR, "Adafruit_BBIO: gpio open_value_file: %u couldn't open '%s': %i-%s",
242242
gpio, filename, errno, strerror(errno));
243243
return -1;
244244
}
@@ -259,7 +259,7 @@ BBIO_err gpio_unexport(unsigned int gpio)
259259
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
260260

261261
if ((fd = open(GPIO_UNEXPORT, O_WRONLY)) < 0) {
262-
syslog(LOG_ERR, "gpio_unexport: %u couldn't open '"GPIO_UNEXPORT"': %i-%s",
262+
syslog(LOG_ERR, "Adafruit_BBIO: gpio_unexport: %u couldn't open '"GPIO_UNEXPORT"': %i-%s",
263263
gpio, errno, strerror(errno));
264264
return BBIO_SYSFS;
265265
}

test/issue170-pwm.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Adafruit_BBIO.PWM as PWM
2+
PWM.start("P9_14", 50, 2000, 1)
3+
PWM.cleanup()
4+
PWM.start("P9_14", 50, 2000, 0)
5+
PWM.cleanup()

0 commit comments

Comments
 (0)