Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions example/stub_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <esp-stub-lib/mem_utils.h>
#include <esp-stub-lib/security.h>
#include <esp-stub-lib/uart.h>
#include <esp-stub-lib/usb_serial_jtag.h>

#include "stub_main.h"

Expand Down Expand Up @@ -63,6 +64,18 @@ static void example_security(void)
}
}

static void __attribute__((unused)) test_usb_serial_jtag_disable_watchdogs(void)
{
stub_lib_usb_serial_jtag_disable_watchdogs();
(void)stub_lib_usb_serial_jtag_is_active();
(void)stub_lib_usb_serial_jtag_rominit_intr_attach(17, NULL, 0);
(void)stub_lib_usb_serial_jtag_clear_intr_flags();
(void)stub_lib_usb_serial_jtag_is_data_available();
(void)stub_lib_usb_serial_jtag_read_rxfifo_byte();
(void)stub_lib_usb_serial_jtag_tx_one_char('A');
(void)stub_lib_usb_serial_jtag_tx_flush();
}

static int __attribute__((unused)) handle_test_uart(void)
{
void *uart_rx_interrupt_handler = NULL;
Expand Down
9 changes: 9 additions & 0 deletions include/esp-stub-lib/usb_serial_jtag.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ extern "C"

#define USB_SERIAL_JTAG_OUT_RECV_PKT_INT_ENA (0x1 << 2)

/**
* @brief Disable watchdogs for USB-Serial/JTAG
*
* This function disables the watchdogs for USB-Serial/JTAG to prevent the device from resetting.
* 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.
Comment on lines +22 to +24
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment - "preven" should be "prevent"

Copilot uses AI. Check for mistakes.
*/
void stub_lib_usb_serial_jtag_disable_watchdogs(void);

/**
* @brief Check if USB-Serial/JTAG is currently being used for communication
*
Expand Down
8 changes: 5 additions & 3 deletions src/target/base/include/target/usb_serial_jtag.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#include <stdbool.h>

/**
* @brief Check if USB-Serial/JTAG is supported on this chip
* @brief Disable watchdogs for USB-Serial/JTAG
*
* @return true if USB-Serial/JTAG is supported, false otherwise
* This function disables the watchdogs for USB-Serial/JTAG to prevent the device from resetting.
* 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.
Comment on lines +15 to +17
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment - "preven" should be "prevent"

Copilot uses AI. Check for mistakes.
*/
bool stub_target_usb_serial_jtag_is_supported(void);
void stub_target_usb_serial_jtag_disable_watchdogs(void);

/**
* @brief Attach interrupt handler to USB-Serial/JTAG and configure interrupts
Expand Down
1 change: 0 additions & 1 deletion src/target/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ set(common_srcs
src/mem_utils.c
src/security.c
src/uart.c
src/usb_serial_jtag.c
)

add_library(${ESP_COMMON_LIB} STATIC ${common_srcs})
16 changes: 14 additions & 2 deletions src/target/esp32c3/src/usb_serial_jtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@
#include <target/usb_serial_jtag.h>
#include <soc/usb_serial_jtag_reg.h>
#include <soc/interrupt_core0_reg.h>
#include <soc/rtc_cntl_reg.h>
#include <soc_utils.h>

// External ROM functions
extern void esp_rom_isr_attach(int int_num, void *handler, void *arg);
extern void esp_rom_isr_unmask(int int_num);
extern void esprv_intc_int_set_priority(int int_num, int priority);

bool stub_target_usb_serial_jtag_is_supported(void)
#define RTC_CNTL_WDT_KEY 0x50D83AA1
#define RTC_CNTL_SWD_KEY 0x8F1D312A

void stub_target_usb_serial_jtag_disable_watchdogs(void)
{
return true;
// Disable RWDT (RTC Watchdog)
WRITE_PERI_REG(RTC_CNTL_WDTWPROTECT_REG, RTC_CNTL_WDT_WKEY);
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The watchdog write protection key macro name uses "WKEY" but should use "KEY" to match the actual macro definition pattern. Line 27 writes RTC_CNTL_WDT_WKEY but the macro is defined as RTC_CNTL_WDT_KEY on line 21. This will cause a compilation error.

Copilot uses AI. Check for mistakes.
WRITE_PERI_REG(RTC_CNTL_WDTCONFIG0_REG, 0x0);
WRITE_PERI_REG(RTC_CNTL_WDTWPROTECT_REG, 0x0);

// Configure SWD (Super Watchdog) to autofeed
WRITE_PERI_REG(RTC_CNTL_SWD_WPROTECT_REG, RTC_CNTL_SWD_WKEY);
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The super watchdog write protection key macro name uses "WKEY" but should use "KEY" to match the actual macro definition pattern. Line 32 writes RTC_CNTL_SWD_WKEY but the macro is defined as RTC_CNTL_SWD_KEY on line 22. This will cause a compilation error.

Copilot uses AI. Check for mistakes.
SET_PERI_REG_MASK(RTC_CNTL_SWD_CONF_REG, RTC_CNTL_SWD_AUTO_FEED_EN);
WRITE_PERI_REG(RTC_CNTL_SWD_WPROTECT_REG, 0x0);
}

void stub_target_usb_serial_jtag_rominit_intr_attach(int intr_num, void *handler, uint32_t flags)
Expand Down
Loading
Loading