Skip to content

Commit 1a1e13d

Browse files
sjoerdsimonseldruin
authored andcommitted
examples: Switch to embassy-stm32 as a pac provider
stmf32f1xx doesn't yet support embedded-hal 1.0. Switch to embassy-stm32 as a pac provider, which can be used in both a sync and async setups. Unfortunately for blocking i2c support embassy git has to be used rather then a releaed version. Also for i2c move the expected configuration from PB8/PB9 to PB6/PB7 to avoid the needed for pin remapping, which embassy doesn't yet support on stm32f1xx chips
1 parent f39ba59 commit 1a1e13d

17 files changed

+322
-634
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
runner = [ "probe-rs", "run", "--chip", "STM32F103C8" ]
33
rustflags = [
44
"-C", "link-arg=-Tlink.x",
5+
"-C", "link-arg=--nmagic",
6+
"-C", "link-arg=-Tdefmt.x",
57
]
68

79
[build]

Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,27 @@ display-interface-spi = "0.5.0"
2424
embedded-graphics-core = { version = "0.4.0", optional = true }
2525

2626
[dev-dependencies]
27-
cortex-m = "0.7.2"
27+
cortex-m = { version = "0.7.2", features = ["critical-section-single-core"] }
2828
cortex-m-rt = "0.7.3"
2929
cortex-m-rtic = "1.1.4"
30-
panic-halt = "0.2.0"
31-
cast = { version = "0.2.6", default-features = false }
30+
defmt = "0.3.6"
31+
defmt-rtt = "0.4.0"
32+
panic-probe = { version = "0.3.1", features = ["print-defmt"] }
3233
# Used to load BMP images in various examples
3334
tinybmp = "0.5.0"
3435
embedded-graphics = "0.8.0"
3536
# Used by the noise_i2c examples
3637
rand = { version = "0.8.4", default-features = false, features = [ "small_rng" ] }
37-
stm32f1xx-hal = { version = "0.10.0", features = [ "rt", "stm32f103" ] }
38+
embassy-stm32 = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = [ "stm32f103c8", "memory-x", "defmt", "exti", "time-driver-tim3" , "unstable-pac"] }
39+
embassy-time = { version = "0.3.1", git = "https://github.com/embassy-rs/embassy" }
40+
embedded-hal-bus = "0.2.0"
3841

3942
[features]
4043
default = ["graphics"]
4144
graphics = ["embedded-graphics-core"]
4245

4346
[profile.dev]
47+
opt-level="s"
4448
codegen-units = 1
4549
incremental = false
4650

build.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/bmp_i2c.rs

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,34 @@
1212
//! Display -> Blue Pill
1313
//! (black) GND -> GND
1414
//! (red) +5V -> VCC
15-
//! (yellow) SDA -> PB9
16-
//! (green) SCL -> PB8
15+
//! (yellow) SDA -> PB7
16+
//! (green) SCL -> PB6
1717
//! ```
1818
//!
1919
//! Run on a Blue Pill with `cargo run --example image_i2c`.
2020
2121
#![no_std]
2222
#![no_main]
2323

24-
use cortex_m_rt::{entry, exception, ExceptionFrame};
24+
use cortex_m::asm::nop;
25+
use cortex_m_rt::entry;
26+
use defmt_rtt as _;
27+
use embassy_stm32::time::Hertz;
2528
use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*};
26-
use panic_halt as _;
29+
use panic_probe as _;
2730
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
28-
use stm32f1xx_hal::{
29-
i2c::{BlockingI2c, DutyCycle, Mode},
30-
prelude::*,
31-
stm32,
32-
};
31+
3332
use tinybmp::Bmp;
3433

3534
#[entry]
3635
fn main() -> ! {
37-
let dp = stm32::Peripherals::take().unwrap();
38-
39-
let mut flash = dp.FLASH.constrain();
40-
let rcc = dp.RCC.constrain();
41-
42-
let clocks = rcc.cfgr.freeze(&mut flash.acr);
43-
44-
let mut afio = dp.AFIO.constrain();
45-
46-
let mut gpiob = dp.GPIOB.split();
47-
48-
let scl = gpiob.pb8.into_alternate_open_drain(&mut gpiob.crh);
49-
let sda = gpiob.pb9.into_alternate_open_drain(&mut gpiob.crh);
50-
51-
let i2c = BlockingI2c::i2c1(
52-
dp.I2C1,
53-
(scl, sda),
54-
&mut afio.mapr,
55-
Mode::Fast {
56-
frequency: 400_000.Hz(),
57-
duty_cycle: DutyCycle::Ratio2to1,
58-
},
59-
clocks,
60-
1000,
61-
10,
62-
1000,
63-
1000,
36+
let p = embassy_stm32::init(Default::default());
37+
let i2c = embassy_stm32::i2c::I2c::new_blocking(
38+
p.I2C1,
39+
p.PB6,
40+
p.PB7,
41+
Hertz::khz(400),
42+
Default::default(),
6443
);
6544

6645
let interface = I2CDisplayInterface::new(i2c);
@@ -80,10 +59,7 @@ fn main() -> ! {
8059

8160
display.flush().unwrap();
8261

83-
loop {}
84-
}
85-
86-
#[exception]
87-
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
88-
panic!("{:#?}", ef);
62+
loop {
63+
nop()
64+
}
8965
}

examples/graphics.rs

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,53 @@
99
//! ```
1010
//! GND -> GND
1111
//! 3V3 -> VCC
12-
//! PA5 -> SCL
13-
//! PA7 -> SDA
12+
//! PA5 -> SCL (D0)
13+
//! PA7 -> SDA (D1)
1414
//! PB0 -> RST
1515
//! PB1 -> D/C
16+
//! PB10 -> CS
1617
//! ```
1718
//!
1819
//! Run on a Blue Pill with `cargo run --example graphics`.
1920
2021
#![no_std]
2122
#![no_main]
2223

23-
use cortex_m_rt::{entry, exception, ExceptionFrame};
24+
use cortex_m::asm::nop;
25+
use cortex_m_rt::entry;
26+
use defmt_rtt as _;
27+
use embassy_stm32::{
28+
gpio,
29+
spi::{self, Spi},
30+
time::Hertz,
31+
};
2432
use embedded_graphics::{
2533
pixelcolor::BinaryColor,
2634
prelude::*,
2735
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
2836
};
29-
use panic_halt as _;
37+
use panic_probe as _;
3038
use ssd1306::{prelude::*, Ssd1306};
31-
use stm32f1xx_hal::{
32-
prelude::*,
33-
spi::{Mode, Phase, Polarity, Spi},
34-
stm32,
35-
timer::Timer,
36-
};
3739

3840
#[entry]
3941
fn main() -> ! {
40-
let cp = cortex_m::Peripherals::take().unwrap();
41-
let dp = stm32::Peripherals::take().unwrap();
42-
43-
let mut flash = dp.FLASH.constrain();
44-
let rcc = dp.RCC.constrain();
45-
46-
let clocks = rcc.cfgr.freeze(&mut flash.acr);
47-
48-
let mut afio = dp.AFIO.constrain();
49-
50-
let mut gpioa = dp.GPIOA.split();
51-
let mut gpiob = dp.GPIOB.split();
42+
let p = embassy_stm32::init(Default::default());
43+
let mut config = spi::Config::default();
44+
config.frequency = Hertz::mhz(8);
45+
let spi = Spi::new_blocking_txonly(p.SPI1, p.PA5, p.PA7, config);
5246

53-
// SPI1
54-
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
55-
let miso = gpioa.pa6;
56-
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
57-
58-
let mut delay = Timer::syst(cp.SYST, &clocks).delay();
59-
60-
let mut rst = gpiob.pb0.into_push_pull_output(&mut gpiob.crl);
61-
let dc = gpiob.pb1.into_push_pull_output(&mut gpiob.crl);
62-
63-
let spi = Spi::spi1(
64-
dp.SPI1,
65-
(sck, miso, mosi),
66-
&mut afio.mapr,
67-
Mode {
68-
polarity: Polarity::IdleLow,
69-
phase: Phase::CaptureOnFirstTransition,
70-
},
71-
8.MHz(),
72-
clocks,
73-
);
47+
let mut rst = gpio::Output::new(p.PB0, gpio::Level::Low, gpio::Speed::Low);
48+
let dc = gpio::Output::new(p.PB1, gpio::Level::Low, gpio::Speed::Low);
49+
let cs = gpio::Output::new(p.PB10, gpio::Level::Low, gpio::Speed::Low);
50+
let spi = embedded_hal_bus::spi::ExclusiveDevice::new_no_delay(spi, cs).unwrap();
7451

7552
let interface = display_interface_spi::SPIInterface::new(spi, dc);
7653
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
7754
.into_buffered_graphics_mode();
7855

79-
display.reset(&mut rst, &mut delay).unwrap();
56+
display
57+
.reset(&mut rst, &mut embassy_time::Delay {})
58+
.unwrap();
8059
display.init().unwrap();
8160

8261
let yoffset = 20;
@@ -118,10 +97,7 @@ fn main() -> ! {
11897

11998
display.flush().unwrap();
12099

121-
loop {}
122-
}
123-
124-
#[exception]
125-
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
126-
panic!("{:#?}", ef);
100+
loop {
101+
nop()
102+
}
127103
}

examples/graphics_i2c.rs

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,36 @@
88
//! Display -> Blue Pill
99
//! (black) GND -> GND
1010
//! (red) +5V -> VCC
11-
//! (yellow) SDA -> PB9
12-
//! (green) SCL -> PB8
11+
//! (yellow) SDA -> PB7
12+
//! (green) SCL -> PB6
1313
//! ```
1414
//!
1515
//! Run on a Blue Pill with `cargo run --example graphics_i2c`.
1616
1717
#![no_std]
1818
#![no_main]
1919

20-
use cortex_m_rt::{entry, exception, ExceptionFrame};
20+
use cortex_m::asm::nop;
21+
use cortex_m_rt::entry;
22+
use defmt_rtt as _;
23+
use embassy_stm32::time::Hertz;
2124
use embedded_graphics::{
2225
pixelcolor::BinaryColor,
2326
prelude::*,
2427
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
2528
};
26-
use panic_halt as _;
29+
use panic_probe as _;
2730
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
28-
use stm32f1xx_hal::{
29-
i2c::{BlockingI2c, DutyCycle, Mode},
30-
prelude::*,
31-
stm32,
32-
};
3331

3432
#[entry]
3533
fn main() -> ! {
36-
let dp = stm32::Peripherals::take().unwrap();
37-
38-
let mut flash = dp.FLASH.constrain();
39-
let rcc = dp.RCC.constrain();
40-
41-
let clocks = rcc.cfgr.freeze(&mut flash.acr);
42-
43-
let mut afio = dp.AFIO.constrain();
44-
45-
let mut gpiob = dp.GPIOB.split();
46-
47-
let scl = gpiob.pb8.into_alternate_open_drain(&mut gpiob.crh);
48-
let sda = gpiob.pb9.into_alternate_open_drain(&mut gpiob.crh);
49-
50-
let i2c = BlockingI2c::i2c1(
51-
dp.I2C1,
52-
(scl, sda),
53-
&mut afio.mapr,
54-
Mode::Fast {
55-
frequency: 400_000.Hz(),
56-
duty_cycle: DutyCycle::Ratio2to1,
57-
},
58-
clocks,
59-
1000,
60-
10,
61-
1000,
62-
1000,
34+
let p = embassy_stm32::init(Default::default());
35+
let i2c = embassy_stm32::i2c::I2c::new_blocking(
36+
p.I2C1,
37+
p.PB6,
38+
p.PB7,
39+
Hertz::khz(400),
40+
Default::default(),
6341
);
6442

6543
let interface = I2CDisplayInterface::new(i2c);
@@ -106,10 +84,7 @@ fn main() -> ! {
10684

10785
display.flush().unwrap();
10886

109-
loop {}
110-
}
111-
112-
#[exception]
113-
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
114-
panic!("{:#?}", ef);
87+
loop {
88+
nop()
89+
}
11590
}

0 commit comments

Comments
 (0)