Skip to content

Commit

Permalink
Remap, Rmp, RFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Oct 13, 2024
1 parent 6a8d6f4 commit 206735e
Show file tree
Hide file tree
Showing 29 changed files with 1,340 additions and 771 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Breaking changes

- Relax pin type generics for `Serial`, `I2c`, `Spi`, `Can`. [#462]
Use enums of pin tuples and `Enum::from<(tuple)>` for pin remap before passing to peripheral.
~~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.
Remove `RemapStruct`s. [#462] [#506] [#509]
- Use independent `Spi` and `SpiSlave` structures instead of `OP` generic [#462]
- Take `&Clocks` instead of `Clocks` [#498]
Expand Down
6 changes: 2 additions & 4 deletions examples/can-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ fn main() -> ! {
// Other boards might have a crystal with another frequency or none at all.
rcc.cfgr.use_hse(8.MHz()).freeze(&mut flash.acr);

let mut afio = dp.AFIO.constrain();

let mut can1 = {
let gpioa = dp.GPIOA.split();
let rx = gpioa.pa11;
Expand All @@ -34,7 +32,7 @@ fn main() -> ! {
let can = dp.CAN1.can(
#[cfg(not(feature = "connectivity"))]
dp.USB,
(tx, rx, &mut afio.mapr),
(tx, rx),
);

// APB1 (PCLK1): 8MHz, Bit rate: 125kBit/s, Sample Point 87.5%
Expand All @@ -51,7 +49,7 @@ fn main() -> ! {
#[cfg(feature = "connectivity")]
let _can2 = {
let gpiob = dp.GPIOB.split();
let can = dp.CAN2.can((gpiob.pb6, gpiob.pb5, &mut afio.mapr));
let can = dp.CAN2.can((gpiob.pb6, gpiob.pb5));

// APB1 (PCLK1): 8MHz, Bit rate: 125kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
Expand Down
11 changes: 3 additions & 8 deletions examples/can-rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod app {
use super::{enqueue_frame, PriorityFrame};
use bxcan::{filter::Mask32, ExtendedId, Fifo, Frame, Interrupts, Rx0, StandardId, Tx};
use heapless::binary_heap::{BinaryHeap, Max};
use stm32f1xx_hal::{can::Can, gpio::Floating, pac::CAN1, prelude::*};
use stm32f1xx_hal::{can::Can, pac::CAN1, prelude::*};

#[local]
struct Local {
Expand Down Expand Up @@ -86,17 +86,12 @@ mod app {
let gpioa = cx.device.GPIOA.split();
let can_rx_pin = gpioa.pa11;
let can_tx_pin = gpioa.pa12;
let mut afio = cx.device.AFIO.constrain();

#[cfg(not(feature = "connectivity"))]
let can = Can::<_, Floating>::new(
cx.device.CAN1,
cx.device.USB,
(can_tx_pin, can_rx_pin, &mut afio.mapr),
);
let can = Can::new(cx.device.CAN1, cx.device.USB, (can_tx_pin, can_rx_pin));

#[cfg(feature = "connectivity")]
let can = Can::<_, Floating>::new(cx.device.CAN1, (can_tx_pin, can_rx_pin, &mut afio.mapr));
let can = Can::new(cx.device.CAN1, (can_tx_pin, can_rx_pin));

// APB1 (PCLK1): 16MHz, Bit rate: 1000kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
Expand Down
3 changes: 1 addition & 2 deletions examples/i2c-bme280/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ cortex-m = "0.7.6"

[dependencies.stm32f1xx-hal]
path = "../.."
version = "0.9.0"
features = ["stm32f103", "rt", "stm32-usbd"]
features = ["stm32f103", "stm32-usbd"]

[profile.dev]
incremental = false
Expand Down
33 changes: 17 additions & 16 deletions examples/i2c-bme280/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,24 @@ fn main() -> ! {
// Acquire the GPIOB peripheral
let mut gpiob = dp.GPIOB.split();

let scl = gpiob.pb6.into_alternate_open_drain(&mut gpiob.crl);
let sda = gpiob.pb7.into_alternate_open_drain(&mut gpiob.crl);
let scl = gpiob.pb6;
let sda = gpiob.pb7;

let i2c = BlockingI2c::i2c1(
dp.I2C1,
(scl, sda),
&mut afio.mapr,
Mode::Fast {
frequency: 400.kHz(),
duty_cycle: DutyCycle::Ratio16to9,
},
clocks,
1000,
10,
1000,
1000,
);
let i2c = dp
.I2C1
//.remap(&mut afio.mapr) // add this if want to use PB8, PB9 instead
.blocking_i2c(
(scl, sda),
Mode::Fast {
frequency: 400.kHz(),
duty_cycle: DutyCycle::Ratio16to9,
},
&clocks,
1000,
10,
1000,
1000,
);

// The Adafruit boards have address 0x77 without closing the jumper on the back, the BME280 lib connects to 0x77 with `new_secondary`, use
// `new_primary` for 0x76 if you close the jumper/solder bridge.
Expand Down
7 changes: 3 additions & 4 deletions examples/mfrc522.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ fn main() -> ! {

let _stim = &mut cp.ITM.stim[0];
let rcc = dp.RCC.constrain();
let mut afio = dp.AFIO.constrain();
let mut flash = dp.FLASH.constrain();
let mut gpioa = dp.GPIOA.split();
let mut gpioc = dp.GPIOC.split();

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
let sck = gpioa.pa5;
let miso = gpioa.pa6;
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
let mosi = gpioa.pa7;
let spi = Spi::new(
dp.SPI1,
(sck, miso, mosi, &mut afio.mapr),
(Some(sck), Some(miso), Some(mosi)),
MODE,
1.MHz(),
&clocks,
Expand Down
12 changes: 5 additions & 7 deletions examples/mpu9250.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,24 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = dp.AFIO.constrain();

let mut gpioa = dp.GPIOA.split();
// let mut gpiob = dp.GPIOB.split();

let nss = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);

// SPI1
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
let sck = gpioa.pa5;
let miso = gpioa.pa6;
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
let mosi = gpioa.pa7;

// SPI2
// let sck = gpiob.pb13.into_alternate_push_pull(&mut gpiob.crh);
// let sck = gpiob.pb13;
// let miso = gpiob.pb14;
// let mosi = gpiob.pb15.into_alternate_push_pull(&mut gpiob.crh);
// let mosi = gpiob.pb15;

let spi = Spi::new(
dp.SPI1,
(sck, miso, mosi, &mut afio.mapr),
(Some(sck), Some(miso), Some(mosi)),
mpu9250::MODE.into(),
1.MHz(),
&clocks,
Expand Down
4 changes: 2 additions & 2 deletions examples/serial-dma-circ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = p.AFIO.constrain();
//let mut afio = p.AFIO.constrain();
let channels = p.DMA1.split();

let mut gpioa = p.GPIOA.split();
Expand All @@ -50,7 +50,7 @@ fn main() -> ! {

let serial = Serial::new(
p.USART1,
(tx, rx, &mut afio.mapr),
(tx, rx),
Config::default().baudrate(9_600.bps()),
&clocks,
);
Expand Down
9 changes: 2 additions & 7 deletions examples/serial-dma-peek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = p.AFIO.constrain();
//let mut afio = p.AFIO.constrain();
let channels = p.DMA1.split();

let mut gpioa = p.GPIOA.split();
Expand All @@ -47,12 +47,7 @@ fn main() -> ! {
// let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
// let rx = gpiob.pb11;

let serial = Serial::new(
p.USART1,
(tx, rx, &mut afio.mapr),
Config::default(),
&clocks,
);
let serial = Serial::new(p.USART1, (tx, rx), Config::default(), &clocks);

let rx = serial.rx.with_dma(channels.5);
let buf = singleton!(: [u8; 8] = [0; 8]).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/serial-dma-rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = p.AFIO.constrain();
//let mut afio = p.AFIO.constrain();
let channels = p.DMA1.split();

let mut gpioa = p.GPIOA.split();
Expand All @@ -49,7 +49,7 @@ fn main() -> ! {

let serial = Serial::new(
p.USART1,
(tx, rx, &mut afio.mapr),
(tx, rx),
Config::default().baudrate(9_600.bps()),
&clocks,
);
Expand Down
4 changes: 2 additions & 2 deletions examples/serial-dma-tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() -> ! {

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = p.AFIO.constrain();
//let mut afio = p.AFIO.constrain();
let channels = p.DMA1.split();

let mut gpioa = p.GPIOA.split();
Expand All @@ -49,7 +49,7 @@ fn main() -> ! {

let serial = Serial::new(
p.USART1,
(tx, rx, &mut afio.mapr),
(tx, rx),
Config::default().baudrate(9600.bps()),
&clocks,
);
Expand Down
4 changes: 2 additions & 2 deletions examples/serial-fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() -> ! {
let clocks = rcc.cfgr.freeze(&mut flash.acr);

// Prepare the alternate function I/O registers
let mut afio = p.AFIO.constrain();
//let mut afio = p.AFIO.constrain();

// Prepare the GPIOB peripheral
let mut gpiob = p.GPIOB.split();
Expand All @@ -61,7 +61,7 @@ fn main() -> ! {
// the registers are used to enable and configure the device.
let serial = Serial::new(
p.USART3,
(tx, rx, &mut afio.mapr),
(tx, rx),
Config::default().baudrate(9600.bps()),
&clocks,
);
Expand Down
13 changes: 7 additions & 6 deletions examples/serial-interrupt-idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ use panic_halt as _;

use cortex_m_rt::entry;
use stm32f1xx_hal::{
pac,
pac::interrupt,
pac::USART1,
pac::{self, interrupt, USART1},
prelude::*,
serial::{Rx, Serial, Tx},
serial::{Rx, Tx},
};

static mut RX: Option<Rx<USART1>> = None;
Expand Down Expand Up @@ -44,8 +42,11 @@ fn main() -> ! {

// Set up the usart device. Takes ownership over the USART register and tx/rx pins. The rest of
// the registers are used to enable and configure the device.
let (mut tx, mut rx) =
Serial::new(p.USART1, (tx, rx, &mut afio.mapr), 115_200.bps(), &clocks).split();
let (mut tx, mut rx) = p
.USART1
.remap(&mut afio.mapr)
.serial((tx, rx), 115_200.bps(), &clocks)
.split();
tx.listen();
rx.listen();
rx.listen_idle();
Expand Down
10 changes: 4 additions & 6 deletions examples/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> ! {
let clocks = rcc.cfgr.freeze(&mut flash.acr);

// Prepare the alternate function I/O registers
let mut afio = p.AFIO.constrain();
//let mut afio = p.AFIO.constrain();

// Prepare the GPIOB peripheral
let mut gpiob = p.GPIOB.split();
Expand All @@ -56,11 +56,9 @@ fn main() -> ! {

// Set up the usart device. Take ownership over the USART register and tx/rx pins. The rest of
// the registers are used to enable and configure the device.
let mut serial = p.USART3.serial(
(tx, rx, &mut afio.mapr),
Config::default().baudrate(9600.bps()),
&clocks,
);
let mut serial = p
.USART3
.serial((tx, rx), Config::default().baudrate(9600.bps()), &clocks);

// Loopback test. Write `X` and wait until the write is successful.
let sent = b'X';
Expand Down
4 changes: 2 additions & 2 deletions examples/serial_9bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn main() -> ! {
let clocks = rcc.cfgr.freeze(&mut flash.acr);

// Prepare the alternate function I/O registers.
let mut afio = p.AFIO.constrain();
//let mut afio = p.AFIO.constrain();

// Prepare the GPIOB peripheral.
let gpiob = p.GPIOB.split();
Expand All @@ -122,7 +122,7 @@ fn main() -> ! {
//let serial = Serial::<_, PushPull, Floating>::new(p.USART3,
// or shorter
let serial = p.USART3.serial(
(tx_pin, rx_pin, &mut afio.mapr),
(tx_pin, rx_pin),
Config::default()
.baudrate(9600.bps())
.wordlength_9bits()
Expand Down
4 changes: 2 additions & 2 deletions examples/serial_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> ! {
let clocks = rcc.cfgr.freeze(&mut flash.acr);

// Prepare the alternate function I/O registers
let mut afio = p.AFIO.constrain();
//let mut afio = p.AFIO.constrain();

// Prepare the GPIOB peripheral
let mut gpiob = p.GPIOB.split();
Expand All @@ -53,7 +53,7 @@ fn main() -> ! {
// the registers are used to enable and configure the device.
let mut tx = Serial::tx(
p.USART3,
(tx, &mut afio.mapr),
tx,
serial::Config::default()
.baudrate(9600.bps())
.stopbits(serial::StopBits::STOP2)
Expand Down
4 changes: 2 additions & 2 deletions examples/serial_reconfigure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() -> ! {
let clocks = rcc.cfgr.freeze(&mut flash.acr);

// Prepare the alternate function I/O registers
let mut afio = p.AFIO.constrain();
//let mut afio = p.AFIO.constrain();

// Prepare the GPIOB peripheral
let mut gpiob = p.GPIOB.split();
Expand All @@ -62,7 +62,7 @@ fn main() -> ! {
// the registers are used to enable and configure the device.
let mut serial = Serial::new(
p.USART3,
(tx, rx, &mut afio.mapr),
(tx, rx),
Config::default().baudrate(9600.bps()),
&clocks,
);
Expand Down
8 changes: 2 additions & 6 deletions examples/spi-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ fn main() -> ! {
let clocks = rcc.cfgr.freeze(&mut flash.acr);

// Acquire the GPIOB peripheral
let mut gpiob = dp.GPIOB.split();
let gpiob = dp.GPIOB.split();

let pins = (
gpiob.pb13.into_alternate_push_pull(&mut gpiob.crh),
gpiob.pb14.into_floating_input(&mut gpiob.crh),
gpiob.pb15.into_alternate_push_pull(&mut gpiob.crh),
);
let pins = (Some(gpiob.pb13), Some(gpiob.pb14), Some(gpiob.pb15));

let spi_mode = Mode {
polarity: Polarity::IdleLow,
Expand Down
Loading

0 comments on commit 206735e

Please sign in to comment.