Skip to content

Commit

Permalink
pygamer pwm examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobrosenthal committed Aug 29, 2020
1 parent 1a88d31 commit bec8c11
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
8 changes: 8 additions & 0 deletions boards/pygamer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ required-features = ["sd-card"]
[[example]]
name = "neopixel_easing"
required-features = ["math"]

[[example]]
name = "pwm_tc4"
required-features = ["math"]

[[example]]
name = "pwm_tcc0"
required-features = ["math"]
62 changes: 62 additions & 0 deletions boards/pygamer/examples/pwm_tc4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! Breath an led in and out without using the BSP split() method.
#![no_std]
#![no_main]

use panic_halt as _;
use pygamer as hal;

use core::f32::consts::FRAC_PI_2;
use hal::clock::GenericClockController;
use hal::delay::Delay;
use hal::entry;
use hal::pac::{CorePeripherals, Peripherals};
use hal::prelude::*;
use hal::pwm::{Pwm4, TC4Pinout};
use micromath::F32Ext;

#[entry]
fn main() -> ! {
let mut peripherals = Peripherals::take().unwrap();
let core = CorePeripherals::take().unwrap();
let mut clocks = GenericClockController::with_internal_32kosc(
peripherals.GCLK,
&mut peripherals.MCLK,
&mut peripherals.OSC32KCTRL,
&mut peripherals.OSCCTRL,
&mut peripherals.NVMCTRL,
);

let mut delay = Delay::new(core.SYST, &mut clocks);
delay.delay_ms(400u16);

let mut pins = hal::Pins::new(peripherals.PORT);

let gclk = clocks.gclk0();

let mut pwm0 = Pwm4::new(
&clocks.tc4_tc5(&gclk).unwrap(),
1.khz(),
peripherals.TC4,
TC4Pinout::Pa23(pins.d13.into_function_e(&mut pins.port)),
&mut peripherals.MCLK,
);
let max_duty = pwm0.get_max_duty();

loop {
for j in 0..255 {
pwm0.set_duty(sine_ease_in(j as f32, 0.0, max_duty.into(), 255.0) as u16);
delay.delay_ms(10u16);
}
for j in (0..255).rev() {
pwm0.set_duty(sine_ease_in(j as f32, 0.0, max_duty.into(), 255.0) as u16);
delay.delay_ms(10u16);
}
}
}

#[inline]
// current step, where oputput starts, where output ends, last step
fn sine_ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 {
-c * (t / d * FRAC_PI_2).cos() + c + b
}
68 changes: 68 additions & 0 deletions boards/pygamer/examples/pwm_tcc0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! Blink an led without using the BSP split() method.
#![no_std]
#![no_main]

use panic_halt as _;
use pygamer as hal;

use core::f32::consts::FRAC_PI_2;
use hal::clock::GenericClockController;
use hal::delay::Delay;
use hal::entry;
use hal::pac::{CorePeripherals, Peripherals};
use hal::prelude::*;
use hal::pwm::{Channel, TCC0Pinout, Tcc0Pwm};
use micromath::F32Ext;

#[entry]
fn main() -> ! {
let mut peripherals = Peripherals::take().unwrap();
let core = CorePeripherals::take().unwrap();
let mut clocks = GenericClockController::with_internal_32kosc(
peripherals.GCLK,
&mut peripherals.MCLK,
&mut peripherals.OSC32KCTRL,
&mut peripherals.OSCCTRL,
&mut peripherals.NVMCTRL,
);

let mut delay = Delay::new(core.SYST, &mut clocks);
delay.delay_ms(400u16);

let mut pins = hal::Pins::new(peripherals.PORT);

let gclk = clocks.gclk0();

let mut pwm0 = Tcc0Pwm::new(
&clocks.tcc0_tcc1(&gclk).unwrap(),
1.khz(),
peripherals.TCC0,
TCC0Pinout::Pa23(pins.d13.into_function_g(&mut pins.port)),
&mut peripherals.MCLK,
);
let max_duty = pwm0.get_max_duty();

loop {
for j in 0..255 {
pwm0.set_duty(
Channel::_3,
sine_ease_in(j as f32, 0.0, max_duty as f32, 255.0) as u32,
);
delay.delay_ms(10u16);
}
for j in (0..255).rev() {
pwm0.set_duty(
Channel::_3,
sine_ease_in(j as f32, 0.0, max_duty as f32, 255.0) as u32,
);
delay.delay_ms(10u16);
}
}
}

#[inline]
// current step, where oputput starts, where output ends, last step
fn sine_ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 {
-c * (t / d * FRAC_PI_2).cos() + c + b
}

0 comments on commit bec8c11

Please sign in to comment.