Skip to content

Commit 207526e

Browse files
committed
Move BBBVERSION41 pwm disable logic into pwm list iterator.
Outside the list iteration loop it causes a seg fault from writing to a closed/invalid handle if PWM.stop is called when the PWM is not open
1 parent bbac61a commit 207526e

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

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);

0 commit comments

Comments
 (0)