Skip to content

Commit

Permalink
refactor: move STM32 setup to backend and tasks futures to task funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
ingobecker committed Apr 4, 2024
1 parent 567eeb3 commit a1c05e8
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 160 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ nb = { version = "1.0.0" }

heapless = { version = "0.8", default-features = false, features = ["serde"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
static_cell = { version = "2.0.0", default-features = false, features = [] }

[patch.crates-io]
embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", rev = "ee1aa80e3063de35a6f68c918a18b506d7f49539" }
Expand Down Expand Up @@ -65,4 +66,3 @@ opt-level = "s"

[profile.release]
debug = 2

6 changes: 1 addition & 5 deletions src/bin/stm32-encoder-async-poc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use futures::future::join;

use defmt::info;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Input as GpioInput, Level, Pull, Speed};
use embassy_stm32::Config;
use embassy_time::{with_timeout, Duration, Timer};
use heapless::Vec;
Expand All @@ -22,9 +21,6 @@ use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let config = Config::default();
let p = embassy_stm32::init(config);

let mut encoder = Encoder::new();
let mut handler = EncoderHandler::MidiAbs(MidiAbs {
channel: 0,
Expand All @@ -37,7 +33,7 @@ async fn main(_spawner: Spawner) {
let mut device = Device::new();

let inputs = device.inputs();
let mut b = Stm32Backend::new(inputs, p.PA0, p.PA1, p.PA2, p.PA3, p.ADC1, p.PA4);
let mut b = Stm32Backend::new(inputs).await;

let mut outputs: Vec<OutputType, 2> = Vec::new();
outputs.push(OutputType::StdOut(StdOut {}));
Expand Down
5 changes: 1 addition & 4 deletions src/bin/stm32-encoder-poc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) -> ! {
let config = Config::default();
let p = embassy_stm32::init(config);

let mut encoder = Encoder::new();
let mut handler = EncoderHandler::MidiAbs(MidiAbs {
channel: 0,
Expand All @@ -41,7 +38,7 @@ async fn main(_spawner: Spawner) -> ! {
device.add_input(input);

let inputs = device.inputs();
let mut b = Stm32Backend::new(inputs, p.PA0, p.PA1, p.PA2, p.PA3, p.ADC1, p.PA4);
let mut b = Stm32Backend::new(inputs).await;
device.init_inputs(&mut b);

// operation
Expand Down
2 changes: 1 addition & 1 deletion src/bin/stm32-poc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) -> ! {
let config = Config::default();
let _p = embassy_stm32::init(config);
let _pp = embassy_stm32::init(config);

reset_ctrl::run();
loop {
Expand Down
136 changes: 7 additions & 129 deletions src/bin/stm32-usb-midi-poc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@

use defmt::{panic, *};
use embassy_executor::Spawner;
use embassy_futures::join::join3;
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::time::Hertz;
use embassy_stm32::usb::{Driver, Instance};
use embassy_stm32::{bind_interrupts, peripherals, usb, Config};
use embassy_time::Timer;
use embassy_usb::class::midi::MidiClass;
use embassy_usb::driver::EndpointError;
use embassy_usb::Builder;

use heapless::Vec;

Expand All @@ -25,87 +17,8 @@ use reset_ctrl::ui::{Input, InputType};

use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
USB_LP_CAN1_RX0 => usb::InterruptHandler<peripherals::USB>;
});

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut config = Config::default();
{
use embassy_stm32::rcc::*;
config.rcc.hse = Some(Hse {
freq: Hertz(8_000_000),
// Oscillator for bluepill, Bypass for nucleos.
mode: HseMode::Oscillator,
});
config.rcc.pll = Some(Pll {
src: PllSource::HSE,
prediv: PllPreDiv::DIV1,
mul: PllMul::MUL9,
});
config.rcc.sys = Sysclk::PLL1_P;
config.rcc.ahb_pre = AHBPrescaler::DIV1;
config.rcc.apb1_pre = APBPrescaler::DIV2;
config.rcc.apb2_pre = APBPrescaler::DIV1;
}
let mut p = embassy_stm32::init(config);

info!("Hello World!");

{
// BluePill board has a pull-up resistor on the D+ line.
// Pull the D+ pin down to send a RESET condition to the USB bus.
// This forced reset is needed only for development, without it host
// will not reset your device when you upload new firmware.
let _dp = Output::new(&mut p.PA12, Level::Low, Speed::Low);
Timer::after_millis(10).await;
}

// Create the driver, from the HAL.
let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11);

// Create embassy-usb Config
let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
config.manufacturer = Some("reset-ctrl");
config.product = Some("reset-ctrl PoC");
config.serial_number = Some("12345678");
config.max_power = 100;
config.max_packet_size_0 = 64;

config.device_class = 0xEF;
config.device_sub_class = 0x02;
config.device_protocol = 0x01;
config.composite_with_iads = true;

// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 64];

let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut [], // no msos descriptors
&mut control_buf,
);

// Create classes on the builder.
let mut class = MidiClass::new(&mut builder, 1, 1, 64);

// Build the builder.
let mut usb = builder.build();

// Run the USB device.
let usb_fut = usb.run();

info!("USB setup completed!");

async fn main(spawner: Spawner) {
// setup device
// encoder
let mut encoder = Encoder::new();
Expand Down Expand Up @@ -133,17 +46,20 @@ async fn main(_spawner: Spawner) {
outputs.push(OutputType::UsbOut(UsbOut {}));

// setup
device.add_input(input);
//device.add_input(input);
device.add_input(pot_input);

info!("Setting up Stm32Backend...");
// reset_ctrl setup
let inputs = device.inputs();
let mut b = Stm32Backend::new(inputs, p.PA0, p.PA1, p.PA2, p.PA3, p.ADC1, p.PA4);
let mut b = Stm32Backend::new(inputs).await;
info!("Stm32Backend setup completed!");

device.init_inputs(&mut b);

b.spawn_usb(spawner);
b.spawn_midi(spawner);

// operation
info!("Starting update loop");
let reset_ctrl_fut = async {
Expand All @@ -155,43 +71,5 @@ async fn main(_spawner: Spawner) {
}
};

// 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.
join3(usb_fut, echo_fut, reset_ctrl_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 MidiClass<'d, Driver<'d, T>>,
) -> Result<(), Disconnected> {
let mut buf = [0; 64];
loop {
if let data = CHANNEL.receive().await {
buf[0] = data[0] >> 4;
buf[1..4].copy_from_slice(&data);
//info!("usb CHANNEL received: {:x}", data);
//info!("usb class.write_packet {:x}", &buf[..4]);
class.write_packet(&buf[..4]).await?;
}
}
reset_ctrl_fut.await;
}
Loading

0 comments on commit a1c05e8

Please sign in to comment.