Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a PWM example for the Matrix Portal M4 #721

Merged
merged 8 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions boards/matrix_portal_m4/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

-

# v0.1.1

- Added RedLedPwm alias to onboard led

# v0.1.0

- Added blinky_basic.rs example
Expand Down
6 changes: 5 additions & 1 deletion boards/matrix_portal_m4/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "matrix_portal_m4"
version = "0.1.0"
version = "0.1.1"
authors = ["Salsa Steve <elmanstevelaguna@gmail.com>"]
description = "Board Support crate for the Matrix Portal M4"
keywords = ["no-std", "arm", "cortex-m", "embedded-hal"]
Expand Down Expand Up @@ -43,3 +43,7 @@ usb = ["atsamd-hal/usb", "usb-device"]

[[example]]
name = "blinky_basic"

[[example]]
name = "pwm"
required-features = ["unproven"]
13 changes: 12 additions & 1 deletion boards/matrix_portal_m4/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ $ cargo hf2 --release --example blinky_basic --vid 0x239a --pid 0x00c9
Flashing "/Users/User/atsamd/boards/matrix_portal_m4/target/thumbv7em-none-eabihf/release/examples/blinky_basic"
Finished in 0.051s
$
```
```
```
$ 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)
57 changes: 57 additions & 0 deletions boards/matrix_portal_m4/examples/pwm.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions boards/matrix_portal_m4/src/pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ hal::bsp_pins!(
aliases: {
PushPullOutput: RedLed,
Reset: RedLedReset,
AlternateG: RedLedPwm,
}
}

Expand Down