stm32/examples: cleanup L4

This commit is contained in:
Dario Nieuwenhuis 2021-07-24 13:57:11 +02:00
parent 4899168534
commit 5b0ae5c25b
10 changed files with 100 additions and 279 deletions

View File

@ -21,7 +21,6 @@ embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "def
embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "unstable-pac", "stm32l4s5vi"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "unstable-pac", "stm32l4s5vi"] }
embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } embassy-extras = {version = "0.1.0", path = "../../embassy-extras" }
stm32l4xx-hal = { version = "0.6.0", features = ["stm32l4x5"] }
defmt = "0.2.0" defmt = "0.2.0"
defmt-rtt = "0.2.0" defmt-rtt = "0.2.0"

View File

@ -9,35 +9,16 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use defmt::panic;
use embassy::executor::Spawner;
use embassy::time::Delay;
use embassy_stm32::adc::{Adc, Resolution};
use embassy_stm32::{pac, Peripherals};
use example_common::*; use example_common::*;
use cortex_m::delay::Delay; #[embassy::main]
use cortex_m_rt::entry; async fn main(_spawner: Spawner, p: Peripherals) {
use embassy_stm32::adc::{Adc, Resolution}; info!("Hello World!");
use embassy_stm32::pac;
use stm32l4xx_hal::prelude::*;
use stm32l4xx_hal::rcc::PllSource;
#[entry]
fn main() -> ! {
info!("Hello World, dude!");
//let pp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let pp = stm32l4xx_hal::stm32::Peripherals::take().unwrap();
let mut flash = pp.FLASH.constrain();
let mut rcc = pp.RCC.constrain();
let mut pwr = pp.PWR.constrain(&mut rcc.apb1r1);
let mut delay = Delay::new(cp.SYST, 80_000_000);
// TRY the other clock configuration
// let clocks = rcc.cfgr.freeze(&mut flash.acr);
rcc.cfgr
.sysclk(80.mhz())
.pclk1(80.mhz())
.pclk2(80.mhz())
.pll_source(PllSource::HSI16)
.freeze(&mut flash.acr, &mut pwr);
unsafe { unsafe {
pac::RCC.ccipr().modify(|w| { pac::RCC.ccipr().modify(|w| {
@ -48,11 +29,10 @@ fn main() -> ! {
w.set_dbg_standby(true); w.set_dbg_standby(true);
w.set_dbg_stop(true); w.set_dbg_stop(true);
}); });
pac::RCC.ahb2enr().modify(|w| w.set_adcen(true));
} }
let p = embassy_stm32::init(Default::default()); let mut adc = Adc::new(p.ADC1, &mut Delay);
let mut adc = Adc::new(p.ADC1, &mut delay);
//adc.enable_vref(); //adc.enable_vref();
adc.set_resolution(Resolution::EightBit); adc.set_resolution(Resolution::EightBit);
let mut channel = p.PC0; let mut channel = p.PC0;

View File

@ -8,14 +8,16 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use cortex_m_rt::entry; use defmt::panic;
use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::pac; use embassy_stm32::{pac, Peripherals};
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use example_common::*; use example_common::*;
#[entry] #[embassy::main]
fn main() -> ! { async fn main(_spawner: Spawner, p: Peripherals) {
info!("Hello World!"); info!("Hello World!");
unsafe { unsafe {
@ -26,17 +28,12 @@ fn main() -> ! {
}); });
} }
let p = embassy_stm32::init(Default::default());
let mut led = Output::new(p.PB14, Level::High, Speed::Low); let mut led = Output::new(p.PB14, Level::High, Speed::Low);
loop { loop {
info!("high");
led.set_high().unwrap(); led.set_high().unwrap();
cortex_m::asm::delay(10_000_000); Timer::after(Duration::from_millis(300)).await;
info!("low");
led.set_low().unwrap(); led.set_low().unwrap();
cortex_m::asm::delay(10_000_000); Timer::after(Duration::from_millis(300)).await;
} }
} }

View File

@ -8,14 +8,15 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use cortex_m_rt::entry; use defmt::panic;
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy::executor::Spawner;
use embassy_stm32::pac; use embassy_stm32::gpio::{Input, Pull};
use embedded_hal::digital::v2::{InputPin, OutputPin}; use embassy_stm32::{pac, Peripherals};
use embedded_hal::digital::v2::InputPin;
use example_common::*; use example_common::*;
#[entry] #[embassy::main]
fn main() -> ! { async fn main(_spawner: Spawner, p: Peripherals) {
info!("Hello World!"); info!("Hello World!");
unsafe { unsafe {
@ -26,21 +27,13 @@ fn main() -> ! {
}); });
} }
let p = embassy_stm32::init(Default::default());
let button = Input::new(p.PC13, Pull::Up); let button = Input::new(p.PC13, Pull::Up);
let mut led1 = Output::new(p.PA5, Level::High, Speed::Low);
let mut led2 = Output::new(p.PB14, Level::High, Speed::Low);
loop { loop {
if button.is_high().unwrap() { if button.is_high().unwrap() {
info!("high"); info!("high");
led1.set_high().unwrap();
led2.set_low().unwrap();
} else { } else {
info!("low"); info!("low");
led1.set_low().unwrap();
led2.set_high().unwrap();
} }
} }
} }

View File

@ -8,19 +8,25 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use cortex_m_rt::entry; use defmt::panic;
use embassy::executor::Executor; use embassy::executor::Spawner;
use embassy::time::Clock;
use embassy::util::Forever;
use embassy_stm32::exti::ExtiInput; use embassy_stm32::exti::ExtiInput;
use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::gpio::{Input, Pull};
use embassy_stm32::pac; use embassy_stm32::{pac, Peripherals};
use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
use example_common::*; use example_common::*;
#[embassy::task] #[embassy::main]
async fn main_task() { async fn main(_spawner: Spawner, p: Peripherals) {
let p = embassy_stm32::init(Default::default()); info!("Hello World!");
unsafe {
pac::DBGMCU.cr().modify(|w| {
w.set_dbg_sleep(true);
w.set_dbg_standby(true);
w.set_dbg_stop(true);
});
}
let button = Input::new(p.PC13, Pull::Up); let button = Input::new(p.PC13, Pull::Up);
let mut button = ExtiInput::new(button, p.EXTI13); let mut button = ExtiInput::new(button, p.EXTI13);
@ -34,34 +40,3 @@ async fn main_task() {
info!("Released!"); info!("Released!");
} }
} }
struct ZeroClock;
impl Clock for ZeroClock {
fn now(&self) -> u64 {
0
}
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
unsafe {
pac::DBGMCU.cr().modify(|w| {
w.set_dbg_sleep(true);
w.set_dbg_standby(true);
w.set_dbg_stop(true);
});
}
unsafe { embassy::time::set_clock(&ZeroClock) };
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(main_task()));
})
}

View File

@ -9,32 +9,16 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use defmt::panic;
use embassy::executor::Spawner;
use embassy_stm32::dac::{Channel, Dac, Value};
use embassy_stm32::gpio::NoPin; use embassy_stm32::gpio::NoPin;
use embassy_stm32::{pac, Peripherals};
use example_common::*; use example_common::*;
use cortex_m_rt::entry; #[embassy::main]
use embassy_stm32::dac::{Channel, Dac, Value}; async fn main(_spawner: Spawner, p: Peripherals) {
use embassy_stm32::pac; info!("Hello World!");
use stm32l4xx_hal::prelude::*;
use stm32l4xx_hal::rcc::PllSource;
#[entry]
fn main() -> ! {
info!("Hello World, dude!");
//let pp = pac::Peripherals::take().unwrap();
let pp = stm32l4xx_hal::stm32::Peripherals::take().unwrap();
let mut flash = pp.FLASH.constrain();
let mut rcc = pp.RCC.constrain();
let mut pwr = pp.PWR.constrain(&mut rcc.apb1r1);
// TRY the other clock configuration
// let clocks = rcc.cfgr.freeze(&mut flash.acr);
rcc.cfgr
.sysclk(80.mhz())
.pclk1(80.mhz())
.pclk2(80.mhz())
.pll_source(PllSource::HSI16)
.freeze(&mut flash.acr, &mut pwr);
unsafe { unsafe {
pac::DBGMCU.cr().modify(|w| { pac::DBGMCU.cr().modify(|w| {
@ -47,8 +31,6 @@ fn main() -> ! {
}); });
} }
let p = embassy_stm32::init(Default::default());
let mut dac = Dac::new(p.DAC1, p.PA4, NoPin); let mut dac = Dac::new(p.DAC1, p.PA4, NoPin);
loop { loop {

View File

@ -9,19 +9,20 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use cortex_m_rt::entry; use defmt::panic;
use embassy::executor::Spawner;
use embassy_stm32::dma::NoDma; use embassy_stm32::dma::NoDma;
use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::pac;
use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::spi::{Config, Spi};
use embassy_stm32::time::Hertz; use embassy_stm32::time::Hertz;
use embassy_stm32::{pac, Peripherals};
use embedded_hal::blocking::spi::Transfer; use embedded_hal::blocking::spi::Transfer;
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use example_common::*; use example_common::*;
#[entry] #[embassy::main]
fn main() -> ! { async fn main(_spawner: Spawner, p: Peripherals) {
info!("Hello World, dude!"); info!("Hello World!");
unsafe { unsafe {
pac::DBGMCU.cr().modify(|w| { pac::DBGMCU.cr().modify(|w| {
@ -31,8 +32,6 @@ fn main() -> ! {
}); });
} }
let p = embassy_stm32::init(Default::default());
let mut spi = Spi::new( let mut spi = Spi::new(
p.SPI3, p.SPI3,
p.PC10, p.PC10,

View File

@ -9,21 +9,27 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use cortex_m_rt::entry; use defmt::panic;
use embassy::executor::Executor; use embassy::executor::Spawner;
use embassy::time::Clock; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
use embassy::util::Forever; use embassy_stm32::spi::{Config, Spi};
use embassy_stm32::pac;
use example_common::*;
use embassy_stm32::spi::{Spi, Config};
use embassy_traits::spi::FullDuplex;
use embassy_stm32::time::Hertz; use embassy_stm32::time::Hertz;
use embassy_stm32::gpio::{Output, Level, Speed, Input, Pull}; use embassy_stm32::{pac, Peripherals};
use embedded_hal::digital::v2::{OutputPin, InputPin}; use embassy_traits::spi::FullDuplex;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use example_common::*;
#[embassy::task] #[embassy::main]
async fn main_task() { async fn main(_spawner: Spawner, p: Peripherals) {
let p = embassy_stm32::init(Default::default()); info!("Hello World!");
unsafe {
pac::DBGMCU.cr().modify(|w| {
w.set_dbg_sleep(true);
w.set_dbg_standby(true);
w.set_dbg_stop(true);
});
}
let mut spi = Spi::new( let mut spi = Spi::new(
p.SPI3, p.SPI3,
@ -36,7 +42,6 @@ async fn main_task() {
Config::default(), Config::default(),
); );
// These are the pins for the Inventek eS-Wifi SPI Wifi Adapter. // These are the pins for the Inventek eS-Wifi SPI Wifi Adapter.
let _boot = Output::new(p.PB12, Level::Low, Speed::VeryHigh); let _boot = Output::new(p.PB12, Level::Low, Speed::VeryHigh);
@ -60,49 +65,3 @@ async fn main_task() {
unwrap!(cs.set_high()); unwrap!(cs.set_high());
info!("xfer {=[u8]:x}", read); info!("xfer {=[u8]:x}", read);
} }
struct ZeroClock;
impl Clock for ZeroClock {
fn now(&self) -> u64 {
0
}
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
unsafe {
pac::DBGMCU.cr().modify(|w| {
w.set_dbg_sleep(true);
w.set_dbg_standby(true);
w.set_dbg_stop(true);
});
pac::RCC.ahb1enr().modify(|w| {
w.set_dmamux1en(true);
w.set_dma1en(true);
w.set_dma2en(true);
});
pac::RCC.ahb2enr().modify(|w| {
w.set_gpioaen(true);
w.set_gpioben(true);
w.set_gpiocen(true);
w.set_gpioden(true);
w.set_gpioeen(true);
w.set_gpiofen(true);
});
}
unsafe { embassy::time::set_clock(&ZeroClock) };
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(main_task()));
})
}

View File

@ -9,18 +9,24 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use cortex_m::prelude::_embedded_hal_blocking_serial_Write; use cortex_m::prelude::_embedded_hal_blocking_serial_Write;
use cortex_m_rt::entry; use defmt::panic;
use embassy::executor::Executor; use embassy::executor::Spawner;
use embassy::time::Clock;
use embassy::util::Forever;
use embassy_stm32::dma::NoDma; use embassy_stm32::dma::NoDma;
use embassy_stm32::pac;
use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::usart::{Config, Uart};
use embassy_stm32::{pac, Peripherals};
use example_common::*; use example_common::*;
#[embassy::task] #[embassy::main]
async fn main_task() { async fn main(_spawner: Spawner, p: Peripherals) {
let p = embassy_stm32::init(Default::default()); info!("Hello World!");
unsafe {
pac::DBGMCU.cr().modify(|w| {
w.set_dbg_sleep(true);
w.set_dbg_standby(true);
w.set_dbg_stop(true);
});
}
let config = Config::default(); let config = Config::default();
let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, NoDma, NoDma, config); let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, NoDma, NoDma, config);
@ -34,42 +40,3 @@ async fn main_task() {
usart.bwrite_all(&buf).unwrap(); usart.bwrite_all(&buf).unwrap();
} }
} }
struct ZeroClock;
impl Clock for ZeroClock {
fn now(&self) -> u64 {
0
}
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
unsafe {
pac::DBGMCU.cr().modify(|w| {
w.set_dbg_sleep(true);
w.set_dbg_standby(true);
w.set_dbg_stop(true);
});
pac::RCC.ahb1enr().modify(|w| {
w.set_dma1en(true);
});
pac::RCC.apb1enr1().modify(|w| {
w.set_uart4en(true);
});
}
unsafe { embassy::time::set_clock(&ZeroClock) };
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(main_task()));
})
}

View File

@ -9,46 +9,17 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use core::fmt::Write; use core::fmt::Write;
use cortex_m_rt::entry; use defmt::panic;
use embassy::executor::Executor; use embassy::executor::Spawner;
use embassy::time::Clock;
use embassy::util::Forever;
use embassy_stm32::dma::NoDma; use embassy_stm32::dma::NoDma;
use embassy_stm32::pac;
use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::usart::{Config, Uart};
use embassy_stm32::{pac, Peripherals};
use embassy_traits::uart::Write as _; use embassy_traits::uart::Write as _;
use example_common::*; use example_common::*;
use heapless::String; use heapless::String;
#[embassy::task] #[embassy::main]
async fn main_task() { async fn main(_spawner: Spawner, p: Peripherals) {
let p = embassy_stm32::init(Default::default());
let config = Config::default();
let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, p.DMA1_CH3, NoDma, config);
for n in 0u32.. {
let mut s: String<128> = String::new();
core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap();
usart.write(s.as_bytes()).await.ok();
info!("wrote DMA");
}
}
struct ZeroClock;
impl Clock for ZeroClock {
fn now(&self) -> u64 {
0
}
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!"); info!("Hello World!");
unsafe { unsafe {
@ -57,19 +28,18 @@ fn main() -> ! {
w.set_dbg_standby(true); w.set_dbg_standby(true);
w.set_dbg_stop(true); w.set_dbg_stop(true);
}); });
pac::RCC.ahb1enr().modify(|w| {
w.set_dmamux1en(true);
w.set_dma1en(true);
w.set_dma2en(true);
});
} }
unsafe { embassy::time::set_clock(&ZeroClock) }; let config = Config::default();
let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, p.DMA1_CH3, NoDma, config);
let executor = EXECUTOR.put(Executor::new()); for n in 0u32.. {
let mut s: String<128> = String::new();
core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap();
executor.run(|spawner| { info!("Writing...");
unwrap!(spawner.spawn(main_task())); usart.write(s.as_bytes()).await.ok();
})
info!("wrote DMA");
}
} }