|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +extern crate esp32_hal as hal; |
| 5 | +extern crate xtensa_lx6_rt; |
| 6 | + |
| 7 | +use { |
| 8 | + core::{fmt::Write, panic::PanicInfo}, |
| 9 | + hal::{ |
| 10 | + clock_control::{self, sleep, CPUSource, ClockControl, ClockControlConfig}, |
| 11 | + dport::Split, |
| 12 | + dprintln, i2c, |
| 13 | + prelude::*, |
| 14 | + serial::{config::Config as SerialConfig, NoRx, NoTx, Serial}, |
| 15 | + timer::Timer, |
| 16 | + }, |
| 17 | + ssd1306::{prelude::*, Builder}, |
| 18 | +}; |
| 19 | + |
| 20 | +const CORE_HZ: u32 = 10_000_000; |
| 21 | + |
| 22 | +#[no_mangle] |
| 23 | +fn main() -> ! { |
| 24 | + let dp = esp32::Peripherals::take().unwrap(); |
| 25 | + |
| 26 | + let (mut dport, dport_clock_control) = dp.DPORT.split(); |
| 27 | + |
| 28 | + // setup clocks & watchdog |
| 29 | + let mut clkcntrl = ClockControl::new( |
| 30 | + dp.RTCCNTL, |
| 31 | + dp.APB_CTRL, |
| 32 | + dport_clock_control, |
| 33 | + clock_control::XTAL_FREQUENCY_AUTO, |
| 34 | + ) |
| 35 | + .unwrap(); |
| 36 | + |
| 37 | + // set desired clock frequencies |
| 38 | + clkcntrl |
| 39 | + .set_cpu_frequencies( |
| 40 | + CPUSource::PLL, |
| 41 | + 80.MHz(), |
| 42 | + CPUSource::PLL, |
| 43 | + 240.MHz(), |
| 44 | + CPUSource::PLL, |
| 45 | + 80.MHz(), |
| 46 | + ) |
| 47 | + .unwrap(); |
| 48 | + |
| 49 | + let (clkcntrl_config, mut watchdog) = clkcntrl.freeze().unwrap(); |
| 50 | + watchdog.disable(); |
| 51 | + let (_, _, _, mut watchdog0) = Timer::new(dp.TIMG0, clkcntrl_config); |
| 52 | + watchdog0.disable(); |
| 53 | + let (_, _, _, mut watchdog1) = Timer::new(dp.TIMG1, clkcntrl_config); |
| 54 | + watchdog1.disable(); |
| 55 | + |
| 56 | + let pins = dp.GPIO.split(); |
| 57 | + |
| 58 | + let mut rst = pins.gpio16.into_push_pull_output(); |
| 59 | + let sda = pins.gpio4.into_open_drain_output(); |
| 60 | + let scl = pins.gpio15.into_open_drain_output(); |
| 61 | + |
| 62 | + let mut serial = Serial::uart0( |
| 63 | + dp.UART0, |
| 64 | + (NoTx, NoRx), |
| 65 | + SerialConfig::default().baudrate(115200.into()), |
| 66 | + clkcntrl_config, |
| 67 | + &mut dport, |
| 68 | + ) |
| 69 | + .unwrap(); |
| 70 | + |
| 71 | + writeln!(serial, "\n\n\nserial initialized").unwrap(); |
| 72 | + |
| 73 | + let mut disp: GraphicsMode<_> = { |
| 74 | + let i2c = i2c::I2C::new(dp.I2C0, i2c::Pins { sda, scl }, &mut dport); |
| 75 | + Builder::new().connect_i2c(i2c).into() |
| 76 | + }; |
| 77 | + writeln!(serial, "display built").unwrap(); |
| 78 | + rst.set_low().unwrap(); |
| 79 | + sleep(1.s()); |
| 80 | + rst.set_high().unwrap(); |
| 81 | + |
| 82 | + disp.init().unwrap(); |
| 83 | + writeln!(serial, "display initialised").unwrap(); |
| 84 | + disp.set_pixel(10, 10, 1); |
| 85 | + disp.set_pixel(20, 20, 1); |
| 86 | + disp.flush().unwrap(); |
| 87 | + writeln!(serial, "display flushed").unwrap(); |
| 88 | + |
| 89 | + loop { |
| 90 | + writeln!(serial, "tick").unwrap(); |
| 91 | + sleep(500.ms()); |
| 92 | + writeln!(serial, "tock").unwrap(); |
| 93 | + sleep(500.ms()); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[panic_handler] |
| 98 | +fn panic(info: &PanicInfo) -> ! { |
| 99 | + // park the other core |
| 100 | + unsafe { ClockControlConfig {}.park_core(esp32_hal::get_other_core()) }; |
| 101 | + |
| 102 | + // print panic message |
| 103 | + dprintln!("\n\n*** {:?}", info); |
| 104 | + |
| 105 | + // park this core |
| 106 | + unsafe { ClockControlConfig {}.park_core(esp32_hal::get_core()) }; |
| 107 | + |
| 108 | + dprintln!("Not reached because core is parked."); |
| 109 | + |
| 110 | + // this statement will not be reached, but is needed to make this a diverging function |
| 111 | + loop {} |
| 112 | +} |
0 commit comments