Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Sympatron authored Dec 22, 2023
2 parents 117a643 + 7c9117c commit ec24ff2
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 95 deletions.
8 changes: 6 additions & 2 deletions rp2040-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ targets = ["thumbv6m-none-eabi"]
[dependencies]
cortex-m = "0.7.2"
embedded-hal = { version = "0.2.5", features = ["unproven"] }
eh1_0_alpha = { package = "embedded-hal", version = "=1.0.0-rc.1", optional = true }
eh_nb_1_0_alpha = { package = "embedded-hal-nb", version = "=1.0.0-rc.1", optional = true }
eh1_0_alpha = { package = "embedded-hal", version = "=1.0.0-rc.3", optional = true }
eh_nb_1_0_alpha = { package = "embedded-hal-nb", version = "=1.0.0-rc.3", optional = true }
embedded-dma = "0.2.0"
embedded-io = "0.6.1"
fugit = "0.3.6"
Expand Down Expand Up @@ -181,6 +181,10 @@ required-features = ["critical-section-impl"]
name = "pwm_blink"
required-features = ["critical-section-impl"]

[[example]]
name = "pwm_blink_embedded_hal_1"
required-features = ["critical-section-impl", "eh1_0_alpha"]

[[example]]
name = "rom_funcs"
required-features = ["critical-section-impl"]
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/pio_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn main() -> ! {
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let installed = pio.install(&program).unwrap();
let (int, frac) = (0, 0); // as slow as possible (0 is interpreted as 65536)
let (sm, _, _) = rp2040_hal::pio::PIOBuilder::from_program(installed)
let (sm, _, _) = rp2040_hal::pio::PIOBuilder::from_installed_program(installed)
.set_pins(led_pin_id, 1)
.clock_divisor_fixed_point(int, frac)
.build(sm0);
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/pio_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn main() -> ! {
// Initialize and start PIO
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let installed = pio.install(&program.program).unwrap();
let (mut sm, rx, tx) = rp2040_hal::pio::PIOBuilder::from_program(installed)
let (mut sm, rx, tx) = rp2040_hal::pio::PIOBuilder::from_installed_program(installed)
.out_pins(led_pin_id, 1)
.clock_divisor_fixed_point(0, 0) // as slow as possible (0 is interpreted as 65536)
.autopull(true)
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/pio_proc_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn main() -> ! {
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let installed = pio.install(&program.program).unwrap();
let (int, frac) = (0, 0); // as slow as possible (0 is interpreted as 65536)
let (mut sm, _, _) = rp2040_hal::pio::PIOBuilder::from_program(installed)
let (mut sm, _, _) = rp2040_hal::pio::PIOBuilder::from_installed_program(installed)
.set_pins(led_pin_id, 1)
.clock_divisor_fixed_point(int, frac)
.build(sm0);
Expand Down
2 changes: 1 addition & 1 deletion rp2040-hal/examples/pio_side_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn main() -> ! {
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let installed = pio.install(&program.program).unwrap();
let (int, frac) = (0, 0); // as slow as possible (0 is interpreted as 65536)
let (mut sm, _, _) = rp2040_hal::pio::PIOBuilder::from_program(installed)
let (mut sm, _, _) = rp2040_hal::pio::PIOBuilder::from_installed_program(installed)
.side_set_pin_base(led_pin_id)
.clock_divisor_fixed_point(int, frac)
.build(sm0);
Expand Down
4 changes: 2 additions & 2 deletions rp2040-hal/examples/pio_synchronized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn main() -> ! {
let (int, frac) = (256, 0);

let installed = pio.install(&program.program).unwrap();
let (mut sm0, _, _) = rp2040_hal::pio::PIOBuilder::from_program(
let (mut sm0, _, _) = rp2040_hal::pio::PIOBuilder::from_installed_program(
// Safety: We won't uninstall the program, ever
unsafe { installed.share() },
)
Expand All @@ -74,7 +74,7 @@ fn main() -> ! {
// The GPIO pin needs to be configured as an output.
sm0.set_pindirs([(pin0, hal::pio::PinDir::Output)]);

let (mut sm1, _, _) = rp2040_hal::pio::PIOBuilder::from_program(installed)
let (mut sm1, _, _) = rp2040_hal::pio::PIOBuilder::from_installed_program(installed)
.set_pins(pin1, 1)
.clock_divisor_fixed_point(int, frac)
.build(sm1);
Expand Down
4 changes: 2 additions & 2 deletions rp2040-hal/examples/pwm_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ fn main() -> ! {
// Infinite loop, fading LED up and down
loop {
// Ramp brightness up
for i in (LOW..=HIGH).skip(100) {
for i in LOW..=HIGH {
delay.delay_us(8);
channel.set_duty(i);
}

// Ramp brightness down
for i in (LOW..=HIGH).rev().skip(100) {
for i in (LOW..=HIGH).rev() {
delay.delay_us(8);
channel.set_duty(i);
}
Expand Down
122 changes: 122 additions & 0 deletions rp2040-hal/examples/pwm_blink_embedded_hal_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//! # PWM Blink Example
//!
//! If you have an LED connected to pin 25, it will fade the LED using the PWM
//! peripheral.
//!
//! It may need to be adapted to your particular board layout and/or pin assignment.
//!
//! See the `Cargo.toml` file for Copyright and license details.
#![no_std]
#![no_main]

// Ensure we halt the program on panic (if we don't mention this crate it won't
// be linked)
use panic_halt as _;

// Alias for our HAL crate
use rp2040_hal as hal;

// Some traits we need
use eh1_0_alpha::pwm::SetDutyCycle;
use rp2040_hal::clocks::Clock;

// A shorter alias for the Peripheral Access Crate, which provides low-level
// register access
use hal::pac;

/// 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.
/// Note: This boot block is not necessary when using a rp-hal based BSP
/// as the BSPs already perform this step.
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H;

/// The minimum PWM value (i.e. LED brightness) we want
const LOW: u16 = 0;

/// The maximum PWM value (i.e. LED brightness) we want
const HIGH: u16 = 25000;

/// External high-speed crystal on the Raspberry Pi Pico board is 12 MHz. Adjust
/// if your board has a different frequency
const XTAL_FREQ_HZ: u32 = 12_000_000u32;

/// Entry point to our bare-metal application.
///
/// The `#[rp2040_hal::entry]` macro ensures the Cortex-M start-up code calls this function
/// as soon as all global variables and the spinlock are initialised.
///
/// The function configures the RP2040 peripherals, then fades the LED in an
/// infinite loop.
#[rp2040_hal::entry]
fn main() -> ! {
// Grab our singleton objects
let mut pac = pac::Peripherals::take().unwrap();
let core = pac::CorePeripherals::take().unwrap();

// Set up the watchdog driver - needed by the clock setup code
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);

// Configure the clocks
//
// The default is to generate a 125 MHz system clock
let clocks = hal::clocks::init_clocks_and_plls(
XTAL_FREQ_HZ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();

// The single-cycle I/O block controls our GPIO pins
let sio = hal::Sio::new(pac.SIO);

// Set the pins up according to their function on this particular board
let pins = hal::gpio::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);

// The delay object lets us wait for specified amounts of time (in
// milliseconds)
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());

// Init PWMs
let mut pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS);

// Configure PWM4
let pwm = &mut pwm_slices.pwm4;
pwm.set_ph_correct();
pwm.enable();

// Output channel B on PWM4 to GPIO 25
let channel = &mut pwm.channel_b;
channel.output_to(pins.gpio25);

// Infinite loop, fading LED up and down
loop {
// Ramp brightness up
for i in LOW..=HIGH {
delay.delay_us(8);
channel.set_duty_cycle(i).unwrap();
}

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

delay.delay_ms(500);
}
}

// End of file
1 change: 1 addition & 0 deletions rp2040-hal/src/gpio/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl DynFunction {
}
}
}
impl<P: super::pin::PinId> ValidFunction<DynFunction> for P {}
macro_rules! pin_valid_func {
($bank:ident as $prefix:ident, [$head:ident $(, $func:ident)*], [$($name:tt),+]) => {
pin_valid_func!($bank as $prefix, [$($func),*], [$($name),+]);
Expand Down
12 changes: 6 additions & 6 deletions rp2040-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,11 +1434,11 @@ mod eh1 {
I: PinId,
P: PullType,
{
fn is_set_high(&self) -> Result<bool, Self::Error> {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_set_high())
}

fn is_set_low(&self) -> Result<bool, Self::Error> {
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_set_low())
}
}
Expand All @@ -1459,11 +1459,11 @@ mod eh1 {
I: PinId,
P: PullType,
{
fn is_high(&self) -> Result<bool, Self::Error> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_high())
}

fn is_low(&self) -> Result<bool, Self::Error> {
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_low())
}
}
Expand All @@ -1480,11 +1480,11 @@ mod eh1 {
impl<'a, I: PinId, F: super::func::Function, P: PullType> InputPin
for super::AsInputPin<'a, I, F, P>
{
fn is_high(&self) -> Result<bool, Self::Error> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(self.0._is_high())
}

fn is_low(&self) -> Result<bool, Self::Error> {
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(self.0._is_low())
}
}
Expand Down
38 changes: 35 additions & 3 deletions rp2040-hal/src/pio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl<P: PIOExt> PIO<P> {
/// ).program;
/// let installed = pio.install(&program).unwrap();
/// // Configure a state machine to use the program.
/// let (sm, rx, tx) = PIOBuilder::from_program(installed).build(sm0);
/// let (sm, rx, tx) = PIOBuilder::from_installed_program(installed).build(sm0);
/// // Uninitialize the state machine again, freeing the program.
/// let (sm, installed) = sm.uninit(rx, tx);
/// // Uninstall the program to free instruction memory.
Expand Down Expand Up @@ -1940,12 +1940,44 @@ pub enum InstallError {
}

impl<P: PIOExt> PIOBuilder<P> {
/// Set config settings based on information from the given [`pio::Program`].
/// Set config settings based on information from the given [`InstalledProgram`].
/// Additional configuration may be needed in addition to this.
///
/// Note: This was formerly called `from_program`. The new function has
/// a different default shift direction, `ShiftDirection::Right`, matching
/// the hardware reset value.
pub fn from_installed_program(p: InstalledProgram<P>) -> Self {
PIOBuilder {
clock_divisor: (1, 0),
program: p,
jmp_pin: 0,
out_sticky: false,
inline_out: None,
mov_status: MovStatusConfig::Tx(0),
fifo_join: Buffers::RxTx,
pull_threshold: 0,
push_threshold: 0,
out_shiftdir: ShiftDirection::Right,
in_shiftdir: ShiftDirection::Right,
autopull: false,
autopush: false,
set_count: 5,
out_count: 0,
in_base: 0,
side_set_base: 0,
set_base: 0,
out_base: 0,
}
}

/// Set config settings based on information from the given [`InstalledProgram`].
/// Additional configuration may be needed in addition to this.
///
/// Note: The shift direction for both input and output shift registers
/// defaults to `ShiftDirection::Left`, which is different from the
/// rp2040 reset value.
/// rp2040 reset value. The alternative [`Self::from_installed_program`],
/// fixes this.
#[deprecated(note = "please use `from_installed_program` instead")]
pub fn from_program(p: InstalledProgram<P>) -> Self {
PIOBuilder {
clock_divisor: (1, 0),
Expand Down
21 changes: 21 additions & 0 deletions rp2040-hal/src/pwm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@
//! min_config() leaves those registers in the state they were before it was called (Careful, this can lead to unexpected behavior)
//! It's recommended to only call min_config() after calling default_config() on a pin that shares a PWM block.
#[cfg(feature = "eh1_0_alpha")]
use core::convert::Infallible;
use core::marker::PhantomData;

#[cfg(feature = "eh1_0_alpha")]
use eh1_0_alpha::pwm::{ErrorType, SetDutyCycle};
use embedded_dma::Word;
use embedded_hal::PwmPin;

Expand Down Expand Up @@ -652,6 +656,23 @@ impl<S: AnySlice> PwmPin for Channel<S, A> {
}
}

#[cfg(feature = "eh1_0_alpha")]
impl<S: AnySlice> ErrorType for Channel<S, B> {
type Error = Infallible;
}

#[cfg(feature = "eh1_0_alpha")]
impl<S: AnySlice> SetDutyCycle for Channel<S, B> {
fn max_duty_cycle(&self) -> u16 {
self.get_max_duty()
}

fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
self.set_duty(duty);
Ok(())
}
}

impl<S: AnySlice> PwmPin for Channel<S, B> {
type Duty = u16;

Expand Down
9 changes: 8 additions & 1 deletion rp2040-hal/src/rtc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError};
/// A reference to the real time clock of the system
pub struct RealTimeClock {
rtc: RTC,
clock: RtcClock,
}

impl RealTimeClock {
Expand Down Expand Up @@ -68,7 +69,7 @@ impl RealTimeClock {
let freq = clock.freq().to_Hz() - 1;
rtc.clkdiv_m1.write(|w| unsafe { w.bits(freq) });

let mut result = Self { rtc };
let mut result = Self { rtc, clock };
result.set_leap_year_check(true); // should be on by default, make sure this is the case.
result.set_datetime(initial_date)?;
Ok(result)
Expand Down Expand Up @@ -196,6 +197,12 @@ impl RealTimeClock {
self.set_match_ena(false);
self.set_match_ena(true);
}

/// Free the RTC peripheral and RTC clock
pub fn free(self, resets: &mut RESETS) -> (RTC, RtcClock) {
resets.reset.modify(|_, w| w.rtc().set_bit());
(self.rtc, self.clock)
}
}

/// Errors that can occur on methods on [RtcClock]
Expand Down
Loading

0 comments on commit ec24ff2

Please sign in to comment.