Skip to content

Adding support for rp235x-hal #40

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build and Test check
jobs:
builds:
name: Build checks
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
strategy:
matrix:
mode: ["", "--release"]
Expand Down
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ documentation = "https://docs.rs/ws2812-pio"
repository = "https://github.com/rp-rs/ws2812-pio-rs/"

[dependencies]
cfg-if = "1.0"
embedded-hal = "0.2.5"
fugit = "0.3.5"
rp2040-hal = "0.11"
pio = "0.2.0"
smart-leds-trait = "0.3"
smart-leds-trait-0-2 = { package = "smart-leds-trait", version = "0.2.1" }
nb = "1.0.0"
cortex-m = "0.7.3"
rp2040-hal = { version = "0.11.0", optional = true }
rp235x-hal = { version = "0.2.0", optional = true }

[features]
default = ["rp2040"]
rp2040 = ["dep:rp2040-hal"]
rp235x = ["dep:rp235x-hal"]
36 changes: 25 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@

use embedded_hal::timer::CountDown;
use fugit::{ExtU32, HertzU32, MicrosDurationU32};
use rp2040_hal::{

use cfg_if::cfg_if;

cfg_if! {
if #[cfg(feature = "rp2040")] {
use rp2040_hal as hal;
} else if #[cfg(feature = "rp235x")] {
use rp235x_hal as hal;
} else {
compile_error!("Either 'rp2040' or 'rp235x' feature must be enabled.");
}
}

use hal::{
gpio::AnyPin,
pio::{PIOExt, StateMachineIndex, Tx, UninitStateMachine, PIO},
};

use smart_leds_trait::SmartLedsWrite;
use smart_leds_trait_0_2::SmartLedsWrite as SmartLedsWrite02;

Expand All @@ -33,9 +47,9 @@ use smart_leds_trait_0_2::SmartLedsWrite as SmartLedsWrite02;
///
/// Typical usage example:
///```ignore
/// use rp2040_hal::clocks::init_clocks_and_plls;
/// use hal::clocks::init_clocks_and_plls;
/// let clocks = init_clocks_and_plls(...);
/// let pins = rp2040_hal::gpio::pin::bank0::Pins::new(...);
/// let pins = hal::gpio::pin::bank0::Pins::new(...);
///
/// let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
/// let mut ws = Ws2812Direct::new(
Expand All @@ -54,6 +68,7 @@ use smart_leds_trait_0_2::SmartLedsWrite as SmartLedsWrite02;
/// delay_for_at_least_60_microseconds();
/// };
///```

pub struct Ws2812Direct<P, SM, I>
where
I: AnyPin<Function = P::PinFunction>,
Expand Down Expand Up @@ -126,20 +141,20 @@ where
let frac: u8 = frac as u8;

let pin = pin.into();
let (mut sm, _, tx) = rp2040_hal::pio::PIOBuilder::from_installed_program(installed)
let (mut sm, _, tx) = hal::pio::PIOBuilder::from_installed_program(installed)
// only use TX FIFO
.buffers(rp2040_hal::pio::Buffers::OnlyTx)
.buffers(hal::pio::Buffers::OnlyTx)
// Pin configuration
.side_set_pin_base(pin.id().num)
// OSR config
.out_shift_direction(rp2040_hal::pio::ShiftDirection::Left)
.out_shift_direction(hal::pio::ShiftDirection::Left)
.autopull(true)
.pull_threshold(24)
.clock_divisor_fixed_point(int, frac)
.build(sm);

// Prepare pin's direction.
sm.set_pindirs([(pin.id().num, rp2040_hal::pio::PinDir::Output)]);
sm.set_pindirs([(pin.id().num, hal::pio::PinDir::Output)]);

sm.start();

Expand Down Expand Up @@ -213,9 +228,9 @@ where
///
/// Typical usage example:
///```ignore
/// use rp2040_hal::clocks::init_clocks_and_plls;
/// use hal::clocks::init_clocks_and_plls;
/// let clocks = init_clocks_and_plls(...);
/// let pins = rp2040_hal::gpio::pin::bank0::Pins::new(...);
/// let pins = hal::gpio::pin::bank0::Pins::new(...);
///
/// let timer = Timer::new(pac.TIMER, &mut pac.RESETS);
///
Expand All @@ -237,6 +252,7 @@ where
/// // Do other stuff here...
/// };
///```

pub struct Ws2812<P, SM, C, I>
where
C: CountDown,
Expand Down Expand Up @@ -268,7 +284,6 @@ where
Self { driver, cd }
}
}

impl<P, SM, I, C> SmartLedsWrite for Ws2812<P, SM, C, I>
where
C: CountDown,
Expand All @@ -293,7 +308,6 @@ where
SmartLedsWrite::write(&mut self.driver, iterator)
}
}

impl<P, SM, I, C> SmartLedsWrite02 for Ws2812<P, SM, C, I>
where
C: CountDown,
Expand Down