diff --git a/spinnyboy_rust/src/bin/dc_driver/afroesc.rs b/spinnyboy_rust/src/bin/dc_driver/afroesc.rs index 9a3b543..950382a 100644 --- a/spinnyboy_rust/src/bin/dc_driver/afroesc.rs +++ b/spinnyboy_rust/src/bin/dc_driver/afroesc.rs @@ -6,12 +6,11 @@ use esp_hal::mcpwm::operator::PwmPin; use esp_hal::mcpwm::operator::Operator; use esp_hal::mcpwm::McPwm; use esp_hal::peripherals::MCPWM0; -use esp_hal::delay::Delay; + pub struct AfroEsc<'a>{ pub pwm_pin: &'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true> } -const ARMING_SEQUENCE:u16 = 1055; const MIN_THROTTLE:u16 = 1121; const MAX_THROTTLE:u16 = 1421; const GAP:u16 = (MAX_THROTTLE - MIN_THROTTLE)/100; @@ -19,11 +18,7 @@ const GAP:u16 = (MAX_THROTTLE - MIN_THROTTLE)/100; impl AfroEsc<'_>{ //this is a little hacky tbh pub fn new<'a>(pwm_pin:&'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>)-> AfroEsc<'a> { - let mut esc = AfroEsc{pwm_pin:pwm_pin}; - let delay = Delay::new(); - esc.set_arming_sequence(); - delay.delay_millis(3000); - esc + AfroEsc{pwm_pin:pwm_pin} } pub fn set_timestamp(&mut self,value:u16){ &self.pwm_pin.set_timestamp(value); @@ -37,8 +32,4 @@ impl AfroEsc<'_>{ let new_timestamp = MIN_THROTTLE+value*GAP; &self.pwm_pin.set_timestamp(new_timestamp); } - pub fn set_arming_sequence(&mut self){ - &self.set_timestamp(1055); - } - } \ No newline at end of file diff --git a/spinnyboy_rust/src/bin/main.rs b/spinnyboy_rust/src/bin/main.rs index daa7490..97ab0f0 100644 --- a/spinnyboy_rust/src/bin/main.rs +++ b/spinnyboy_rust/src/bin/main.rs @@ -11,30 +11,19 @@ use esp_hal::uart::{Config, Uart}; use esp_hal::clock::CpuClock; use esp_hal::delay::Delay; use esp_hal::main; - -use esp_hal::gpio::Event; -use esp_hal::gpio::{Input, InputConfig}; use esp_hal::mcpwm::operator::PwmPinConfig; use esp_hal::mcpwm::timer::PwmWorkingMode; use esp_hal::time::Rate; - -use esp_hal::handler; - -use core::cell::RefCell; use esp_hal::time::{Duration, Instant}; use esp_hal::timer::timg::TimerGroup; - -use critical_section::Mutex; use rtt_target::rprintln; mod dc_driver; - use dc_driver::afroesc::AfroEsc; #[panic_handler] fn panic(_: &core::panic::PanicInfo) -> ! { loop {} } -// -static BUTTON: Mutex>> = Mutex::new(RefCell::new(None)); + extern crate alloc; // This creates a default app-descriptor required by the esp-idf bootloader. @@ -58,6 +47,7 @@ fn main() -> ! { let mut mcpwm = esp_hal::mcpwm::McPwm::new(peripherals.MCPWM0, clock_config); mcpwm.operator0.set_timer(&mcpwm.timer0); let pin = peripherals.GPIO18; + let mut pwm_pin = mcpwm .operator0 .with_pin_a(pin, PwmPinConfig::UP_ACTIVE_HIGH); @@ -65,29 +55,19 @@ fn main() -> ! { .timer_clock_with_frequency(19_999, PwmWorkingMode::Increase, Rate::from_hz(50)) .unwrap(); mcpwm.timer0.start(timer_clock_cfg); - // let mut esc = AfroEsc::new(&mut pwm_pin); - // esc.set_timestamp(1000); - // delay.delay_millis(3000); - // esc.set_duty_percent(10); - // delay.delay_millis(3000); + let mut esc = AfroEsc::new(&mut pwm_pin); + esc.set_timestamp(1000); + delay.delay_millis(3000); + esc.set_duty_percent(10); + delay.delay_millis(3000); //1421 is I think the best, makes calculating the throttle easier - // esc.set_timestamp(1055); - // delay.delay_millis(3000); - //gpio 9 is the button - let input_config = InputConfig::default().with_pull(esp_hal::gpio::Pull::Up); - //let emergency_button = peripherals.GPIO9; - let mut emergency_button = Input::new(peripherals.GPIO9, input_config); - critical_section::with(|cs| { - emergency_button.listen(Event::FallingEdge); - BUTTON.borrow_ref_mut(cs).replace(emergency_button) - }); - let mut io = esp_hal::gpio::Io::new(peripherals.IO_MUX); - io.set_interrupt_handler(handler); + esc.set_timestamp(1055); + delay.delay_millis(3000); let mut uart0 = Uart::new(peripherals.UART0, Config::default().with_baudrate(9600)) .unwrap() - .with_rx(peripherals.GPIO10) - .with_tx(peripherals.GPIO11); + .with_rx(peripherals.GPIO9) + .with_tx(peripherals.GPIO10); uart0.write(b"page page1"); uart0.write(b"\xff"); uart0.write(b"\xff"); @@ -117,29 +97,3 @@ fn main() -> ! { // for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0/examples/src/bin } - -#[handler] -// #[ram] -fn handler() { - - - if critical_section::with(|cs| { - BUTTON - .borrow_ref_mut(cs) - .as_mut() - .unwrap() - .is_interrupt_set() - }) { - rprintln!("Button was the source of the interrupt"); - } else { - rprintln!("Button was not the source of the interrupt"); - } - - critical_section::with(|cs| { - BUTTON - .borrow_ref_mut(cs) - .as_mut() - .unwrap() - .clear_interrupt() - }); -} \ No newline at end of file