Skip to content

[openocd] srst reset #17

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

Open
wants to merge 1 commit into
base: master
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
51 changes: 39 additions & 12 deletions lib/openocd.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const MCU_INFO = [
baseMcu: 'rtl872x',
targetConfig: 'rtl872x.tcl',
mcuManufacturer: 'Realtek',
deviceIdProcedure: 'rtl872x_read_efuse_mac; rtl872x_wdg_reset',
deviceIdProcedure: 'reset_config none; rtl872x_read_efuse_mac; rtl872x_wdg_reset',
deviceIdPrefix: '0a10aced2021',
deviceIdRegex: new RegExp(`MAC:\\s([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2})`),
// FIXME: verification is disabled, it fails on some versions of OpenOCD
Expand Down Expand Up @@ -473,21 +473,29 @@ class OpenOcdDevice extends Device {
this._openocd.on('error', err => {
this._log.error(err.message);
});
this._usesSrst = false;
}

async open(/* options */) {
if (this._openocd.state !== OpenOcdState.STOPPED) {
throw new Error('Device is already open');
}
try {
let srstResetWorked = false;
if (!this._target) {
this._log.verbose('Detecting target platform');
let platform = null;
try {
platform = await this._detectPlatform({ assertSrst: false });
} catch (err) {
this._log.verbose('Retrying with asserted SRST');
platform = await this._detectPlatform({ assertSrst: true });
try {
this._log.verbose('Retrying after SRST reset');
platform = await this._detectPlatform({ resetWithSrst: true });
srstResetWorked = true;
} catch (e) {
this._log.verbose('Retrying with asserted SRST');
platform = await this._detectPlatform({ assertSrst: true });
}
}
this._log.verbose(`Target platform: ${platform.baseMcu}`);
this._target = platform;
Expand All @@ -498,16 +506,21 @@ class OpenOcdDevice extends Device {
'gdb_port disabled',
'tcl_port disabled'
];
this._usesSrst = false;
if (this._target.assertSrstOnConnect) {
cmds.push('reset_config connect_assert_srst srst_only srst_nogate');
} else if (srstResetWorked) {
cmds.push('reset_config srst_only');
cmds.push('adapter srst delay 1000');
this._usesSrst = true;
}
cmds.push('init');
const args = [
'-f', `interface/${this._info.interfaceConfig}`,
'-f', `target/${this._target.targetConfig}`,
'-c', cmds.join('; ')
];
await this._startOpenOcd(args, { resetAndHalt: this._target.assertSrstOnConnect });
await this._startOpenOcd(args, { resetAndHalt: this._target.assertSrstOnConnect, resetAndInit: srstResetWorked });
if (!this.id) {
this.id = await this._getDeviceId();
this._log.verbose('Device ID:', this.id);
Expand All @@ -519,7 +532,7 @@ class OpenOcdDevice extends Device {
}

async close() {
if (this._target && this._target.assertSrstOnConnect && this._openocd.state === OpenOcdState.RUNNING &&
if (((this._target && this._target.assertSrstOnConnect) || this._usesSrst) && this._openocd.state === OpenOcdState.RUNNING &&
!this._openocd.isCommandRunning) {
try {
await this._resetTarget('run');
Expand Down Expand Up @@ -601,7 +614,7 @@ class OpenOcdDevice extends Device {
return super.log;
}

async _detectPlatform({ assertSrst = false } = {}) {
async _detectPlatform({ assertSrst = false, resetWithSrst = false } = {}) {
try {
const platformMcu = this._info.platformMcu;
if (platformMcu.length === 1) {
Expand All @@ -626,12 +639,16 @@ class OpenOcdDevice extends Device {
if (assertSrst) {
cmds.push('reset_config connect_assert_srst srst_only srst_nogate');
}
if (resetWithSrst) {
cmds.push('reset_config srst_only');
cmds.push('adapter srst delay 1000');
}
cmds.push('init');
const args = [
'-f', `interface/${this._info.interfaceConfig}`,
'-c', cmds.join('; ')
];
await this._startOpenOcd(args, { resetAndHalt: assertSrst });
await this._startOpenOcd(args, { resetAndHalt: assertSrst, resetAndInit: resetWithSrst });
let resp = '';
for (let i = 0; i < ARM_MAX_DEBUG_PORTS; i++) {
const r = await this._openocd.command(`dap info ${i}`);
Expand All @@ -648,7 +665,7 @@ class OpenOcdDevice extends Device {
break;
}
}
if (assertSrst) {
if (assertSrst || resetWithSrst) {
await this._resetTarget('run');
}
if (!platform) {
Expand Down Expand Up @@ -688,7 +705,7 @@ class OpenOcdDevice extends Device {
return id;
}

async _startOpenOcd(args, { resetAndHalt = false } = {}) {
async _startOpenOcd(args, { resetAndHalt = false, resetAndInit = false } = {}) {
const now = Date.now();
let startDelay = 0;
const restartInterval = MIN_OPENOCD_RESTART_INTERVAL +
Expand All @@ -697,7 +714,7 @@ class OpenOcdDevice extends Device {
if (dt < restartInterval) {
startDelay = restartInterval - dt;
}
if (resetAndHalt) {
if (resetAndHalt || resetAndInit) {
dt = now - this._lastReset;
if (dt < MIN_DEVICE_RESET_INTERVAL) {
dt = MIN_DEVICE_RESET_INTERVAL - dt;
Expand All @@ -718,6 +735,8 @@ class OpenOcdDevice extends Device {
}
if (resetAndHalt) {
await this._resetTarget('halt');
} else if (resetAndInit) {
await this._resetTarget('init');
}
}

Expand All @@ -734,9 +753,17 @@ class OpenOcdDevice extends Device {
if (dt < MIN_DEVICE_RESET_INTERVAL) {
await delay(MIN_DEVICE_RESET_INTERVAL - dt);
}

let resetHandled = false;
if (mode === 'run' && this._target && this._target.resetRunProcedure) {
await this._openocd.command(this._target.resetRunProcedure);
} else {
try {
await this._openocd.command(this._target.resetRunProcedure);
resetHandled = true;
} catch (err) {
// Ignore
}
}
if (!resetHandled) {
const resp = await this._openocd.command('reset ' + mode);
if ((mode === 'init' || mode === 'halt') && !resp.match(/target halted due to/i)) {
this._log.debug('Falling back to soft reset and halt');
Expand Down