1
0
Fork 0

squash (most) warnings, cleanup

This commit is contained in:
topquark12 2022-10-22 15:49:01 +08:00
parent dbb1b03638
commit 6867a293e5
8 changed files with 151 additions and 196 deletions

View File

@ -1,29 +1,29 @@
use stm32f4xx_hal::{ use super::{gpio, log_setup, sys_timer, usb};
watchdog::IndependentWatchdog, use crate::laser_diode::current_sources::*;
rcc::RccExt,
pac::{CorePeripherals, Peripherals},
time::MegaHertz
};
use fugit::ExtU32; use fugit::ExtU32;
use log::info; use log::info;
use super::{log_setup, sys_timer, usb, gpio}; use stm32f4xx_hal::{
use crate::laser_diode::current_sources::*; pac::{CorePeripherals, Peripherals},
rcc::RccExt,
time::MegaHertz,
watchdog::IndependentWatchdog,
};
#[cfg(not(feature = "semihosting"))] #[cfg(not(feature = "semihosting"))]
const WATCHDOG_PERIOD: u32 = 1000; const WATCHDOG_PERIOD: u32 = 1000;
#[cfg(feature = "semihosting")] #[cfg(feature = "semihosting")]
const WATCHDOG_PERIOD: u32 = 30000; const WATCHDOG_PERIOD: u32 = 30000;
pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> IndependentWatchdog { pub fn bootup(mut core_perif: CorePeripherals, perif: Peripherals) -> IndependentWatchdog {
log_setup::init_log(); log_setup::init_log();
info!("Kirdy init"); info!("Kirdy init");
core_perif.SCB.enable_icache(); core_perif.SCB.enable_icache();
core_perif.SCB.enable_dcache(&mut core_perif.CPUID); core_perif.SCB.enable_dcache(&mut core_perif.CPUID);
let clocks = perif
let clocks = perif.RCC.constrain() .RCC
.constrain()
.cfgr .cfgr
.use_hse(MegaHertz::from_raw(8).convert()) .use_hse(MegaHertz::from_raw(8).convert())
.sysclk(MegaHertz::from_raw(168).convert()) .sysclk(MegaHertz::from_raw(168).convert())
@ -34,24 +34,26 @@ pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> Indepen
sys_timer::setup(core_perif.SYST, clocks); sys_timer::setup(core_perif.SYST, clocks);
let ( let (eth_pins, usb, current_source_phy) = gpio::setup(
eth_pins, clocks,
usb, perif.GPIOA,
current_source_phy) = gpio::setup( perif.GPIOB,
clocks, perif.TIM4, perif.GPIOC,
perif.GPIOA, perif.GPIOB, perif.GPIOC, perif.GPIOD, perif.GPIOE, perif.GPIOF, perif.GPIOG, perif.GPIOD,
perif.SPI1, perif.SPI2, perif.SPI3, perif.GPIOG,
perif.ADC1, perif.SPI2,
perif.OTG_FS_GLOBAL, perif.OTG_FS_DEVICE, perif.OTG_FS_PWRCLK, perif.OTG_FS_GLOBAL,
perif.OTG_FS_DEVICE,
perif.OTG_FS_PWRCLK,
); );
usb::State::setup(usb); usb::State::setup(usb);
let mut laser = current_source { let mut laser = CurrentSource {
phy: current_source_phy, phy: current_source_phy,
setting: current_source_settings_construct { setting: CurrentSourceSettingsConstruct {
output_current : 0.0 output_current: 0.0,
} },
}; };
laser.setup(); laser.setup();
@ -64,5 +66,4 @@ pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> Indepen
info!("Kirdy setup complete"); info!("Kirdy setup complete");
wd wd
} }

View File

@ -1,68 +1,40 @@
use stm32f4xx_hal::{ use crate::laser_diode::current_sources::*;
adc::Adc,
gpio::{
AF5, Alternate, Analog, Input,
gpioa::*,
gpiob::*,
gpioc::*,
gpiod::*,
gpioe::*,
gpiof::*,
gpiog::*,
GpioExt,
Output, PushPull,
},
hal::{self, blocking::spi::Transfer, digital::v2::OutputPin},
otg_fs::USB,
rcc::Clocks,
spi,
spi::{Spi, NoMiso, TransferModeNormal},
pac::{
ADC1,
GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG,
OTG_FS_GLOBAL, OTG_FS_DEVICE, OTG_FS_PWRCLK,
SPI1, SPI2, SPI3,
TIM4
},
timer::Timer,
time::U32Ext,
time::MegaHertz
};
use fugit::RateExtU32; use fugit::RateExtU32;
use stm32_eth::EthPins; use stm32_eth::EthPins;
use crate::laser_diode::current_sources::*; use stm32f4xx_hal::{
gpio::{gpioa::*, gpiob::*, gpioc::*, gpiog::*, GpioExt, Input},
otg_fs::USB,
pac::{GPIOA, GPIOB, GPIOC, GPIOD, GPIOG, OTG_FS_DEVICE, OTG_FS_GLOBAL, OTG_FS_PWRCLK, SPI2},
rcc::Clocks,
spi,
spi::{NoMiso, Spi},
};
pub type EthernetPins = EthPins< pub type EthernetPins =
PA1<Input>, EthPins<PA1<Input>, PA7<Input>, PB11<Input>, PG13<Input>, PB13<Input>, PC4<Input>, PC5<Input>>;
PA7<Input>,
PB11<Input>,
PG13<Input>,
PB13<Input>,
PC4<Input>,
PC5<Input>,
>;
pub fn setup( pub fn setup(
clocks: Clocks, clocks: Clocks,
tim4: TIM4, gpioa: GPIOA,
gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpiod: GPIOD, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG, gpiob: GPIOB,
spi1: SPI1, spi2: SPI2, spi3: SPI3, gpioc: GPIOC,
adc1: ADC1, gpiod: GPIOD,
otg_fs_global: OTG_FS_GLOBAL, otg_fs_device: OTG_FS_DEVICE, otg_fs_pwrclk: OTG_FS_PWRCLK, gpiog: GPIOG,
spi2: SPI2,
otg_fs_global: OTG_FS_GLOBAL,
otg_fs_device: OTG_FS_DEVICE,
otg_fs_pwrclk: OTG_FS_PWRCLK,
) -> ( ) -> (
EthernetPins, EthernetPins,
USB, USB,
current_source_phy_construct<current_source_phy_ch0>, CurrentSourcePhyConstruct<CurrentSourcePhyCh0>,
// photo_diode_phy, // photo_diode_phy,
// thermostat_phy // thermostat_phy
) { ) {
let gpioa = gpioa.split(); let gpioa = gpioa.split();
let gpiob = gpiob.split(); let gpiob = gpiob.split();
let gpioc = gpioc.split(); let gpioc = gpioc.split();
let gpiod = gpiod.split(); let gpiod = gpiod.split();
let gpioe = gpioe.split();
let gpiof = gpiof.split();
let gpiog = gpiog.split(); let gpiog = gpiog.split();
let usb = USB { let usb = USB {
@ -84,23 +56,26 @@ pub fn setup(
rx_d1: gpioc.pc5, rx_d1: gpioc.pc5,
}; };
let current_source_phy = current_source_phy_construct { let current_source_phy = CurrentSourcePhyConstruct {
max5719_spi: Spi::new( max5719_spi: Spi::new(
spi2, spi2,
(gpiob.pb10.into_alternate(), NoMiso {}, gpiob.pb15.into_alternate()), (
gpiob.pb10.into_alternate(),
NoMiso {},
gpiob.pb15.into_alternate(),
),
spi::Mode { spi::Mode {
polarity: spi::Polarity::IdleLow, polarity: spi::Polarity::IdleLow,
phase: spi::Phase::CaptureOnFirstTransition, phase: spi::Phase::CaptureOnFirstTransition,
}, },
10_u32.MHz(), 10_u32.MHz(),
&clocks), &clocks,
),
max5719_load: gpiob.pb14.into_push_pull_output(), max5719_load: gpiob.pb14.into_push_pull_output(),
max5719_cs: gpiod.pd8.into_push_pull_output(), max5719_cs: gpiod.pd8.into_push_pull_output(),
current_source_ldo_en: gpiod.pd9.into_push_pull_output(), current_source_ldo_en: gpiod.pd9.into_push_pull_output(),
current_source_short: gpioa.pa4.into_push_pull_output() current_source_short: gpioa.pa4.into_push_pull_output(),
}; };
(eth_pins, usb, current_source_phy) (eth_pins, usb, current_source_phy)
} }

View File

@ -10,17 +10,15 @@ pub fn init_log() {
#[cfg(feature = "semihosting")] #[cfg(feature = "semihosting")]
pub fn init_log() { pub fn init_log() {
use cortex_m_log::log::{init, Logger};
use cortex_m_log::printer::semihosting::{hio::HStdout, InterruptOk};
use log::LevelFilter; use log::LevelFilter;
use cortex_m_log::log::{Logger, init};
use cortex_m_log::printer::semihosting::{InterruptOk, hio::HStdout};
static mut LOGGER: Option<Logger<InterruptOk<HStdout>>> = None; static mut LOGGER: Option<Logger<InterruptOk<HStdout>>> = None;
let logger = Logger { let logger = Logger {
inner: InterruptOk::<_>::stdout().expect("semihosting stdout"), inner: InterruptOk::<_>::stdout().expect("semihosting stdout"),
level: LevelFilter::Info, level: LevelFilter::Info,
}; };
let logger = unsafe { let logger = unsafe { LOGGER.get_or_insert(logger) };
LOGGER.get_or_insert(logger)
};
init(logger).expect("set logger"); init(logger).expect("set logger");
} }

View File

@ -1,5 +1,5 @@
pub mod log_setup;
pub mod usb;
pub mod sys_timer;
pub mod boot; pub mod boot;
pub mod gpio; pub mod gpio;
pub mod log_setup;
pub mod sys_timer;
pub mod usb;

View File

@ -3,10 +3,7 @@ use core::ops::Deref;
use cortex_m::interrupt::Mutex; use cortex_m::interrupt::Mutex;
use cortex_m::peripheral::syst::SystClkSource; use cortex_m::peripheral::syst::SystClkSource;
use cortex_m_rt::exception; use cortex_m_rt::exception;
use stm32f4xx_hal::{ use stm32f4xx_hal::{pac::SYST, rcc::Clocks};
rcc::Clocks,
pac::SYST,
};
/// Rate in Hz /// Rate in Hz
const TIMER_RATE: u32 = 1000; const TIMER_RATE: u32 = 1000;
@ -27,18 +24,13 @@ pub fn setup(mut syst: SYST, clocks: Clocks) {
#[exception] #[exception]
fn SysTick() { fn SysTick() {
cortex_m::interrupt::free(|cs| { cortex_m::interrupt::free(|cs| {
*TIMER_MS.borrow(cs) *TIMER_MS.borrow(cs).borrow_mut() += TIMER_DELTA;
.borrow_mut() += TIMER_DELTA;
}); });
} }
/// Obtain current time in milliseconds /// Obtain current time in milliseconds
pub fn now() -> u32 { pub fn now() -> u32 {
cortex_m::interrupt::free(|cs| { cortex_m::interrupt::free(|cs| *TIMER_MS.borrow(cs).borrow().deref())
*TIMER_MS.borrow(cs)
.borrow()
.deref()
})
} }
/// block for `amount` milliseconds /// block for `amount` milliseconds

View File

@ -1,15 +1,18 @@
use core::{fmt::{self, Write}, mem::MaybeUninit}; use core::{
fmt::{self, Write},
mem::MaybeUninit,
};
use cortex_m::interrupt::free; use cortex_m::interrupt::free;
use log::{Log, Metadata, Record};
use stm32f4xx_hal::{ use stm32f4xx_hal::{
otg_fs::{USB, UsbBus as Bus}, otg_fs::{UsbBus as Bus, USB},
pac::{interrupt, Interrupt, NVIC}, pac::{interrupt, Interrupt, NVIC},
}; };
use usb_device::{ use usb_device::{
class_prelude::{UsbBusAllocator}, class_prelude::UsbBusAllocator,
prelude::{UsbDevice, UsbDeviceBuilder, UsbVidPid}, prelude::{UsbDevice, UsbDeviceBuilder, UsbVidPid},
}; };
use usbd_serial::SerialPort; use usbd_serial::SerialPort;
use log::{Record, Log, Metadata};
static mut EP_MEMORY: [u32; 1024] = [0; 1024]; static mut EP_MEMORY: [u32; 1024] = [0; 1024];
@ -36,8 +39,8 @@ impl State {
.device_class(usbd_serial::USB_CLASS_CDC) .device_class(usbd_serial::USB_CLASS_CDC)
.build(); .build();
free(|_| { free(|_| unsafe {
unsafe { STATE = Some(State { serial, dev }); } STATE = Some(State { serial, dev });
}); });
unsafe { unsafe {
@ -94,8 +97,7 @@ impl Write for SerialOutput {
fn write_str(&mut self, s: &str) -> core::result::Result<(), core::fmt::Error> { fn write_str(&mut self, s: &str) -> core::result::Result<(), core::fmt::Error> {
if let Some(ref mut state) = State::get() { if let Some(ref mut state) = State::get() {
for chunk in s.as_bytes().chunks(16) { for chunk in s.as_bytes().chunks(16) {
free(|_| state.serial.write(chunk)) free(|_| state.serial.write(chunk)).map_err(|_| fmt::Error)?;
.map_err(|_| fmt::Error)?;
} }
} }
Ok(()) Ok(())

View File

@ -1,77 +1,71 @@
use stm32f4xx_hal::{ use stm32f4xx_hal::{
adc::Adc, gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull, PD9},
gpio::{ hal::{blocking::spi::Write, digital::v2::OutputPin},
AF5, Alternate,
gpioa::*,
gpiob::*,
gpioe::*,
gpiod::*,
Output,
PushPull, PD9,
},
hal::{self, blocking::spi::Write, digital::v2::OutputPin},
spi::{Spi, NoMiso, TransferModeNormal},
pac::SPI2, pac::SPI2,
spi::{NoMiso, Spi, TransferModeNormal},
}; };
use crate::device::sys_timer::sleep; use crate::device::sys_timer::sleep;
pub trait current_source_phy { pub trait CurrentSourcePhy {
type current_source_ldo_en: OutputPin; type CurrentSourceLdoEn: OutputPin;
type current_source_short: OutputPin; type CurrentSourceShort: OutputPin;
type max5719_load: OutputPin; type Max5719Load: OutputPin;
type max5719_cs: OutputPin; type Max5719Cs: OutputPin;
type max5719_spi: Write<u8>; type Max5719Spi: Write<u8>;
} }
pub struct current_source_phy_construct<C: current_source_phy> { pub struct CurrentSourcePhyConstruct<C: CurrentSourcePhy> {
pub max5719_spi: C::max5719_spi, pub max5719_spi: C::Max5719Spi,
pub max5719_load: C::max5719_load, pub max5719_load: C::Max5719Load,
pub max5719_cs: C::max5719_cs, pub max5719_cs: C::Max5719Cs,
pub current_source_ldo_en: C::current_source_ldo_en, pub current_source_ldo_en: C::CurrentSourceLdoEn,
pub current_source_short: C::current_source_short, pub current_source_short: C::CurrentSourceShort,
} }
pub struct current_source_settings_construct { pub struct CurrentSourceSettingsConstruct {
pub output_current: f32 pub output_current: f32,
} }
pub struct current_source<C:current_source_phy> { pub struct CurrentSource<C: CurrentSourcePhy> {
pub phy : current_source_phy_construct<C>, pub phy: CurrentSourcePhyConstruct<C>,
pub setting: current_source_settings_construct pub setting: CurrentSourceSettingsConstruct,
} }
pub struct current_source_phy_ch0; pub struct CurrentSourcePhyCh0;
impl current_source_phy for current_source_phy_ch0 { impl CurrentSourcePhy for CurrentSourcePhyCh0 {
type current_source_ldo_en = PD9<Output<PushPull>>; type CurrentSourceLdoEn = PD9<Output<PushPull>>;
type current_source_short = PA4<Output<PushPull>>; type CurrentSourceShort = PA4<Output<PushPull>>;
type max5719_load = PB14<Output<PushPull>>; type Max5719Load = PB14<Output<PushPull>>;
type max5719_cs = PD8<Output<PushPull>>; type Max5719Cs = PD8<Output<PushPull>>;
type max5719_spi = Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>; type Max5719Spi =
Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>;
} }
impl<C:current_source_phy> current_source<C> { impl<C: CurrentSourcePhy> CurrentSource<C> {
pub fn setup(&mut self) { pub fn setup(&mut self) {
self.phy.max5719_load.set_high(); let _ = self.phy.max5719_load.set_high();
self.phy.max5719_cs.set_high(); let _ = self.phy.max5719_cs.set_high();
self.phy.current_source_ldo_en.set_high(); let _ = self.phy.current_source_ldo_en.set_high();
sleep(50_u32); sleep(10_u32);
self.phy.current_source_short.set_high(); let _ = self.phy.current_source_short.set_high();
sleep(50_u32); sleep(10_u32);
} }
pub fn set_current(&mut self, current: f32) { pub fn set_current(
self.phy.max5719_load.set_high(); &mut self,
self.phy.max5719_cs.set_low(); current: f32,
let v_dac = current * 10.0 / 0.75; ) -> Result<(), <<C as CurrentSourcePhy>::Max5719Spi as Write<u8>>::Error> {
let word = (((v_dac / 4.096) * 1048576.0) as u32) << 4; let _ = self.phy.max5719_load.set_high();
let _ = self.phy.max5719_cs.set_low();
self.setting.output_current = current * 10.0 / 0.75;
let word = (((self.setting.output_current / 4.096) * 1048576.0) as u32) << 4;
let mut buf = [ let mut buf = [
((word >> 16) & 0xFF) as u8, ((word >> 16) & 0xFF) as u8,
((word >> 8) & 0xFF) as u8, ((word >> 8) & 0xFF) as u8,
((word >> 0) & 0xFF) as u8, ((word >> 0) & 0xFF) as u8,
]; ];
self.phy.max5719_spi.write(&mut buf); self.phy.max5719_spi.write(&mut buf)?;
self.phy.max5719_cs.set_high(); let _ = self.phy.max5719_cs.set_high();
self.phy.max5719_load.set_low(); let _ = self.phy.max5719_load.set_low();
Ok(())
} }
} }

View File

@ -1,32 +1,25 @@
#![no_main] #![no_main]
#![no_std] #![no_std]
use panic_halt as _;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use stm32f4xx_hal::{
pac::{CorePeripherals, Peripherals}
};
use log::info; use log::info;
use panic_halt as _;
use stm32f4xx_hal::pac::{CorePeripherals, Peripherals};
mod device; mod device;
mod laser_diode; mod laser_diode;
use device::{ use device::{boot::bootup, sys_timer};
boot::bootup,
sys_timer
};
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
let core_perif = CorePeripherals::take().unwrap();
let mut core_perif = CorePeripherals::take().unwrap();
let perif = Peripherals::take().unwrap(); let perif = Peripherals::take().unwrap();
let (mut wd) = bootup(core_perif, perif); let mut wd = bootup(core_perif, perif);
loop { loop {
wd.feed(); wd.feed();
info!("looping"); info!("looping");
sys_timer::sleep(10); sys_timer::sleep(10);
} }
} }