Scott Mabin 248fb356f8
PeripheralRef init: uart (#272)
* Add the peripheral module plus some helper macros in preparation

* peripheral macro

* Add peripheral generation macro

* Fixes after rebase

* Update the signature of Peripherals::take

* syncronise hello world example

* fmt the entire repo

Co-authored-by: Jesse Braham <jesse@beta7.io>
2022-12-12 14:45:33 +00:00

76 lines
2.0 KiB
Rust

//! This example shows how to use the DAC on PIN 17 and 18
//! You can connect an LED (with a suitable resistor) or check the changing
//! voltage using a voltmeter on those pins.
#![no_std]
#![no_main]
use esp32s2_hal::{
clock::ClockControl,
dac,
gpio::IO,
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
Delay,
Rtc,
};
use esp_backtrace as _;
use xtensa_atomic_emulation_trap as _;
use xtensa_lx_rt::entry;
#[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 wdt = timer_group0.wdt;
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
// Disable MWDT and RWDT (Watchdog) flash boot protection
wdt.disable();
rtc.rwdt.disable();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let pin17 = io.pins.gpio17.into_analog();
let pin18 = io.pins.gpio18.into_analog();
// Create DAC instances
let analog = peripherals.SENS.split();
let mut dac1 = dac::DAC1::dac(analog.dac1, pin17).unwrap();
let mut dac2 = dac::DAC2::dac(analog.dac2, pin18).unwrap();
let mut delay = Delay::new(&clocks);
let mut voltage_dac1: u8 = 200;
let mut voltage_dac2: u8 = 255;
loop {
// Change voltage on the pins using write function
voltage_dac1 = voltage_dac1.wrapping_add(1);
dac1.write(voltage_dac1);
voltage_dac2 = voltage_dac2.wrapping_sub(1);
dac2.write(voltage_dac2);
delay.delay_ms(50u32);
}
}
#[xtensa_lx_rt::exception]
fn exception(
cause: xtensa_lx_rt::exception::ExceptionCause,
frame: xtensa_lx_rt::exception::Context,
) {
use esp_println::*;
println!("\n\nException occured {:?} {:x?}", cause, frame);
let backtrace = esp_backtrace::arch::backtrace();
for b in backtrace.iter() {
if let Some(addr) = b {
println!("0x{:x}", addr)
}
}
}