aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f3/.cargo/config.toml6
-rw-r--r--examples/stm32f3/Cargo.toml22
-rw-r--r--examples/stm32f3/build.rs5
-rw-r--r--examples/stm32f3/src/bin/blinky.rs30
-rw-r--r--examples/stm32f3/src/bin/button.rs33
-rw-r--r--examples/stm32f3/src/bin/button_exti.rs29
-rw-r--r--examples/stm32f3/src/bin/hello.rs28
-rw-r--r--examples/stm32f3/src/bin/spi_dma.rs41
-rw-r--r--examples/stm32f3/src/bin/usart_dma.rs30
-rw-r--r--examples/stm32f3/src/example_common.rs19
10 files changed, 243 insertions, 0 deletions
diff --git a/examples/stm32f3/.cargo/config.toml b/examples/stm32f3/.cargo/config.toml
new file mode 100644
index 000000000..eb8a8b335
--- /dev/null
+++ b/examples/stm32f3/.cargo/config.toml
@@ -0,0 +1,6 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2# replace STM32F429ZITx with your chip as listed in `probe-run --list-chips`
3runner = "probe-run --chip STM32F303VCTx"
4
5[build]
6target = "thumbv7em-none-eabihf"
diff --git a/examples/stm32f3/Cargo.toml b/examples/stm32f3/Cargo.toml
new file mode 100644
index 000000000..cd9cd3a8f
--- /dev/null
+++ b/examples/stm32f3/Cargo.toml
@@ -0,0 +1,22 @@
1[package]
2authors = ["Dario Nieuwenhuis <[email protected]>"]
3edition = "2018"
4name = "embassy-stm32f3-examples"
5version = "0.1.0"
6resolver = "2"
7
8[dependencies]
9embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] }
10embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
11embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32f303vc", "unstable-pac", "memory-x", "time-driver-tim2"] }
12
13defmt = "0.3"
14defmt-rtt = "0.3"
15
16cortex-m = "0.7.3"
17cortex-m-rt = "0.7.0"
18embedded-hal = "0.2.6"
19panic-probe = { version = "0.3", features = ["print-defmt"] }
20futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
21heapless = { version = "0.7.5", default-features = false }
22nb = "1.0.0"
diff --git a/examples/stm32f3/build.rs b/examples/stm32f3/build.rs
new file mode 100644
index 000000000..8cd32d7ed
--- /dev/null
+++ b/examples/stm32f3/build.rs
@@ -0,0 +1,5 @@
1fn main() {
2 println!("cargo:rustc-link-arg-bins=--nmagic");
3 println!("cargo:rustc-link-arg-bins=-Tlink.x");
4 println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
5}
diff --git a/examples/stm32f3/src/bin/blinky.rs b/examples/stm32f3/src/bin/blinky.rs
new file mode 100644
index 000000000..321643557
--- /dev/null
+++ b/examples/stm32f3/src/bin/blinky.rs
@@ -0,0 +1,30 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7
8use embassy::executor::Spawner;
9use embassy::time::{Duration, Timer};
10use embassy_stm32::gpio::{Level, Output, Speed};
11use embassy_stm32::Peripherals;
12use embedded_hal::digital::v2::OutputPin;
13use example_common::*;
14
15#[embassy::main]
16async fn main(_spawner: Spawner, p: Peripherals) {
17 info!("Hello World!");
18
19 let mut led = Output::new(p.PE12, Level::High, Speed::Low);
20
21 loop {
22 info!("high");
23 unwrap!(led.set_high());
24 Timer::after(Duration::from_millis(1000)).await;
25
26 info!("low");
27 unwrap!(led.set_low());
28 Timer::after(Duration::from_millis(1000)).await;
29 }
30}
diff --git a/examples/stm32f3/src/bin/button.rs b/examples/stm32f3/src/bin/button.rs
new file mode 100644
index 000000000..c5fab138b
--- /dev/null
+++ b/examples/stm32f3/src/bin/button.rs
@@ -0,0 +1,33 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use cortex_m_rt::entry;
8use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
9use embedded_hal::digital::v2::{InputPin, OutputPin};
10use example_common::*;
11
12#[entry]
13fn main() -> ! {
14 info!("Hello World!");
15
16 let p = embassy_stm32::init(Default::default());
17
18 let button = Input::new(p.PA0, Pull::Down);
19 let mut led1 = Output::new(p.PE9, Level::High, Speed::Low);
20 let mut led2 = Output::new(p.PE15, Level::High, Speed::Low);
21
22 loop {
23 if unwrap!(button.is_high()) {
24 info!("high");
25 unwrap!(led1.set_high());
26 unwrap!(led2.set_low());
27 } else {
28 info!("low");
29 unwrap!(led1.set_low());
30 unwrap!(led2.set_high());
31 }
32 }
33}
diff --git a/examples/stm32f3/src/bin/button_exti.rs b/examples/stm32f3/src/bin/button_exti.rs
new file mode 100644
index 000000000..d45e4365b
--- /dev/null
+++ b/examples/stm32f3/src/bin/button_exti.rs
@@ -0,0 +1,29 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use embassy::executor::Spawner;
8use embassy_stm32::exti::ExtiInput;
9use embassy_stm32::gpio::{Input, Pull};
10use embassy_stm32::Peripherals;
11use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
12use example_common::*;
13
14#[embassy::main]
15async fn main(_spawner: Spawner, p: Peripherals) {
16 info!("Hello World!");
17
18 let button = Input::new(p.PA0, Pull::Down);
19 let mut button = ExtiInput::new(button, p.EXTI0);
20
21 info!("Press the USER button...");
22
23 loop {
24 button.wait_for_rising_edge().await;
25 info!("Pressed!");
26 button.wait_for_falling_edge().await;
27 info!("Released!");
28 }
29}
diff --git a/examples/stm32f3/src/bin/hello.rs b/examples/stm32f3/src/bin/hello.rs
new file mode 100644
index 000000000..9a67419fb
--- /dev/null
+++ b/examples/stm32f3/src/bin/hello.rs
@@ -0,0 +1,28 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::info;
6use embassy::executor::Spawner;
7use embassy::time::{Duration, Timer};
8use embassy_stm32::time::Hertz;
9use embassy_stm32::Config;
10use embassy_stm32::Peripherals;
11
12#[path = "../example_common.rs"]
13mod example_common;
14
15fn config() -> Config {
16 let mut config = Config::default();
17 config.rcc.hse = Some(Hertz(8_000_000));
18 config.rcc.sysclk = Some(Hertz(16_000_000));
19 config
20}
21
22#[embassy::main(config = "config()")]
23async fn main(_spawner: Spawner, _p: Peripherals) -> ! {
24 loop {
25 info!("Hello World!");
26 Timer::after(Duration::from_secs(1)).await;
27 }
28}
diff --git a/examples/stm32f3/src/bin/spi_dma.rs b/examples/stm32f3/src/bin/spi_dma.rs
new file mode 100644
index 000000000..a87a36f73
--- /dev/null
+++ b/examples/stm32f3/src/bin/spi_dma.rs
@@ -0,0 +1,41 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use core::fmt::Write;
8use core::str::from_utf8;
9use embassy::executor::Spawner;
10use embassy_stm32::spi::{Config, Spi};
11use embassy_stm32::time::Hertz;
12use embassy_stm32::Peripherals;
13use embassy_traits::spi::FullDuplex;
14use example_common::*;
15use heapless::String;
16
17#[embassy::main]
18async fn main(_spawner: Spawner, p: Peripherals) {
19 info!("Hello World!");
20
21 let mut spi = Spi::new(
22 p.SPI1,
23 p.PB3,
24 p.PB5,
25 p.PB4,
26 p.DMA1_CH3,
27 p.DMA1_CH2,
28 Hertz(1_000_000),
29 Config::default(),
30 );
31
32 for n in 0u32.. {
33 let mut write: String<128> = String::new();
34 let mut read = [0; 128];
35 core::write!(&mut write, "Hello DMA World {}!\r\n", n).unwrap();
36 spi.read_write(&mut read[0..write.len()], write.as_bytes())
37 .await
38 .ok();
39 info!("read via spi+dma: {}", from_utf8(&read).unwrap());
40 }
41}
diff --git a/examples/stm32f3/src/bin/usart_dma.rs b/examples/stm32f3/src/bin/usart_dma.rs
new file mode 100644
index 000000000..99530b5c0
--- /dev/null
+++ b/examples/stm32f3/src/bin/usart_dma.rs
@@ -0,0 +1,30 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use core::fmt::Write;
8use embassy::executor::Spawner;
9use embassy_stm32::dma::NoDma;
10use embassy_stm32::usart::{Config, Uart};
11use embassy_stm32::Peripherals;
12use embassy_traits::uart::Write as _;
13use example_common::*;
14use heapless::String;
15
16#[embassy::main]
17async fn main(_spawner: Spawner, p: Peripherals) {
18 info!("Hello World!");
19
20 let config = Config::default();
21 let mut usart = Uart::new(p.USART1, p.PE1, p.PE0, p.DMA1_CH4, NoDma, config);
22
23 for n in 0u32.. {
24 let mut s: String<128> = String::new();
25 core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap();
26
27 unwrap!(usart.write(s.as_bytes()).await);
28 info!("wrote DMA");
29 }
30}
diff --git a/examples/stm32f3/src/example_common.rs b/examples/stm32f3/src/example_common.rs
new file mode 100644
index 000000000..e14517033
--- /dev/null
+++ b/examples/stm32f3/src/example_common.rs
@@ -0,0 +1,19 @@
1#![macro_use]
2
3use defmt_rtt as _; // global logger
4use panic_probe as _;
5
6pub use defmt::*;
7
8use core::sync::atomic::{AtomicUsize, Ordering};
9
10defmt::timestamp! {
11 "{=u64}",
12 {
13 static COUNT: AtomicUsize = AtomicUsize::new(0);
14 // NOTE(no-CAS) `timestamps` runs with interrupts disabled
15 let n = COUNT.load(Ordering::Relaxed);
16 COUNT.store(n + 1, Ordering::Relaxed);
17 n as u64
18 }
19}