Skip to content

Commit c03c3e9

Browse files
esp32_hal example code updated
1 parent cb57665 commit c03c3e9

File tree

1 file changed

+24
-69
lines changed

1 file changed

+24
-69
lines changed

src/main.rs

+24-69
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,50 @@
11
#![no_std]
22
#![no_main]
33

4-
use panic_halt as _;
54
{% if mcu == "esp32" -%}
6-
use esp32_hal::target;
7-
use hal::prelude::*;
8-
use xtensa_lx::timer::delay;
9-
use esp32_hal as hal;
10-
11-
/// The default clock source is the onboard crystal
12-
/// In most cases 40mhz (but can be as low as 2mhz depending on the board)
13-
const CORE_HZ: u32 = 40_000_000;
14-
15-
const WDT_WKEY_VALUE: u32 = 0x50D83AA1;
5+
use esp32_hal::{clock::ClockControl, pac::Peripherals, prelude::*, timer::TimerGroup, Delay, Rtc, IO};
6+
use esp_backtrace as _;
167
{% else -%}
178
use esp8266_hal::{ prelude::*, target::Peripherals };
9+
use panic_halt as _;
1810
{% endif %}
1911

2012
#[xtensa_lx_rt::entry]
2113
fn main() -> ! {
14+
let peripherals = Peripherals::take().unwrap();
2215
{% if mcu == "esp32" -%}
23-
let dp = target::Peripherals::take().expect("Failed to obtain Peripherals");
24-
25-
let mut rtccntl = dp.RTCCNTL;
26-
let mut timg0 = dp.TIMG0;
27-
let mut timg1 = dp.TIMG1;
28-
29-
// (https://github.com/espressif/openocd-esp32/blob/97ba3a6bb9eaa898d91df923bbedddfeaaaf28c9/src/target/esp32.c#L431)
30-
// openocd disables the wdt's on halt
31-
// we will do it manually on startup
32-
disable_timg_wdts(&mut timg0, &mut timg1);
33-
disable_rtc_wdt(&mut rtccntl);
16+
let system = peripherals.DPORT.split();
17+
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
18+
let pins = IO::new(peripherals.GPIO, peripherals.IO_MUX).pins;
19+
let mut delay = Delay::new(&clocks);
20+
21+
// Disable the RTC and TIMG watchdog timers
22+
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
23+
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
24+
let mut wdt0 = timer_group0.wdt;
25+
let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks);
26+
let mut wdt1 = timer_group1.wdt;
27+
28+
rtc.rwdt.disable();
29+
wdt0.disable();
30+
wdt1.disable();
3431
{% else -%}
35-
let dp = Peripherals::take().unwrap();
32+
let pins = peripherals.GPIO.split();
3633
{% endif %}
3734

38-
let pins = dp.GPIO.split();
3935
let mut led = pins.gpio2.into_push_pull_output();
36+
led.set_high().unwrap();
4037

4138
{% if mcu == "esp8266" -%}
42-
let (mut timer1, _) = dp.TIMER.timers();
39+
let (mut timer1, _) = peripherals.TIMER.timers();
4340
{% endif %}
4441

4542
loop {
46-
led.set_high().unwrap();
43+
led.toggle().unwrap();
4744
{% if mcu == "esp32" -%}
48-
delay(CORE_HZ);
49-
{% else -%}
50-
timer1.delay_ms(500);
51-
{% endif %}
52-
led.set_low().unwrap();
53-
{% if mcu == "esp32" -%}
54-
delay(CORE_HZ);
45+
delay.delay_ms(500u32);
5546
{% else -%}
5647
timer1.delay_ms(500);
5748
{% endif %}
5849
}
5950
}
60-
61-
{% if mcu == "esp32" -%}
62-
fn disable_rtc_wdt(rtccntl: &mut target::RTCCNTL) {
63-
/* Disables the RTCWDT */
64-
rtccntl
65-
.wdtwprotect
66-
.write(|w| unsafe { w.bits(WDT_WKEY_VALUE) });
67-
rtccntl.wdtconfig0.modify(|_, w| unsafe {
68-
w.wdt_stg0()
69-
.bits(0x0)
70-
.wdt_stg1()
71-
.bits(0x0)
72-
.wdt_stg2()
73-
.bits(0x0)
74-
.wdt_stg3()
75-
.bits(0x0)
76-
.wdt_flashboot_mod_en()
77-
.clear_bit()
78-
.wdt_en()
79-
.clear_bit()
80-
});
81-
rtccntl.wdtwprotect.write(|w| unsafe { w.bits(0x0) });
82-
}
83-
84-
fn disable_timg_wdts(timg0: &mut target::TIMG0, timg1: &mut target::TIMG1) {
85-
timg0
86-
.wdtwprotect
87-
.write(|w| unsafe { w.bits(WDT_WKEY_VALUE) });
88-
timg1
89-
.wdtwprotect
90-
.write(|w| unsafe { w.bits(WDT_WKEY_VALUE) });
91-
92-
timg0.wdtconfig0.write(|w| unsafe { w.bits(0x0) });
93-
timg1.wdtconfig0.write(|w| unsafe { w.bits(0x0) });
94-
}
95-
{% endif %}

0 commit comments

Comments
 (0)