diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f334/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/stm32l4/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/stm32u0/Cargo.toml | 6 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/adc.rs | 30 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/crc.rs | 31 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/dac.rs | 35 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/flash.rs | 43 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/i2c.rs | 21 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/rng.rs | 43 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/rtc.rs | 49 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/spi.rs | 30 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/usart.rs | 25 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/usb_serial.rs | 109 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/wdt.rs | 41 | ||||
| -rw-r--r-- | examples/stm32wb/.cargo/config.toml | 2 |
15 files changed, 465 insertions, 4 deletions
diff --git a/examples/stm32f334/.cargo/config.toml b/examples/stm32f334/.cargo/config.toml index caf947be6..f38c90a31 100644 --- a/examples/stm32f334/.cargo/config.toml +++ b/examples/stm32f334/.cargo/config.toml | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] |
| 2 | # replace STM32F429ZITx with your chip as listed in `probe-rs-cli chip list` | 2 | # replace STM32F429ZITx with your chip as listed in `probe-rs-cli chip list` |
| 3 | runner = "probe-run --chip STM32F334R8" | 3 | runner = "probe-rs run --chip STM32F334R8" |
| 4 | 4 | ||
| 5 | [build] | 5 | [build] |
| 6 | target = "thumbv7em-none-eabihf" | 6 | target = "thumbv7em-none-eabihf" |
diff --git a/examples/stm32l4/.cargo/config.toml b/examples/stm32l4/.cargo/config.toml index db3a7ceff..83fc6d6f8 100644 --- a/examples/stm32l4/.cargo/config.toml +++ b/examples/stm32l4/.cargo/config.toml | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | # replace STM32F429ZITx with your chip as listed in `probe-rs chip list` | 2 | # replace STM32F429ZITx with your chip as listed in `probe-rs chip list` |
| 3 | #runner = "probe-rs run --chip STM32L475VGT6" | 3 | #runner = "probe-rs run --chip STM32L475VGT6" |
| 4 | #runner = "probe-rs run --chip STM32L475VG" | 4 | #runner = "probe-rs run --chip STM32L475VG" |
| 5 | runner = "probe-run --chip STM32L4S5QI" | 5 | runner = "probe-rs run --chip STM32L4S5QI" |
| 6 | 6 | ||
| 7 | [build] | 7 | [build] |
| 8 | target = "thumbv7em-none-eabi" | 8 | target = "thumbv7em-none-eabi" |
diff --git a/examples/stm32u0/Cargo.toml b/examples/stm32u0/Cargo.toml index 495be3e75..5868372dd 100644 --- a/examples/stm32u0/Cargo.toml +++ b/examples/stm32u0/Cargo.toml | |||
| @@ -6,10 +6,11 @@ license = "MIT OR Apache-2.0" | |||
| 6 | 6 | ||
| 7 | [dependencies] | 7 | [dependencies] |
| 8 | # Change stm32u083rc to your chip name, if necessary. | 8 | # Change stm32u083rc to your chip name, if necessary. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [ "defmt", "time-driver-any", "stm32u083rc", "memory-x", "unstable-pac", "exti"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [ "defmt", "time-driver-any", "stm32u083rc", "memory-x", "unstable-pac", "exti", "chrono"] } |
| 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", default-features = false, features = ["defmt"] } | ||
| 13 | 14 | ||
| 14 | defmt = "0.3" | 15 | defmt = "0.3" |
| 15 | defmt-rtt = "0.4" | 16 | defmt-rtt = "0.4" |
| @@ -21,5 +22,8 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } | |||
| 21 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 22 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 22 | heapless = { version = "0.8", default-features = false } | 23 | heapless = { version = "0.8", default-features = false } |
| 23 | 24 | ||
| 25 | micromath = "2.0.0" | ||
| 26 | chrono = { version = "0.4.38", default-features = false } | ||
| 27 | |||
| 24 | [profile.release] | 28 | [profile.release] |
| 25 | debug = 2 | 29 | debug = 2 |
diff --git a/examples/stm32u0/src/bin/adc.rs b/examples/stm32u0/src/bin/adc.rs new file mode 100644 index 000000000..4410448f1 --- /dev/null +++ b/examples/stm32u0/src/bin/adc.rs | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_stm32::adc::{Adc, Resolution}; | ||
| 6 | use embassy_stm32::Config; | ||
| 7 | use embassy_time::Duration; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[cortex_m_rt::entry] | ||
| 11 | fn main() -> ! { | ||
| 12 | info!("Hello World!"); | ||
| 13 | |||
| 14 | let mut config = Config::default(); | ||
| 15 | { | ||
| 16 | use embassy_stm32::rcc::*; | ||
| 17 | config.rcc.mux.adcsel = mux::Adcsel::SYS; | ||
| 18 | } | ||
| 19 | let p = embassy_stm32::init(config); | ||
| 20 | |||
| 21 | let mut adc = Adc::new(p.ADC1); | ||
| 22 | adc.set_resolution(Resolution::BITS8); | ||
| 23 | let mut channel = p.PC0; | ||
| 24 | |||
| 25 | loop { | ||
| 26 | let v = adc.read(&mut channel); | ||
| 27 | info!("--> {}", v); | ||
| 28 | embassy_time::block_for(Duration::from_millis(200)); | ||
| 29 | } | ||
| 30 | } | ||
diff --git a/examples/stm32u0/src/bin/crc.rs b/examples/stm32u0/src/bin/crc.rs new file mode 100644 index 000000000..d1b545d5b --- /dev/null +++ b/examples/stm32u0/src/bin/crc.rs | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::crc::{Config, Crc, InputReverseConfig, PolySize}; | ||
| 7 | use {defmt_rtt as _, panic_probe as _}; | ||
| 8 | |||
| 9 | #[embassy_executor::main] | ||
| 10 | async fn main(_spawner: Spawner) { | ||
| 11 | let p = embassy_stm32::init(Default::default()); | ||
| 12 | info!("Hello World!"); | ||
| 13 | |||
| 14 | // Setup for: https://crccalc.com/?crc=Life, it never dieWomen are my favorite guy&method=crc32&datatype=ascii&outtype=0 | ||
| 15 | let mut crc = Crc::new( | ||
| 16 | p.CRC, | ||
| 17 | unwrap!(Config::new( | ||
| 18 | InputReverseConfig::Byte, | ||
| 19 | true, | ||
| 20 | PolySize::Width32, | ||
| 21 | 0xFFFFFFFF, | ||
| 22 | 0x04C11DB7 | ||
| 23 | )), | ||
| 24 | ); | ||
| 25 | |||
| 26 | let output = crc.feed_bytes(b"Life, it never die\nWomen are my favorite guy") ^ 0xFFFFFFFF; | ||
| 27 | |||
| 28 | defmt::assert_eq!(output, 0x33F0E26B); | ||
| 29 | |||
| 30 | cortex_m::asm::bkpt(); | ||
| 31 | } | ||
diff --git a/examples/stm32u0/src/bin/dac.rs b/examples/stm32u0/src/bin/dac.rs new file mode 100644 index 000000000..fdbf1d374 --- /dev/null +++ b/examples/stm32u0/src/bin/dac.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_stm32::dac::{DacCh1, Value}; | ||
| 6 | use embassy_stm32::dma::NoDma; | ||
| 7 | use {defmt_rtt as _, panic_probe as _}; | ||
| 8 | |||
| 9 | #[cortex_m_rt::entry] | ||
| 10 | fn main() -> ! { | ||
| 11 | let p = embassy_stm32::init(Default::default()); | ||
| 12 | info!("Hello World!"); | ||
| 13 | |||
| 14 | let mut dac = DacCh1::new(p.DAC1, NoDma, p.PA4); | ||
| 15 | |||
| 16 | loop { | ||
| 17 | for v in 0..=255 { | ||
| 18 | dac.set(Value::Bit8(to_sine_wave(v))); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | use micromath::F32Ext; | ||
| 24 | |||
| 25 | fn to_sine_wave(v: u8) -> u8 { | ||
| 26 | if v >= 128 { | ||
| 27 | // top half | ||
| 28 | let r = 3.14 * ((v - 128) as f32 / 128.0); | ||
| 29 | (r.sin() * 128.0 + 127.0) as u8 | ||
| 30 | } else { | ||
| 31 | // bottom half | ||
| 32 | let r = 3.14 + 3.14 * (v as f32 / 128.0); | ||
| 33 | (r.sin() * 128.0 + 127.0) as u8 | ||
| 34 | } | ||
| 35 | } | ||
diff --git a/examples/stm32u0/src/bin/flash.rs b/examples/stm32u0/src/bin/flash.rs new file mode 100644 index 000000000..01b80a76b --- /dev/null +++ b/examples/stm32u0/src/bin/flash.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::flash::Flash; | ||
| 7 | use {defmt_rtt as _, panic_probe as _}; | ||
| 8 | |||
| 9 | #[embassy_executor::main] | ||
| 10 | async fn main(_spawner: Spawner) { | ||
| 11 | let p = embassy_stm32::init(Default::default()); | ||
| 12 | info!("Hello World!"); | ||
| 13 | |||
| 14 | let addr: u32 = 0x40000 - 2 * 1024; | ||
| 15 | |||
| 16 | let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank1_region; | ||
| 17 | |||
| 18 | info!("Reading..."); | ||
| 19 | let mut buf = [0u8; 32]; | ||
| 20 | unwrap!(f.blocking_read(addr, &mut buf)); | ||
| 21 | info!("Read: {=[u8]:x}", buf); | ||
| 22 | info!("Erasing..."); | ||
| 23 | unwrap!(f.blocking_erase(addr, addr + 2 * 1024)); | ||
| 24 | |||
| 25 | info!("Reading..."); | ||
| 26 | let mut buf = [0u8; 32]; | ||
| 27 | unwrap!(f.blocking_read(addr, &mut buf)); | ||
| 28 | info!("Read after erase: {=[u8]:x}", buf); | ||
| 29 | |||
| 30 | info!("Writing..."); | ||
| 31 | unwrap!(f.blocking_write( | ||
| 32 | addr, | ||
| 33 | &[ | ||
| 34 | 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, | ||
| 35 | 30, 31, 32 | ||
| 36 | ] | ||
| 37 | )); | ||
| 38 | |||
| 39 | info!("Reading..."); | ||
| 40 | let mut buf = [0u8; 32]; | ||
| 41 | unwrap!(f.blocking_read(addr, &mut buf)); | ||
| 42 | info!("Read: {=[u8]:x}", buf); | ||
| 43 | } | ||
diff --git a/examples/stm32u0/src/bin/i2c.rs b/examples/stm32u0/src/bin/i2c.rs new file mode 100644 index 000000000..2861bc091 --- /dev/null +++ b/examples/stm32u0/src/bin/i2c.rs | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::i2c::I2c; | ||
| 7 | use embassy_stm32::time::Hertz; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | const ADDRESS: u8 = 0x5F; | ||
| 11 | const WHOAMI: u8 = 0x0F; | ||
| 12 | |||
| 13 | #[embassy_executor::main] | ||
| 14 | async fn main(_spawner: Spawner) { | ||
| 15 | let p = embassy_stm32::init(Default::default()); | ||
| 16 | let mut i2c = I2c::new_blocking(p.I2C2, p.PB10, p.PB11, Hertz(100_000), Default::default()); | ||
| 17 | |||
| 18 | let mut data = [0u8; 1]; | ||
| 19 | unwrap!(i2c.blocking_write_read(ADDRESS, &[WHOAMI], &mut data)); | ||
| 20 | info!("Whoami: {}", data[0]); | ||
| 21 | } | ||
diff --git a/examples/stm32u0/src/bin/rng.rs b/examples/stm32u0/src/bin/rng.rs new file mode 100644 index 000000000..89445b042 --- /dev/null +++ b/examples/stm32u0/src/bin/rng.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::rcc::mux::Clk48sel; | ||
| 7 | use embassy_stm32::rng::Rng; | ||
| 8 | use embassy_stm32::{bind_interrupts, peripherals, rng, Config}; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | bind_interrupts!(struct Irqs { | ||
| 12 | RNG_CRYP => rng::InterruptHandler<peripherals::RNG>; | ||
| 13 | }); | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async fn main(_spawner: Spawner) { | ||
| 17 | let mut config = Config::default(); | ||
| 18 | { | ||
| 19 | use embassy_stm32::rcc::*; | ||
| 20 | config.rcc.hsi = true; | ||
| 21 | config.rcc.pll = Some(Pll { | ||
| 22 | source: PllSource::HSI, // 16 MHz | ||
| 23 | prediv: PllPreDiv::DIV1, | ||
| 24 | mul: PllMul::MUL7, // 16 * 7 = 112 MHz | ||
| 25 | divp: None, | ||
| 26 | divq: None, | ||
| 27 | divr: Some(PllRDiv::DIV2), // 112 / 2 = 56 MHz | ||
| 28 | }); | ||
| 29 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 30 | config.rcc.hsi48 = Some(Hsi48Config { sync_from_usb: false }); // needed for RNG | ||
| 31 | config.rcc.mux.clk48sel = Clk48sel::HSI48; // needed for RNG (or use MSI or PLLQ if you want) | ||
| 32 | } | ||
| 33 | |||
| 34 | let p = embassy_stm32::init(config); | ||
| 35 | |||
| 36 | info!("Hello World!"); | ||
| 37 | |||
| 38 | let mut rng = Rng::new(p.RNG, Irqs); | ||
| 39 | |||
| 40 | let mut buf = [0u8; 16]; | ||
| 41 | unwrap!(rng.async_fill_bytes(&mut buf).await); | ||
| 42 | info!("random bytes: {:02x}", buf); | ||
| 43 | } | ||
diff --git a/examples/stm32u0/src/bin/rtc.rs b/examples/stm32u0/src/bin/rtc.rs new file mode 100644 index 000000000..72fa0fde4 --- /dev/null +++ b/examples/stm32u0/src/bin/rtc.rs | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use chrono::{NaiveDate, NaiveDateTime}; | ||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 8 | use embassy_stm32::Config; | ||
| 9 | use embassy_time::Timer; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | #[embassy_executor::main] | ||
| 13 | async fn main(_spawner: Spawner) { | ||
| 14 | let mut config = Config::default(); | ||
| 15 | { | ||
| 16 | use embassy_stm32::rcc::*; | ||
| 17 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 18 | config.rcc.hsi = true; | ||
| 19 | config.rcc.pll = Some(Pll { | ||
| 20 | source: PllSource::HSI, // 16 MHz | ||
| 21 | prediv: PllPreDiv::DIV1, | ||
| 22 | mul: PllMul::MUL7, // 16 * 7 = 112 MHz | ||
| 23 | divp: None, | ||
| 24 | divq: None, | ||
| 25 | divr: Some(PllRDiv::DIV2), // 112 / 2 = 56 MHz | ||
| 26 | }); | ||
| 27 | config.rcc.ls = LsConfig::default(); | ||
| 28 | } | ||
| 29 | |||
| 30 | let p = embassy_stm32::init(config); | ||
| 31 | |||
| 32 | info!("Hello World!"); | ||
| 33 | |||
| 34 | let now = NaiveDate::from_ymd_opt(2020, 5, 15) | ||
| 35 | .unwrap() | ||
| 36 | .and_hms_opt(10, 30, 15) | ||
| 37 | .unwrap(); | ||
| 38 | |||
| 39 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | ||
| 40 | info!("Got RTC! {:?}", now.and_utc().timestamp()); | ||
| 41 | |||
| 42 | rtc.set_datetime(now.into()).expect("datetime not set"); | ||
| 43 | |||
| 44 | // In reality the delay would be much longer | ||
| 45 | Timer::after_millis(20000).await; | ||
| 46 | |||
| 47 | let then: NaiveDateTime = rtc.now().unwrap().into(); | ||
| 48 | info!("Got RTC! {:?}", then.and_utc().timestamp()); | ||
| 49 | } | ||
diff --git a/examples/stm32u0/src/bin/spi.rs b/examples/stm32u0/src/bin/spi.rs new file mode 100644 index 000000000..5693a3765 --- /dev/null +++ b/examples/stm32u0/src/bin/spi.rs | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 6 | use embassy_stm32::spi::{Config, Spi}; | ||
| 7 | use embassy_stm32::time::Hertz; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[cortex_m_rt::entry] | ||
| 11 | fn main() -> ! { | ||
| 12 | info!("Hello World!"); | ||
| 13 | |||
| 14 | let p = embassy_stm32::init(Default::default()); | ||
| 15 | |||
| 16 | let mut spi_config = Config::default(); | ||
| 17 | spi_config.frequency = Hertz(1_000_000); | ||
| 18 | |||
| 19 | let mut spi = Spi::new_blocking(p.SPI3, p.PC10, p.PC12, p.PC11, spi_config); | ||
| 20 | |||
| 21 | let mut cs = Output::new(p.PE0, Level::High, Speed::VeryHigh); | ||
| 22 | |||
| 23 | loop { | ||
| 24 | let mut buf = [0x0Au8; 4]; | ||
| 25 | cs.set_low(); | ||
| 26 | unwrap!(spi.blocking_transfer_in_place(&mut buf)); | ||
| 27 | cs.set_high(); | ||
| 28 | info!("xfer {=[u8]:x}", buf); | ||
| 29 | } | ||
| 30 | } | ||
diff --git a/examples/stm32u0/src/bin/usart.rs b/examples/stm32u0/src/bin/usart.rs new file mode 100644 index 000000000..037a5c833 --- /dev/null +++ b/examples/stm32u0/src/bin/usart.rs | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_stm32::usart::{Config, Uart}; | ||
| 6 | use {defmt_rtt as _, panic_probe as _}; | ||
| 7 | |||
| 8 | #[cortex_m_rt::entry] | ||
| 9 | fn main() -> ! { | ||
| 10 | info!("Hello World!"); | ||
| 11 | |||
| 12 | let p = embassy_stm32::init(Default::default()); | ||
| 13 | |||
| 14 | let config = Config::default(); | ||
| 15 | let mut usart = Uart::new_blocking(p.USART2, p.PA3, p.PA2, config).unwrap(); | ||
| 16 | |||
| 17 | unwrap!(usart.blocking_write(b"Hello Embassy World!\r\n")); | ||
| 18 | info!("wrote Hello, starting echo"); | ||
| 19 | |||
| 20 | let mut buf = [0u8; 1]; | ||
| 21 | loop { | ||
| 22 | unwrap!(usart.blocking_read(&mut buf)); | ||
| 23 | unwrap!(usart.blocking_write(&buf)); | ||
| 24 | } | ||
| 25 | } | ||
diff --git a/examples/stm32u0/src/bin/usb_serial.rs b/examples/stm32u0/src/bin/usb_serial.rs new file mode 100644 index 000000000..9b38fd5dc --- /dev/null +++ b/examples/stm32u0/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::{panic, *}; | ||
| 5 | use defmt_rtt as _; // global logger | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::usb::{Driver, Instance}; | ||
| 8 | use embassy_stm32::{bind_interrupts, peripherals, usb, Config}; | ||
| 9 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 10 | use embassy_usb::driver::EndpointError; | ||
| 11 | use embassy_usb::Builder; | ||
| 12 | use futures::future::join; | ||
| 13 | use panic_probe as _; | ||
| 14 | |||
| 15 | bind_interrupts!(struct Irqs { | ||
| 16 | USB_DRD_FS => usb::InterruptHandler<peripherals::USB>; | ||
| 17 | }); | ||
| 18 | |||
| 19 | #[embassy_executor::main] | ||
| 20 | async fn main(_spawner: Spawner) { | ||
| 21 | let mut config = Config::default(); | ||
| 22 | { | ||
| 23 | use embassy_stm32::rcc::*; | ||
| 24 | config.rcc.hsi = true; | ||
| 25 | config.rcc.pll = Some(Pll { | ||
| 26 | source: PllSource::HSI, // 16 MHz | ||
| 27 | prediv: PllPreDiv::DIV1, | ||
| 28 | mul: PllMul::MUL7, | ||
| 29 | divp: None, | ||
| 30 | divq: None, | ||
| 31 | divr: Some(PllRDiv::DIV2), // 56 MHz | ||
| 32 | }); | ||
| 33 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 34 | config.rcc.hsi48 = Some(Hsi48Config { sync_from_usb: true }); // needed for USB | ||
| 35 | config.rcc.mux.clk48sel = mux::Clk48sel::HSI48; // USB uses ICLK | ||
| 36 | } | ||
| 37 | |||
| 38 | let p = embassy_stm32::init(config); | ||
| 39 | |||
| 40 | info!("Hello World!"); | ||
| 41 | |||
| 42 | // Create the driver, from the HAL. | ||
| 43 | let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11); | ||
| 44 | |||
| 45 | // Create embassy-usb Config | ||
| 46 | let config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 47 | //config.max_packet_size_0 = 64; | ||
| 48 | |||
| 49 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 50 | // It needs some buffers for building the descriptors. | ||
| 51 | let mut config_descriptor = [0; 256]; | ||
| 52 | let mut bos_descriptor = [0; 256]; | ||
| 53 | let mut control_buf = [0; 7]; | ||
| 54 | |||
| 55 | let mut state = State::new(); | ||
| 56 | |||
| 57 | let mut builder = Builder::new( | ||
| 58 | driver, | ||
| 59 | config, | ||
| 60 | &mut config_descriptor, | ||
| 61 | &mut bos_descriptor, | ||
| 62 | &mut [], // no msos descriptors | ||
| 63 | &mut control_buf, | ||
| 64 | ); | ||
| 65 | |||
| 66 | // Create classes on the builder. | ||
| 67 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 68 | |||
| 69 | // Build the builder. | ||
| 70 | let mut usb = builder.build(); | ||
| 71 | |||
| 72 | // Run the USB device. | ||
| 73 | let usb_fut = usb.run(); | ||
| 74 | |||
| 75 | // Do stuff with the class! | ||
| 76 | let echo_fut = async { | ||
| 77 | loop { | ||
| 78 | class.wait_connection().await; | ||
| 79 | info!("Connected"); | ||
| 80 | let _ = echo(&mut class).await; | ||
| 81 | info!("Disconnected"); | ||
| 82 | } | ||
| 83 | }; | ||
| 84 | |||
| 85 | // Run everything concurrently. | ||
| 86 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 87 | join(usb_fut, echo_fut).await; | ||
| 88 | } | ||
| 89 | |||
| 90 | struct Disconnected {} | ||
| 91 | |||
| 92 | impl From<EndpointError> for Disconnected { | ||
| 93 | fn from(val: EndpointError) -> Self { | ||
| 94 | match val { | ||
| 95 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 96 | EndpointError::Disabled => Disconnected {}, | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 102 | let mut buf = [0; 64]; | ||
| 103 | loop { | ||
| 104 | let n = class.read_packet(&mut buf).await?; | ||
| 105 | let data = &buf[..n]; | ||
| 106 | info!("data: {:x}", data); | ||
| 107 | class.write_packet(data).await?; | ||
| 108 | } | ||
| 109 | } | ||
diff --git a/examples/stm32u0/src/bin/wdt.rs b/examples/stm32u0/src/bin/wdt.rs new file mode 100644 index 000000000..f6276e2e9 --- /dev/null +++ b/examples/stm32u0/src/bin/wdt.rs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 7 | use embassy_stm32::wdg::IndependentWatchdog; | ||
| 8 | use embassy_time::Timer; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | #[embassy_executor::main] | ||
| 12 | async fn main(_spawner: Spawner) { | ||
| 13 | let p = embassy_stm32::init(Default::default()); | ||
| 14 | info!("Hello World!"); | ||
| 15 | |||
| 16 | let mut led = Output::new(p.PA5, Level::High, Speed::Low); | ||
| 17 | |||
| 18 | let mut wdt = IndependentWatchdog::new(p.IWDG, 1_000_000); | ||
| 19 | wdt.unleash(); | ||
| 20 | |||
| 21 | let mut i = 0; | ||
| 22 | |||
| 23 | loop { | ||
| 24 | info!("high"); | ||
| 25 | led.set_high(); | ||
| 26 | Timer::after_millis(300).await; | ||
| 27 | |||
| 28 | info!("low"); | ||
| 29 | led.set_low(); | ||
| 30 | Timer::after_millis(300).await; | ||
| 31 | |||
| 32 | // Pet watchdog for 5 iterations and then stop. | ||
| 33 | // MCU should restart in 1 second after the last pet. | ||
| 34 | if i < 5 { | ||
| 35 | info!("Petting watchdog"); | ||
| 36 | wdt.pet(); | ||
| 37 | } | ||
| 38 | |||
| 39 | i += 1; | ||
| 40 | } | ||
| 41 | } | ||
diff --git a/examples/stm32wb/.cargo/config.toml b/examples/stm32wb/.cargo/config.toml index 51c499ee7..8b6d6d754 100644 --- a/examples/stm32wb/.cargo/config.toml +++ b/examples/stm32wb/.cargo/config.toml | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] |
| 2 | # replace STM32WB55CCUx with your chip as listed in `probe-rs chip list` | 2 | # replace STM32WB55CCUx with your chip as listed in `probe-rs chip list` |
| 3 | # runner = "probe-run --chip STM32WB55RGVx --speed 1000 --connect-under-reset" | 3 | # runner = "probe-rs run --chip STM32WB55RGVx --speed 1000 --connect-under-reset" |
| 4 | runner = "teleprobe local run --chip STM32WB55RG --elf" | 4 | runner = "teleprobe local run --chip STM32WB55RG --elf" |
| 5 | 5 | ||
| 6 | [build] | 6 | [build] |
