Skip to content

Commit

Permalink
Change the examples to use EH 1.0 traits where possible.
Browse files Browse the repository at this point in the history
Adds notes where this isn't (yet) possible.

This also involved adding an inherent method to PWM Channels, so you can enable or disable them without using the old trait.
  • Loading branch information
jonathanpallant committed Jan 12, 2024
1 parent d1c49ae commit 2e5237a
Show file tree
Hide file tree
Showing 24 changed files with 120 additions and 80 deletions.
1 change: 1 addition & 0 deletions rp2040-hal/examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rp2040_hal as hal;

// Some traits we need
use core::fmt::Write;
// Embedded HAL 1.0.0 doesn't have an ADC trait, so use the one from 0.2
use embedded_hal_0_2::adc::OneShot;
use hal::fugit::RateExtU32;
use rp2040_hal::Clock;
Expand Down
4 changes: 2 additions & 2 deletions rp2040-hal/examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use rp2040_hal as hal;
use hal::pac;

// Some traits we need
use embedded_hal_0_2::blocking::delay::DelayMs;
use embedded_hal_0_2::digital::v2::OutputPin;
use embedded_hal::delay::DelayNs;
use embedded_hal::digital::OutputPin;

/// The linker will place this boot block at the start of our program image. We
/// need this to help the ROM bootloader get our code up and running.
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/dht11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rp2040_hal as hal;
use hal::pac;

// Some traits we need
use embedded_hal_0_2::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use hal::Clock;

/// The linker will place this boot block at the start of our program image. We
Expand Down
4 changes: 2 additions & 2 deletions rp2040-hal/examples/gpio_dyn_pin_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use rp2040_hal as hal;
use hal::pac;

// Some traits we need
use embedded_hal_0_2::blocking::delay::DelayMs;
use embedded_hal_0_2::digital::v2::OutputPin;
use embedded_hal::delay::DelayNs;
use embedded_hal::digital::OutputPin;

/// The linker will place this boot block at the start of our program image. We
/// need this to help the ROM bootloader get our code up and running.
Expand Down
6 changes: 3 additions & 3 deletions rp2040-hal/examples/gpio_in_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use rp2040_hal as hal;
use hal::pac;

// Some traits we need
use embedded_hal_0_2::digital::v2::InputPin;
use embedded_hal_0_2::digital::v2::OutputPin;
use embedded_hal::digital::InputPin;
use embedded_hal::digital::OutputPin;

/// The linker will place this boot block at the start of our program image. We
/// need this to help the ROM bootloader get our code up and running.
Expand Down Expand Up @@ -78,7 +78,7 @@ fn main() -> ! {
let mut out_pin = pins.gpio25.into_push_pull_output();

// Configure GPIO 23 as an input
let in_pin = pins.gpio23.into_pull_down_input();
let mut in_pin = pins.gpio23.into_pull_down_input();

// Output is the opposite of the input
loop {
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/gpio_irq_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use rp2040_hal as hal;
use hal::pac;

// Some traits we need
use embedded_hal_0_2::digital::v2::ToggleableOutputPin;
use embedded_hal::digital::StatefulOutputPin;

// Our interrupt macro
use hal::pac::interrupt;
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/mem_to_mem_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use cortex_m::singleton;
use cortex_m_rt::entry;
use embedded_hal_0_2::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use hal::dma::{single_buffer, DMAExt};
use hal::pac;
use panic_halt as _;
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/multicore_fifo_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use rp2040_hal as hal;
use hal::pac;

// Some traits we need
use embedded_hal_0_2::digital::v2::ToggleableOutputPin;
use embedded_hal::digital::StatefulOutputPin;

/// The linker will place this boot block at the start of our program image. We
/// need this to help the ROM bootloader get our code up and running.
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/multicore_polyblink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rp2040_hal as hal;
use hal::pac;

// Some traits we need
use embedded_hal_0_2::digital::v2::ToggleableOutputPin;
use embedded_hal::digital::StatefulOutputPin;

/// The linker will place this boot block at the start of our program image. We
/// need this to help the ROM bootloader get our code up and running.
Expand Down
6 changes: 3 additions & 3 deletions rp2040-hal/examples/pwm_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use panic_halt as _;
use rp2040_hal as hal;

// Some traits we need
use embedded_hal_0_2::PwmPin;
use embedded_hal::pwm::SetDutyCycle;
use rp2040_hal::clocks::Clock;

// A shorter alias for the Peripheral Access Crate, which provides low-level
Expand Down Expand Up @@ -105,13 +105,13 @@ fn main() -> ! {
// Ramp brightness up
for i in LOW..=HIGH {
delay.delay_us(8);
channel.set_duty(i);
let _ = channel.set_duty_cycle(i);
}

// Ramp brightness down
for i in (LOW..=HIGH).rev() {
delay.delay_us(8);
channel.set_duty(i);
let _ = channel.set_duty_cycle(i);
}

delay.delay_ms(500);
Expand Down
5 changes: 2 additions & 3 deletions rp2040-hal/examples/pwm_irq_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use panic_halt as _;
use rp2040_hal as hal;

// Some traits we need
use embedded_hal_0_2::digital::v2::OutputPin;
use embedded_hal_0_2::PwmPin;
use embedded_hal::digital::OutputPin;

// Our interrupt macro
use hal::pac::interrupt;
Expand Down Expand Up @@ -135,7 +134,7 @@ fn main() -> ! {
// Connect to GPI O1 as the input to channel B on PWM0
let input_pin = pins.gpio1.reconfigure();
let channel = &mut pwm.channel_b;
channel.enable();
channel.set_enabled(true);

// Enable an interrupt whenever GPI O1 goes from high to low (the end of a pulse)
input_pin.set_interrupt_enabled(gpio::Interrupt::EdgeLow, true);
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/rosc_as_system_clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rp2040_hal as hal;
// register access
use hal::pac;

use embedded_hal_0_2::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use fugit::{HertzU32, RateExtU32};
use hal::clocks::{Clock, ClockSource, ClocksManager, StoppableClock};
use hal::pac::rosc::ctrl::FREQ_RANGE_A;
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/rtc_irq_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rp2040_hal as hal;
use hal::{gpio, pac, rtc};

// Some traits we need
use embedded_hal_0_2::digital::v2::ToggleableOutputPin;
use embedded_hal::digital::StatefulOutputPin;

// Our interrupt macro
use hal::pac::interrupt;
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/rtc_sleep_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rp2040_hal as hal;
use hal::{clocks::ClockGate, gpio, pac, rtc};

// Some traits we need
use embedded_hal_0_2::digital::v2::ToggleableOutputPin;
use embedded_hal::digital::StatefulOutputPin;

// Our interrupt macro
use hal::pac::interrupt;
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/spi_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use cortex_m::singleton;
use cortex_m_rt::entry;
use embedded_hal_0_2::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use hal::dma::{bidirectional, DMAExt};
use hal::fugit::RateExtU32;
use hal::pac;
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/vector_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use hal::pac;
// Some traits we need
use core::cell::RefCell;
use critical_section::Mutex;
use embedded_hal_0_2::digital::v2::ToggleableOutputPin;
use embedded_hal::digital::StatefulOutputPin;
use hal::fugit::MicrosDurationU32;
use pac::interrupt;
use rp2040_hal::clocks::Clock;
Expand Down
3 changes: 2 additions & 1 deletion rp2040-hal/examples/watchdog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use rp2040_hal as hal;
use hal::pac;

// Some traits we need
use embedded_hal_0_2::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
// Embedded HAL 1.0.0 doesn't have an Watchdog trait, so use the one from 0.2
use embedded_hal_0_2::watchdog::{Watchdog, WatchdogEnable};
use hal::fugit::ExtU32;
use rp2040_hal::clocks::Clock;
Expand Down
7 changes: 6 additions & 1 deletion rp2040-hal/src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
//!
//! ## Usage
//!
//! Capture ADC reading from a pin
//! Capture ADC reading from a pin:
//! ```no_run
//! // Embedded HAL 1.0.0 doesn't have an ADC trait, so use the one from 0.2
//! use embedded_hal_0_2::adc::OneShot;
//! use rp2040_hal::{adc::Adc, adc::AdcPin, gpio::Pins, pac, Sio};
//! let mut peripherals = pac::Peripherals::take().unwrap();
Expand All @@ -20,7 +22,9 @@
//! ```
//!
//! Capture ADC reading from temperature sensor. Note that this needs conversion to be a real-world temperature.
//!
//! ```no_run
//! // Embedded HAL 1.0.0 doesn't have an ADC trait, so use the one from 0.2
//! use embedded_hal_0_2::adc::OneShot;
//! use rp2040_hal::{adc::Adc, gpio::Pins, pac, Sio};
//! let mut peripherals = pac::Peripherals::take().unwrap();
Expand Down Expand Up @@ -118,6 +122,7 @@
use core::convert::Infallible;
use core::marker::PhantomData;
// Embedded HAL 1.0.0 doesn't have an ADC trait, so use the one from 0.2
use embedded_hal_0_2::adc::{Channel, OneShot};

use crate::{
Expand Down
6 changes: 3 additions & 3 deletions rp2040-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! ## Basic usage
//! ```no_run
//! use embedded_hal_0_2::digital::v2::{InputPin, OutputPin};
//! use embedded_hal::digital::{InputPin, OutputPin};
//! use rp2040_hal::{clocks::init_clocks_and_plls, gpio::Pins, watchdog::Watchdog, pac, Sio};
//! let mut peripherals = pac::Peripherals::take().unwrap();
//! let mut watchdog = Watchdog::new(peripherals.WATCHDOG);
Expand All @@ -19,7 +19,7 @@
//! // Drive output to 0V
//! output_pin.set_low().unwrap();
//! // Set a pin to input
//! let input_pin = pins.gpio24.into_floating_input();
//! let mut input_pin = pins.gpio24.into_floating_input();
//! // pinstate will be true if the pin is above 2V
//! let pinstate = input_pin.is_high().unwrap();
//! // pinstate_low will be true if the pin is below 1.15V
Expand All @@ -39,7 +39,7 @@
// advanced usage of the pin (relative to reading/writing a gpio) and it is the responsibility of
// the user to make sure these are in a correct state when converting and passing the pin around.

pub use embedded_hal_0_2::digital::v2::PinState;
pub use embedded_hal::digital::PinState;

use crate::{
atomic_register_access::{write_bitmask_clear, write_bitmask_set},
Expand Down
6 changes: 5 additions & 1 deletion rp2040-hal/src/gpio/pin_group.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use embedded_hal_0_2::digital::v2::PinState;
//! Pin Groups
//!
//! Lets you set multiple GPIOs simultaneously.
use embedded_hal::digital::PinState;
use frunk::{hlist::Plucker, HCons, HNil};

use crate::typelevel::Sealed;
Expand Down
Loading

0 comments on commit 2e5237a

Please sign in to comment.