Skip to content

Commit

Permalink
Merge pull request #499 from stm32-rs/into_mode
Browse files Browse the repository at this point in the history
into_mode + clippy fixes
  • Loading branch information
burrbull authored Oct 5, 2024
2 parents f74efe5 + 6f8ae9d commit 0da0af4
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ macro_rules! adc_hal {
/// Returns the largest possible sample value for the current settings
pub fn max_sample(&self) -> u16 {
match self.align {
Align::Left => u16::max_value(),
Align::Left => u16::MAX,
Align::Right => (1 << 12) - 1,
}
}
Expand Down
57 changes: 27 additions & 30 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
//! - **Dynamic**: Pin mode is selected at runtime. See changing configurations for more details
//! - Input
//! - **PullUp**: Input connected to high with a weak pull-up resistor. Will be high when nothing
//! is connected
//! is connected
//! - **PullDown**: Input connected to ground with a weak pull-down resistor. Will be low when nothing
//! is connected
//! is connected
//! - **Floating**: Input not pulled to high or low. Will be undefined when nothing is connected
//! - Output
//! - **PushPull**: Output which either drives the pin high or low
//! - **OpenDrain**: Output which leaves the gate floating, or pulls it to ground in drain
//! mode. Can be used as an input in the `open` configuration
//! mode. Can be used as an input in the `open` configuration
//! - **Debugger**: Some pins start out being used by the debugger. A pin in this mode can only be
//! used if the [JTAG peripheral has been turned off](#accessing-pa15-pb3-and-pb14).
//! used if the [JTAG peripheral has been turned off](#accessing-pa15-pb3-and-pb14).
//!
//! ## Changing modes
//! The simplest way to change the pin mode is to use the `into_<mode>` functions. These return a
Expand Down Expand Up @@ -215,7 +215,7 @@ mod sealed {
}

use sealed::Interruptable;
use sealed::PinMode;
pub(crate) use sealed::PinMode;

impl<MODE> Interruptable for Input<MODE> {}
impl Interruptable for Dynamic {}
Expand Down Expand Up @@ -639,43 +639,38 @@ where
/// pin.
#[inline]
pub fn into_alternate_push_pull(
mut self,
self,
cr: &mut <Self as HL>::Cr,
) -> Pin<P, N, Alternate<PushPull>> {
self.mode::<Alternate<PushPull>>(cr);
Pin::new()
self.into_mode(cr)
}

/// Configures the pin to operate as an alternate function open-drain output
/// pin.
#[inline]
pub fn into_alternate_open_drain(
mut self,
self,
cr: &mut <Self as HL>::Cr,
) -> Pin<P, N, Alternate<OpenDrain>> {
self.mode::<Alternate<OpenDrain>>(cr);
Pin::new()
self.into_mode(cr)
}

/// Configures the pin to operate as a floating input pin
#[inline]
pub fn into_floating_input(mut self, cr: &mut <Self as HL>::Cr) -> Pin<P, N, Input<Floating>> {
self.mode::<Input<Floating>>(cr);
Pin::new()
pub fn into_floating_input(self, cr: &mut <Self as HL>::Cr) -> Pin<P, N, Input<Floating>> {
self.into_mode(cr)
}

/// Configures the pin to operate as a pulled down input pin
#[inline]
pub fn into_pull_down_input(mut self, cr: &mut <Self as HL>::Cr) -> Pin<P, N, Input<PullDown>> {
self.mode::<Input<PullDown>>(cr);
Pin::new()
pub fn into_pull_down_input(self, cr: &mut <Self as HL>::Cr) -> Pin<P, N, Input<PullDown>> {
self.into_mode(cr)
}

/// Configures the pin to operate as a pulled up input pin
#[inline]
pub fn into_pull_up_input(mut self, cr: &mut <Self as HL>::Cr) -> Pin<P, N, Input<PullUp>> {
self.mode::<Input<PullUp>>(cr);
Pin::new()
pub fn into_pull_up_input(self, cr: &mut <Self as HL>::Cr) -> Pin<P, N, Input<PullUp>> {
self.into_mode(cr)
}

/// Configures the pin to operate as an open-drain output pin.
Expand All @@ -694,8 +689,7 @@ where
initial_state: PinState,
) -> Pin<P, N, Output<OpenDrain>> {
self._set_state(initial_state);
self.mode::<Output<OpenDrain>>(cr);
Pin::new()
self.into_mode(cr)
}

/// Configures the pin to operate as an push-pull output pin.
Expand All @@ -714,26 +708,23 @@ where
initial_state: PinState,
) -> Pin<P, N, Output<PushPull>> {
self._set_state(initial_state);
self.mode::<Output<PushPull>>(cr);
Pin::new()
self.into_mode(cr)
}

/// Configures the pin to operate as an push-pull output pin.
/// The state will not be changed.
#[inline]
pub fn into_push_pull_output_with_current_state(
mut self,
self,
cr: &mut <Self as HL>::Cr,
) -> Pin<P, N, Output<PushPull>> {
self.mode::<Output<PushPull>>(cr);
Pin::new()
self.into_mode(cr)
}

/// Configures the pin to operate as an analog input pin
#[inline]
pub fn into_analog(mut self, cr: &mut <Self as HL>::Cr) -> Pin<P, N, Analog> {
self.mode::<Analog>(cr);
Pin::new()
pub fn into_analog(self, cr: &mut <Self as HL>::Cr) -> Pin<P, N, Analog> {
self.into_mode(cr)
}

/// Configures the pin as a pin that can change between input
Expand Down Expand Up @@ -977,6 +968,12 @@ where
(r_bits & !(0b1111 << Self::OFFSET)) | (bits << Self::OFFSET)
});
}

#[inline]
pub(crate) fn into_mode<MODE: PinMode>(mut self, cr: &mut <Self as HL>::Cr) -> Pin<P, N, MODE> {
self.mode::<MODE>(cr);
Pin::new()
}
}

gpio!(GPIOA, gpioa, PAx, 'A', [
Expand Down
2 changes: 0 additions & 2 deletions src/qei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
NOTE: In some cases you need to specify remap you need, especially for TIM2
(see [Alternate function remapping](super::timer)):
*/
use core::u16;

use core::marker::PhantomData;

#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "connectivity",))]
Expand Down
2 changes: 1 addition & 1 deletion src/rcc/enable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ bus! {
TIM11 => (APB2, 21),
}

#[cfg(any(feature = "stm32f103"))] // feature = "stm32f102"
#[cfg(feature = "stm32f103")] // feature = "stm32f102"
bus! {
USB => (APB1, 23),
}
42 changes: 21 additions & 21 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,13 @@ macro_rules! serialdma {

atomic::compiler_fence(Ordering::Release);

self.channel.ch().cr.modify(|_, w| { w
.mem2mem() .clear_bit()
.pl() .medium()
.msize() .bits8()
.psize() .bits8()
.circ() .set_bit()
.dir() .clear_bit()
self.channel.ch().cr.modify(|_, w| {
w.mem2mem().clear_bit();
w.pl().medium();
w.msize().bits8();
w.psize().bits8();
w.circ().set_bit();
w.dir().clear_bit()
});

self.start();
Expand All @@ -787,13 +787,13 @@ macro_rules! serialdma {
self.channel.set_transfer_length(len);

atomic::compiler_fence(Ordering::Release);
self.channel.ch().cr.modify(|_, w| { w
.mem2mem() .clear_bit()
.pl() .medium()
.msize() .bits8()
.psize() .bits8()
.circ() .clear_bit()
.dir() .clear_bit()
self.channel.ch().cr.modify(|_, w| {
w.mem2mem().clear_bit();
w.pl().medium();
w.msize().bits8();
w.psize().bits8();
w.circ().clear_bit();
w.dir().clear_bit()
});
self.start();

Expand All @@ -817,13 +817,13 @@ macro_rules! serialdma {

atomic::compiler_fence(Ordering::Release);

self.channel.ch().cr.modify(|_, w| { w
.mem2mem() .clear_bit()
.pl() .medium()
.msize() .bits8()
.psize() .bits8()
.circ() .clear_bit()
.dir() .set_bit()
self.channel.ch().cr.modify(|_, w| {
w.mem2mem().clear_bit();
w.pl().medium();
w.msize().bits8();
w.psize().bits8();
w.circ().clear_bit();
w.dir().set_bit()
});
self.start();

Expand Down
2 changes: 1 addition & 1 deletion src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ where
// disable SS output
spi.cr2.write(|w| w.ssoe().clear_bit());

let br = match SPI::clock(&clocks) / freq {
let br = match SPI::clock(clocks) / freq {
0 => unreachable!(),
1..=2 => 0b000,
3..=5 => 0b001,
Expand Down
4 changes: 2 additions & 2 deletions src/timer/pwm_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ impl Timer<TIM4> {
/// Courtesy of @TeXitoi (https://github.com/stm32-rs/stm32f1xx-hal/pull/10#discussion_r259535503)
fn compute_arr_presc(freq: u32, clock: u32) -> (u16, u16) {
if freq == 0 {
return (core::u16::MAX, core::u16::MAX);
return (u16::MAX, u16::MAX);
}
let presc = clock / freq.saturating_mul(core::u16::MAX as u32 + 1);
let presc = clock / freq.saturating_mul(u16::MAX as u32 + 1);
let arr = clock / freq.saturating_mul(presc + 1);
(core::cmp::max(1, arr as u16), presc as u16)
}
Expand Down

0 comments on commit 0da0af4

Please sign in to comment.