55use std:: { io, os:: fd:: AsRawFd } ;
66use std:: { thread:: sleep, time:: Duration } ;
77
8+ use bitflags:: bitflags;
89#[ cfg( unix) ]
910use libc:: ioctl;
1011use log:: debug;
@@ -208,7 +209,7 @@ trait RtcWdtReset {
208209
209210impl RtcWdtReset for crate :: targets:: esp32c3:: Esp32c3 {
210211 fn wdt_wprotect ( & self ) -> u32 {
211- 0x6000_8000 + 0x00B0
212+ 0x6000_8000 + 0x00A0
212213 }
213214 fn wdt_wkey ( & self ) -> u32 {
214215 0x50D8_3AA1
@@ -223,16 +224,16 @@ impl RtcWdtReset for crate::targets::esp32c3::Esp32c3 {
223224
224225impl RtcWdtReset for crate :: targets:: esp32s2:: Esp32s2 {
225226 fn wdt_wprotect ( & self ) -> u32 {
226- 0x6000_F000 + 0x00B0
227+ 0x3F40_8000 + 0x00AC
227228 }
228229 fn wdt_wkey ( & self ) -> u32 {
229- 0xA123_B456
230+ 0x50D8_3AA1
230231 }
231232 fn wdt_config0 ( & self ) -> u32 {
232- 0x6000_F000 + 0x0090
233+ 0x3F40_8000 + 0x0094
233234 }
234235 fn wdt_config1 ( & self ) -> u32 {
235- 0x6000_F000 + 0x0094
236+ 0x3F40_8000 + 0x0098
236237 }
237238}
238239
@@ -241,13 +242,13 @@ impl RtcWdtReset for crate::targets::esp32s3::Esp32s3 {
241242 0x6000_E000 + 0x00B0
242243 }
243244 fn wdt_wkey ( & self ) -> u32 {
244- 0xB987_C654
245+ 0x50D8_3AA1
245246 }
246247 fn wdt_config0 ( & self ) -> u32 {
247- 0x6000_E000 + 0x0090
248+ 0x6000_E000 + 0x0098
248249 }
249250 fn wdt_config1 ( & self ) -> u32 {
250- 0x6000_E000 + 0x0094
251+ 0x6000_E000 + 0x009C
251252 }
252253}
253254
@@ -346,6 +347,15 @@ pub fn soft_reset(
346347 Ok ( ( ) )
347348}
348349
350+ bitflags ! {
351+ struct WdtConfig0Flags : u32 {
352+ const EN = 1 << 31 ;
353+ const STAGE0 = 5 << 28 ; // 5 (binary: 101) in bits 28-30
354+ const CHIP_RESET_EN = 1 << 8 ; // 8th bit
355+ const CHIP_RESET_WIDTH = 1 << 2 ; // 1st bit
356+ }
357+ }
358+
349359/// Perform Watchdog reset
350360pub fn wdt_reset ( chip : Chip , connection : & mut Connection ) -> Result < ( ) , Error > {
351361 debug ! ( "Resetting with RTC WDT" ) ;
@@ -358,14 +368,19 @@ pub fn wdt_reset(chip: Chip, connection: &mut Connection) -> Result<(), Error> {
358368 } ;
359369
360370 connection. write_reg ( chip. wdt_wprotect ( ) , chip. wdt_wkey ( ) , None ) ?;
361- connection. write_reg ( chip. wdt_config1 ( ) , 5000 , None ) ?;
371+ connection. write_reg ( chip. wdt_config1 ( ) , 2000 , None ) ?;
362372 connection. write_reg (
363373 chip. wdt_config0 ( ) ,
364- ( 1 << 31 ) | ( 5 << 28 ) | ( 1 << 8 ) | 2 ,
374+ ( WdtConfig0Flags :: EN . bits ( ) ) // enable RTC watchdog
375+ | ( WdtConfig0Flags :: STAGE0 . bits ( ) ) // enable at the interrupt/system and RTC stage
376+ | ( WdtConfig0Flags :: CHIP_RESET_EN . bits ( ) ) // enable chip reset
377+ | WdtConfig0Flags :: CHIP_RESET_WIDTH . bits ( ) , // set chip reset width
365378 None ,
366379 ) ?;
367380 connection. write_reg ( chip. wdt_wprotect ( ) , 0 , None ) ?;
368381
382+ sleep ( Duration :: from_millis ( 50 ) ) ;
383+
369384 Ok ( ( ) )
370385}
371386
0 commit comments