Skip to content

Commit 52d92f9

Browse files
authored
Merge pull request #110 from MatthewWest/develop
Read current duty_ns and pwm_ns before setting default values
2 parents 017383c + 0620b43 commit 52d92f9

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

source/c_pwm.c

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct pwm_exp
5858
};
5959
struct pwm_exp *exported_pwms = NULL;
6060

61-
struct pwm_exp *lookup_exported_pwm(const char *key)
61+
struct pwm_exp *lookup_exported_pwm(const char *key)
6262
{
6363
struct pwm_exp *pwm = exported_pwms;
6464

@@ -77,7 +77,7 @@ struct pwm_exp *lookup_exported_pwm(const char *key)
7777
void export_pwm(struct pwm_exp *new_pwm)
7878
{
7979
struct pwm_exp *pwm;
80-
80+
8181
if (exported_pwms == NULL)
8282
{
8383
// create new list
@@ -129,7 +129,6 @@ BBIO_err pwm_set_frequency(const char *key, float freq) {
129129

130130
period_ns = (unsigned long)(1e9 / freq);
131131

132-
133132
// If we're going to a shorter period, update the
134133
// duty cycle first, in order to avoid ever setting
135134
// the period < duty cycle (which would throw error)
@@ -258,7 +257,7 @@ BBIO_err pwm_set_duty_cycle(const char *key, float duty) {
258257

259258
if (pwm == NULL) {
260259
return BBIO_GEN;
261-
}
260+
}
262261

263262
pwm->duty = duty;
264263
pwm->duty_ns = (unsigned long)(pwm->period_ns * (duty / 100.0));
@@ -356,7 +355,7 @@ BBIO_err pwm_setup(const char *key, float duty, float freq, int polarity)
356355
} else {
357356
perror("stat");
358357
return BBIO_GEN;
359-
}
358+
}
360359
} else {
361360
if (S_ISDIR(s.st_mode)) {
362361
/* It is a directory. Already exported */
@@ -423,7 +422,7 @@ BBIO_err pwm_setup(const char *key, float duty, float freq, int polarity)
423422
snprintf(period_path, sizeof(period_path), "%s/period", pwm_path);
424423
snprintf(polarity_path, sizeof(polarity_path), "%s/polarity", pwm_path);
425424

426-
//add period and duty fd to pwm list
425+
//add period and duty fd to pwm list
427426
if ((period_fd = open(period_path, O_RDWR)) < 0)
428427
return BBIO_SYSFS;
429428

@@ -480,10 +479,8 @@ BBIO_err pwm_setup(const char *key, float duty, float freq, int polarity)
480479
BBIO_err pwm_start(const char *key, float duty, float freq, int polarity)
481480
{
482481
BBIO_err err;
483-
#ifdef BBBVERSION41
484482
char buffer[20];
485-
size_t len;
486-
#endif
483+
int len;
487484

488485
struct pwm_exp *pwm = lookup_exported_pwm(key);
489486
if (pwm == NULL) {
@@ -505,6 +502,38 @@ BBIO_err pwm_start(const char *key, float duty, float freq, int polarity)
505502
return err;
506503
}
507504

505+
// Read out current period_ns from the file, in order for it to behave
506+
// properly
507+
memset(buffer, 0, sizeof(buffer)); // Initialize buffer
508+
lseek(pwm->period_fd, 0, SEEK_SET);
509+
len = read(pwm->period_fd, buffer, sizeof(buffer));
510+
if (len < 0) {
511+
return BBIO_SYSFS;
512+
} else if (len >= sizeof(buffer)) {
513+
// If this is the case, there's more in the file.
514+
// This should never happen, as it would mean that
515+
// the period is 10^8 seconds
516+
return BBIO_SYSFS;
517+
}
518+
// Set the period_ns from the file
519+
sscanf(buffer, "%lu", &(pwm->period_ns));
520+
521+
// Read out the current duty_ns from the file, in order for it to
522+
// behave properly
523+
memset(buffer, 0, sizeof(buffer));
524+
lseek(pwm->duty_fd, 0, SEEK_SET);
525+
len = read(pwm->duty_fd, buffer, sizeof(buffer));
526+
if (len < 0) {
527+
return BBIO_SYSFS;
528+
} else if (len >= sizeof(buffer)) {
529+
// If this is the case, there's more in the file.
530+
// This should never happen, as it would mean that
531+
// the period is 10^8 seconds
532+
return BBIO_SYSFS;
533+
}
534+
// Set the duty_ns from the file
535+
sscanf(buffer, "%lu", &(pwm->duty_ns));
536+
508537
// Initialize pwm->duty to avoid weirdness
509538
pwm->duty = duty;
510539
err = pwm_set_frequency(key, freq);
@@ -541,7 +570,7 @@ BBIO_err pwm_disable(const char *key)
541570
char buffer[2];
542571
size_t len;
543572
pwm = lookup_exported_pwm(key);
544-
573+
545574
// Disable the PWM
546575
lseek(pwm->enable_fd, 0, SEEK_SET);
547576
len = snprintf(buffer, sizeof(buffer), "0");

0 commit comments

Comments
 (0)