Skip to content

Commit c3c0d3e

Browse files
Paul OsbornePaul Osborne
authored andcommitted
core: changes based on testing on bbb
Still not working but I think this is more because of issues with the new capemgr in 4.1. The documentation for PWM seems to not match up perfectly with what is on devices. Signed-off-by: Paul Osborne <Paul.Osborne@digi.com>
1 parent c0751dd commit c3c0d3e

File tree

2 files changed

+32
-48
lines changed

2 files changed

+32
-48
lines changed

examples/breathe.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,43 @@
1212
extern crate sysfs_pwm;
1313
use sysfs_pwm::{Pwm, Result};
1414

15-
const RPI_PWM_CHIP: u32 = 1;
16-
const RPI_PWM_NUMBER: u32 = 127;
15+
// PIN: EHRPWM0A (P9_22)
16+
const BB_PWM_CHIP: u32 = 0;
17+
const BB_PWM_NUMBER: u32 = 0;
1718

1819
fn pwm_increase_to_max(pwm: &Pwm,
1920
duration_ms: u32,
20-
update_period: u32) -> Result<()> {
21-
let step: f32 = duration_ms as f32 / update_period as f32;
22-
let mut duty_cycle: f32 = 0.0;
21+
update_period_ms: u32) -> Result<()> {
22+
let step: f32 = duration_ms as f32 / update_period_ms as f32;
23+
let mut duty_cycle = 0.0;
24+
let period_ns: u32 = try!(pwm.get_period_ns());
2325
while duty_cycle < 1.0 {
24-
try!(pwm.set_duty_cycle(duty_cycle));
26+
try!(pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32));
2527
duty_cycle += step;
2628
}
27-
pwm.set_duty_cycle(1.0)
29+
pwm.set_duty_cycle_ns(period_ns)
2830
}
2931

3032
fn pwm_decrease_to_minimum(pwm: &Pwm,
3133
duration_ms: u32,
32-
update_period: u32) -> Result<()> {
33-
let step: f32 = duration_ms as f32 / update_period as f32;
34+
update_period_ms: u32) -> Result<()> {
35+
let step: f32 = duration_ms as f32 / update_period_ms as f32;
3436
let mut duty_cycle = 1.0;
37+
let period_ns: u32 = try!(pwm.get_period_ns());
3538
while duty_cycle > 0.0 {
36-
try!(pwm.set_duty_cycle(duty_cycle));
39+
try!(pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32));
3740
duty_cycle -= step;
3841
}
39-
pwm.set_duty_cycle(0.0)
42+
pwm.set_duty_cycle_ns(0)
4043
}
4144

4245
/// Make an LED "breathe" by increasing and
4346
/// decreasing the brightness
4447
fn main() {
45-
let pwm = Pwm::new(RPI_PWM_CHIP, RPI_PWM_NUMBER).unwrap(); // number depends on chip, etc.
48+
let pwm = Pwm::new(BB_PWM_CHIP, BB_PWM_NUMBER).unwrap(); // number depends on chip, etc.
4649
pwm.with_exported(|| {
47-
pwm.set_active(true).unwrap();
50+
pwm.enable(true).unwrap();
51+
pwm.set_period_ns(20_000).unwrap();
4852
loop {
4953
pwm_increase_to_max(&pwm, 1000, 20).unwrap();
5054
pwm_decrease_to_minimum(&pwm, 1000, 20).unwrap();

src/lib.rs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ use std::io::prelude::*;
1818
use std::os::unix::prelude::*;
1919
use std::fs::File;
2020
use std::fs;
21+
use std::fs::OpenOptions;
2122
use std::str::FromStr;
22-
use std::cmp::{min, max};
23-
use std::path::Path;
2423

2524
mod error;
2625
pub use error::Error;
@@ -45,11 +44,12 @@ pub enum Polarity {
4544
pub type Result<T> = ::std::result::Result<T, error::Error>;
4645

4746
/// Open the specified entry name as a writable file
48-
fn pwm_file_rw(chip: &PwmChip, pin: u32, name: &str) -> Result<File> {
49-
let f = try!(File::create(format!("/sys/class/pwm/pwmchip{}/pwm{}/{}",
50-
chip.number,
51-
pin,
52-
name)));
47+
fn pwm_file_wo(chip: &PwmChip, pin: u32, name: &str) -> Result<File> {
48+
let f = try!(OpenOptions::new().write(true)
49+
.open(format!("/sys/class/pwm/pwmchip{}/pwm{}/{}",
50+
chip.number,
51+
pin,
52+
name)));
5353
Ok(f)
5454
}
5555

@@ -64,7 +64,7 @@ fn pwm_file_parse<T: FromStr>(chip: &PwmChip, pin: u32, name: &str) -> Result<T>
6464
let mut s = String::with_capacity(10);
6565
let mut f = try!(pwm_file_ro(chip, pin, name));
6666
try!(f.read_to_string(&mut s));
67-
match s.parse::<T>() {
67+
match s.trim().parse::<T>() {
6868
Ok(r) => Ok(r),
6969
Err(_) => Err(Error::Unexpected(format!("Unexpeted value file contents: {:?}", s))),
7070
}
@@ -147,39 +147,19 @@ impl Pwm {
147147
}
148148

149149
/// Enable/Disable the PWM Signal
150-
pub fn set_active(&self, active: bool) -> Result<()> {
151-
let mut active_file = try!(pwm_file_rw(&self.chip, self.number, "active"));
152-
let contents = if active {
150+
pub fn enable(&self, enable: bool) -> Result<()> {
151+
let mut enable_file = try!(pwm_file_wo(&self.chip, self.number, "enable"));
152+
let contents = if enable {
153153
"1"
154154
} else {
155155
"0"
156156
};
157-
try!(active_file.write_all(contents.as_bytes()));
158-
Ok(())
159-
}
160-
161-
/// Get the currently configured duty cycle as a percentage
162-
pub fn get_duty_cycle(&self) -> Result<f32> {
163-
let raw_duty_cycle = try!(pwm_file_parse::<u32>(&self.chip, self.number, "duty"));
164-
Ok((raw_duty_cycle as f32) / 1000.0)
165-
}
166-
167-
/// Set the duty cycle as a percentage of time active
168-
///
169-
/// This value is expected to be a floating point value
170-
/// between 0.0 and 1.0. It maps to a value with resolution 0 -
171-
/// 1000. Values < 0 or > 1.0 are capped at the minimum or
172-
/// maximum respectively.
173-
pub fn set_duty_cycle(&self, percent: f32) -> Result<()> {
174-
let raw_percent_adj: u32 = (percent * 1000.0).floor() as u32;
175-
let percent_adj: u32 = max(0, min(raw_percent_adj, 1000));
176-
let mut dc_file = try!(pwm_file_rw(&self.chip, self.number, "duty"));
177-
try!(dc_file.write_all(format!("{}", percent_adj).as_bytes()));
157+
try!(enable_file.write_all(contents.as_bytes()));
178158
Ok(())
179159
}
180160

181161
/// Get the currently configured duty_cycle in nanoseconds
182-
pub fn get_duty_cycle_ns(&self, duty_cycle_ns: u32) -> Result<u32> {
162+
pub fn get_duty_cycle_ns(&self) -> Result<u32> {
183163
pwm_file_parse::<u32>(&self.chip, self.number, "duty_cycle")
184164
}
185165

@@ -188,7 +168,7 @@ impl Pwm {
188168
/// Value is in nanoseconds and must be less than the period.
189169
pub fn set_duty_cycle_ns(&self, duty_cycle_ns: u32) -> Result<()> {
190170
// we'll just let the kernel do the validation
191-
let mut duty_cycle_file = try!(pwm_file_rw(&self.chip, self.number, "duty_cycle"));
171+
let mut duty_cycle_file = try!(pwm_file_wo(&self.chip, self.number, "duty_cycle"));
192172
try!(duty_cycle_file.write_all(format!("{}", duty_cycle_ns).as_bytes()));
193173
Ok(())
194174
}
@@ -200,7 +180,7 @@ impl Pwm {
200180

201181
/// The period of the PWM signal in Nanoseconds
202182
pub fn set_period_ns(&self, period_ns: u32) -> Result<()> {
203-
let mut period_file = try!(pwm_file_rw(&self.chip, self.number, "period"));
183+
let mut period_file = try!(pwm_file_wo(&self.chip, self.number, "period"));
204184
try!(period_file.write_all(format!("{}", period_ns).as_bytes()));
205185
Ok(())
206186
}

0 commit comments

Comments
 (0)