WIP: dma
This commit is contained in:
@ -28,6 +28,7 @@ defmt-rtt = "0.2.0"
|
||||
cortex-m = "0.7.1"
|
||||
cortex-m-rt = "0.6.13"
|
||||
embedded-hal = { version = "0.2.4" }
|
||||
panic-probe = "0.1.0"
|
||||
panic-probe = { version = "0.2.0", features= ["print-defmt"] }
|
||||
futures = { version = "0.3.8", default-features = false, features = ["async-await"] }
|
||||
rtt-target = { version = "0.3", features = ["cortex-m"] }
|
||||
heapless = "0.7"
|
@ -1,57 +0,0 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(trait_alias)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::{panic, *};
|
||||
|
||||
use bxcan::filter::Mask32;
|
||||
use cortex_m_rt::entry;
|
||||
use embassy::executor::Executor;
|
||||
use embassy::util::Forever;
|
||||
use embassy_stm32::hal::prelude::*;
|
||||
use embassy_stm32::hal::{can::Can, stm32};
|
||||
use embassy_stm32::{can, interrupt};
|
||||
|
||||
#[embassy::task]
|
||||
async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) {
|
||||
let gpioa = dp.GPIOA.split();
|
||||
|
||||
let rx = gpioa.pa11.into_alternate_af9();
|
||||
let tx = gpioa.pa12.into_alternate_af9();
|
||||
let mut can = bxcan::Can::new(Can::new(dp.CAN1, (tx, rx)));
|
||||
|
||||
// APB1 (PCLK1): 24MHz, Bit rate: 20kBit/s, Sample Point 87.5%
|
||||
// Value was calculated with http://www.bittiming.can-wiki.info/
|
||||
can.modify_config().set_bit_timing(0x001c_004a);
|
||||
// Configure filters so that can frames can be received.
|
||||
can.modify_filters().enable_bank(0, Mask32::accept_all());
|
||||
|
||||
let mut can = can::Can::new(can, interrupt::take!(CAN1_TX), interrupt::take!(CAN1_RX0));
|
||||
|
||||
let _frame = can.receive().await;
|
||||
}
|
||||
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let dp = stm32::Peripherals::take().unwrap();
|
||||
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
|
||||
|
||||
dp.DBGMCU.cr.modify(|_, w| {
|
||||
w.dbg_sleep().set_bit();
|
||||
w.dbg_standby().set_bit();
|
||||
w.dbg_stop().set_bit()
|
||||
});
|
||||
dp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
|
||||
|
||||
let executor = EXECUTOR.put(Executor::new());
|
||||
executor.run(|spawner| {
|
||||
unwrap!(spawner.spawn(run(dp, cp)));
|
||||
});
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(trait_alias)]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::{panic, *};
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use embassy::executor::Executor;
|
||||
use embassy::traits::gpio::*;
|
||||
use embassy::util::Forever;
|
||||
use embassy_stm32::exti::ExtiPin;
|
||||
use embassy_stm32::hal::prelude::*;
|
||||
use embassy_stm32::interrupt;
|
||||
use embassy_stm32::pac as stm32;
|
||||
use futures::pin_mut;
|
||||
|
||||
#[embassy::task]
|
||||
async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) {
|
||||
let gpioa = dp.GPIOA.split();
|
||||
|
||||
let button = gpioa.pa0.into_pull_up_input();
|
||||
let mut syscfg = dp.SYSCFG.constrain();
|
||||
|
||||
let mut pin = ExtiPin::new(button, interrupt::take!(EXTI0), &mut syscfg);
|
||||
|
||||
info!("Starting loop");
|
||||
|
||||
loop {
|
||||
pin.wait_for_rising_edge().await;
|
||||
info!("edge detected!");
|
||||
}
|
||||
}
|
||||
|
||||
static EXECUTOR: Forever<Executor> = Forever::new();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let dp = stm32::Peripherals::take().unwrap();
|
||||
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
|
||||
|
||||
dp.DBGMCU.cr.modify(|_, w| {
|
||||
w.dbg_sleep().set_bit();
|
||||
w.dbg_standby().set_bit();
|
||||
w.dbg_stop().set_bit()
|
||||
});
|
||||
dp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
|
||||
|
||||
let executor = EXECUTOR.put(Executor::new());
|
||||
executor.run(|spawner| {
|
||||
unwrap!(spawner.spawn(run(dp, cp)));
|
||||
});
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use defmt::panic;
|
||||
use embassy;
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_stm32;
|
||||
use embassy_stm32::hal;
|
||||
|
||||
#[embassy::task]
|
||||
async fn run1() {
|
||||
loop {
|
||||
info!("BIG INFREQUENT TICK");
|
||||
Timer::after(Duration::from_ticks(32768 * 2 as u64)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy::task]
|
||||
async fn run2() {
|
||||
loop {
|
||||
info!("tick");
|
||||
Timer::after(Duration::from_ticks(13000 as u64)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy::main(use_hse = 16)]
|
||||
async fn main(spawner: Spawner) {
|
||||
let (dp, clocks) = embassy_stm32::Peripherals::take().unwrap();
|
||||
|
||||
spawner.spawn(run1()).unwrap();
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(trait_alias)]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::{panic, *};
|
||||
|
||||
use cortex_m::singleton;
|
||||
use cortex_m_rt::entry;
|
||||
use embassy::executor::{Executor, Spawner};
|
||||
use embassy::traits::uart::{Read, ReadUntilIdle, Write};
|
||||
use embassy::util::Forever;
|
||||
use embassy_stm32::hal::dma::StreamsTuple;
|
||||
use embassy_stm32::hal::prelude::*;
|
||||
use embassy_stm32::hal::serial::config::Config;
|
||||
use embassy_stm32::interrupt;
|
||||
use embassy_stm32::pac as stm32;
|
||||
use embassy_stm32::serial;
|
||||
use futures::pin_mut;
|
||||
|
||||
#[embassy::main(use_hse = 16, sysclk = 48, pclk1 = 24)]
|
||||
async fn main(spawner: Spawner) {
|
||||
let (dp, clocks) = embassy_stm32::Peripherals::take().unwrap();
|
||||
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
|
||||
|
||||
dp.DBGMCU.cr.modify(|_, w| {
|
||||
w.dbg_sleep().set_bit();
|
||||
w.dbg_standby().set_bit();
|
||||
w.dbg_stop().set_bit()
|
||||
});
|
||||
|
||||
// https://gist.github.com/thalesfragoso/a07340c5df6eee3b04c42fdc69ecdcb1
|
||||
let gpioa = dp.GPIOA.split();
|
||||
let streams = StreamsTuple::new(dp.DMA2);
|
||||
|
||||
let _serial = unsafe {
|
||||
serial::Serial::new(
|
||||
dp.USART1,
|
||||
(streams.7, streams.2),
|
||||
(
|
||||
gpioa.pa9.into_alternate_af7(),
|
||||
gpioa.pa10.into_alternate_af7(),
|
||||
),
|
||||
interrupt::take!(DMA2_STREAM7),
|
||||
interrupt::take!(DMA2_STREAM2),
|
||||
interrupt::take!(USART1),
|
||||
Config::default().baudrate(9600.bps()),
|
||||
clocks,
|
||||
)
|
||||
};
|
||||
|
||||
let streams = StreamsTuple::new(dp.DMA1);
|
||||
|
||||
let mut serial = unsafe {
|
||||
serial::Serial::new(
|
||||
dp.USART2,
|
||||
(streams.6, streams.5),
|
||||
(
|
||||
gpioa.pa2.into_alternate_af7(),
|
||||
gpioa.pa3.into_alternate_af7(),
|
||||
),
|
||||
interrupt::take!(DMA1_STREAM6),
|
||||
interrupt::take!(DMA1_STREAM5),
|
||||
interrupt::take!(USART2),
|
||||
Config::default().baudrate(9600.bps()),
|
||||
clocks,
|
||||
)
|
||||
};
|
||||
pin_mut!(serial);
|
||||
|
||||
let buf = singleton!(: [u8; 30] = [0; 30]).unwrap();
|
||||
|
||||
buf[5] = 0x01;
|
||||
serial.as_mut().write(buf).await.unwrap();
|
||||
serial.as_mut().read_until_idle(buf).await.unwrap();
|
||||
}
|
89
embassy-stm32-examples/src/bin/usart_dma.rs
Normal file
89
embassy-stm32-examples/src/bin/usart_dma.rs
Normal file
@ -0,0 +1,89 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(trait_alias)]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use core::fmt::Write;
|
||||
use cortex_m_rt::entry;
|
||||
use embassy::executor::Executor;
|
||||
use embassy::time::Clock;
|
||||
use embassy::util::Forever;
|
||||
use embassy_stm32::usart::{Config, Uart};
|
||||
use example_common::*;
|
||||
use heapless::String;
|
||||
use stm32f4::stm32f429 as pac;
|
||||
|
||||
#[embassy::task]
|
||||
async fn main_task() {
|
||||
let mut p = embassy_stm32::init(Default::default());
|
||||
|
||||
let config = Config::default();
|
||||
let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, config, 16_000_000);
|
||||
|
||||
for n in 0.. {
|
||||
let mut s: String<128> = String::new();
|
||||
core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap();
|
||||
|
||||
usart
|
||||
.write_dma(&mut p.DMA1_CH3, s.as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
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!");
|
||||
|
||||
let pp = pac::Peripherals::take().unwrap();
|
||||
|
||||
pp.DBGMCU.cr.modify(|_, w| {
|
||||
w.dbg_sleep().set_bit();
|
||||
w.dbg_standby().set_bit();
|
||||
w.dbg_stop().set_bit()
|
||||
});
|
||||
pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
|
||||
|
||||
pp.RCC.ahb1enr.modify(|_, w| {
|
||||
w.gpioaen().enabled();
|
||||
w.gpioben().enabled();
|
||||
w.gpiocen().enabled();
|
||||
w.gpioden().enabled();
|
||||
w.gpioeen().enabled();
|
||||
w.gpiofen().enabled();
|
||||
w.dma1en().enabled();
|
||||
w.dma2en().enabled();
|
||||
w
|
||||
});
|
||||
pp.RCC.apb2enr.modify(|_, w| {
|
||||
w.syscfgen().enabled();
|
||||
w
|
||||
});
|
||||
pp.RCC.apb1enr.modify(|_, w| {
|
||||
w.usart3en().enabled();
|
||||
w
|
||||
});
|
||||
|
||||
unsafe { embassy::time::set_clock(&ZeroClock) };
|
||||
|
||||
let executor = EXECUTOR.put(Executor::new());
|
||||
|
||||
executor.run(|spawner| {
|
||||
unwrap!(spawner.spawn(main_task()));
|
||||
})
|
||||
}
|
@ -1,117 +0,0 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(min_type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_bindings)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::panic;
|
||||
use embassy::executor::{Executor, Spawner};
|
||||
use embassy::interrupt::InterruptExt;
|
||||
use embassy::io::{AsyncBufReadExt, AsyncWriteExt};
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy::util::Forever;
|
||||
use embassy_extras::usb::usb_serial::UsbSerial;
|
||||
use embassy_extras::usb::Usb;
|
||||
use embassy_stm32::hal::otg_fs::{UsbBus, USB};
|
||||
use embassy_stm32::hal::prelude::*;
|
||||
use embassy_stm32::{interrupt, pac, rtc};
|
||||
use futures::future::{select, Either};
|
||||
use futures::pin_mut;
|
||||
use usb_device::bus::UsbBusAllocator;
|
||||
use usb_device::prelude::*;
|
||||
|
||||
#[embassy::task]
|
||||
async fn run1(bus: &'static mut UsbBusAllocator<UsbBus<USB>>) {
|
||||
info!("Async task");
|
||||
|
||||
let mut read_buf = [0u8; 128];
|
||||
let mut write_buf = [0u8; 128];
|
||||
let serial = UsbSerial::new(bus, &mut read_buf, &mut write_buf);
|
||||
|
||||
let device = UsbDeviceBuilder::new(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::Level3);
|
||||
|
||||
let usb = Usb::new(device, serial, irq);
|
||||
pin_mut!(usb);
|
||||
usb.as_mut().start();
|
||||
|
||||
let (mut read_interface, mut write_interface) = usb.as_ref().take_serial_0();
|
||||
|
||||
let mut buf = [0u8; 64];
|
||||
loop {
|
||||
let mut n = 0;
|
||||
let left = {
|
||||
let recv_fut = async {
|
||||
loop {
|
||||
let byte = unwrap!(read_interface.read_byte().await);
|
||||
unwrap!(write_interface.write_byte(byte).await);
|
||||
buf[n] = byte;
|
||||
|
||||
n += 1;
|
||||
if byte == b'\n' || byte == b'\r' || n == buf.len() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
pin_mut!(recv_fut);
|
||||
|
||||
let timeout = Timer::after(Duration::from_ticks(32768 * 10));
|
||||
|
||||
match select(recv_fut, timeout).await {
|
||||
Either::Left(_) => true,
|
||||
Either::Right(_) => false,
|
||||
}
|
||||
};
|
||||
|
||||
if left {
|
||||
for c in buf[..n].iter_mut() {
|
||||
if 0x61 <= *c && *c <= 0x7a {
|
||||
*c &= !0x20;
|
||||
}
|
||||
}
|
||||
unwrap!(write_interface.write_byte(b'\n').await);
|
||||
unwrap!(write_interface.write_all(&buf[..n]).await);
|
||||
unwrap!(write_interface.write_byte(b'\n').await);
|
||||
} else {
|
||||
unwrap!(write_interface.write_all(b"\r\nSend something\r\n").await);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static USB_BUS: Forever<UsbBusAllocator<UsbBus<USB>>> = Forever::new();
|
||||
|
||||
#[embassy::main(use_hse = 25, sysclk = 48, require_pll48clk)]
|
||||
async fn main(spawner: Spawner) -> ! {
|
||||
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
|
||||
|
||||
info!("Hello World!");
|
||||
|
||||
let (p, clocks) = embassy_stm32::Peripherals::take().unwrap();
|
||||
|
||||
let gpioa = p.GPIOA.split();
|
||||
let usb = USB {
|
||||
usb_global: p.OTG_FS_GLOBAL,
|
||||
usb_device: p.OTG_FS_DEVICE,
|
||||
usb_pwrclk: p.OTG_FS_PWRCLK,
|
||||
pin_dm: gpioa.pa11.into_alternate_af10(),
|
||||
pin_dp: gpioa.pa12.into_alternate_af10(),
|
||||
hclk: clocks.hclk(),
|
||||
};
|
||||
// Rust analyzer isn't recognizing the static ref magic `cortex-m` does
|
||||
#[allow(unused_unsafe)]
|
||||
let usb_bus = USB_BUS.put(UsbBus::new(usb, unsafe { &mut EP_MEMORY }));
|
||||
|
||||
spawner.spawn(run1(usb_bus)).unwrap();
|
||||
}
|
Reference in New Issue
Block a user