354 lines
11 KiB
Rust
354 lines
11 KiB
Rust
#![no_main]
|
|
#![no_std]
|
|
#![feature(type_alias_impl_trait)]
|
|
use defmt_rtt as _; // global logger
|
|
|
|
use panic_probe as _;
|
|
use stm32f4xx_hal as _;
|
|
|
|
// same panicking *behavior* as `panic-probe` but doesn't print a panic message
|
|
// this prevents the panic message being printed *twice* when `defmt::panic` is invoked
|
|
#[defmt::panic_handler]
|
|
fn panic() -> ! {
|
|
cortex_m::asm::udf()
|
|
}
|
|
|
|
use rtic::app;
|
|
|
|
mod filters;
|
|
mod si5153;
|
|
mod ui;
|
|
|
|
#[app(device = stm32f4xx_hal::pac, peripherals = true, dispatchers = [SPI3])]
|
|
mod app {
|
|
|
|
use core::mem;
|
|
|
|
use num::Complex;
|
|
use stm32f4xx_hal::{
|
|
adc::{
|
|
self,
|
|
config::{AdcConfig, Dma, ExternalTrigger, SampleTime, Scan, Sequence, TriggerMode},
|
|
Adc,
|
|
},
|
|
dma::{config::DmaConfig, PeripheralToMemory, Stream0, Stream7, StreamsTuple, Transfer},
|
|
gpio::{self, gpioa, gpioc, Analog, Output, PushPull},
|
|
i2c::{self, I2c},
|
|
pac::{ADC1, DMA1, DMA2, I2C1, TIM4},
|
|
prelude::*,
|
|
timer::{Channel, Channel1, Channel3, ChannelBuilder, PwmHz, CCR, CCR3},
|
|
};
|
|
|
|
use cortex_m::singleton;
|
|
|
|
use crate::filters;
|
|
use crate::si5153;
|
|
use crate::ui::UI;
|
|
|
|
type AudioPwm = PwmHz<TIM4, ChannelBuilder<TIM4, 2>>;
|
|
|
|
#[local]
|
|
struct Local {
|
|
board_led: gpioc::PC13<Output<PushPull>>,
|
|
rx_en: gpioa::PA7<Output<PushPull>>,
|
|
//mic_in: gpio::Pin<'A', 4, Analog>,
|
|
i_in: gpio::Pin<'A', 2, Analog>,
|
|
q_in: gpio::Pin<'A', 3, Analog>,
|
|
i_offset: f32,
|
|
q_offset: f32,
|
|
audio_pwm: AudioPwm,
|
|
usb_filter: filters::FirFilter<63>,
|
|
adc_transfer:
|
|
Transfer<Stream0<DMA2>, 0, Adc<ADC1>, PeripheralToMemory, &'static mut [u16; 256]>,
|
|
iq_buffer: Option<&'static mut [u16; 256]>,
|
|
|
|
pwm_transfer: Transfer<
|
|
Stream7<DMA1>,
|
|
2,
|
|
CCR<stm32f4xx_hal::pac::TIM4, 2>,
|
|
stm32f4xx_hal::dma::MemoryToPeripheral,
|
|
&'static mut [u16; 128],
|
|
>,
|
|
audio_max_duty: u16,
|
|
|
|
ui: UI,
|
|
}
|
|
|
|
#[shared]
|
|
struct Shared {
|
|
pll: si5153::Si5153<I2c<I2C1>>,
|
|
i2c: I2c<I2C1>,
|
|
|
|
audio_buffer: Option<&'static mut [u16; 128]>,
|
|
}
|
|
|
|
#[init]
|
|
fn init(cx: init::Context) -> (Shared, Local) {
|
|
let rcc = cx.device.RCC.constrain();
|
|
|
|
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
|
|
// `clocks`
|
|
let clocks = rcc
|
|
.cfgr
|
|
.use_hse(25.MHz())
|
|
.require_pll48clk()
|
|
.sysclk(84.MHz())
|
|
.hclk(84.MHz())
|
|
.pclk1(42.MHz())
|
|
.pclk2(84.MHz())
|
|
.freeze();
|
|
|
|
defmt::info!("Clock Setup done");
|
|
|
|
// Acquire the GPIOC peripheral
|
|
let gpioa = cx.device.GPIOA.split();
|
|
let gpiob = cx.device.GPIOB.split();
|
|
let gpioc = cx.device.GPIOC.split();
|
|
|
|
let board_led = gpioc.pc13.into_push_pull_output();
|
|
|
|
/*
|
|
let enc_a = gpioa.pa0.into_input();
|
|
let enc_b = gpioa.pa1.into_input();
|
|
|
|
let encoder = qei::Qei::new(cx.device.TIM2, (enc_a, enc_b));
|
|
|
|
defmt::info!("Encoder Setup done");
|
|
*/
|
|
|
|
let ui = UI::setup(
|
|
&clocks,
|
|
gpioa.pa0.into_input(),
|
|
gpioa.pa1.into_input(),
|
|
cx.device.TIM2,
|
|
gpioa.pa5.into_input(),
|
|
gpioa.pa11.into_push_pull_output(),
|
|
gpioa.pa12.into_push_pull_output(),
|
|
gpioa.pa10.into_push_pull_output(),
|
|
gpioa.pa15.into_push_pull_output(),
|
|
gpiob.pb3.into_alternate(),
|
|
gpiob.pb5.into_alternate(),
|
|
cx.device.SPI1,
|
|
cx.core.SYST,
|
|
);
|
|
|
|
let scl = gpiob.pb6.into_alternate_open_drain();
|
|
let sda = gpiob.pb7.into_alternate_open_drain();
|
|
let mut i2c = i2c::I2c::new(
|
|
cx.device.I2C1,
|
|
(scl, sda),
|
|
i2c::Mode::Standard {
|
|
frequency: 400.kHz(),
|
|
},
|
|
&clocks,
|
|
);
|
|
|
|
let mut pll = si5153::Si5153::new(&i2c);
|
|
|
|
/*
|
|
let phase = 100;
|
|
let freq = 7_100_000;
|
|
*/
|
|
|
|
let phase = 100;
|
|
let freq = 7_100_000;
|
|
|
|
pll.init(&mut i2c, 25_000_000, freq * phase, freq * phase);
|
|
pll.set_ms_source(&mut i2c, si5153::Multisynth::MS0, si5153::PLL::A);
|
|
pll.set_ms_source(&mut i2c, si5153::Multisynth::MS1, si5153::PLL::A);
|
|
pll.set_ms_source(&mut i2c, si5153::Multisynth::MS2, si5153::PLL::B);
|
|
|
|
pll.set_ms_freq(&mut i2c, si5153::Multisynth::MS0, freq);
|
|
pll.set_ms_phase(&mut i2c, si5153::Multisynth::MS0, 0);
|
|
pll.enable_ms_output(&mut i2c, si5153::Multisynth::MS0);
|
|
|
|
pll.set_ms_freq(&mut i2c, si5153::Multisynth::MS1, freq);
|
|
pll.set_ms_phase(&mut i2c, si5153::Multisynth::MS1, 100);
|
|
pll.enable_ms_output(&mut i2c, si5153::Multisynth::MS1);
|
|
|
|
defmt::info!("PLL chip setup done");
|
|
|
|
let i_in = gpioa.pa2.into_analog();
|
|
let q_in = gpioa.pa3.into_analog();
|
|
|
|
let adc_config = AdcConfig::default()
|
|
.dma(Dma::Continuous)
|
|
.external_trigger(TriggerMode::RisingEdge, ExternalTrigger::Tim_1_cc_1)
|
|
.scan(Scan::Enabled);
|
|
let mut adc1 = adc::Adc::adc1(cx.device.ADC1, true, adc_config);
|
|
adc1.configure_channel(&i_in, Sequence::One, SampleTime::Cycles_480);
|
|
adc1.configure_channel(&q_in, Sequence::Two, SampleTime::Cycles_480);
|
|
|
|
let pa8 = Channel1::new(gpioa.pa8);
|
|
let mut sample_pwm = cx.device.TIM1.pwm_hz(pa8, 8.kHz(), &clocks);
|
|
let max_duty = sample_pwm.get_max_duty();
|
|
sample_pwm.set_duty(Channel::C1, max_duty / 2);
|
|
sample_pwm.enable(Channel::C1);
|
|
|
|
//let mic_in = gpioa.pa4.into_analog()
|
|
|
|
defmt::info!("ADC Setup done");
|
|
|
|
let iq_buff1 = singleton!(: [u16; 256] = [0; 256]).unwrap();
|
|
let iq_buff2 = singleton!(: [u16; 256] = [0; 256]).unwrap();
|
|
|
|
let dma2 = StreamsTuple::new(cx.device.DMA2);
|
|
|
|
let config = DmaConfig::default()
|
|
.transfer_complete_interrupt(true)
|
|
.memory_increment(true)
|
|
.double_buffer(false);
|
|
|
|
let mut adc_transfer =
|
|
Transfer::init_peripheral_to_memory(dma2.0, adc1, iq_buff1, None, config);
|
|
|
|
adc_transfer.start(|_| {});
|
|
|
|
defmt::info!("ADC DMA Setup done");
|
|
|
|
let ccr3_tim4: CCR3<TIM4> = unsafe { mem::transmute_copy(&cx.device.TIM4) };
|
|
|
|
let audio_out = Channel3::new(gpiob.pb8);
|
|
let mut audio_pwm = cx.device.TIM4.pwm_hz(audio_out, 8.kHz(), &clocks);
|
|
audio_pwm.enable(Channel::C3);
|
|
audio_pwm.set_duty(Channel::C3, audio_pwm.get_max_duty() / 2);
|
|
let audio_max_duty = audio_pwm.get_max_duty();
|
|
|
|
defmt::info!("Max duty: {}", audio_pwm.get_max_duty());
|
|
|
|
unsafe {
|
|
(*TIM4::ptr()).dier.modify(|_, w| {
|
|
w.tde().set_bit(); // enable DMA trigger
|
|
w.cc3de().set_bit(); // dma on comperator match
|
|
w
|
|
});
|
|
};
|
|
|
|
let audio_buff1 = singleton!(: [u16; 128] = [100; 128]).unwrap();
|
|
let audio_buff2 = singleton!(: [u16; 128] = [100; 128]).unwrap();
|
|
|
|
let dma1 = StreamsTuple::new(cx.device.DMA1);
|
|
|
|
let config = DmaConfig::default()
|
|
.transfer_complete_interrupt(true)
|
|
.memory_increment(true)
|
|
.double_buffer(false);
|
|
|
|
let mut pwm_transfer =
|
|
Transfer::init_memory_to_peripheral(dma1.7, ccr3_tim4, audio_buff1, None, config);
|
|
|
|
pwm_transfer.start(|_| {});
|
|
|
|
defmt::info!("PWM DMA Setup done");
|
|
|
|
let mut rx_en = gpioa.pa7.into_push_pull_output();
|
|
rx_en.set_high();
|
|
|
|
let bias_pin = Channel1::new(gpioa.pa6);
|
|
let mut bias_pwm = cx.device.TIM3.pwm_hz(bias_pin, 64.kHz(), &clocks);
|
|
bias_pwm.enable(Channel::C1);
|
|
bias_pwm.set_duty(Channel::C1, 0);
|
|
|
|
(
|
|
Shared {
|
|
i2c,
|
|
pll,
|
|
audio_buffer: Some(audio_buff2),
|
|
},
|
|
Local {
|
|
board_led,
|
|
rx_en,
|
|
//mic_in,
|
|
i_in,
|
|
q_in,
|
|
i_offset: 2048.0,
|
|
q_offset: 2048.0,
|
|
audio_pwm,
|
|
usb_filter: filters::usb_firfilter(),
|
|
adc_transfer,
|
|
iq_buffer: Some(iq_buff2),
|
|
|
|
pwm_transfer,
|
|
audio_max_duty,
|
|
|
|
ui,
|
|
},
|
|
)
|
|
}
|
|
|
|
#[task(priority = 0, local = [ui], shared=[pll, i2c])]
|
|
async fn update_display(cx: update_display::Context, row: [Complex<f32>; 128]) {
|
|
let pll = cx.shared.pll;
|
|
let i2c = cx.shared.i2c;
|
|
let ui = cx.local.ui;
|
|
|
|
(pll, i2c).lock(|pll, i2c| {
|
|
ui.update_display(row, pll, i2c);
|
|
});
|
|
}
|
|
|
|
#[task(binds = DMA2_STREAM0, local = [adc_transfer, iq_buffer, board_led, i_offset, q_offset, usb_filter, audio_max_duty], shared = [audio_buffer])]
|
|
fn dma2_stream0(mut cx: dma2_stream0::Context) {
|
|
let (buffer, _) = cx
|
|
.local
|
|
.adc_transfer
|
|
.next_transfer(cx.local.iq_buffer.take().unwrap())
|
|
.unwrap();
|
|
|
|
defmt::info!("ADC transfer complete");
|
|
cx.local.board_led.toggle();
|
|
|
|
let mut samples = [Complex::<f32>::default(); 128];
|
|
for idx in 0..buffer.len() / 2 {
|
|
let i_raw = buffer[idx * 2];
|
|
let q_raw = buffer[idx * 2 + 1];
|
|
|
|
*cx.local.i_offset = 0.95 * *cx.local.i_offset + 0.05 * (i_raw as f32);
|
|
*cx.local.q_offset = 0.95 * *cx.local.q_offset + 0.05 * (q_raw as f32);
|
|
|
|
let i_sample = (i_raw as f32) - *cx.local.i_offset;
|
|
let q_sample = (q_raw as f32) - *cx.local.q_offset;
|
|
|
|
samples[idx] = Complex::new(i_sample as f32 / 4096.0, q_sample as f32 / 4096.0);
|
|
}
|
|
|
|
let mut fft_input = [Complex::<f32>::default(); 128];
|
|
|
|
let usb_filter = cx.local.usb_filter;
|
|
let audio_max_duty = *cx.local.audio_max_duty;
|
|
|
|
cx.shared.audio_buffer.lock(|buffer| {
|
|
let audio_buffer = buffer.take().unwrap();
|
|
for idx in 0..samples.len() {
|
|
//let filtered = usb_filter.compute(samples[idx]);
|
|
let filtered = usb_filter.compute(Complex::new(samples[idx].im, samples[idx].re));
|
|
fft_input[idx] = samples[idx];
|
|
|
|
audio_buffer[idx] = ((filtered.re * (audio_max_duty as f32)) * 3.0f32) as u16;
|
|
}
|
|
|
|
*buffer = Some(audio_buffer);
|
|
});
|
|
|
|
match update_display::spawn(fft_input) {
|
|
Ok(_) => {}
|
|
Err(_) => defmt::warn!("Skipping display update."),
|
|
}
|
|
*cx.local.iq_buffer = Some(buffer);
|
|
}
|
|
|
|
#[task(binds = DMA1_STREAM7, local = [pwm_transfer], shared = [audio_buffer])]
|
|
fn dma1_stream7(mut cx: dma1_stream7::Context) {
|
|
let pwm_transfer = cx.local.pwm_transfer;
|
|
cx.shared.audio_buffer.lock(|next_buffer| {
|
|
let (last_buffer, _) = pwm_transfer
|
|
.next_transfer(next_buffer.take().unwrap())
|
|
.unwrap();
|
|
|
|
defmt::info!("PWM transfer complete");
|
|
|
|
*next_buffer = Some(last_buffer);
|
|
});
|
|
}
|
|
}
|