From fc32b3750c448a81b7dd44cf9de98723b8eb4fcf Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 4 May 2022 01:00:38 +0200 Subject: Remove embassy_hal_common::usb. The replacement is `embassy-usb`. There's a WIP driver for stm32 USBD in #709, there's no WIP driver for stm32 USB_OTG. This means we're left without USB_OTG support for now. Reason for removing is I'm going to soon remove `embassy::io`, and USB uses it. I don't want to spend time maintaining "dead" code that is going to be removed. Volunteers welcome, either to update old USB to the new IO, or write a USB_OTG driver fo the new USB. --- examples/stm32f4/Cargo.toml | 2 +- examples/stm32f4/src/bin/usb_uart.rs | 99 ------------------------- examples/stm32f4/src/bin/usb_uart_ulpi.rs | 114 ----------------------------- examples/stm32l4/Cargo.toml | 2 +- examples/stm32l4/src/bin/usb_uart.rs | 115 ------------------------------ 5 files changed, 2 insertions(+), 330 deletions(-) delete mode 100644 examples/stm32f4/src/bin/usb_uart.rs delete mode 100644 examples/stm32f4/src/bin/usb_uart_ulpi.rs delete mode 100644 examples/stm32l4/src/bin/usb_uart.rs (limited to 'examples') diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index 1bc406495..e2065bed9 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml @@ -8,7 +8,7 @@ resolver = "2" [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti", "usb-otg"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } defmt = "0.3" defmt-rtt = "0.3" diff --git a/examples/stm32f4/src/bin/usb_uart.rs b/examples/stm32f4/src/bin/usb_uart.rs deleted file mode 100644 index 251ed1eb0..000000000 --- a/examples/stm32f4/src/bin/usb_uart.rs +++ /dev/null @@ -1,99 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use defmt_rtt as _; // global logger -use panic_probe as _; - -use defmt::{info, unwrap}; -use defmt_rtt as _; // global logger -use embassy::interrupt::InterruptExt; -use futures::pin_mut; -use panic_probe as _; // print out panic messages - -use embassy::executor::Spawner; -use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; -use embassy_stm32::usb_otg::{State, Usb, UsbBus, UsbOtg, UsbSerial}; -use embassy_stm32::{interrupt, time::Hertz, Config, Peripherals}; -use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; - -static mut EP_MEMORY: [u32; 2048] = [0; 2048]; - -// USB requires at least 48 MHz clock -fn config() -> Config { - let mut config = Config::default(); - config.rcc.sys_ck = Some(Hertz(48_000_000)); - config -} - -#[embassy::main(config = "config()")] -async fn main(_spawner: Spawner, p: Peripherals) { - let mut rx_buffer = [0u8; 64]; - // we send back input + cr + lf - let mut tx_buffer = [0u8; 66]; - - let peri = UsbOtg::new_fs(p.USB_OTG_FS, p.PA12, p.PA11); - let usb_bus = UsbBus::new(peri, unsafe { &mut EP_MEMORY }); - - let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); - - let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) - .manufacturer("Fake company") - .product("Serial port") - .serial_number("TEST") - .device_class(0x02) - .build(); - - let irq = interrupt::take!(OTG_FS); - irq.set_priority(interrupt::Priority::P3); - - let mut state = State::new(); - let usb = unsafe { Usb::new(&mut state, device, serial, irq) }; - pin_mut!(usb); - - let (mut reader, mut writer) = usb.as_ref().take_serial_0(); - - info!("usb initialized!"); - - unwrap!( - writer - .write_all(b"\r\nInput returned upper cased on CR+LF\r\n") - .await - ); - - let mut buf = [0u8; 64]; - loop { - let mut n = 0; - - async { - loop { - let char = unwrap!(reader.read_byte().await); - - if char == b'\r' || char == b'\n' { - break; - } - - buf[n] = char; - n += 1; - - // stop if we're out of room - if n == buf.len() { - break; - } - } - } - .await; - - if n > 0 { - for char in buf[..n].iter_mut() { - // upper case - if 0x61 <= *char && *char <= 0x7a { - *char &= !0x20; - } - } - unwrap!(writer.write_all(&buf[..n]).await); - unwrap!(writer.write_all(b"\r\n").await); - unwrap!(writer.flush().await); - } - } -} diff --git a/examples/stm32f4/src/bin/usb_uart_ulpi.rs b/examples/stm32f4/src/bin/usb_uart_ulpi.rs deleted file mode 100644 index f6c373602..000000000 --- a/examples/stm32f4/src/bin/usb_uart_ulpi.rs +++ /dev/null @@ -1,114 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use defmt_rtt as _; // global logger -use panic_probe as _; - -use defmt::{info, unwrap}; -use defmt_rtt as _; // global logger -use embassy::interrupt::InterruptExt; -use futures::pin_mut; -use panic_probe as _; // print out panic messages - -use embassy::executor::Spawner; -use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; -use embassy_stm32::usb_otg::{State, Usb, UsbBus, UsbOtg, UsbSerial}; -use embassy_stm32::{interrupt, time::Hertz, Config, Peripherals}; -use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; - -static mut EP_MEMORY: [u32; 2048] = [0; 2048]; - -// USB requires at least 48 MHz clock -fn config() -> Config { - let mut config = Config::default(); - config.rcc.sys_ck = Some(Hertz(48_000_000)); - config -} - -#[embassy::main(config = "config()")] -async fn main(_spawner: Spawner, p: Peripherals) { - let mut rx_buffer = [0u8; 64]; - // we send back input + cr + lf - let mut tx_buffer = [0u8; 66]; - - // USB with external high-speed PHY - let peri = UsbOtg::new_hs_ulpi( - p.USB_OTG_HS, - p.PA5, - p.PC2, - p.PC3, - p.PC0, - p.PA3, - p.PB0, - p.PB1, - p.PB10, - p.PB11, - p.PB12, - p.PB13, - p.PB5, - ); - let usb_bus = UsbBus::new(peri, unsafe { &mut EP_MEMORY }); - - let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); - - let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) - .manufacturer("Fake company") - .product("Serial port") - .serial_number("TEST") - .device_class(0x02) - .build(); - - let irq = interrupt::take!(OTG_FS); - irq.set_priority(interrupt::Priority::P3); - - let mut state = State::new(); - let usb = unsafe { Usb::new(&mut state, device, serial, irq) }; - pin_mut!(usb); - - let (mut reader, mut writer) = usb.as_ref().take_serial_0(); - - info!("usb initialized!"); - - unwrap!( - writer - .write_all(b"\r\nInput returned upper cased on CR+LF\r\n") - .await - ); - - let mut buf = [0u8; 64]; - loop { - let mut n = 0; - - async { - loop { - let char = unwrap!(reader.read_byte().await); - - if char == b'\r' || char == b'\n' { - break; - } - - buf[n] = char; - n += 1; - - // stop if we're out of room - if n == buf.len() { - break; - } - } - } - .await; - - if n > 0 { - for char in buf[..n].iter_mut() { - // upper case - if 0x61 <= *char && *char <= 0x7a { - *char &= !0x20; - } - } - unwrap!(writer.write_all(&buf[..n]).await); - unwrap!(writer.write_all(b"\r\n").await); - unwrap!(writer.flush().await); - } - } -} diff --git a/examples/stm32l4/Cargo.toml b/examples/stm32l4/Cargo.toml index 2da549bb2..b3478f74e 100644 --- a/examples/stm32l4/Cargo.toml +++ b/examples/stm32l4/Cargo.toml @@ -10,7 +10,7 @@ resolver = "2" [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits" } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits", "usb-otg"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits"] } defmt = "0.3" defmt-rtt = "0.3" diff --git a/examples/stm32l4/src/bin/usb_uart.rs b/examples/stm32l4/src/bin/usb_uart.rs deleted file mode 100644 index 878309550..000000000 --- a/examples/stm32l4/src/bin/usb_uart.rs +++ /dev/null @@ -1,115 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use defmt_rtt as _; // global logger -use panic_probe as _; - -use defmt::{info, unwrap}; -use defmt_rtt as _; // global logger -use embassy::interrupt::InterruptExt; -use futures::pin_mut; -use panic_probe as _; // print out panic messages - -use embassy::executor::Spawner; -use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; -use embassy_stm32::pac::pwr::vals::Usv; -use embassy_stm32::pac::{PWR, RCC}; -use embassy_stm32::rcc::{ClockSrc, PLLClkDiv, PLLMul, PLLSource, PLLSrcDiv}; -use embassy_stm32::usb_otg::{State, Usb, UsbBus, UsbOtg, UsbSerial}; -use embassy_stm32::{interrupt, Config, Peripherals}; -use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; - -static mut EP_MEMORY: [u32; 2048] = [0; 2048]; - -// USB requires at least 48 MHz clock -fn config() -> Config { - let mut config = Config::default(); - // set up a 80Mhz clock - config.rcc.mux = ClockSrc::PLL( - PLLSource::HSI16, - PLLClkDiv::Div2, - PLLSrcDiv::Div2, - PLLMul::Mul20, - None, - ); - // enable HSI48 clock for USB - config.rcc.hsi48 = true; - config -} - -#[embassy::main(config = "config()")] -async fn main(_spawner: Spawner, p: Peripherals) { - // Enable PWR peripheral - unsafe { RCC.apb1enr1().modify(|w| w.set_pwren(true)) }; - unsafe { PWR.cr2().modify(|w| w.set_usv(Usv::VALID)) } - - let mut rx_buffer = [0u8; 64]; - // we send back input + cr + lf - let mut tx_buffer = [0u8; 66]; - - let peri = UsbOtg::new_fs(p.USB_OTG_FS, p.PA12, p.PA11); - let usb_bus = UsbBus::new(peri, unsafe { &mut EP_MEMORY }); - - let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); - - let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) - .manufacturer("Fake company") - .product("Serial port") - .serial_number("TEST") - .device_class(0x02) - .build(); - - let irq = interrupt::take!(OTG_FS); - irq.set_priority(interrupt::Priority::P3); - - let mut state = State::new(); - let usb = unsafe { Usb::new(&mut state, device, serial, irq) }; - pin_mut!(usb); - - let (mut reader, mut writer) = usb.as_ref().take_serial_0(); - - info!("usb initialized!"); - - unwrap!( - writer - .write_all(b"\r\nInput returned upper cased on CR+LF\r\n") - .await - ); - - let mut buf = [0u8; 64]; - loop { - let mut n = 0; - - async { - loop { - let char = unwrap!(reader.read_byte().await); - - if char == b'\r' || char == b'\n' { - break; - } - - buf[n] = char; - n += 1; - - // stop if we're out of room - if n == buf.len() { - break; - } - } - } - .await; - - if n > 0 { - for char in buf[..n].iter_mut() { - // upper case - if 0x61 <= *char && *char <= 0x7a { - *char &= !0x20; - } - } - unwrap!(writer.write_all(&buf[..n]).await); - unwrap!(writer.write_all(b"\r\n").await); - unwrap!(writer.flush().await); - } - } -} -- cgit From 4f5088d79d1a0f52ec2c12c87de0a280a0836e7d Mon Sep 17 00:00:00 2001 From: Matous Hybl Date: Mon, 2 May 2022 15:14:10 +0200 Subject: Add support for F3 flash --- examples/boot/stm32f3/.cargo/config.toml | 6 +++++ examples/boot/stm32f3/Cargo.toml | 26 +++++++++++++++++++ examples/boot/stm32f3/README.md | 29 +++++++++++++++++++++ examples/boot/stm32f3/build.rs | 37 +++++++++++++++++++++++++++ examples/boot/stm32f3/memory.x | 15 +++++++++++ examples/boot/stm32f3/src/bin/a.rs | 44 ++++++++++++++++++++++++++++++++ examples/boot/stm32f3/src/bin/b.rs | 25 ++++++++++++++++++ examples/stm32f3/Cargo.toml | 1 + examples/stm32f3/src/bin/blinky.rs | 2 +- examples/stm32f3/src/bin/flash.rs | 43 +++++++++++++++++++++++++++++++ 10 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 examples/boot/stm32f3/.cargo/config.toml create mode 100644 examples/boot/stm32f3/Cargo.toml create mode 100644 examples/boot/stm32f3/README.md create mode 100644 examples/boot/stm32f3/build.rs create mode 100644 examples/boot/stm32f3/memory.x create mode 100644 examples/boot/stm32f3/src/bin/a.rs create mode 100644 examples/boot/stm32f3/src/bin/b.rs create mode 100644 examples/stm32f3/src/bin/flash.rs (limited to 'examples') diff --git a/examples/boot/stm32f3/.cargo/config.toml b/examples/boot/stm32f3/.cargo/config.toml new file mode 100644 index 000000000..eb8a8b335 --- /dev/null +++ b/examples/boot/stm32f3/.cargo/config.toml @@ -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" diff --git a/examples/boot/stm32f3/Cargo.toml b/examples/boot/stm32f3/Cargo.toml new file mode 100644 index 000000000..d4ca600f8 --- /dev/null +++ b/examples/boot/stm32f3/Cargo.toml @@ -0,0 +1,26 @@ +[package] +authors = ["Ulf Lilleengen "] +edition = "2021" +name = "embassy-boot-stm32f3-examples" +version = "0.1.0" + +[dependencies] +embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } +embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32f303re", "time-driver-any", "exti"] } +embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" } +embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } + +defmt = { version = "0.3", optional = true } +defmt-rtt = { version = "0.3", optional = true } +panic-reset = { version = "0.1.1" } +embedded-hal = { version = "0.2.6" } + +cortex-m = "0.7.3" +cortex-m-rt = "0.7.0" + +[features] +defmt = [ + "dep:defmt", + "embassy-stm32/defmt", + "embassy-boot-stm32/defmt", +] diff --git a/examples/boot/stm32f3/README.md b/examples/boot/stm32f3/README.md new file mode 100644 index 000000000..e92ffb692 --- /dev/null +++ b/examples/boot/stm32f3/README.md @@ -0,0 +1,29 @@ +# Examples using bootloader + +Example for STM32F3 demonstrating the bootloader. The example consists of application binaries, 'a' +which allows you to press a button to start the DFU process, and 'b' which is the updated +application. + + +## Prerequisites + +* `cargo-binutils` +* `cargo-flash` +* `embassy-boot-stm32` + +## Usage + +``` +# Flash bootloader +cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32f303re --chip STM32F303RETx +# Build 'b' +cargo build --release --bin b +# Generate binary for 'b' +cargo objcopy --release --bin b -- -O binary b.bin +``` + +# Flash `a` (which includes b.bin) + +``` +cargo flash --release --bin a --chip STM32F303RETx +``` diff --git a/examples/boot/stm32f3/build.rs b/examples/boot/stm32f3/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/stm32f3/build.rs @@ -0,0 +1,37 @@ +//! 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"); + + println!("cargo:rustc-link-arg-bins=--nmagic"); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + if env::var("CARGO_FEATURE_DEFMT").is_ok() { + println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); + } +} diff --git a/examples/boot/stm32f3/memory.x b/examples/boot/stm32f3/memory.x new file mode 100644 index 000000000..14b2a2c9f --- /dev/null +++ b/examples/boot/stm32f3/memory.x @@ -0,0 +1,15 @@ +MEMORY +{ + /* NOTE 1 K = 1 KiBi = 1024 bytes */ + BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 24K + BOOTLOADER_STATE : ORIGIN = 0x08006000, LENGTH = 4K + FLASH : ORIGIN = 0x08008000, LENGTH = 32K + DFU : ORIGIN = 0x08010000, LENGTH = 36K + RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 32K +} + +__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER); +__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER); + +__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOTLOADER); +__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOTLOADER); diff --git a/examples/boot/stm32f3/src/bin/a.rs b/examples/boot/stm32f3/src/bin/a.rs new file mode 100644 index 000000000..db9262f43 --- /dev/null +++ b/examples/boot/stm32f3/src/bin/a.rs @@ -0,0 +1,44 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy_boot_stm32::FirmwareUpdater; +use embassy_stm32::exti::ExtiInput; +use embassy_stm32::flash::Flash; +use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; +use embassy_stm32::Peripherals; +use embassy_traits::adapter::BlockingAsync; +use panic_reset as _; + +#[cfg(feature = "defmt-rtt")] +use defmt_rtt::*; + +static APP_B: &[u8] = include_bytes!("../../b.bin"); + +#[embassy::main] +async fn main(_s: embassy::executor::Spawner, p: Peripherals) { + let flash = Flash::unlock(p.FLASH); + let mut flash = BlockingAsync::new(flash); + + let button = Input::new(p.PC13, Pull::Up); + let mut button = ExtiInput::new(button, p.EXTI13); + + let mut led = Output::new(p.PA5, Level::Low, Speed::Low); + led.set_high(); + + let mut updater = FirmwareUpdater::default(); + button.wait_for_falling_edge().await; + let mut offset = 0; + for chunk in APP_B.chunks(2048) { + let mut buf: [u8; 2048] = [0; 2048]; + buf[..chunk.len()].copy_from_slice(chunk); + updater + .write_firmware(offset, &buf, &mut flash, 2048) + .await + .unwrap(); + offset += chunk.len(); + } + updater.update(&mut flash).await.unwrap(); + led.set_low(); + cortex_m::peripheral::SCB::sys_reset(); +} diff --git a/examples/boot/stm32f3/src/bin/b.rs b/examples/boot/stm32f3/src/bin/b.rs new file mode 100644 index 000000000..814275988 --- /dev/null +++ b/examples/boot/stm32f3/src/bin/b.rs @@ -0,0 +1,25 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; +use panic_reset as _; + +#[cfg(feature = "defmt-rtt")] +use defmt_rtt::*; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let mut led = Output::new(p.PA5, Level::High, Speed::Low); + + loop { + led.set_high(); + Timer::after(Duration::from_millis(500)).await; + + led.set_low(); + Timer::after(Duration::from_millis(500)).await; + } +} diff --git a/examples/stm32f3/Cargo.toml b/examples/stm32f3/Cargo.toml index 37f99fea2..e7a44c11e 100644 --- a/examples/stm32f3/Cargo.toml +++ b/examples/stm32f3/Cargo.toml @@ -19,3 +19,4 @@ 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" +embedded-storage = "0.3.0" diff --git a/examples/stm32f3/src/bin/blinky.rs b/examples/stm32f3/src/bin/blinky.rs index b3643a26f..4b181a784 100644 --- a/examples/stm32f3/src/bin/blinky.rs +++ b/examples/stm32f3/src/bin/blinky.rs @@ -15,7 +15,7 @@ use panic_probe as _; async fn main(_spawner: Spawner, p: Peripherals) { info!("Hello World!"); - let mut led = Output::new(p.PE12, Level::High, Speed::Low); + let mut led = Output::new(p.PA5, Level::High, Speed::Low); loop { info!("high"); diff --git a/examples/stm32f3/src/bin/flash.rs b/examples/stm32f3/src/bin/flash.rs new file mode 100644 index 000000000..3890f0514 --- /dev/null +++ b/examples/stm32f3/src/bin/flash.rs @@ -0,0 +1,43 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::{info, unwrap}; +use embassy::executor::Spawner; +use embassy_stm32::flash::Flash; +use embassy_stm32::Peripherals; +use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; + +use defmt_rtt as _; // global logger +use panic_probe as _; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello Flash!"); + + const ADDR: u32 = 0x26000; + + let mut f = Flash::unlock(p.FLASH); + + info!("Reading..."); + let mut buf = [0u8; 8]; + unwrap!(f.read(ADDR, &mut buf)); + info!("Read: {=[u8]:x}", buf); + + info!("Erasing..."); + unwrap!(f.erase(ADDR, ADDR + 2048)); + + info!("Reading..."); + let mut buf = [0u8; 8]; + unwrap!(f.read(ADDR, &mut buf)); + info!("Read after erase: {=[u8]:x}", buf); + + info!("Writing..."); + unwrap!(f.write(ADDR, &[1, 2, 3, 4, 5, 6, 7, 8])); + + info!("Reading..."); + let mut buf = [0u8; 8]; + unwrap!(f.read(ADDR, &mut buf)); + info!("Read: {=[u8]:x}", buf); + assert_eq!(&buf[..], &[1, 2, 3, 4, 5, 6, 7, 8]); +} -- cgit From 6d56f772e11eaae7495e08ba55d9c98d11e0ac09 Mon Sep 17 00:00:00 2001 From: Matous Hybl Date: Tue, 3 May 2022 16:16:37 +0200 Subject: Add F7 flash and bootloader support --- examples/boot/stm32f7/.cargo/config.toml | 6 ++++ examples/boot/stm32f7/Cargo.toml | 26 ++++++++++++++ examples/boot/stm32f7/README.md | 29 ++++++++++++++++ examples/boot/stm32f7/build.rs | 37 ++++++++++++++++++++ examples/boot/stm32f7/flash-boot.sh | 8 +++++ examples/boot/stm32f7/memory-bl.x | 18 ++++++++++ examples/boot/stm32f7/memory.x | 15 ++++++++ examples/boot/stm32f7/src/bin/a.rs | 44 ++++++++++++++++++++++++ examples/boot/stm32f7/src/bin/b.rs | 27 +++++++++++++++ examples/stm32f7/Cargo.toml | 1 + examples/stm32f7/src/bin/flash.rs | 59 ++++++++++++++++++++++++++++++++ 11 files changed, 270 insertions(+) create mode 100644 examples/boot/stm32f7/.cargo/config.toml create mode 100644 examples/boot/stm32f7/Cargo.toml create mode 100644 examples/boot/stm32f7/README.md create mode 100644 examples/boot/stm32f7/build.rs create mode 100755 examples/boot/stm32f7/flash-boot.sh create mode 100644 examples/boot/stm32f7/memory-bl.x create mode 100644 examples/boot/stm32f7/memory.x create mode 100644 examples/boot/stm32f7/src/bin/a.rs create mode 100644 examples/boot/stm32f7/src/bin/b.rs create mode 100644 examples/stm32f7/src/bin/flash.rs (limited to 'examples') diff --git a/examples/boot/stm32f7/.cargo/config.toml b/examples/boot/stm32f7/.cargo/config.toml new file mode 100644 index 000000000..df5114520 --- /dev/null +++ b/examples/boot/stm32f7/.cargo/config.toml @@ -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 STM32F767ZITx -v" + +[build] +target = "thumbv7em-none-eabihf" diff --git a/examples/boot/stm32f7/Cargo.toml b/examples/boot/stm32f7/Cargo.toml new file mode 100644 index 000000000..857b287d5 --- /dev/null +++ b/examples/boot/stm32f7/Cargo.toml @@ -0,0 +1,26 @@ +[package] +authors = ["Ulf Lilleengen "] +edition = "2021" +name = "embassy-boot-stm32f7-examples" +version = "0.1.0" + +[dependencies] +embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } +embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32f767zi", "time-driver-any", "exti"] } +embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" } +embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } + +defmt = { version = "0.3", optional = true } +defmt-rtt = { version = "0.3", optional = true } +panic-reset = { version = "0.1.1" } +embedded-hal = { version = "0.2.6" } + +cortex-m = "0.7.3" +cortex-m-rt = "0.7.0" + +[features] +defmt = [ + "dep:defmt", + "embassy-stm32/defmt", + "embassy-boot-stm32/defmt", +] diff --git a/examples/boot/stm32f7/README.md b/examples/boot/stm32f7/README.md new file mode 100644 index 000000000..bf9142a1c --- /dev/null +++ b/examples/boot/stm32f7/README.md @@ -0,0 +1,29 @@ +# Examples using bootloader + +Example for STM32F7 demonstrating the bootloader. The example consists of application binaries, 'a' +which allows you to press a button to start the DFU process, and 'b' which is the updated +application. + + +## Prerequisites + +* `cargo-binutils` +* `cargo-flash` +* `embassy-boot-stm32` + +## Usage + +``` +# Flash bootloader +./flash-boot.sh +# Build 'b' +cargo build --release --bin b +# Generate binary for 'b' +cargo objcopy --release --bin b -- -O binary b.bin +``` + +# Flash `a` (which includes b.bin) + +``` +cargo flash --release --bin a --chip STM32F767ZITx +``` diff --git a/examples/boot/stm32f7/build.rs b/examples/boot/stm32f7/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/stm32f7/build.rs @@ -0,0 +1,37 @@ +//! 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"); + + println!("cargo:rustc-link-arg-bins=--nmagic"); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + if env::var("CARGO_FEATURE_DEFMT").is_ok() { + println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); + } +} diff --git a/examples/boot/stm32f7/flash-boot.sh b/examples/boot/stm32f7/flash-boot.sh new file mode 100755 index 000000000..86074ffa3 --- /dev/null +++ b/examples/boot/stm32f7/flash-boot.sh @@ -0,0 +1,8 @@ +#!/bin/bash +mv ../../../embassy-boot/stm32/memory.x ../../../embassy-boot/stm32/memory-old.x +cp memory-bl.x ../../../embassy-boot/stm32/memory.x + +cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32f767zi --chip STM32F767ZITx --target thumbv7em-none-eabihf + +rm ../../../embassy-boot/stm32/memory.x +mv ../../../embassy-boot/stm32/memory-old.x ../../../embassy-boot/stm32/memory.x diff --git a/examples/boot/stm32f7/memory-bl.x b/examples/boot/stm32f7/memory-bl.x new file mode 100644 index 000000000..47f3f4d9b --- /dev/null +++ b/examples/boot/stm32f7/memory-bl.x @@ -0,0 +1,18 @@ +MEMORY +{ + /* NOTE 1 K = 1 KiBi = 1024 bytes */ + FLASH : ORIGIN = 0x08000000, LENGTH = 256K + BOOTLOADER_STATE : ORIGIN = 0x08040000, LENGTH = 256K + ACTIVE : ORIGIN = 0x08080000, LENGTH = 256K + DFU : ORIGIN = 0x080c0000, LENGTH = 512K + RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 368K + 16K +} + +__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(FLASH); +__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(FLASH); + +__bootloader_active_start = ORIGIN(ACTIVE) - ORIGIN(FLASH); +__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE) - ORIGIN(FLASH); + +__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(FLASH); +__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(FLASH); diff --git a/examples/boot/stm32f7/memory.x b/examples/boot/stm32f7/memory.x new file mode 100644 index 000000000..1c5537d17 --- /dev/null +++ b/examples/boot/stm32f7/memory.x @@ -0,0 +1,15 @@ +MEMORY +{ + /* NOTE 1 K = 1 KiBi = 1024 bytes */ + BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 256K + BOOTLOADER_STATE : ORIGIN = 0x08040000, LENGTH = 256K + FLASH : ORIGIN = 0x08080000, LENGTH = 256K + DFU : ORIGIN = 0x080c0000, LENGTH = 512K + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 368K + 16K +} + +__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER); +__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER); + +__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOTLOADER); +__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOTLOADER); diff --git a/examples/boot/stm32f7/src/bin/a.rs b/examples/boot/stm32f7/src/bin/a.rs new file mode 100644 index 000000000..ca154f0af --- /dev/null +++ b/examples/boot/stm32f7/src/bin/a.rs @@ -0,0 +1,44 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy_boot_stm32::FirmwareUpdater; +use embassy_stm32::exti::ExtiInput; +use embassy_stm32::flash::Flash; +use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; +use embassy_stm32::Peripherals; +use embassy_traits::adapter::BlockingAsync; +use panic_reset as _; + +#[cfg(feature = "defmt-rtt")] +use defmt_rtt::*; + +static APP_B: &[u8] = include_bytes!("../../b.bin"); + +#[embassy::main] +async fn main(_s: embassy::executor::Spawner, p: Peripherals) { + let flash = Flash::unlock(p.FLASH); + let mut flash = BlockingAsync::new(flash); + + let button = Input::new(p.PC13, Pull::Down); + let mut button = ExtiInput::new(button, p.EXTI13); + + let mut led = Output::new(p.PB7, Level::Low, Speed::Low); + led.set_high(); + + let mut updater = FirmwareUpdater::default(); + button.wait_for_rising_edge().await; + let mut offset = 0; + let mut buf: [u8; 256 * 1024] = [0; 256 * 1024]; + for chunk in APP_B.chunks(256 * 1024) { + buf[..chunk.len()].copy_from_slice(chunk); + updater + .write_firmware(offset, &buf, &mut flash, 2048) + .await + .unwrap(); + offset += chunk.len(); + } + updater.update(&mut flash).await.unwrap(); + led.set_low(); + cortex_m::peripheral::SCB::sys_reset(); +} diff --git a/examples/boot/stm32f7/src/bin/b.rs b/examples/boot/stm32f7/src/bin/b.rs new file mode 100644 index 000000000..ed37137f5 --- /dev/null +++ b/examples/boot/stm32f7/src/bin/b.rs @@ -0,0 +1,27 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; +use panic_reset as _; + +#[cfg(feature = "defmt-rtt")] +use defmt_rtt::*; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + Timer::after(Duration::from_millis(300)).await; + let mut led = Output::new(p.PB7, Level::High, Speed::Low); + led.set_high(); + + loop { + led.set_high(); + Timer::after(Duration::from_millis(500)).await; + + led.set_low(); + Timer::after(Duration::from_millis(500)).await; + } +} diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml index 09a06aa7f..ce0a6d48f 100644 --- a/examples/stm32f7/Cargo.toml +++ b/examples/stm32f7/Cargo.toml @@ -22,6 +22,7 @@ heapless = { version = "0.7.5", default-features = false } nb = "1.0.0" rand_core = "0.6.3" critical-section = "0.2.3" +embedded-storage = "0.3.0" [dependencies.smoltcp] version = "0.8.0" diff --git a/examples/stm32f7/src/bin/flash.rs b/examples/stm32f7/src/bin/flash.rs new file mode 100644 index 000000000..9eb8e4b94 --- /dev/null +++ b/examples/stm32f7/src/bin/flash.rs @@ -0,0 +1,59 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::{info, unwrap}; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::flash::Flash; +use embassy_stm32::Peripherals; +use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; + +use defmt_rtt as _; // global logger +use panic_probe as _; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello Flash!"); + + const ADDR: u32 = 0x8_0000; + + // wait a bit before accessing the flash + Timer::after(Duration::from_millis(300)).await; + + let mut f = Flash::unlock(p.FLASH); + + info!("Reading..."); + let mut buf = [0u8; 32]; + unwrap!(f.read(ADDR, &mut buf)); + info!("Read: {=[u8]:x}", buf); + + info!("Erasing..."); + unwrap!(f.erase(ADDR, ADDR + 256 * 1024)); + + info!("Reading..."); + let mut buf = [0u8; 32]; + unwrap!(f.read(ADDR, &mut buf)); + info!("Read after erase: {=[u8]:x}", buf); + + info!("Writing..."); + unwrap!(f.write( + ADDR, + &[ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32 + ] + )); + + info!("Reading..."); + let mut buf = [0u8; 32]; + unwrap!(f.read(ADDR, &mut buf)); + info!("Read: {=[u8]:x}", buf); + assert_eq!( + &buf[..], + &[ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32 + ] + ); +} -- cgit From 118532278c0f08cfc06c3e113038a47161c254bd Mon Sep 17 00:00:00 2001 From: Matous Hybl Date: Fri, 6 May 2022 09:21:29 +0200 Subject: Add H7 flash and bootloader support --- examples/boot/stm32h7/.cargo/config.toml | 6 ++++ examples/boot/stm32h7/Cargo.toml | 26 ++++++++++++++ examples/boot/stm32h7/README.md | 29 ++++++++++++++++ examples/boot/stm32h7/build.rs | 37 ++++++++++++++++++++ examples/boot/stm32h7/flash-boot.sh | 8 +++++ examples/boot/stm32h7/memory-bl.x | 18 ++++++++++ examples/boot/stm32h7/memory.x | 15 +++++++++ examples/boot/stm32h7/src/bin/a.rs | 44 ++++++++++++++++++++++++ examples/boot/stm32h7/src/bin/b.rs | 27 +++++++++++++++ examples/stm32h7/Cargo.toml | 1 + examples/stm32h7/src/bin/flash.rs | 58 ++++++++++++++++++++++++++++++++ 11 files changed, 269 insertions(+) create mode 100644 examples/boot/stm32h7/.cargo/config.toml create mode 100644 examples/boot/stm32h7/Cargo.toml create mode 100644 examples/boot/stm32h7/README.md create mode 100644 examples/boot/stm32h7/build.rs create mode 100755 examples/boot/stm32h7/flash-boot.sh create mode 100644 examples/boot/stm32h7/memory-bl.x create mode 100644 examples/boot/stm32h7/memory.x create mode 100644 examples/boot/stm32h7/src/bin/a.rs create mode 100644 examples/boot/stm32h7/src/bin/b.rs create mode 100644 examples/stm32h7/src/bin/flash.rs (limited to 'examples') diff --git a/examples/boot/stm32h7/.cargo/config.toml b/examples/boot/stm32h7/.cargo/config.toml new file mode 100644 index 000000000..8475e7f66 --- /dev/null +++ b/examples/boot/stm32h7/.cargo/config.toml @@ -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 STM32H743ZITx" + +[build] +target = "thumbv7em-none-eabihf" diff --git a/examples/boot/stm32h7/Cargo.toml b/examples/boot/stm32h7/Cargo.toml new file mode 100644 index 000000000..1fd03906f --- /dev/null +++ b/examples/boot/stm32h7/Cargo.toml @@ -0,0 +1,26 @@ +[package] +authors = ["Ulf Lilleengen "] +edition = "2021" +name = "embassy-boot-stm32f7-examples" +version = "0.1.0" + +[dependencies] +embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } +embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32h743zi", "time-driver-any", "exti"] } +embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" } +embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } + +defmt = { version = "0.3", optional = true } +defmt-rtt = { version = "0.3", optional = true } +panic-reset = { version = "0.1.1" } +embedded-hal = { version = "0.2.6" } + +cortex-m = "0.7.3" +cortex-m-rt = "0.7.0" + +[features] +defmt = [ + "dep:defmt", + "embassy-stm32/defmt", + "embassy-boot-stm32/defmt", +] diff --git a/examples/boot/stm32h7/README.md b/examples/boot/stm32h7/README.md new file mode 100644 index 000000000..1fdc305e6 --- /dev/null +++ b/examples/boot/stm32h7/README.md @@ -0,0 +1,29 @@ +# Examples using bootloader + +Example for STM32H7 demonstrating the bootloader. The example consists of application binaries, 'a' +which allows you to press a button to start the DFU process, and 'b' which is the updated +application. + + +## Prerequisites + +* `cargo-binutils` +* `cargo-flash` +* `embassy-boot-stm32` + +## Usage + +``` +# Flash bootloader +./flash-boot.sh +# Build 'b' +cargo build --release --bin b +# Generate binary for 'b' +cargo objcopy --release --bin b -- -O binary b.bin +``` + +# Flash `a` (which includes b.bin) + +``` +cargo flash --release --bin a --chip STM32H743ZITx +``` diff --git a/examples/boot/stm32h7/build.rs b/examples/boot/stm32h7/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/stm32h7/build.rs @@ -0,0 +1,37 @@ +//! 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"); + + println!("cargo:rustc-link-arg-bins=--nmagic"); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + if env::var("CARGO_FEATURE_DEFMT").is_ok() { + println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); + } +} diff --git a/examples/boot/stm32h7/flash-boot.sh b/examples/boot/stm32h7/flash-boot.sh new file mode 100755 index 000000000..a910b7312 --- /dev/null +++ b/examples/boot/stm32h7/flash-boot.sh @@ -0,0 +1,8 @@ +#!/bin/bash +mv ../../../embassy-boot/stm32/memory.x ../../../embassy-boot/stm32/memory-old.x +cp memory-bl.x ../../../embassy-boot/stm32/memory.x + +cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32f767zi --chip STM32H743ZITx --target thumbv7em-none-eabihf + +rm ../../../embassy-boot/stm32/memory.x +mv ../../../embassy-boot/stm32/memory-old.x ../../../embassy-boot/stm32/memory.x diff --git a/examples/boot/stm32h7/memory-bl.x b/examples/boot/stm32h7/memory-bl.x new file mode 100644 index 000000000..c6f447d8b --- /dev/null +++ b/examples/boot/stm32h7/memory-bl.x @@ -0,0 +1,18 @@ +MEMORY +{ + /* NOTE 1 K = 1 KiBi = 1024 bytes */ + FLASH : ORIGIN = 0x08000000, LENGTH = 128K + BOOTLOADER_STATE : ORIGIN = 0x08020000, LENGTH = 128K + ACTIVE : ORIGIN = 0x08040000, LENGTH = 128K + DFU : ORIGIN = 0x08100000, LENGTH = 512K + RAM (rwx) : ORIGIN = 0x24000000, LENGTH = 368K +} + +__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(FLASH); +__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(FLASH); + +__bootloader_active_start = ORIGIN(ACTIVE) - ORIGIN(FLASH); +__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE) - ORIGIN(FLASH); + +__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(FLASH); +__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(FLASH); diff --git a/examples/boot/stm32h7/memory.x b/examples/boot/stm32h7/memory.x new file mode 100644 index 000000000..497a09e41 --- /dev/null +++ b/examples/boot/stm32h7/memory.x @@ -0,0 +1,15 @@ +MEMORY +{ + /* NOTE 1 K = 1 KiBi = 1024 bytes */ + BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 128K + BOOTLOADER_STATE : ORIGIN = 0x08020000, LENGTH = 128K + FLASH : ORIGIN = 0x08040000, LENGTH = 256K + DFU : ORIGIN = 0x08100000, LENGTH = 512K + RAM (rwx) : ORIGIN = 0x24000000, LENGTH = 368K +} + +__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER); +__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER); + +__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOTLOADER); +__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOTLOADER); diff --git a/examples/boot/stm32h7/src/bin/a.rs b/examples/boot/stm32h7/src/bin/a.rs new file mode 100644 index 000000000..1f23a8bc2 --- /dev/null +++ b/examples/boot/stm32h7/src/bin/a.rs @@ -0,0 +1,44 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy_boot_stm32::FirmwareUpdater; +use embassy_stm32::exti::ExtiInput; +use embassy_stm32::flash::Flash; +use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; +use embassy_stm32::Peripherals; +use embassy_traits::adapter::BlockingAsync; +use panic_reset as _; + +#[cfg(feature = "defmt-rtt")] +use defmt_rtt::*; + +static APP_B: &[u8] = include_bytes!("../../b.bin"); + +#[embassy::main] +async fn main(_s: embassy::executor::Spawner, p: Peripherals) { + let flash = Flash::unlock(p.FLASH); + let mut flash = BlockingAsync::new(flash); + + let button = Input::new(p.PC13, Pull::Down); + let mut button = ExtiInput::new(button, p.EXTI13); + + let mut led = Output::new(p.PB14, Level::Low, Speed::Low); + led.set_high(); + + let mut updater = FirmwareUpdater::default(); + button.wait_for_rising_edge().await; + let mut offset = 0; + let mut buf: [u8; 128 * 1024] = [0; 128 * 1024]; + for chunk in APP_B.chunks(128 * 1024) { + buf[..chunk.len()].copy_from_slice(chunk); + updater + .write_firmware(offset, &buf, &mut flash, 2048) + .await + .unwrap(); + offset += chunk.len(); + } + updater.update(&mut flash).await.unwrap(); + led.set_low(); + cortex_m::peripheral::SCB::sys_reset(); +} diff --git a/examples/boot/stm32h7/src/bin/b.rs b/examples/boot/stm32h7/src/bin/b.rs new file mode 100644 index 000000000..233b93e1a --- /dev/null +++ b/examples/boot/stm32h7/src/bin/b.rs @@ -0,0 +1,27 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; +use panic_reset as _; + +#[cfg(feature = "defmt-rtt")] +use defmt_rtt::*; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + Timer::after(Duration::from_millis(300)).await; + let mut led = Output::new(p.PB14, Level::High, Speed::Low); + led.set_high(); + + loop { + led.set_high(); + Timer::after(Duration::from_millis(500)).await; + + led.set_low(); + Timer::after(Duration::from_millis(500)).await; + } +} diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index 419bbec31..8906a1d0b 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml @@ -27,6 +27,7 @@ rand_core = "0.6.3" critical-section = "0.2.5" micromath = "2.0.0" stm32-fmc = "0.2.4" +embedded-storage = "0.3.0" [dependencies.smoltcp] version = "0.8.0" diff --git a/examples/stm32h7/src/bin/flash.rs b/examples/stm32h7/src/bin/flash.rs new file mode 100644 index 000000000..b008c0884 --- /dev/null +++ b/examples/stm32h7/src/bin/flash.rs @@ -0,0 +1,58 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::{info, unwrap}; +use defmt_rtt as _; // global logger +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::flash::Flash; +use embassy_stm32::Peripherals; +use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; +use panic_probe as _; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello Flash!"); + + const ADDR: u32 = 0x08_0000; + + // wait a bit before accessing the flash + Timer::after(Duration::from_millis(300)).await; + + let mut f = Flash::unlock(p.FLASH); + + info!("Reading..."); + let mut buf = [0u8; 32]; + unwrap!(f.read(ADDR, &mut buf)); + info!("Read: {=[u8]:x}", buf); + + info!("Erasing..."); + unwrap!(f.erase(ADDR, ADDR + 128 * 1024)); + + info!("Reading..."); + let mut buf = [0u8; 32]; + unwrap!(f.read(ADDR, &mut buf)); + info!("Read after erase: {=[u8]:x}", buf); + + info!("Writing..."); + unwrap!(f.write( + ADDR, + &[ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32 + ] + )); + + info!("Reading..."); + let mut buf = [0u8; 32]; + unwrap!(f.read(ADDR, &mut buf)); + info!("Read: {=[u8]:x}", buf); + assert_eq!( + &buf[..], + &[ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32 + ] + ); +} -- cgit From 931a137f8c5a760c2e06c437c98d14eff3e3a587 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 4 May 2022 20:48:37 +0200 Subject: Replace embassy::io with embedded_io. --- examples/nrf/Cargo.toml | 3 ++- examples/nrf/src/bin/buffered_uart.rs | 2 +- examples/nrf/src/bin/usb_ethernet.rs | 8 +++++--- examples/std/Cargo.toml | 3 ++- examples/std/src/bin/net.rs | 5 +++-- examples/std/src/bin/serial.rs | 14 +++++++------- examples/stm32f7/Cargo.toml | 1 + examples/stm32f7/src/bin/eth.rs | 7 +++---- examples/stm32h7/Cargo.toml | 3 +-- examples/stm32h7/src/bin/eth.rs | 7 +++---- examples/stm32l0/.cargo/config.toml | 2 +- examples/stm32l0/Cargo.toml | 4 ++-- examples/stm32l0/src/bin/usart_irq.rs | 11 +++++++---- 13 files changed, 38 insertions(+), 32 deletions(-) (limited to 'examples') diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index 4258544f0..ffac0a769 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml @@ -6,7 +6,7 @@ version = "0.1.0" [features] default = ["nightly"] -nightly = ["embassy-nrf/nightly", "embassy-nrf/unstable-traits", "embassy-usb", "embassy-usb-serial", "embassy-usb-hid", "embassy-usb-ncm"] +nightly = ["embassy-nrf/nightly", "embassy-nrf/unstable-traits", "embassy-usb", "embassy-usb-serial", "embassy-usb-hid", "embassy-usb-ncm", "embedded-io/async", "embassy-net/nightly"] [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } @@ -16,6 +16,7 @@ embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defm embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", features = ["defmt"], optional = true } embassy-usb-hid = { version = "0.1.0", path = "../../embassy-usb-hid", features = ["defmt"], optional = true } embassy-usb-ncm = { version = "0.1.0", path = "../../embassy-usb-ncm", features = ["defmt"], optional = true } +embedded-io = "0.2.0" defmt = "0.3" defmt-rtt = "0.3" diff --git a/examples/nrf/src/bin/buffered_uart.rs b/examples/nrf/src/bin/buffered_uart.rs index 2cd163a9f..a64c5821b 100644 --- a/examples/nrf/src/bin/buffered_uart.rs +++ b/examples/nrf/src/bin/buffered_uart.rs @@ -4,9 +4,9 @@ use defmt::*; use embassy::executor::Spawner; -use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; use embassy_nrf::buffered_uarte::State; use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals}; +use embedded_io::asynch::{Read, Write}; use futures::pin_mut; use defmt_rtt as _; // global logger diff --git a/examples/nrf/src/bin/usb_ethernet.rs b/examples/nrf/src/bin/usb_ethernet.rs index f14a29c49..843487c03 100644 --- a/examples/nrf/src/bin/usb_ethernet.rs +++ b/examples/nrf/src/bin/usb_ethernet.rs @@ -10,9 +10,9 @@ use defmt::*; use embassy::blocking_mutex::raw::ThreadModeRawMutex; use embassy::channel::Channel; use embassy::executor::Spawner; -use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; use embassy::util::Forever; -use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, TcpSocket}; +use embassy_net::tcp::TcpSocket; +use embassy_net::{PacketBox, PacketBoxExt, PacketBuf}; use embassy_nrf::pac; use embassy_nrf::usb::Driver; use embassy_nrf::Peripherals; @@ -20,7 +20,9 @@ use embassy_nrf::{interrupt, peripherals}; use embassy_usb::{Builder, Config, UsbDevice}; use embassy_usb_ncm::{CdcNcmClass, Receiver, Sender, State}; -use defmt_rtt as _; // global logger +use defmt_rtt as _; +use embedded_io::asynch::{Read, Write}; +// global logger use panic_probe as _; type MyDriver = Driver<'static, peripherals::USBD>; diff --git a/examples/std/Cargo.toml b/examples/std/Cargo.toml index 0853c323e..fa2a98a49 100644 --- a/examples/std/Cargo.toml +++ b/examples/std/Cargo.toml @@ -6,7 +6,8 @@ version = "0.1.0" [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["log", "std", "time", "nightly"] } -embassy-net = { version = "0.1.0", path = "../../embassy-net", features=["std", "log", "medium-ethernet", "tcp", "dhcpv4", "pool-16"] } +embassy-net = { version = "0.1.0", path = "../../embassy-net", features=["nightly", "std", "log", "medium-ethernet", "tcp", "dhcpv4", "pool-16"] } +embedded-io = { version = "0.2.0", features = ["async", "std"] } async-io = "1.6.0" env_logger = "0.9.0" diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs index 3b4bc6fea..daedffb0f 100644 --- a/examples/std/src/bin/net.rs +++ b/examples/std/src/bin/net.rs @@ -2,12 +2,13 @@ use clap::Parser; use embassy::executor::{Executor, Spawner}; -use embassy::io::AsyncWriteExt; use embassy::util::Forever; +use embassy_net::tcp::TcpSocket; use embassy_net::{ Config, Configurator, DhcpConfigurator, Ipv4Address, Ipv4Cidr, StackResources, - StaticConfigurator, TcpSocket, + StaticConfigurator, }; +use embedded_io::asynch::Write; use heapless::Vec; use log::*; diff --git a/examples/std/src/bin/serial.rs b/examples/std/src/bin/serial.rs index 129dc2090..b1e5b0142 100644 --- a/examples/std/src/bin/serial.rs +++ b/examples/std/src/bin/serial.rs @@ -5,8 +5,8 @@ mod serial_port; use async_io::Async; use embassy::executor::Executor; -use embassy::io::AsyncBufReadExt; use embassy::util::Forever; +use embedded_io::asynch::Read; use log::*; use nix::sys::termios; @@ -24,12 +24,12 @@ async fn run() { // Essentially, async_io::Async converts from AsRawFd+Read+Write to futures's AsyncRead+AsyncWrite let port = Async::new(port).unwrap(); - // This implements futures's AsyncBufRead based on futures's AsyncRead - let port = futures::io::BufReader::new(port); - - // We can then use FromStdIo to convert from futures's AsyncBufRead+AsyncWrite - // to embassy's AsyncBufRead+AsyncWrite - let mut port = embassy::io::FromStdIo::new(port); + // We can then use FromStdIo to convert from futures's AsyncRead+AsyncWrite + // to embedded_io's async Read+Write. + // + // This is not really needed, you could write the code below using futures::io directly. + // It's useful if you want to have portable code across embedded and std. + let mut port = embedded_io::adapters::FromFutures::new(port); info!("Serial opened!"); diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml index 09a06aa7f..bbeee179d 100644 --- a/examples/stm32f7/Cargo.toml +++ b/examples/stm32f7/Cargo.toml @@ -9,6 +9,7 @@ resolver = "2" embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "net", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"] } embassy-net = { path = "../../embassy-net", features = ["defmt", "tcp", "medium-ethernet", "pool-16"] } +embedded-io = { version = "0.2.0", features = ["async"] } defmt = "0.3" defmt-rtt = "0.3" diff --git a/examples/stm32f7/src/bin/eth.rs b/examples/stm32f7/src/bin/eth.rs index 33e41de9c..dca9338b2 100644 --- a/examples/stm32f7/src/bin/eth.rs +++ b/examples/stm32f7/src/bin/eth.rs @@ -5,12 +5,10 @@ use cortex_m_rt::entry; use defmt::*; use embassy::executor::{Executor, Spawner}; -use embassy::io::AsyncWriteExt; use embassy::time::{Duration, Timer}; use embassy::util::Forever; -use embassy_net::{ - Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket, -}; +use embassy_net::tcp::TcpSocket; +use embassy_net::{Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator}; use embassy_stm32::eth::generic_smi::GenericSMI; use embassy_stm32::eth::{Ethernet, State}; use embassy_stm32::interrupt; @@ -19,6 +17,7 @@ use embassy_stm32::peripherals::RNG; use embassy_stm32::rng::Rng; use embassy_stm32::time::U32Ext; use embassy_stm32::Config; +use embedded_io::asynch::Write; use heapless::Vec; use defmt_rtt as _; // global logger diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index 419bbec31..ff70e6db6 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml @@ -5,12 +5,11 @@ name = "embassy-stm32h7-examples" version = "0.1.0" resolver = "2" -[features] - [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "net", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } embassy-net = { path = "../../embassy-net", features = ["defmt", "tcp", "medium-ethernet", "pool-16"] } +embedded-io = { version = "0.2.0", features = ["async"] } defmt = "0.3" defmt-rtt = "0.3" diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs index 9a2e7a33d..8ece29403 100644 --- a/examples/stm32h7/src/bin/eth.rs +++ b/examples/stm32h7/src/bin/eth.rs @@ -8,12 +8,10 @@ use panic_probe as _; use cortex_m_rt::entry; use defmt::*; use embassy::executor::{Executor, Spawner}; -use embassy::io::AsyncWriteExt; use embassy::time::{Duration, Timer}; use embassy::util::Forever; -use embassy_net::{ - Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket, -}; +use embassy_net::tcp::TcpSocket; +use embassy_net::{Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator}; use embassy_stm32::eth::generic_smi::GenericSMI; use embassy_stm32::eth::{Ethernet, State}; use embassy_stm32::interrupt; @@ -22,6 +20,7 @@ use embassy_stm32::peripherals::RNG; use embassy_stm32::rng::Rng; use embassy_stm32::time::U32Ext; use embassy_stm32::Config; +use embedded_io::asynch::Write; use heapless::Vec; #[embassy::task] diff --git a/examples/stm32l0/.cargo/config.toml b/examples/stm32l0/.cargo/config.toml index 840faa62e..ec0b931f2 100644 --- a/examples/stm32l0/.cargo/config.toml +++ b/examples/stm32l0/.cargo/config.toml @@ -1,6 +1,6 @@ [target.'cfg(all(target_arch = "arm", target_os = "none"))'] # replace your chip as listed in `probe-run --list-chips` -runner = "probe-run --chip STM32L072CZTx" +runner = "probe-run --chip STM32L053R8Tx" [build] target = "thumbv6m-none-eabi" diff --git a/examples/stm32l0/Cargo.toml b/examples/stm32l0/Cargo.toml index 1cdb2f374..d6848c27a 100644 --- a/examples/stm32l0/Cargo.toml +++ b/examples/stm32l0/Cargo.toml @@ -7,12 +7,11 @@ resolver = "2" [features] default = ["nightly"] -nightly = ["embassy-stm32/nightly", "embassy-lora", "lorawan-device", "lorawan"] +nightly = ["embassy-stm32/nightly", "embassy-lora", "lorawan-device", "lorawan", "embedded-io/async"] [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32l072cz", "time-driver-any", "exti", "unstable-traits", "memory-x"] } - embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx127x", "time", "defmt"], optional = true} lorawan-device = { version = "0.7.1", default-features = false, features = ["async"], optional = true } @@ -22,6 +21,7 @@ defmt = "0.3" defmt-rtt = "0.3" embedded-storage = "0.3.0" +embedded-io = "0.2.0" cortex-m = "0.7.3" cortex-m-rt = "0.7.0" diff --git a/examples/stm32l0/src/bin/usart_irq.rs b/examples/stm32l0/src/bin/usart_irq.rs index abb27fa4f..4413a2945 100644 --- a/examples/stm32l0/src/bin/usart_irq.rs +++ b/examples/stm32l0/src/bin/usart_irq.rs @@ -2,13 +2,14 @@ #![no_main] #![feature(type_alias_impl_trait)] -use defmt_rtt as _; // global logger +use defmt_rtt as _; +use embedded_io::asynch::{Read, Write}; +// global logger use panic_probe as _; use defmt::*; use embassy::executor::Spawner; -use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; use embassy_stm32::dma::NoDma; use embassy_stm32::interrupt; use embassy_stm32::usart::{BufferedUart, Config, State, Uart}; @@ -16,19 +17,21 @@ use embassy_stm32::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hi!"); + static mut TX_BUFFER: [u8; 8] = [0; 8]; static mut RX_BUFFER: [u8; 256] = [0; 256]; let mut config = Config::default(); config.baudrate = 9600; - let usart = Uart::new(p.USART1, p.PA10, p.PA9, NoDma, NoDma, config); + let usart = Uart::new(p.USART2, p.PA3, p.PA2, NoDma, NoDma, config); let mut state = State::new(); let mut usart = unsafe { BufferedUart::new( &mut state, usart, - interrupt::take!(USART1), + interrupt::take!(USART2), &mut TX_BUFFER, &mut RX_BUFFER, ) -- cgit