[examples] Add examples for STM32F3
This commit is contained in:
		
							
								
								
									
										6
									
								
								examples/stm32f3/.cargo/config.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								examples/stm32f3/.cargo/config.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
				
			|||||||
 | 
					[target.'cfg(all(target_arch = "arm", target_os = "none"))']
 | 
				
			||||||
 | 
					# replace STM32F429ZITx with your chip as listed in `probe-run --list-chips`
 | 
				
			||||||
 | 
					runner = "probe-run --chip STM32F303VCTx"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[build]
 | 
				
			||||||
 | 
					target = "thumbv7em-none-eabihf"
 | 
				
			||||||
							
								
								
									
										22
									
								
								examples/stm32f3/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								examples/stm32f3/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					[package]
 | 
				
			||||||
 | 
					authors = ["Dario Nieuwenhuis <dirbaio@dirbaio.net>"]
 | 
				
			||||||
 | 
					edition = "2018"
 | 
				
			||||||
 | 
					name = "embassy-stm32f3-examples"
 | 
				
			||||||
 | 
					version = "0.1.0"
 | 
				
			||||||
 | 
					resolver = "2"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[dependencies]
 | 
				
			||||||
 | 
					embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] }
 | 
				
			||||||
 | 
					embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
 | 
				
			||||||
 | 
					embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32f303vc", "unstable-pac", "memory-x", "time-driver-tim2"]  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					defmt = "0.3"
 | 
				
			||||||
 | 
					defmt-rtt = "0.3"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					cortex-m = "0.7.3"
 | 
				
			||||||
 | 
					cortex-m-rt = "0.7.0"
 | 
				
			||||||
 | 
					embedded-hal = "0.2.6"
 | 
				
			||||||
 | 
					panic-probe = { version = "0.3", features = ["print-defmt"] }
 | 
				
			||||||
 | 
					futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
 | 
				
			||||||
 | 
					heapless = { version = "0.7.5", default-features = false }
 | 
				
			||||||
 | 
					nb = "1.0.0"
 | 
				
			||||||
							
								
								
									
										5
									
								
								examples/stm32f3/build.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								examples/stm32f3/build.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					fn main() {
 | 
				
			||||||
 | 
					    println!("cargo:rustc-link-arg-bins=--nmagic");
 | 
				
			||||||
 | 
					    println!("cargo:rustc-link-arg-bins=-Tlink.x");
 | 
				
			||||||
 | 
					    println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										30
									
								
								examples/stm32f3/src/bin/blinky.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								examples/stm32f3/src/bin/blinky.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
				
			|||||||
 | 
					#![no_std]
 | 
				
			||||||
 | 
					#![no_main]
 | 
				
			||||||
 | 
					#![feature(type_alias_impl_trait)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[path = "../example_common.rs"]
 | 
				
			||||||
 | 
					mod example_common;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use embassy::executor::Spawner;
 | 
				
			||||||
 | 
					use embassy::time::{Duration, Timer};
 | 
				
			||||||
 | 
					use embassy_stm32::gpio::{Level, Output, Speed};
 | 
				
			||||||
 | 
					use embassy_stm32::Peripherals;
 | 
				
			||||||
 | 
					use embedded_hal::digital::v2::OutputPin;
 | 
				
			||||||
 | 
					use example_common::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[embassy::main]
 | 
				
			||||||
 | 
					async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			||||||
 | 
					    info!("Hello World!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut led = Output::new(p.PE12, Level::High, Speed::Low);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    loop {
 | 
				
			||||||
 | 
					        info!("high");
 | 
				
			||||||
 | 
					        unwrap!(led.set_high());
 | 
				
			||||||
 | 
					        Timer::after(Duration::from_millis(1000)).await;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        info!("low");
 | 
				
			||||||
 | 
					        unwrap!(led.set_low());
 | 
				
			||||||
 | 
					        Timer::after(Duration::from_millis(1000)).await;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										33
									
								
								examples/stm32f3/src/bin/button.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								examples/stm32f3/src/bin/button.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					#![no_std]
 | 
				
			||||||
 | 
					#![no_main]
 | 
				
			||||||
 | 
					#![feature(type_alias_impl_trait)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[path = "../example_common.rs"]
 | 
				
			||||||
 | 
					mod example_common;
 | 
				
			||||||
 | 
					use cortex_m_rt::entry;
 | 
				
			||||||
 | 
					use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
 | 
				
			||||||
 | 
					use embedded_hal::digital::v2::{InputPin, OutputPin};
 | 
				
			||||||
 | 
					use example_common::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[entry]
 | 
				
			||||||
 | 
					fn main() -> ! {
 | 
				
			||||||
 | 
					    info!("Hello World!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let p = embassy_stm32::init(Default::default());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let button = Input::new(p.PA0, Pull::Down);
 | 
				
			||||||
 | 
					    let mut led1 = Output::new(p.PE9, Level::High, Speed::Low);
 | 
				
			||||||
 | 
					    let mut led2 = Output::new(p.PE15, Level::High, Speed::Low);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    loop {
 | 
				
			||||||
 | 
					        if unwrap!(button.is_high()) {
 | 
				
			||||||
 | 
					            info!("high");
 | 
				
			||||||
 | 
					            unwrap!(led1.set_high());
 | 
				
			||||||
 | 
					            unwrap!(led2.set_low());
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            info!("low");
 | 
				
			||||||
 | 
					            unwrap!(led1.set_low());
 | 
				
			||||||
 | 
					            unwrap!(led2.set_high());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										29
									
								
								examples/stm32f3/src/bin/button_exti.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								examples/stm32f3/src/bin/button_exti.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					#![no_std]
 | 
				
			||||||
 | 
					#![no_main]
 | 
				
			||||||
 | 
					#![feature(type_alias_impl_trait)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[path = "../example_common.rs"]
 | 
				
			||||||
 | 
					mod example_common;
 | 
				
			||||||
 | 
					use embassy::executor::Spawner;
 | 
				
			||||||
 | 
					use embassy_stm32::exti::ExtiInput;
 | 
				
			||||||
 | 
					use embassy_stm32::gpio::{Input, Pull};
 | 
				
			||||||
 | 
					use embassy_stm32::Peripherals;
 | 
				
			||||||
 | 
					use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
 | 
				
			||||||
 | 
					use example_common::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[embassy::main]
 | 
				
			||||||
 | 
					async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			||||||
 | 
					    info!("Hello World!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let button = Input::new(p.PA0, Pull::Down);
 | 
				
			||||||
 | 
					    let mut button = ExtiInput::new(button, p.EXTI0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    info!("Press the USER button...");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    loop {
 | 
				
			||||||
 | 
					        button.wait_for_rising_edge().await;
 | 
				
			||||||
 | 
					        info!("Pressed!");
 | 
				
			||||||
 | 
					        button.wait_for_falling_edge().await;
 | 
				
			||||||
 | 
					        info!("Released!");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										28
									
								
								examples/stm32f3/src/bin/hello.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								examples/stm32f3/src/bin/hello.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
				
			|||||||
 | 
					#![no_std]
 | 
				
			||||||
 | 
					#![no_main]
 | 
				
			||||||
 | 
					#![feature(type_alias_impl_trait)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use defmt::info;
 | 
				
			||||||
 | 
					use embassy::executor::Spawner;
 | 
				
			||||||
 | 
					use embassy::time::{Duration, Timer};
 | 
				
			||||||
 | 
					use embassy_stm32::time::Hertz;
 | 
				
			||||||
 | 
					use embassy_stm32::Config;
 | 
				
			||||||
 | 
					use embassy_stm32::Peripherals;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[path = "../example_common.rs"]
 | 
				
			||||||
 | 
					mod example_common;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn config() -> Config {
 | 
				
			||||||
 | 
					    let mut config = Config::default();
 | 
				
			||||||
 | 
					    config.rcc.hse = Some(Hertz(8_000_000));
 | 
				
			||||||
 | 
					    config.rcc.sysclk = Some(Hertz(16_000_000));
 | 
				
			||||||
 | 
					    config
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[embassy::main(config = "config()")]
 | 
				
			||||||
 | 
					async fn main(_spawner: Spawner, _p: Peripherals) -> ! {
 | 
				
			||||||
 | 
					    loop {
 | 
				
			||||||
 | 
					        info!("Hello World!");
 | 
				
			||||||
 | 
					        Timer::after(Duration::from_secs(1)).await;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										41
									
								
								examples/stm32f3/src/bin/spi_dma.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								examples/stm32f3/src/bin/spi_dma.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
				
			|||||||
 | 
					#![no_std]
 | 
				
			||||||
 | 
					#![no_main]
 | 
				
			||||||
 | 
					#![feature(type_alias_impl_trait)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[path = "../example_common.rs"]
 | 
				
			||||||
 | 
					mod example_common;
 | 
				
			||||||
 | 
					use core::fmt::Write;
 | 
				
			||||||
 | 
					use core::str::from_utf8;
 | 
				
			||||||
 | 
					use embassy::executor::Spawner;
 | 
				
			||||||
 | 
					use embassy_stm32::spi::{Config, Spi};
 | 
				
			||||||
 | 
					use embassy_stm32::time::Hertz;
 | 
				
			||||||
 | 
					use embassy_stm32::Peripherals;
 | 
				
			||||||
 | 
					use embassy_traits::spi::FullDuplex;
 | 
				
			||||||
 | 
					use example_common::*;
 | 
				
			||||||
 | 
					use heapless::String;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[embassy::main]
 | 
				
			||||||
 | 
					async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			||||||
 | 
					    info!("Hello World!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut spi = Spi::new(
 | 
				
			||||||
 | 
					        p.SPI1,
 | 
				
			||||||
 | 
					        p.PB3,
 | 
				
			||||||
 | 
					        p.PB5,
 | 
				
			||||||
 | 
					        p.PB4,
 | 
				
			||||||
 | 
					        p.DMA1_CH3,
 | 
				
			||||||
 | 
					        p.DMA1_CH2,
 | 
				
			||||||
 | 
					        Hertz(1_000_000),
 | 
				
			||||||
 | 
					        Config::default(),
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for n in 0u32.. {
 | 
				
			||||||
 | 
					        let mut write: String<128> = String::new();
 | 
				
			||||||
 | 
					        let mut read = [0; 128];
 | 
				
			||||||
 | 
					        core::write!(&mut write, "Hello DMA World {}!\r\n", n).unwrap();
 | 
				
			||||||
 | 
					        spi.read_write(&mut read[0..write.len()], write.as_bytes())
 | 
				
			||||||
 | 
					            .await
 | 
				
			||||||
 | 
					            .ok();
 | 
				
			||||||
 | 
					        info!("read via spi+dma: {}", from_utf8(&read).unwrap());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										30
									
								
								examples/stm32f3/src/bin/usart_dma.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								examples/stm32f3/src/bin/usart_dma.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
				
			|||||||
 | 
					#![no_std]
 | 
				
			||||||
 | 
					#![no_main]
 | 
				
			||||||
 | 
					#![feature(type_alias_impl_trait)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[path = "../example_common.rs"]
 | 
				
			||||||
 | 
					mod example_common;
 | 
				
			||||||
 | 
					use core::fmt::Write;
 | 
				
			||||||
 | 
					use embassy::executor::Spawner;
 | 
				
			||||||
 | 
					use embassy_stm32::dma::NoDma;
 | 
				
			||||||
 | 
					use embassy_stm32::usart::{Config, Uart};
 | 
				
			||||||
 | 
					use embassy_stm32::Peripherals;
 | 
				
			||||||
 | 
					use embassy_traits::uart::Write as _;
 | 
				
			||||||
 | 
					use example_common::*;
 | 
				
			||||||
 | 
					use heapless::String;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[embassy::main]
 | 
				
			||||||
 | 
					async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			||||||
 | 
					    info!("Hello World!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let config = Config::default();
 | 
				
			||||||
 | 
					    let mut usart = Uart::new(p.USART1, p.PE1, p.PE0, p.DMA1_CH4, NoDma, config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for n in 0u32.. {
 | 
				
			||||||
 | 
					        let mut s: String<128> = String::new();
 | 
				
			||||||
 | 
					        core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        unwrap!(usart.write(s.as_bytes()).await);
 | 
				
			||||||
 | 
					        info!("wrote DMA");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										19
									
								
								examples/stm32f3/src/example_common.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								examples/stm32f3/src/example_common.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					#![macro_use]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use defmt_rtt as _; // global logger
 | 
				
			||||||
 | 
					use panic_probe as _;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub use defmt::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use core::sync::atomic::{AtomicUsize, Ordering};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					defmt::timestamp! {
 | 
				
			||||||
 | 
					    "{=u64}",
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        static COUNT: AtomicUsize = AtomicUsize::new(0);
 | 
				
			||||||
 | 
					        // NOTE(no-CAS) `timestamps` runs with interrupts disabled
 | 
				
			||||||
 | 
					        let n = COUNT.load(Ordering::Relaxed);
 | 
				
			||||||
 | 
					        COUNT.store(n + 1, Ordering::Relaxed);
 | 
				
			||||||
 | 
					        n as u64
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user