Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powering down the DP when disabling DAPLink SWD control. #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
52 changes: 49 additions & 3 deletions source/daplink/interface/swd_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ typedef struct {
uint32_t xpsr;
} DEBUG_STATE;

static bool swd_enabled = false;
static DAP_STATE dap_state;

static uint8_t swd_read_core_register(uint32_t n, uint32_t *val);
Expand Down Expand Up @@ -111,12 +112,38 @@ uint8_t swd_init(void)
// and fixed.
DAP_Setup();
PORT_SWD_SETUP();
swd_enabled = true;
return 1;
}

uint8_t swd_off(void)
{
// Clear CSYSPWRUPREQ and CDBGPWRUPREQ if SWD had been enabled. This is to ensure we don't
// leave the target's debug logic powered up.
if (swd_enabled) {

// Ensure CTRL/STAT register selected in DPBANKSEL
if (!swd_write_dp(DP_SELECT, 0)) {
return 0;
}
if (!swd_write_dp(DP_ABORT, 0x04)) {
return 0;
}

// Power down.
if (!swd_write_dp(DP_CTRL_STAT, CDBGRSTREQ)) {
return 0;
}

//The debugger requires reading from it before it succesfully resets itself
uint32_t temp = 0;
if (!swd_read_dp(DP_CTRL_STAT, &temp)) {
return 0;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

swd_off is just for shutting off the pins. This logic should be added to swd_uninit_debug().

PORT_OFF();
swd_enabled = false;
return 1;
}

Expand Down Expand Up @@ -835,17 +862,17 @@ uint8_t swd_uninit_debug(void)
// Assume debug is powered up already
// Clear (CSYSPWRUPREQ | CDBGPWRUPREQ) in DP_CTRL_STAT register
uint32_t ctrl_stat_read;

if (!swd_write_dp(DP_CTRL_STAT, 0x0)) {
return 0;
}

do {
if (!swd_read_dp(DP_CTRL_STAT, &ctrl_stat_read)) {
return 0;
}
} while ((ctrl_stat_read & (CSYSPWRUPREQ | CDBGPWRUPREQ)));

return 1;
}

Expand Down Expand Up @@ -916,6 +943,16 @@ uint8_t swd_set_target_state_hw(TARGET_RESET_STATE state)
return 0;
}

// Ensure CTRL/STAT register selected in DPBANKSEL
if (!swd_write_dp(DP_SELECT, 0)) {
return 0;
}

// Power down, reset the debugger
if (!swd_write_dp(DP_CTRL_STAT, CDBGRSTREQ)) {
return 0;
}

break;

case DEBUG:
Expand Down Expand Up @@ -1015,6 +1052,15 @@ uint8_t swd_set_target_state_sw(TARGET_RESET_STATE state)
if (!swd_write_word(DBG_HCSR, DBGKEY)) {
return 0;
}
// Ensure CTRL/STAT register selected in DPBANKSEL
if (!swd_write_dp(DP_SELECT, 0)) {
return 0;
}

// Power down, reset the debugger
if (!swd_write_dp(DP_CTRL_STAT, CDBGRSTREQ)) {
return 0;
}

break;

Expand Down