From c79485c286d96ca7b1d0bdeb7360488b838a841e Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Tue, 21 Sep 2021 13:42:27 +0200 Subject: Support for STM32L1 * Add RCC * Fix more issues with dash in chip names * Update stm32-data version * Add blinky and spi example --- examples/stm32l1/.cargo/config.toml | 18 ++++++++++++++ examples/stm32l1/Cargo.toml | 35 +++++++++++++++++++++++++++ examples/stm32l1/build.rs | 31 ++++++++++++++++++++++++ examples/stm32l1/memory.x | 5 ++++ examples/stm32l1/src/bin/blinky.rs | 30 ++++++++++++++++++++++++ examples/stm32l1/src/bin/spi.rs | 43 ++++++++++++++++++++++++++++++++++ examples/stm32l1/src/example_common.rs | 17 ++++++++++++++ examples/stm32l4/.cargo/config.toml | 3 ++- 8 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 examples/stm32l1/.cargo/config.toml create mode 100644 examples/stm32l1/Cargo.toml create mode 100644 examples/stm32l1/build.rs create mode 100644 examples/stm32l1/memory.x create mode 100644 examples/stm32l1/src/bin/blinky.rs create mode 100644 examples/stm32l1/src/bin/spi.rs create mode 100644 examples/stm32l1/src/example_common.rs (limited to 'examples') diff --git a/examples/stm32l1/.cargo/config.toml b/examples/stm32l1/.cargo/config.toml new file mode 100644 index 000000000..e0d2bddf0 --- /dev/null +++ b/examples/stm32l1/.cargo/config.toml @@ -0,0 +1,18 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +# replace your chip as listed in `probe-run --list-chips` +runner = "probe-run --chip STM32L151CBxxA" + +rustflags = [ + # LLD (shipped with the Rust toolchain) is used as the default linker + "-C", "link-arg=--nmagic", + "-C", "link-arg=-Tlink.x", + "-C", "link-arg=-Tdefmt.x", + + # Code-size optimizations. + "-Z", "trap-unreachable=no", + "-C", "inline-threshold=5", + "-C", "no-vectorize-loops", +] + +[build] +target = "thumbv7m-none-eabi" diff --git a/examples/stm32l1/Cargo.toml b/examples/stm32l1/Cargo.toml new file mode 100644 index 000000000..e4dd7186a --- /dev/null +++ b/examples/stm32l1/Cargo.toml @@ -0,0 +1,35 @@ +[package] +authors = ["Dario Nieuwenhuis ", "Ulf Lilleengen "] +edition = "2018" +name = "embassy-stm32l1-examples" +version = "0.1.0" +resolver = "2" + +[features] +default = [ + "defmt-default", +] +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] + +[dependencies] +embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } +embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32l151cb-a", "time-driver-tim2", "memory-x"] } +embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } +embassy-macros = { path = "../../embassy-macros" } + +defmt = "0.2.3" +defmt-rtt = "0.2.0" + +cortex-m = "0.7.3" +cortex-m-rt = "0.7.0" +embedded-hal = "0.2.6" +panic-probe = { version = "0.2.0", features = ["print-defmt"] } +futures = { version = "0.3.17", default-features = false, features = ["async-await"] } +rtt-target = { version = "0.3.1", features = ["cortex-m"] } +heapless = { version = "0.7.5", default-features = false } diff --git a/examples/stm32l1/build.rs b/examples/stm32l1/build.rs new file mode 100644 index 000000000..d534cc3df --- /dev/null +++ b/examples/stm32l1/build.rs @@ -0,0 +1,31 @@ +//! This build script copies the `memory.x` file from the crate root into +//! a directory where the linker can always find it at build time. +//! For many projects this is optional, as the linker always searches the +//! project root directory -- wherever `Cargo.toml` is. However, if you +//! are using a workspace or have a more complicated build setup, this +//! build script becomes required. Additionally, by requesting that +//! Cargo re-run the build script whenever `memory.x` is changed, +//! updating `memory.x` ensures a rebuild of the application with the +//! new memory settings. + +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + // Put `memory.x` in our output directory and ensure it's + // on the linker search path. + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + // By default, Cargo will re-run a build script whenever + // any file in the project changes. By specifying `memory.x` + // here, we ensure the build script is only re-run when + // `memory.x` is changed. + println!("cargo:rerun-if-changed=memory.x"); +} diff --git a/examples/stm32l1/memory.x b/examples/stm32l1/memory.x new file mode 100644 index 000000000..c94d395c8 --- /dev/null +++ b/examples/stm32l1/memory.x @@ -0,0 +1,5 @@ +MEMORY +{ + FLASH : ORIGIN = 0x08000000, LENGTH = 128K + RAM : ORIGIN = 0x20000000, LENGTH = 32K +} diff --git a/examples/stm32l1/src/bin/blinky.rs b/examples/stm32l1/src/bin/blinky.rs new file mode 100644 index 000000000..deabdddba --- /dev/null +++ b/examples/stm32l1/src/bin/blinky.rs @@ -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.PA12, 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; + } +} diff --git a/examples/stm32l1/src/bin/spi.rs b/examples/stm32l1/src/bin/spi.rs new file mode 100644 index 000000000..3cfbe3fc4 --- /dev/null +++ b/examples/stm32l1/src/bin/spi.rs @@ -0,0 +1,43 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; + +use embassy::executor::Spawner; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embedded_hal::digital::v2::OutputPin; +use example_common::*; + +use embassy_stm32::dma::NoDma; +use embassy_stm32::spi::{Config, Spi}; +use embassy_stm32::time::Hertz; +use embassy_stm32::Peripherals; +use embedded_hal::blocking::spi::Transfer; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World, folks!"); + + let mut spi = Spi::new( + p.SPI1, + p.PA5, + p.PA7, + p.PA6, + NoDma, + NoDma, + Hertz(1_000_000), + Config::default(), + ); + + let mut cs = Output::new(p.PA4, Level::High, Speed::VeryHigh); + + loop { + let mut buf = [0x0Au8; 4]; + unwrap!(cs.set_low()); + unwrap!(spi.transfer(&mut buf)); + unwrap!(cs.set_high()); + info!("xfer {=[u8]:x}", buf); + } +} diff --git a/examples/stm32l1/src/example_common.rs b/examples/stm32l1/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/examples/stm32l1/src/example_common.rs @@ -0,0 +1,17 @@ +#![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 + } +} diff --git a/examples/stm32l4/.cargo/config.toml b/examples/stm32l4/.cargo/config.toml index 38dd98d1e..b157e3aeb 100644 --- a/examples/stm32l4/.cargo/config.toml +++ b/examples/stm32l4/.cargo/config.toml @@ -1,6 +1,7 @@ [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 STM32L4S5VI" +#runner = "probe-run --chip STM32L475VGT6" +runner = "probe-run --chip STM32L475VG" rustflags = [ # LLD (shipped with the Rust toolchain) is used as the default linker -- cgit