adds interrupts

This commit is contained in:
Robert Schauklies 2025-12-28 18:38:13 +01:00
parent 266f09c4b1
commit afd1a61bfe

View file

@ -11,19 +11,30 @@ 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<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None));
extern crate alloc;
// 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);
mcpwm.operator0.set_timer(&mcpwm.timer0);
let pin = peripherals.GPIO18;
let mut pwm_pin = mcpwm
.operator0
.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))
.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);
// 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);
let mut uart0 = Uart::new(peripherals.UART0, Config::default().with_baudrate(9600))
.unwrap()
.with_rx(peripherals.GPIO9)
.with_tx(peripherals.GPIO10);
.with_rx(peripherals.GPIO10)
.with_tx(peripherals.GPIO11);
uart0.write(b"page page1");
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
}
#[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()
});
}