mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-22 06:42:32 +00:00
Add some examples.
- usart works - dac works - rng gets stuck on while loop - usb_serial works, but cannot test due to lack of user usb port - adc needs work and does not work yet
This commit is contained in:
parent
af03e1653a
commit
5f23e39052
@ -235,6 +235,23 @@ pub enum TriggerSel {
|
||||
Exti9 = 13,
|
||||
}
|
||||
|
||||
/// Trigger selection for U0.
|
||||
#[cfg(stm32u0)]
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub enum TriggerSel {
|
||||
Software = 0,
|
||||
Tim1 = 1,
|
||||
Tim2 = 2,
|
||||
Tim3 = 3,
|
||||
Tim6 = 5,
|
||||
Tim7 = 6,
|
||||
Tim15 = 8,
|
||||
Lptim1 = 11,
|
||||
Lptim2 = 12,
|
||||
Exti9 = 14,
|
||||
}
|
||||
|
||||
/// Trigger selection for G4.
|
||||
#[cfg(stm32g4)]
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
|
@ -107,14 +107,14 @@ const EP_COUNT: usize = 8;
|
||||
|
||||
#[cfg(any(usbram_16x1_512, usbram_16x2_512))]
|
||||
const USBRAM_SIZE: usize = 512;
|
||||
#[cfg(usbram_16x2_1024)]
|
||||
#[cfg(any(usbram_16x2_1024, usbram_32_1024))]
|
||||
const USBRAM_SIZE: usize = 1024;
|
||||
#[cfg(usbram_32_2048)]
|
||||
const USBRAM_SIZE: usize = 2048;
|
||||
|
||||
#[cfg(not(usbram_32_2048))]
|
||||
#[cfg(not(any(usbram_32_2048, usbram_32_1024)))]
|
||||
const USBRAM_ALIGN: usize = 2;
|
||||
#[cfg(usbram_32_2048)]
|
||||
#[cfg(any(usbram_32_2048, usbram_32_1024))]
|
||||
const USBRAM_ALIGN: usize = 4;
|
||||
|
||||
const NEW_AW: AtomicWaker = AtomicWaker::new();
|
||||
@ -159,7 +159,7 @@ fn calc_out_len(len: u16) -> (u16, u16) {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(usbram_32_2048))]
|
||||
#[cfg(not(any(usbram_32_2048, usbram_32_1024)))]
|
||||
mod btable {
|
||||
use super::*;
|
||||
|
||||
@ -180,7 +180,7 @@ mod btable {
|
||||
USBRAM.mem(index * 4 + 3).read()
|
||||
}
|
||||
}
|
||||
#[cfg(usbram_32_2048)]
|
||||
#[cfg(any(usbram_32_2048, usbram_32_1024))]
|
||||
mod btable {
|
||||
use super::*;
|
||||
|
||||
@ -224,9 +224,9 @@ impl<T: Instance> EndpointBuffer<T> {
|
||||
let n = USBRAM_ALIGN.min(buf.len() - i * USBRAM_ALIGN);
|
||||
val[..n].copy_from_slice(&buf[i * USBRAM_ALIGN..][..n]);
|
||||
|
||||
#[cfg(not(usbram_32_2048))]
|
||||
#[cfg(not(any(usbram_32_2048, usbram_32_1024)))]
|
||||
let val = u16::from_le_bytes(val);
|
||||
#[cfg(usbram_32_2048)]
|
||||
#[cfg(any(usbram_32_2048, usbram_32_1024))]
|
||||
let val = u32::from_le_bytes(val);
|
||||
USBRAM.mem(self.addr as usize / USBRAM_ALIGN + i).write_value(val);
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [
|
||||
embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] }
|
||||
embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
|
||||
embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
|
||||
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", default-features = false, features = ["defmt"] }
|
||||
|
||||
defmt = "0.3"
|
||||
defmt-rtt = "0.4"
|
||||
@ -21,5 +22,7 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
|
||||
heapless = { version = "0.8", default-features = false }
|
||||
|
||||
micromath = "2.0.0"
|
||||
|
||||
[profile.release]
|
||||
debug = 2
|
||||
|
33
examples/stm32u0/src/bin/adc.rs
Normal file
33
examples/stm32u0/src/bin/adc.rs
Normal file
@ -0,0 +1,33 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_stm32::adc::{Adc, Resolution};
|
||||
use embassy_stm32::Config;
|
||||
use embassy_time::Duration;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[cortex_m_rt::entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
let mut config = Config::default();
|
||||
{
|
||||
use embassy_stm32::rcc::*;
|
||||
config.rcc.mux.adcsel = mux::Adcsel::SYS;
|
||||
}
|
||||
let p = embassy_stm32::init(config);
|
||||
|
||||
let mut adc = Adc::new(p.ADC1);
|
||||
let mut temp = adc.enable_temperature();
|
||||
adc.set_resolution(Resolution::BITS8);
|
||||
let mut channel = p.PC0;
|
||||
|
||||
loop {
|
||||
let v = adc.read(&mut channel);
|
||||
info!("--> {}", v);
|
||||
let v = adc.read(&mut temp);
|
||||
info!("Temp: --> {}", v);
|
||||
embassy_time::block_for(Duration::from_millis(1000));
|
||||
}
|
||||
}
|
35
examples/stm32u0/src/bin/dac.rs
Normal file
35
examples/stm32u0/src/bin/dac.rs
Normal file
@ -0,0 +1,35 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_stm32::dac::{DacCh1, Value};
|
||||
use embassy_stm32::dma::NoDma;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[cortex_m_rt::entry]
|
||||
fn main() -> ! {
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
info!("Hello World!");
|
||||
|
||||
let mut dac = DacCh1::new(p.DAC1, NoDma, p.PA4);
|
||||
|
||||
loop {
|
||||
for v in 0..=255 {
|
||||
dac.set(Value::Bit8(to_sine_wave(v)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use micromath::F32Ext;
|
||||
|
||||
fn to_sine_wave(v: u8) -> u8 {
|
||||
if v >= 128 {
|
||||
// top half
|
||||
let r = 3.14 * ((v - 128) as f32 / 128.0);
|
||||
(r.sin() * 128.0 + 127.0) as u8
|
||||
} else {
|
||||
// bottom half
|
||||
let r = 3.14 + 3.14 * (v as f32 / 128.0);
|
||||
(r.sin() * 128.0 + 127.0) as u8
|
||||
}
|
||||
}
|
41
examples/stm32u0/src/bin/rng.rs
Normal file
41
examples/stm32u0/src/bin/rng.rs
Normal file
@ -0,0 +1,41 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::rng::Rng;
|
||||
use embassy_stm32::{bind_interrupts, peripherals, rng, Config};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
RNG_CRYP => rng::InterruptHandler<peripherals::RNG>;
|
||||
});
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let mut config = Config::default();
|
||||
{
|
||||
use embassy_stm32::rcc::*;
|
||||
config.rcc.hsi = true;
|
||||
config.rcc.pll = Some(Pll {
|
||||
source: PllSource::HSI, // 16 MHz
|
||||
prediv: PllPreDiv::DIV1,
|
||||
mul: PllMul::MUL7, // 16 * 7 = 112 MHz
|
||||
divp: None,
|
||||
divq: None,
|
||||
divr: Some(PllRDiv::DIV2), // 112 / 2 = 56 MHz
|
||||
});
|
||||
config.rcc.sys = Sysclk::PLL1_R;
|
||||
}
|
||||
|
||||
let p = embassy_stm32::init(config);
|
||||
|
||||
info!("Hello World!");
|
||||
|
||||
let mut rng = Rng::new(p.RNG, Irqs);
|
||||
info!("Hello World 2!");
|
||||
|
||||
let mut buf = [0u8; 16];
|
||||
unwrap!(rng.async_fill_bytes(&mut buf).await);
|
||||
info!("random bytes: {:02x}", buf);
|
||||
}
|
31
examples/stm32u0/src/bin/usart.rs
Normal file
31
examples/stm32u0/src/bin/usart.rs
Normal file
@ -0,0 +1,31 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_stm32::dma::NoDma;
|
||||
use embassy_stm32::usart::{Config, Uart};
|
||||
use embassy_stm32::{bind_interrupts, peripherals, usart};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
USART2_LPUART2 => usart::InterruptHandler<peripherals::USART2>;
|
||||
});
|
||||
|
||||
#[cortex_m_rt::entry]
|
||||
fn main() -> ! {
|
||||
info!("Hello World!");
|
||||
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
let config = Config::default();
|
||||
let mut usart = Uart::new(p.USART2, p.PA3, p.PA2, Irqs, NoDma, NoDma, config).unwrap();
|
||||
|
||||
unwrap!(usart.blocking_write(b"Hello Embassy World!\r\n"));
|
||||
info!("wrote Hello, starting echo");
|
||||
|
||||
let mut buf = [0u8; 1];
|
||||
loop {
|
||||
unwrap!(usart.blocking_read(&mut buf));
|
||||
unwrap!(usart.blocking_write(&buf));
|
||||
}
|
||||
}
|
109
examples/stm32u0/src/bin/usb_serial.rs
Normal file
109
examples/stm32u0/src/bin/usb_serial.rs
Normal file
@ -0,0 +1,109 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::{panic, *};
|
||||
use defmt_rtt as _; // global logger
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::usb::{Driver, Instance};
|
||||
use embassy_stm32::{bind_interrupts, peripherals, usb, Config};
|
||||
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
|
||||
use embassy_usb::driver::EndpointError;
|
||||
use embassy_usb::Builder;
|
||||
use futures::future::join;
|
||||
use panic_probe as _;
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
USB_DRD_FS => usb::InterruptHandler<peripherals::USB>;
|
||||
});
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let mut config = Config::default();
|
||||
{
|
||||
use embassy_stm32::rcc::*;
|
||||
config.rcc.hsi = true;
|
||||
config.rcc.pll = Some(Pll {
|
||||
source: PllSource::HSI, // 16 MHz
|
||||
prediv: PllPreDiv::DIV1,
|
||||
mul: PllMul::MUL7,
|
||||
divp: None,
|
||||
divq: None,
|
||||
divr: Some(PllRDiv::DIV2), // 56 MHz
|
||||
});
|
||||
config.rcc.sys = Sysclk::PLL1_R;
|
||||
config.rcc.hsi48 = Some(Hsi48Config { sync_from_usb: true }); // needed for USB
|
||||
config.rcc.mux.clk48sel = mux::Clk48sel::HSI48; // USB uses ICLK
|
||||
}
|
||||
|
||||
let p = embassy_stm32::init(config);
|
||||
|
||||
info!("Hello World!");
|
||||
|
||||
// Create the driver, from the HAL.
|
||||
let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11);
|
||||
|
||||
// Create embassy-usb Config
|
||||
let config = embassy_usb::Config::new(0xc0de, 0xcafe);
|
||||
//config.max_packet_size_0 = 64;
|
||||
|
||||
// Create embassy-usb DeviceBuilder using the driver and config.
|
||||
// It needs some buffers for building the descriptors.
|
||||
let mut config_descriptor = [0; 256];
|
||||
let mut bos_descriptor = [0; 256];
|
||||
let mut control_buf = [0; 7];
|
||||
|
||||
let mut state = State::new();
|
||||
|
||||
let mut builder = Builder::new(
|
||||
driver,
|
||||
config,
|
||||
&mut config_descriptor,
|
||||
&mut bos_descriptor,
|
||||
&mut [], // no msos descriptors
|
||||
&mut control_buf,
|
||||
);
|
||||
|
||||
// Create classes on the builder.
|
||||
let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
|
||||
|
||||
// Build the builder.
|
||||
let mut usb = builder.build();
|
||||
|
||||
// Run the USB device.
|
||||
let usb_fut = usb.run();
|
||||
|
||||
// Do stuff with the class!
|
||||
let echo_fut = async {
|
||||
loop {
|
||||
class.wait_connection().await;
|
||||
info!("Connected");
|
||||
let _ = echo(&mut class).await;
|
||||
info!("Disconnected");
|
||||
}
|
||||
};
|
||||
|
||||
// Run everything concurrently.
|
||||
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
|
||||
join(usb_fut, echo_fut).await;
|
||||
}
|
||||
|
||||
struct Disconnected {}
|
||||
|
||||
impl From<EndpointError> for Disconnected {
|
||||
fn from(val: EndpointError) -> Self {
|
||||
match val {
|
||||
EndpointError::BufferOverflow => panic!("Buffer overflow"),
|
||||
EndpointError::Disabled => Disconnected {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
|
||||
let mut buf = [0; 64];
|
||||
loop {
|
||||
let n = class.read_packet(&mut buf).await?;
|
||||
let data = &buf[..n];
|
||||
info!("data: {:x}", data);
|
||||
class.write_packet(data).await?;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user