Skip to content

I2c::new remap #520

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

Merged
merged 3 commits into from
Dec 15, 2024
Merged
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
15 changes: 15 additions & 0 deletions .zed/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"lsp": {
"rust-analyzer": {
"initialization_options": {
"cargo": {
"features": ["defmt", "rtic", "stm32f103", "medium"]
},
"check": {
"allTargets": false,
"targets": "thumbv7em-none-eabi"
}
}
}
}
}
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Relax pin type generics for `Serial`, `I2c`, `Spi`, `Can`, `Qei`, `PwmInput`. [#462] [#516]
~~Use enums of pin tuples and `Enum::from<(tuple)>` for pin remap before passing to peripheral.~~
Use pin enums and `impl RInto<(enum), R>` for peripheral constructors.
Add `RInto` trait and `Rmp` peripheral wrapper, add `remap` for peripherals. [#514]
Add `RInto` trait and `Rmp` peripheral wrapper, add `remap` for peripherals. [#514] [#520]
Remove `RemapStruct`s. [#462] [#506] [#509]
- Use independent `Spi` and `SpiSlave` structures instead of `OP` generic [#462]
- Take `&Clocks` instead of `Clocks` [#498]
Expand All @@ -33,7 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Unmacro `dma.rs` [#505]
- Updated `usb-device` and `usbd-serial` to latest versions [#510]
- Rework pin remaps, fix CAN1 remap [#511]
- Rework USART remap,
- Rework USART remap,

### Added

Expand Down Expand Up @@ -68,6 +68,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#511]: https://github.com/stm32-rs/stm32f1xx-hal/pull/511
[#514]: https://github.com/stm32-rs/stm32f1xx-hal/pull/514
[#516]: https://github.com/stm32-rs/stm32f1xx-hal/pull/516
[#520]: https://github.com/stm32-rs/stm32f1xx-hal/pull/520

## [v0.10.0] - 2022-12-12

Expand Down
6 changes: 6 additions & 0 deletions src/afio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ remap! {

pub struct Rmp<T, const R: u8>(pub(crate) T);

impl<T> From<T> for Rmp<T, 0> {
fn from(value: T) -> Self {
Self(value)
}
}

pub trait RFrom<T, const R: u8> {
fn rfrom(value: T) -> Self;
}
Expand Down
39 changes: 11 additions & 28 deletions src/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ pub struct Can<CAN: Instance, PULL = Floating> {
impl<CAN: Instance, const R: u8> Rmp<CAN, R> {
pub fn can<PULL: UpMode>(
self,
#[cfg(not(feature = "connectivity"))] usb: pac::USB,
#[cfg(not(feature = "connectivity"))] _usb: pac::USB,
pins: (impl RInto<CAN::Tx, R>, impl RInto<CAN::Rx<PULL>, R>),
) -> Can<CAN, PULL> {
Can::_new(
self.0,
#[cfg(not(feature = "connectivity"))]
usb,
(Some(pins.0), Some(pins.1)),
)
let rcc = unsafe { &(*RCC::ptr()) };
CAN::enable(rcc);

let pins = (Some(pins.0.rinto()), Some(pins.1.rinto()));
Can { can: self.0, pins }
}
pub fn can_loopback(
self,
Expand All @@ -99,34 +98,18 @@ impl<CAN: Instance, PULL: UpMode> Can<CAN, PULL> {
///
/// CAN shares SRAM with the USB peripheral. Take ownership of USB to
/// prevent accidental shared usage.
pub fn new(
can: CAN,
pub fn new<const R: u8>(
can: impl Into<Rmp<CAN, R>>,
#[cfg(not(feature = "connectivity"))] _usb: pac::USB,
pins: (impl RInto<CAN::Tx, 0>, impl RInto<CAN::Rx<PULL>, 0>),
pins: (impl RInto<CAN::Tx, R>, impl RInto<CAN::Rx<PULL>, R>),
) -> Can<CAN, PULL> {
Self::_new(
can,
can.into().can(
#[cfg(not(feature = "connectivity"))]
_usb,
(Some(pins.0), Some(pins.1)),
pins,
)
}

fn _new<const R: u8>(
can: CAN,
#[cfg(not(feature = "connectivity"))] _usb: pac::USB,
pins: (
Option<impl RInto<CAN::Tx, R>>,
Option<impl RInto<CAN::Rx<PULL>, R>>,
),
) -> Can<CAN, PULL> {
let rcc = unsafe { &(*RCC::ptr()) };
CAN::enable(rcc);

let pins = (pins.0.map(RInto::rinto), pins.1.map(RInto::rinto));
Can { can, pins }
}

/// Creates a CAN interface in loopback mode
pub fn new_loopback(
can: CAN,
Expand Down
24 changes: 4 additions & 20 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,29 +164,13 @@ impl Instance for pac::I2C2 {}

impl<I2C: Instance> I2c<I2C> {
/// Creates a generic I2C object
pub fn new(
i2c: I2C,
pins: (impl RInto<I2C::Scl, 0>, impl RInto<I2C::Sda, 0>),
pub fn new<const R: u8>(
i2c: impl Into<Rmp<I2C, R>>,
pins: (impl RInto<I2C::Scl, R>, impl RInto<I2C::Sda, R>),
mode: impl Into<Mode>,
clocks: &Clocks,
) -> Self {
let mode = mode.into();
let rcc = unsafe { &(*RCC::ptr()) };
I2C::enable(rcc);
I2C::reset(rcc);

let pclk1 = I2C::clock(clocks);

assert!(mode.get_frequency() <= kHz(400));

let mut i2c = I2c {
i2c,
pins: (pins.0.rinto(), pins.1.rinto()),
mode,
pclk1,
};
i2c.init();
i2c
i2c.into().i2c(pins, mode, clocks)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/i2c/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub struct DwtTimeouts {
impl<I2C: Instance> BlockingI2c<I2C> {
/// Creates a blocking I2C1 object on pins PB6 and PB7 or PB8 and PB9 using the embedded-hal `BlockingI2c` trait.
#[allow(clippy::too_many_arguments)]
pub fn new(
i2c: I2C,
pins: (impl RInto<I2C::Scl, 0>, impl RInto<I2C::Sda, 0>),
pub fn new<const R: u8>(
i2c: impl Into<Rmp<I2C, R>>,
pins: (impl RInto<I2C::Scl, R>, impl RInto<I2C::Sda, R>),
mode: impl Into<Mode>,
clocks: &Clocks,
start_timeout_us: u32,
Expand Down
58 changes: 15 additions & 43 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,15 @@ impl<USART: Instance> SerialExt for USART {
config: impl Into<Config>,
clocks: &Clocks,
) -> Tx<Self> {
Serial::_new(
self,
(Some(tx_pin), None::<USART::Rx<Floating>>),
config,
clocks,
)
.split()
.0
Serial::tx(self, tx_pin, config, clocks)
}
fn rx<PULL: UpMode>(
self,
rx_pin: impl RInto<Self::Rx<PULL>, 0>,
config: impl Into<Config>,
clocks: &Clocks,
) -> Rx<Self> {
Serial::_new(
self,
(None::<USART::Tx<PushPull>>, Some(rx_pin)),
config,
clocks,
)
.split()
.1
Serial::rx(self, rx_pin, config, clocks)
}
}

Expand Down Expand Up @@ -388,38 +374,24 @@ impl<USART: Instance, const R: u8> Rmp<USART, R> {
}

impl<USART: Instance, Otype> Serial<USART, Otype, Floating> {
pub fn tx(
usart: USART,
tx_pin: impl RInto<USART::Tx<Otype>, 0>,
pub fn tx<const R: u8>(
usart: impl Into<Rmp<USART, R>>,
tx_pin: impl RInto<USART::Tx<Otype>, R>,
config: impl Into<Config>,
clocks: &Clocks,
) -> Tx<USART> {
Self::_new(
usart,
(Some(tx_pin), None::<USART::Rx<Floating>>),
config,
clocks,
)
.split()
.0
usart.into().tx(tx_pin, config, clocks)
}
}

impl<USART: Instance, PULL: UpMode> Serial<USART, PushPull, PULL> {
pub fn rx(
usart: USART,
rx_pin: impl RInto<USART::Rx<PULL>, 0>,
pub fn rx<const R: u8>(
usart: impl Into<Rmp<USART, R>>,
rx_pin: impl RInto<USART::Rx<PULL>, R>,
config: impl Into<Config>,
clocks: &Clocks,
) -> Rx<USART> {
Self::_new(
usart,
(None::<USART::Tx<PushPull>>, Some(rx_pin)),
config,
clocks,
)
.split()
.1
usart.into().rx(rx_pin, config, clocks)
}
}

Expand All @@ -439,16 +411,16 @@ impl<USART: Instance, Otype, PULL: UpMode> Serial<USART, Otype, PULL> {
/// `MAPR` and `APBX` are register handles which are passed for
/// configuration. (`MAPR` is used to map the USART to the
/// corresponding pins. `APBX` is used to reset the USART.)
pub fn new(
usart: USART,
pub fn new<const R: u8>(
usart: impl Into<Rmp<USART, R>>,
pins: (
impl RInto<USART::Tx<Otype>, 0>,
impl RInto<USART::Rx<PULL>, 0>,
impl RInto<USART::Tx<Otype>, R>,
impl RInto<USART::Rx<PULL>, R>,
),
config: impl Into<Config>,
clocks: &Clocks,
) -> Self {
Self::_new(usart, (Some(pins.0), Some(pins.1)), config, clocks)
usart.into().serial(pins, config, clocks)
}

fn _new<const R: u8>(
Expand Down
Loading
Loading