Skip to content

Commit 445143f

Browse files
committed
feat: Disable Watchdogs when USB-Serial-JTAG is used
Watchdog needs to be disabled in order to preven reset in the middle of a communication. By default watchdog is disabled after chip reset, but second stage bootloader may enable it again and if application does not disable it, it will reset the device, because USB-Serial/JTAG does only software reset.
1 parent 9517c8f commit 445143f

File tree

19 files changed

+2684
-0
lines changed

19 files changed

+2684
-0
lines changed

include/esp-stub-lib/usb_serial_jtag.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ extern "C"
1616

1717
#define USB_SERIAL_JTAG_OUT_RECV_PKT_INT_ENA (0x1 << 2)
1818

19+
/**
20+
* @brief Disable watchdogs for USB-Serial/JTAG
21+
*
22+
* This function disables the watchdogs for USB-Serial/JTAG to prevent the device from resetting.
23+
* By default watchdog is disabled after chip reset, but second stage bootloader may enable it again,
24+
* and if application does not disable it, it will reset the device, because USB-Serial/JTAG does only software reset.
25+
*/
26+
void stub_lib_usb_serial_jtag_disable_watchdogs(void);
27+
1928
/**
2029
* @brief Check if USB-Serial/JTAG is currently being used for communication
2130
*

src/target/base/include/target/usb_serial_jtag.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
#include <stdint.h>
1010
#include <stdbool.h>
1111

12+
/**
13+
* @brief Disable watchdogs for USB-Serial/JTAG
14+
*
15+
* This function disables the watchdogs for USB-Serial/JTAG to prevent the device from resetting.
16+
* By default watchdog is disabled after chip reset, but second stage bootloader may enable it again,
17+
* and if application does not disable it, it will reset the device, because USB-Serial/JTAG does only software reset.
18+
*/
19+
void stub_target_usb_serial_jtag_disable_watchdogs(void);
20+
1221
/**
1322
* @brief Attach interrupt handler to USB-Serial/JTAG and configure interrupts
1423
*

src/target/esp32c3/src/usb_serial_jtag.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,30 @@
1010
#include <target/usb_serial_jtag.h>
1111
#include <soc/usb_serial_jtag_reg.h>
1212
#include <soc/interrupt_core0_reg.h>
13+
#include <soc/rtc_cntl_reg.h>
1314
#include <soc_utils.h>
1415

1516
// External ROM functions
1617
extern void esp_rom_isr_attach(int int_num, void *handler, void *arg);
1718
extern void esp_rom_isr_unmask(int int_num);
1819
extern void esprv_intc_int_set_priority(int int_num, int priority);
1920

21+
#define RTC_CNTL_WDT_KEY 0x50D83AA1
22+
#define RTC_CNTL_SWD_KEY 0x8F1D312A
23+
24+
void stub_target_usb_serial_jtag_disable_watchdogs(void)
25+
{
26+
// Disable RWDT (RTC Watchdog)
27+
WRITE_PERI_REG(RTC_CNTL_WDTWPROTECT_REG, RTC_CNTL_WDT_WKEY);
28+
WRITE_PERI_REG(RTC_CNTL_WDTCONFIG0_REG, 0x0);
29+
WRITE_PERI_REG(RTC_CNTL_WDTWPROTECT_REG, 0x0);
30+
31+
// Configure SWD (Super Watchdog) to autofeed
32+
WRITE_PERI_REG(RTC_CNTL_SWD_WPROTECT_REG, RTC_CNTL_SWD_WKEY);
33+
SET_PERI_REG_MASK(RTC_CNTL_SWD_CONF_REG, RTC_CNTL_SWD_AUTO_FEED_EN);
34+
WRITE_PERI_REG(RTC_CNTL_SWD_WPROTECT_REG, 0x0);
35+
}
36+
2037
void stub_target_usb_serial_jtag_rominit_intr_attach(int intr_num, void *handler, uint32_t flags)
2138
{
2239
// Route USB interrupt to CPU

0 commit comments

Comments
 (0)