Compare commits

...

2 commits

Author SHA1 Message Date
fly
4a301af6ae Fox timer in Pedestal Box firmware
Signed-off-by: fly <merspieler@alwaysdata.net>
2024-09-23 23:22:32 +02:00
fly
deaeebcebb Fix timer in MCDU firmware
Signed-off-by: fly <merspieler@alwaysdata.net>
2024-09-23 23:18:29 +02:00
2 changed files with 38 additions and 45 deletions

View file

@ -11,13 +11,15 @@ mod device;
use panic_halt as _; use panic_halt as _;
use nb::block;
use cortex_m::asm::delay; use cortex_m::asm::delay;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use stm32f1xx_hal::{ use stm32f1xx_hal::{
gpio::{Pin, Input, PullDown, Output, PushPull}, gpio::{Pin, Input, PullDown, Output, PushPull},
pac, pac,
prelude::*, prelude::*,
// timer::{Channel}, timer::Timer,
usb::{Peripheral, UsbBus}, usb::{Peripheral, UsbBus},
}; };
@ -82,6 +84,7 @@ struct MyPins {
fn main() -> ! { fn main() -> ! {
// ====================== general setup ================= // ====================== general setup =================
// Acquire peripherals // Acquire peripherals
let cp = cortex_m::Peripherals::take().unwrap();
let p = pac::Peripherals::take().unwrap(); let p = pac::Peripherals::take().unwrap();
let mut flash = p.FLASH.constrain(); let mut flash = p.FLASH.constrain();
let rcc = p.RCC.constrain(); let rcc = p.RCC.constrain();
@ -182,31 +185,26 @@ fn main() -> ! {
pc14: gpioc.pc14.into_push_pull_output(&mut gpioc.crh), // FM1 pc14: gpioc.pc14.into_push_pull_output(&mut gpioc.crh), // FM1
}; };
// let mut last = get_report(&mut io_pins);
// ====================== Timer setup =============== // ====================== Timer setup ===============
// let timer = Instant; let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
// let mut last_report_sent = timer.elapsed(); timer.start(1.kHz()).unwrap();
// ====================== Main loop ================= // ====================== Main loop =================
loop { loop {
block!(timer.wait()).unwrap();
let report = get_report(&mut io_pins); let report = get_report(&mut io_pins);
// TODO figure out timer and only send in like 1ms intervals or on change match consumer.device().write_report(&report) {
// if report != last { Err(UsbHidError::WouldBlock) => {}
match consumer.device().write_report(&report) { Err(UsbHidError::UsbError(usb_device::UsbError::BufferOverflow)) => {
Err(UsbHidError::WouldBlock) => {} core::panic!("Failed to write consumer report, report is too big")
Err(UsbHidError::UsbError(usb_device::UsbError::BufferOverflow)) => {
core::panic!("Failed to write consumer report, report is too big")
}
Ok(_) => {
// last = report;
}
Err(e) => {
io_pins.pa10.set_high(); // set as indicator that this has happened
core::panic!("Failed to write consumer report: {:?}", e)
}
} }
// } Ok(_) => {
}
Err(e) => {
io_pins.pa10.set_high(); // set as indicator that this has happened
core::panic!("Failed to write consumer report: {:?}", e)
}
}
if usb_dev.poll(&mut [&mut consumer]) { if usb_dev.poll(&mut [&mut consumer]) {
match consumer.device().read_report() { match consumer.device().read_report() {

View file

@ -11,6 +11,8 @@ mod device;
use panic_halt as _; use panic_halt as _;
use nb::block;
use cortex_m::asm::delay; use cortex_m::asm::delay;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use stm32f1xx_hal::{ use stm32f1xx_hal::{
@ -18,7 +20,7 @@ use stm32f1xx_hal::{
gpio::{Pin, Input, Analog, PullDown, Output, PushPull}, gpio::{Pin, Input, Analog, PullDown, Output, PushPull},
pac, pac,
prelude::*, prelude::*,
timer::{Tim2FullRemap}, timer::{Tim2FullRemap, Timer},
flash::{FlashWriter, FlashSize, SectorSize}, flash::{FlashWriter, FlashSize, SectorSize},
usb::{Peripheral, UsbBus}, usb::{Peripheral, UsbBus},
}; };
@ -176,6 +178,7 @@ impl Calibration {
fn main() -> ! { fn main() -> ! {
// ====================== general setup ================= // ====================== general setup =================
// Acquire peripherals // Acquire peripherals
let cp = cortex_m::Peripherals::take().unwrap();
let p = pac::Peripherals::take().unwrap(); let p = pac::Peripherals::take().unwrap();
let mut flash = p.FLASH.constrain(); let mut flash = p.FLASH.constrain();
let rcc = p.RCC.constrain(); let rcc = p.RCC.constrain();
@ -294,8 +297,6 @@ fn main() -> ! {
pe13: gpioe.pe13.into_push_pull_output(&mut gpioe.crh), pe13: gpioe.pe13.into_push_pull_output(&mut gpioe.crh),
}; };
// let mut last = get_report(&mut io_pins, &mut adc1, &cal);
// ====================== PWM setup ================= // ====================== PWM setup =================
let c1 = pa15.into_alternate_push_pull(&mut gpioa.crh); let c1 = pa15.into_alternate_push_pull(&mut gpioa.crh);
let c3 = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh); let c3 = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
@ -303,37 +304,31 @@ fn main() -> ! {
.TIM2 .TIM2
.pwm_hz::<Tim2FullRemap, _, _>((c1, c3), &mut afio.mapr, 1.kHz(), &clocks).split(); .pwm_hz::<Tim2FullRemap, _, _>((c1, c3), &mut afio.mapr, 1.kHz(), &clocks).split();
integ_lt_pwm.enable(); integ_lt_pwm.enable();
// let mut rudder_trim_display_pwm = p
// .TIM2
// .pwm_hz::<Tim2FullRemap, _, _>(c3, &mut afio.mapr, 1.kHz(), &clocks);
rudder_trim_display_pwm.enable(); rudder_trim_display_pwm.enable();
let pwm_max = integ_lt_pwm.get_max_duty(); //48000 in our case let pwm_max = integ_lt_pwm.get_max_duty(); //48000 in our case
// ====================== Timer setup =============== // ====================== Timer setup ===============
// let timer = Instant; let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
// let mut last_report_sent = timer.elapsed(); timer.start(1.kHz()).unwrap();
// ====================== Main loop ================= // ====================== Main loop =================
loop { loop {
block!(timer.wait()).unwrap();
let report = get_report(&mut io_pins, &mut adc1, &cal); let report = get_report(&mut io_pins, &mut adc1, &cal);
// TODO figure out timer and only send in like 1ms intervals or on change match consumer.device().write_report(&report) {
// if report != last { Err(UsbHidError::WouldBlock) => {}
match consumer.device().write_report(&report) { Err(UsbHidError::UsbError(usb_device::UsbError::BufferOverflow)) => {
Err(UsbHidError::WouldBlock) => {} core::panic!("Failed to write consumer report, report is too big")
Err(UsbHidError::UsbError(usb_device::UsbError::BufferOverflow)) => {
core::panic!("Failed to write consumer report, report is too big")
}
Ok(_) => {
// last = report;
}
Err(e) => {
// set as indicator that this has happened
// io_pins.pe1.set_high();
// io_pins.pe4.set_high();
core::panic!("Failed to write consumer report: {:?}", e)
}
} }
// } Ok(_) => {
}
Err(e) => {
// set as indicator that this has happened
io_pins.pe1.set_high();
io_pins.pe4.set_high();
core::panic!("Failed to write consumer report: {:?}", e)
}
}
if usb_dev.poll(&mut [&mut consumer]) { if usb_dev.poll(&mut [&mut consumer]) {
match consumer.device().read_report() { match consumer.device().read_report() {