Compare commits

...

2 commits

Author SHA1 Message Date
Robert Schauklies
afd1a61bfe adds interrupts 2025-12-28 18:38:13 +01:00
Robert Schauklies
266f09c4b1 adds arming sequence for the AfroESC 2025-12-28 18:37:49 +01:00
2 changed files with 68 additions and 13 deletions

View file

@ -6,11 +6,12 @@ use esp_hal::mcpwm::operator::PwmPin;
use esp_hal::mcpwm::operator::Operator; use esp_hal::mcpwm::operator::Operator;
use esp_hal::mcpwm::McPwm; use esp_hal::mcpwm::McPwm;
use esp_hal::peripherals::MCPWM0; use esp_hal::peripherals::MCPWM0;
use esp_hal::delay::Delay;
pub struct AfroEsc<'a>{ pub struct AfroEsc<'a>{
pub pwm_pin: &'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true> 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 MIN_THROTTLE:u16 = 1121;
const MAX_THROTTLE:u16 = 1421; const MAX_THROTTLE:u16 = 1421;
const GAP:u16 = (MAX_THROTTLE - MIN_THROTTLE)/100; const GAP:u16 = (MAX_THROTTLE - MIN_THROTTLE)/100;
@ -18,7 +19,11 @@ const GAP:u16 = (MAX_THROTTLE - MIN_THROTTLE)/100;
impl AfroEsc<'_>{ impl AfroEsc<'_>{
//this is a little hacky tbh //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> { pub fn new<'a>(pwm_pin:&'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>)-> AfroEsc<'a> {
AfroEsc{pwm_pin:pwm_pin} let mut esc = AfroEsc{pwm_pin:pwm_pin};
let delay = Delay::new();
esc.set_arming_sequence();
delay.delay_millis(3000);
esc
} }
pub fn set_timestamp(&mut self,value:u16){ pub fn set_timestamp(&mut self,value:u16){
&self.pwm_pin.set_timestamp(value); &self.pwm_pin.set_timestamp(value);
@ -32,4 +37,8 @@ impl AfroEsc<'_>{
let new_timestamp = MIN_THROTTLE+value*GAP; let new_timestamp = MIN_THROTTLE+value*GAP;
&self.pwm_pin.set_timestamp(new_timestamp); &self.pwm_pin.set_timestamp(new_timestamp);
} }
pub fn set_arming_sequence(&mut self){
&self.set_timestamp(1055);
}
} }

View file

@ -11,19 +11,30 @@ use esp_hal::uart::{Config, Uart};
use esp_hal::clock::CpuClock; use esp_hal::clock::CpuClock;
use esp_hal::delay::Delay; use esp_hal::delay::Delay;
use esp_hal::main; 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::operator::PwmPinConfig;
use esp_hal::mcpwm::timer::PwmWorkingMode; use esp_hal::mcpwm::timer::PwmWorkingMode;
use esp_hal::time::Rate; use esp_hal::time::Rate;
use esp_hal::handler;
use core::cell::RefCell;
use esp_hal::time::{Duration, Instant}; use esp_hal::time::{Duration, Instant};
use esp_hal::timer::timg::TimerGroup; use esp_hal::timer::timg::TimerGroup;
use critical_section::Mutex;
use rtt_target::rprintln; use rtt_target::rprintln;
mod dc_driver; mod dc_driver;
use dc_driver::afroesc::AfroEsc; use dc_driver::afroesc::AfroEsc;
#[panic_handler] #[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! { fn panic(_: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
//
static BUTTON: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None));
extern crate alloc; extern crate alloc;
// This creates a default app-descriptor required by the esp-idf bootloader. // This creates a default app-descriptor required by the esp-idf bootloader.
@ -47,7 +58,6 @@ fn main() -> ! {
let mut mcpwm = esp_hal::mcpwm::McPwm::new(peripherals.MCPWM0, clock_config); let mut mcpwm = esp_hal::mcpwm::McPwm::new(peripherals.MCPWM0, clock_config);
mcpwm.operator0.set_timer(&mcpwm.timer0); mcpwm.operator0.set_timer(&mcpwm.timer0);
let pin = peripherals.GPIO18; let pin = peripherals.GPIO18;
let mut pwm_pin = mcpwm let mut pwm_pin = mcpwm
.operator0 .operator0
.with_pin_a(pin, PwmPinConfig::UP_ACTIVE_HIGH); .with_pin_a(pin, PwmPinConfig::UP_ACTIVE_HIGH);
@ -55,19 +65,29 @@ fn main() -> ! {
.timer_clock_with_frequency(19_999, PwmWorkingMode::Increase, Rate::from_hz(50)) .timer_clock_with_frequency(19_999, PwmWorkingMode::Increase, Rate::from_hz(50))
.unwrap(); .unwrap();
mcpwm.timer0.start(timer_clock_cfg); mcpwm.timer0.start(timer_clock_cfg);
let mut esc = AfroEsc::new(&mut pwm_pin); // let mut esc = AfroEsc::new(&mut pwm_pin);
esc.set_timestamp(1000); // esc.set_timestamp(1000);
delay.delay_millis(3000); // delay.delay_millis(3000);
esc.set_duty_percent(10); // esc.set_duty_percent(10);
delay.delay_millis(3000); // delay.delay_millis(3000);
//1421 is I think the best, makes calculating the throttle easier //1421 is I think the best, makes calculating the throttle easier
esc.set_timestamp(1055); // esc.set_timestamp(1055);
delay.delay_millis(3000); // 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);
let mut uart0 = Uart::new(peripherals.UART0, Config::default().with_baudrate(9600)) let mut uart0 = Uart::new(peripherals.UART0, Config::default().with_baudrate(9600))
.unwrap() .unwrap()
.with_rx(peripherals.GPIO9) .with_rx(peripherals.GPIO10)
.with_tx(peripherals.GPIO10); .with_tx(peripherals.GPIO11);
uart0.write(b"page page1"); uart0.write(b"page page1");
uart0.write(b"\xff"); uart0.write(b"\xff");
uart0.write(b"\xff"); uart0.write(b"\xff");
@ -97,3 +117,29 @@ 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 // 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()
});
}