1
0
forked from M-Labs/kirdy

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

View File

@ -1,70 +1,42 @@
use stm32f4xx_hal::{
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 crate::laser_diode::current_sources::*;
use fugit::RateExtU32;
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<
PA1<Input>,
PA7<Input>,
PB11<Input>,
PG13<Input>,
PB13<Input>,
PC4<Input>,
PC5<Input>,
>;
pub type EthernetPins =
EthPins<PA1<Input>, PA7<Input>, PB11<Input>, PG13<Input>, PB13<Input>, PC4<Input>, PC5<Input>>;
pub fn setup(
clocks: Clocks,
tim4: TIM4,
gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpiod: GPIOD, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG,
spi1: SPI1, spi2: SPI2, spi3: SPI3,
adc1: ADC1,
otg_fs_global: OTG_FS_GLOBAL, otg_fs_device: OTG_FS_DEVICE, otg_fs_pwrclk: OTG_FS_PWRCLK,
) -> (
EthernetPins,
USB,
current_source_phy_construct<current_source_phy_ch0>,
// photo_diode_phy,
// thermostat_phy
gpioa: GPIOA,
gpiob: GPIOB,
gpioc: GPIOC,
gpiod: GPIOD,
gpiog: GPIOG,
spi2: SPI2,
otg_fs_global: OTG_FS_GLOBAL,
otg_fs_device: OTG_FS_DEVICE,
otg_fs_pwrclk: OTG_FS_PWRCLK,
) -> (
EthernetPins,
USB,
CurrentSourcePhyConstruct<CurrentSourcePhyCh0>,
// photo_diode_phy,
// thermostat_phy
) {
let gpioa = gpioa.split();
let gpiob = gpiob.split();
let gpioc = gpioc.split();
let gpiod = gpiod.split();
let gpioe = gpioe.split();
let gpiof = gpiof.split();
let gpiog = gpiog.split();
let gpiog = gpiog.split();
let usb = USB {
usb_global: otg_fs_global,
usb_device: otg_fs_device,
@ -75,32 +47,35 @@ pub fn setup(
};
let eth_pins = EthPins {
ref_clk: gpioa.pa1,
crs: gpioa.pa7,
ref_clk: gpioa.pa1,
crs: gpioa.pa7,
tx_en: gpiob.pb11,
tx_d0: gpiog.pg13,
tx_d0: gpiog.pg13,
tx_d1: gpiob.pb13,
rx_d0: gpioc.pc4,
rx_d0: gpioc.pc4,
rx_d1: gpioc.pc5,
};
let current_source_phy = current_source_phy_construct {
let current_source_phy = CurrentSourcePhyConstruct {
max5719_spi: Spi::new(
spi2,
(gpiob.pb10.into_alternate(), NoMiso {}, gpiob.pb15.into_alternate()),
spi2,
(
gpiob.pb10.into_alternate(),
NoMiso {},
gpiob.pb15.into_alternate(),
),
spi::Mode {
polarity: spi::Polarity::IdleLow,
phase: spi::Phase::CaptureOnFirstTransition,
},
10_u32.MHz(),
&clocks),
max5719_load: gpiob.pb14.into_push_pull_output(),
max5719_cs: gpiod.pd8.into_push_pull_output(),
},
10_u32.MHz(),
&clocks,
),
max5719_load: gpiob.pb14.into_push_pull_output(),
max5719_cs: gpiod.pd8.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)
}

View File

@ -10,17 +10,15 @@ pub fn init_log() {
#[cfg(feature = "semihosting")]
pub fn init_log() {
use cortex_m_log::log::{init, Logger};
use cortex_m_log::printer::semihosting::{hio::HStdout, InterruptOk};
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;
let logger = Logger {
inner: InterruptOk::<_>::stdout().expect("semihosting stdout"),
level: LevelFilter::Info,
};
let logger = unsafe {
LOGGER.get_or_insert(logger)
};
let logger = unsafe { LOGGER.get_or_insert(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 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::peripheral::syst::SystClkSource;
use cortex_m_rt::exception;
use stm32f4xx_hal::{
rcc::Clocks,
pac::SYST,
};
use stm32f4xx_hal::{pac::SYST, rcc::Clocks};
/// Rate in Hz
const TIMER_RATE: u32 = 1000;
@ -16,7 +13,7 @@ const TIMER_DELTA: u32 = 1000 / TIMER_RATE;
static TIMER_MS: Mutex<RefCell<u32>> = Mutex::new(RefCell::new(0));
/// Setup SysTick exception
pub fn setup(mut syst: SYST, clocks: Clocks) {
pub fn setup(mut syst: SYST, clocks: Clocks) {
syst.set_clock_source(SystClkSource::Core);
syst.set_reload(clocks.hclk().to_Hz() / TIMER_RATE - 1);
syst.enable_counter();
@ -27,18 +24,13 @@ pub fn setup(mut syst: SYST, clocks: Clocks) {
#[exception]
fn SysTick() {
cortex_m::interrupt::free(|cs| {
*TIMER_MS.borrow(cs)
.borrow_mut() += TIMER_DELTA;
*TIMER_MS.borrow(cs).borrow_mut() += TIMER_DELTA;
});
}
/// Obtain current time in milliseconds
pub fn now() -> u32 {
cortex_m::interrupt::free(|cs| {
*TIMER_MS.borrow(cs)
.borrow()
.deref()
})
cortex_m::interrupt::free(|cs| *TIMER_MS.borrow(cs).borrow().deref())
}
/// 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 log::{Log, Metadata, Record};
use stm32f4xx_hal::{
otg_fs::{USB, UsbBus as Bus},
otg_fs::{UsbBus as Bus, USB},
pac::{interrupt, Interrupt, NVIC},
};
use usb_device::{
class_prelude::{UsbBusAllocator},
class_prelude::UsbBusAllocator,
prelude::{UsbDevice, UsbDeviceBuilder, UsbVidPid},
};
use usbd_serial::SerialPort;
use log::{Record, Log, Metadata};
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
@ -36,8 +39,8 @@ impl State {
.device_class(usbd_serial::USB_CLASS_CDC)
.build();
free(|_| {
unsafe { STATE = Some(State { serial, dev }); }
free(|_| unsafe {
STATE = Some(State { serial, dev });
});
unsafe {
@ -94,8 +97,7 @@ impl Write for SerialOutput {
fn write_str(&mut self, s: &str) -> core::result::Result<(), core::fmt::Error> {
if let Some(ref mut state) = State::get() {
for chunk in s.as_bytes().chunks(16) {
free(|_| state.serial.write(chunk))
.map_err(|_| fmt::Error)?;
free(|_| state.serial.write(chunk)).map_err(|_| fmt::Error)?;
}
}
Ok(())

View File

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

View File

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