Skip to content

Commit

Permalink
feat: add SPI shift register for muxing muxer ctrl lines
Browse files Browse the repository at this point in the history
  • Loading branch information
ingobecker committed Apr 10, 2024
1 parent a1c05e8 commit e978d66
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 42 deletions.
3 changes: 1 addition & 2 deletions src/bin/stm32-encoder-async-poc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ async fn main(_spawner: Spawner) {
let mut input = InputType::Encoder(encoder);
let mut device = Device::new();

let inputs = device.inputs();
let mut b = Stm32Backend::new(inputs).await;
let mut b = Stm32Backend::new().await;

let mut outputs: Vec<OutputType, 2> = Vec::new();
outputs.push(OutputType::StdOut(StdOut {}));
Expand Down
3 changes: 1 addition & 2 deletions src/bin/stm32-encoder-poc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ async fn main(_spawner: Spawner) -> ! {
// setup
device.add_input(input);

let inputs = device.inputs();
let mut b = Stm32Backend::new(inputs).await;
let mut b = Stm32Backend::new().await;
device.init_inputs(&mut b);

// operation
Expand Down
5 changes: 2 additions & 3 deletions src/bin/stm32-usb-midi-poc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ 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).await;
let mut b = Stm32Backend::new().await;
info!("Stm32Backend setup completed!");

device.init_inputs(&mut b);
Expand Down
6 changes: 2 additions & 4 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl Device {
_ => (),
};
}
backend.rewind();
}

pub async fn update(&mut self, backend: &mut impl Backend) {
Expand All @@ -54,6 +55,7 @@ impl Device {
self.updated.push(idx);
}
}
backend.rewind();
}

pub async fn run_handler(&mut self, outputs: &[OutputType]) {
Expand All @@ -78,8 +80,4 @@ impl Device {
}
self.updated.clear();
}

pub fn inputs(&self) -> u8 {
self.inputs.len() as u8
}
}
1 change: 1 addition & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ pub enum InputType {
pub trait Backend {
async fn read_adc(&mut self) -> u16;
fn read_input(&mut self) -> bool;
fn rewind(&mut self);
}
2 changes: 2 additions & 0 deletions src/ui/backend/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl Backend for InMemoryBackend {
}
}
}

fn rewind(&mut self) {}
}

impl InMemoryBackend {
Expand Down
57 changes: 26 additions & 31 deletions src/ui/backend/stm32.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use embassy_executor::Spawner;
use embassy_stm32::adc::{Adc, AdcPin, InterruptHandler};
use embassy_stm32::dma::NoDma;
use embassy_stm32::gpio::{Flex, Input, Level, Output, Pull, Speed};
use embassy_stm32::spi::{Config as SpiConfig, Spi};
use embassy_stm32::time::Hertz;
use embassy_stm32::usb::Driver;
use embassy_stm32::{adc, bind_interrupts, peripherals, usb, Config, Peripheral};
Expand All @@ -9,6 +11,7 @@ use embassy_usb::class::midi::{MidiClass, Sender};
use embassy_usb::driver::EndpointError;
use embassy_usb::Builder;

use defmt::info;
use static_cell::StaticCell;

use crate::output::CHANNEL;
Expand All @@ -26,7 +29,6 @@ type USBDriver = Driver<'static, peripherals::USB>;
type USBMidiClass = MidiClass<'static, USBDriver>;

pub struct Stm32Backend {
inputs: u8,
addr: u8,
out_a: Output<'static>,
out_b: Output<'static>,
Expand All @@ -36,22 +38,30 @@ pub struct Stm32Backend {
adc_pin: peripherals::PA4,
usb_builder: Option<Builder<'static, USBDriver>>,
usb_midi_class: Option<USBMidiClass>,
spi: Spi<'static, peripherals::SPI1, NoDma, NoDma>,
rclk: Output<'static>,
}

impl Backend for Stm32Backend {
async fn read_adc(&mut self) -> u16 {
self.set_addr();
self.flex_com.set_as_input(Pull::None);
let v = self.adc.read(&mut self.adc_pin).await;
self.next();
v
}

fn read_input(&mut self) -> bool {
self.set_addr();
self.flex_com.set_as_input(Pull::Up);
let level = self.flex_com.is_high();
self.next();
level
}

fn rewind(&mut self) {
self.addr = 0;
}
}

#[embassy_executor::task(pool_size = 1)]
Expand All @@ -74,7 +84,7 @@ pub async fn midi_task(mut class: USBMidiClass) -> ! {
}

impl Stm32Backend {
pub async fn new(inputs: u8) -> Self {
pub async fn new() -> Self {
let mut config = Config::default();
{
use embassy_stm32::rcc::*;
Expand Down Expand Up @@ -141,8 +151,13 @@ impl Stm32Backend {

let mut class = MidiClass::new(&mut builder, 1, 1, 64);

// default = MODE_0(CPOL = 0, CPHA = 0)
let mut spi_config = SpiConfig::default();
spi_config.frequency = Hertz(1_000_000);

let mut spi = Spi::new_txonly(p.SPI1, p.PA5, p.PA7, NoDma, NoDma, spi_config);

Self {
inputs: inputs,
addr: 0,
out_a: Output::new(p.PA0, Level::Low, Speed::Low),
out_b: Output::new(p.PA1, Level::Low, Speed::Low),
Expand All @@ -152,6 +167,8 @@ impl Stm32Backend {
adc_pin: p.PA4,
usb_builder: Some(builder),
usb_midi_class: Some(class),
spi: spi,
rclk: Output::new(p.PB0, Level::Low, Speed::Low),
}
}

Expand All @@ -168,36 +185,14 @@ impl Stm32Backend {
}

fn next(&mut self) {
if self.addr == self.inputs {
self.addr = 0;
} else {
self.addr = self.addr + 1;
}
self.set_addr();
self.addr = self.addr + 1;
}

fn set_addr(&mut self) {
let mut addr_tmp = self.addr;

if (addr_tmp & 1) == 1 {
self.out_a.set_high();
} else {
self.out_a.set_low();
}
addr_tmp >>= 1;

if (addr_tmp & 1) == 1 {
self.out_b.set_high();
} else {
self.out_b.set_low();
}
addr_tmp >>= 1;

if (addr_tmp & 1) == 1 {
self.out_c.set_high();
} else {
self.out_c.set_low();
}
addr_tmp >>= 1;
let mut buf = [0u8; 1];
buf[0] = self.addr;
self.spi.blocking_write(&buf);
self.rclk.set_high();
self.rclk.set_low();
}
}

0 comments on commit e978d66

Please sign in to comment.