diff --git a/boards/matrix_portal_m4/CHANGELOG.md b/boards/matrix_portal_m4/CHANGELOG.md index 7a6c21934a04..09849a885b5f 100644 --- a/boards/matrix_portal_m4/CHANGELOG.md +++ b/boards/matrix_portal_m4/CHANGELOG.md @@ -4,6 +4,7 @@ # v0.1.0 +- Added RedLedPwm alias to onboard led - Added blinky_basic.rs example - Added pin mapping to pin.rs diff --git a/boards/matrix_portal_m4/Cargo.toml b/boards/matrix_portal_m4/Cargo.toml index 27731af88e3f..02761339462b 100644 --- a/boards/matrix_portal_m4/Cargo.toml +++ b/boards/matrix_portal_m4/Cargo.toml @@ -43,3 +43,7 @@ usb = ["atsamd-hal/usb", "usb-device"] [[example]] name = "blinky_basic" + +[[example]] +name = "pwm" +required-features = ["unproven"] diff --git a/boards/matrix_portal_m4/README.md b/boards/matrix_portal_m4/README.md index d3713264a54a..bad711889405 100644 --- a/boards/matrix_portal_m4/README.md +++ b/boards/matrix_portal_m4/README.md @@ -15,12 +15,23 @@ https://github.com/atsamd-rs/atsamd/tree/master/boards/matrix_portal_m4/examples * Be in this directory `cd boards/matrix_portal_m4` * Put your device in bootloader mode usually by hitting the reset button twice. * Build and upload in one step -``` +```Shell $ cargo hf2 --release --example blinky_basic --vid 0x239a --pid 0x00c9 Finished release [optimized] target(s) in 0.74s Trying Ok(Some("Adafruit Industries")) Ok(Some("Matrix Portal M4")) Flashing "/Users/User/atsamd/boards/matrix_portal_m4/target/thumbv7em-none-eabihf/release/examples/blinky_basic" Finished in 0.051s -$ -``` \ No newline at end of file +``` + +```Shell +$ cargo hf2 --release --example pwm --vid 0x239a --pid 0x00c9 --features="unproven" + Finished release [optimized] target(s) in 0.67s + Trying Ok(Some("Adafruit Industries")) Ok(Some("Matrix Portal M4")) + Flashing "/Users/User/atsamd/boards/matrix_portal_m4/target/thumbv7em-none-eabihf/release/examples/pwm" + Finished in 0.146s +``` + +## Matrix Portal M4 +[PCB DOCs](https://github.com/adafruit/Adafruit-MatrixPortal-M4-PCB/tree/main) +[Adafruit Pinout](https://cdn-learn.adafruit.com/assets/assets/000/111/881/original/led_matrices_Adafruit_MatrixPortal_M4_Pinout.png?1653078587) \ No newline at end of file diff --git a/boards/matrix_portal_m4/examples/pwm.rs b/boards/matrix_portal_m4/examples/pwm.rs new file mode 100644 index 000000000000..4264e0d9060b --- /dev/null +++ b/boards/matrix_portal_m4/examples/pwm.rs @@ -0,0 +1,57 @@ +#![no_std] +#![no_main] + +use matrix_portal_m4::{entry, hal, Pins, RedLedPwm}; +use panic_halt as _; + +use hal::clock::GenericClockController; +use hal::delay::Delay; +use hal::fugit::RateExtU32; +use hal::pac::{CorePeripherals, Peripherals}; +use hal::prelude::*; +use hal::pwm::{Channel, TCC1Pinout, Tcc1Pwm}; + +/// Entry point for the PWM example on Matrix Portal M4. +/// +/// This function sets up the necessary peripherals for PWM control of the +/// onboard LED. It uses TCC1 for PWM, setting different duty cycles to vary the +/// brightness of the LED. The LED brightness alternates between maximum and +/// minimum (1/8th of max) every second. +/// +/// Note: The LED is connected to channel 2 of TCC1 as per the Adafruit Matrix +/// Portal M4 pinout. +#[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); + let pins = Pins::new(peripherals.PORT); + let red_led: RedLedPwm = pins.led.into(); + + let gclk0 = clocks.gclk0(); + let mut tcc1pwm = Tcc1Pwm::new( + &clocks.tcc0_tcc1(&gclk0).unwrap(), + 1.kHz(), + peripherals.TCC1, + TCC1Pinout::Pa14(red_led), + &mut peripherals.MCLK, + ); + let max_duty = tcc1pwm.get_max_duty(); + let min_duty = max_duty / 8; + + loop { + tcc1pwm.set_duty(Channel::_2, max_duty); + delay.delay_ms(1000u16); + tcc1pwm.set_duty(Channel::_2, min_duty); + delay.delay_ms(1000u16); + } +} diff --git a/boards/matrix_portal_m4/src/pins.rs b/boards/matrix_portal_m4/src/pins.rs index 5455b4e3bd83..408da8d38f86 100644 --- a/boards/matrix_portal_m4/src/pins.rs +++ b/boards/matrix_portal_m4/src/pins.rs @@ -292,6 +292,7 @@ hal::bsp_pins!( aliases: { PushPullOutput: RedLed, Reset: RedLedReset, + AlternateG: RedLedPwm, } }