esp-hal/esp32h2-hal/examples/serial_interrupts.rs
Scott Mabin db409ffe7b
Unify the system peripheral (#832)
* Unify the system peripheral

Whilst the PCR, SYSTEM and DPORT peripherals are different, we currently
use them all in the same way. This PR unifies the peripheral name in the
hal to `SYSTEM`. The idea is that they all do the same sort of thing, so
we can collect them under the same name, and later down the line we can
being to expose differences under an extended API.

The benifits to this are imo quite big, the examples now are all identical,
which makes things easier for esp-wifi, and paves a path towards the
multichip hal.

Why not do this in the PAC? Imo the pac should be as close to the
hardware as possible, and the HAL is where we should abstractions such
as this.

* changelog
2023-09-29 08:14:50 -07:00

94 lines
2.5 KiB
Rust

//! This shows some of the interrupts that can be generated by UART/Serial.
//! Use a proper serial terminal to connect to the board (espmonitor and
//! espflash won't work)
#![no_std]
#![no_main]
use core::{cell::RefCell, fmt::Write};
use critical_section::Mutex;
use esp32h2_hal::{
clock::ClockControl,
interrupt,
peripherals::{self, Peripherals, UART0},
prelude::*,
riscv,
timer::TimerGroup,
uart::config::AtCmdConfig,
Cpu,
Uart,
};
use esp_backtrace as _;
use nb::block;
static SERIAL: Mutex<RefCell<Option<Uart<UART0>>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut timer0 = timer_group0.timer0;
let mut uart0 = Uart::new(peripherals.UART0, &clocks);
uart0.set_at_cmd(AtCmdConfig::new(None, None, None, b'#', None));
uart0.set_rx_fifo_full_threshold(30).unwrap();
uart0.listen_at_cmd();
uart0.listen_rx_fifo_full();
timer0.start(1u64.secs());
critical_section::with(|cs| SERIAL.borrow_ref_mut(cs).replace(uart0));
interrupt::enable(
peripherals::Interrupt::UART0,
interrupt::Priority::Priority1,
)
.unwrap();
interrupt::set_kind(
Cpu::ProCpu,
interrupt::CpuInterrupt::Interrupt1, // Interrupt 1 handles priority one interrupts
interrupt::InterruptKind::Edge,
);
unsafe {
riscv::interrupt::enable();
}
loop {
critical_section::with(|cs| {
writeln!(SERIAL.borrow_ref_mut(cs).as_mut().unwrap(), "Hello World! Send a single `#` character or send at least 30 characters and see the interrupts trigger.").ok();
});
block!(timer0.wait()).unwrap();
}
}
#[interrupt]
fn UART0() {
critical_section::with(|cs| {
let mut serial = SERIAL.borrow_ref_mut(cs);
let serial = serial.as_mut().unwrap();
let mut cnt = 0;
while let nb::Result::Ok(_c) = serial.read() {
cnt += 1;
}
writeln!(serial, "Read {} bytes", cnt,).ok();
writeln!(
serial,
"Interrupt AT-CMD: {} RX-FIFO-FULL: {}",
serial.at_cmd_interrupt_set(),
serial.rx_fifo_full_interrupt_set(),
)
.ok();
serial.reset_at_cmd_interrupt();
serial.reset_rx_fifo_full_interrupt();
});
}