Add examples for STM32L0
This commit is contained in:
committed by
Dario Nieuwenhuis
parent
3d16e922d5
commit
1bb7123156
36
examples/stm32l0/src/bin/blinky.rs
Normal file
36
examples/stm32l0/src/bin/blinky.rs
Normal file
@ -0,0 +1,36 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(trait_alias)]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![allow(incomplete_features)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::{rcc::*, gpio::{Level, Output}};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
let mut p = embassy_stm32::init(Default::default());
|
||||
|
||||
Rcc::new(p.RCC).enable_debug_wfe(&mut p.DBGMCU, true);
|
||||
|
||||
let mut led = Output::new(p.PB5, Level::High);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
led.set_high().unwrap();
|
||||
cortex_m::asm::delay(1_000_000);
|
||||
|
||||
info!("low");
|
||||
led.set_low().unwrap();
|
||||
cortex_m::asm::delay(1_000_000);
|
||||
}
|
||||
}
|
40
examples/stm32l0/src/bin/button.rs
Normal file
40
examples/stm32l0/src/bin/button.rs
Normal file
@ -0,0 +1,40 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(trait_alias)]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![allow(incomplete_features)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::{rcc::*, gpio::{Input, Level, Output, Pull}};
|
||||
use embedded_hal::digital::v2::{InputPin, OutputPin};
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
let mut p = embassy_stm32::init(Default::default());
|
||||
Rcc::new(p.RCC).enable_debug_wfe(&mut p.DBGMCU, true);
|
||||
|
||||
let button = Input::new(p.PB2, Pull::Up);
|
||||
let mut led1 = Output::new(p.PA5, Level::High);
|
||||
let mut led2 = Output::new(p.PB5, Level::High);
|
||||
|
||||
loop {
|
||||
if button.is_high().unwrap() {
|
||||
info!("high");
|
||||
led1.set_high().unwrap();
|
||||
led2.set_low().unwrap();
|
||||
} else {
|
||||
info!("low");
|
||||
led1.set_low().unwrap();
|
||||
led2.set_high().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
64
examples/stm32l0/src/bin/button_exti.rs
Normal file
64
examples/stm32l0/src/bin/button_exti.rs
Normal file
@ -0,0 +1,64 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(trait_alias)]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![allow(incomplete_features)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy::executor::Executor;
|
||||
use embassy::time::Clock;
|
||||
use embassy::util::Forever;
|
||||
use embassy_stm32::exti::ExtiInput;
|
||||
use embassy_stm32::gpio::{Input, Pull};
|
||||
use embassy_stm32::rcc;
|
||||
use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
|
||||
#[embassy::task]
|
||||
async fn main_task() {
|
||||
let mut p = embassy_stm32::init(Default::default());
|
||||
let mut rcc = rcc::Rcc::new(p.RCC);
|
||||
rcc.enable_debug_wfe(&mut p.DBGMCU, true);
|
||||
// Enables SYSCFG
|
||||
let _ = rcc.enable_hsi48(&mut p.SYSCFG, p.CRS);
|
||||
|
||||
let button = Input::new(p.PB2, Pull::Up);
|
||||
let mut button = ExtiInput::new(button, p.EXTI2);
|
||||
|
||||
info!("Press the USER button...");
|
||||
|
||||
loop {
|
||||
button.wait_for_falling_edge().await;
|
||||
info!("Pressed!");
|
||||
button.wait_for_rising_edge().await;
|
||||
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 { embassy::time::set_clock(&ZeroClock) };
|
||||
|
||||
let executor = EXECUTOR.put(Executor::new());
|
||||
|
||||
executor.run(|spawner| {
|
||||
unwrap!(spawner.spawn(main_task()));
|
||||
})
|
||||
}
|
49
examples/stm32l0/src/bin/spi.rs
Normal file
49
examples/stm32l0/src/bin/spi.rs
Normal file
@ -0,0 +1,49 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(trait_alias)]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![allow(incomplete_features)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
|
||||
use embassy_stm32::gpio::{Level, Output};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use embassy_stm32::rcc;
|
||||
use embassy_stm32::spi::{Config, Spi};
|
||||
use embassy_stm32::time::Hertz;
|
||||
use embedded_hal::blocking::spi::Transfer;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World, dude!");
|
||||
|
||||
let mut p = embassy_stm32::init(Default::default());
|
||||
let mut rcc = rcc::Rcc::new(p.RCC);
|
||||
rcc.enable_debug_wfe(&mut p.DBGMCU, true);
|
||||
|
||||
let mut spi = Spi::new(
|
||||
Hertz(16_000_000),
|
||||
p.SPI1,
|
||||
p.PB3,
|
||||
p.PA7,
|
||||
p.PA6,
|
||||
Hertz(1_000_000),
|
||||
Config::default(),
|
||||
);
|
||||
|
||||
let mut cs = Output::new(p.PA15, Level::High);
|
||||
|
||||
loop {
|
||||
let mut buf = [0x0A; 4];
|
||||
unwrap!(cs.set_low());
|
||||
unwrap!(spi.transfer(&mut buf));
|
||||
unwrap!(cs.set_high());
|
||||
info!("xfer {=[u8]:x}", buf);
|
||||
}
|
||||
}
|
17
examples/stm32l0/src/example_common.rs
Normal file
17
examples/stm32l0/src/example_common.rs
Normal file
@ -0,0 +1,17 @@
|
||||
#![macro_use]
|
||||
|
||||
use defmt_rtt as _; // global logger
|
||||
use panic_probe as _;
|
||||
|
||||
pub use defmt::*;
|
||||
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
defmt::timestamp! {"{=u64}", {
|
||||
static COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
// NOTE(no-CAS) `timestamps` runs with interrupts disabled
|
||||
let n = COUNT.load(Ordering::Relaxed);
|
||||
COUNT.store(n + 1, Ordering::Relaxed);
|
||||
n as u64
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user