-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathopenocd.ts
201 lines (166 loc) · 6.5 KB
/
openocd.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { DebugProtocol } from 'vscode-debugprotocol';
import { GDBServerController, ConfigurationArguments, SWOConfigureEvent, calculatePortMask, createPortName } from './common';
import * as os from 'os';
import * as tmp from 'tmp';
import * as fs from 'fs';
import * as ChildProcess from 'child_process';
import { EventEmitter } from 'events';
export class OpenOCDServerController extends EventEmitter implements GDBServerController {
public portsNeeded = ['gdbPort'];
public name = 'OpenOCD';
private swoPath: string;
private args: ConfigurationArguments;
private ports: { [name: string]: number };
constructor() {
super();
this.swoPath = tmp.tmpNameSync();
}
public setPorts(ports: { [name: string]: number }): void {
this.ports = ports;
}
public setArguments(args: ConfigurationArguments): void {
this.args = args;
}
public customRequest(command: string, response: DebugProtocol.Response, args: any): boolean {
return false;
}
public initCommands(): string[] {
const gdbport = this.ports[createPortName(this.args.targetProcessor)];
return [
`target-select extended-remote localhost:${gdbport}`
];
}
public launchCommands(): string[] {
const commands = [
'interpreter-exec console "monitor reset halt"',
'target-download',
'interpreter-exec console "monitor reset halt"',
'enable-pretty-printing'
];
return commands;
}
public attachCommands(): string[] {
const commands = [
'interpreter-exec console "monitor halt"',
'enable-pretty-printing'
];
return commands;
}
public restartCommands(): string[] {
const commands: string[] = [
'interpreter-exec console "monitor reset halt"'
];
return commands;
}
public swoCommands(): string[] {
const commands = [];
if (this.args.swoConfig.enabled) {
const swocommands = this.SWOConfigurationCommands();
commands.push(...swocommands);
}
return commands;
}
private SWOConfigurationCommands(): string[] {
const portMask = '0x' + calculatePortMask(this.args.swoConfig.decoders).toString(16);
const swoFrequency = this.args.swoConfig.swoFrequency;
const cpuFrequency = this.args.swoConfig.cpuFrequency;
const ratio = Math.floor(cpuFrequency / swoFrequency) - 1;
const commands: string[] = [
'EnableITMAccess',
`BaseSWOSetup ${ratio}`,
'SetITMId 1',
'ITMDWTTransferEnable',
'DisableITMPorts 0xFFFFFFFF',
`EnableITMPorts ${portMask}`,
'EnableDWTSync',
'ITMSyncEnable',
'ITMGlobalEnable'
];
commands.push(this.args.swoConfig.profile ? 'EnablePCSample' : 'DisablePCSample');
return commands.map((c) => `interpreter-exec console "${c}"`);
}
public serverExecutable(): string {
if (this.args.serverpath) { return this.args.serverpath; }
else {
return os.platform() === 'win32' ? 'openocd.exe' : 'openocd';
}
}
public serverArguments(): string[] {
const gdbport = this.ports['gdbPort'];
let serverargs = [];
serverargs.push('-c', `gdb_port ${gdbport}`);
this.args.searchDir.forEach((cs, idx) => {
serverargs.push('-s', cs);
});
if (this.args.searchDir.length === 0) {
serverargs.push('-s', this.args.cwd);
}
for (const cmd of this.args.openOCDPreConfigLaunchCommands || []) {
serverargs.push('-c', cmd);
}
this.args.configFiles.forEach((cf, idx) => {
serverargs.push('-f', cf);
});
if (this.args.rtos) {
const tmpCfgPath = tmp.tmpNameSync();
fs.writeFileSync(tmpCfgPath, `$_TARGETNAME configure -rtos ${this.args.rtos}\n`, 'utf8');
serverargs.push('-f', tmpCfgPath);
}
if (this.args.serverArgs) {
serverargs = serverargs.concat(this.args.serverArgs);
}
const commands = [];
if (this.args.swoConfig.enabled) {
let tpiuIntExt;
if (os.platform() === 'win32') {
this.swoPath = this.swoPath.replace(/\\/g, '/');
}
if (this.args.swoConfig.source === 'probe') {
tpiuIntExt = `internal ${this.swoPath}`;
}
else {
tpiuIntExt = 'external';
}
// tslint:disable-next-line:max-line-length
commands.push(`tpiu config ${tpiuIntExt} uart off ${this.args.swoConfig.cpuFrequency} ${this.args.swoConfig.swoFrequency}`);
}
if (commands.length > 0) {
serverargs.push('-c', commands.join('; '));
}
for (const cmd of this.args.openOCDLaunchCommands || []) {
serverargs.push('-c', cmd);
}
return serverargs;
}
public initMatch(): RegExp {
/*
// Following will work with or without the -d flag to openocd or using the tcl
// command `debug_level 3`; and we are looking specifically for gdb port(s) opening up
// When debug is enabled, you get too many matches looking for the cpu. This message
// has been there atleast since 2016-12-19
*/
return /Info\s:[^\n]*Listening on port \d+ for gdb connection/i;
}
public serverLaunchStarted(): void {
if (this.args.swoConfig.enabled && this.args.swoConfig.source === 'probe' && os.platform() !== 'win32') {
const mkfifoReturn = ChildProcess.spawnSync('mkfifo', [this.swoPath]);
this.emit('event', new SWOConfigureEvent({ type: 'fifo', path: this.swoPath }));
}
}
public serverLaunchCompleted(): void {
if (this.args.swoConfig.enabled) {
if (this.args.swoConfig.source === 'probe' && os.platform() === 'win32') {
this.emit('event', new SWOConfigureEvent({ type: 'file', path: this.swoPath }));
}
else if (this.args.swoConfig.source !== 'probe') {
this.emit('event', new SWOConfigureEvent({
type: 'serial',
device: this.args.swoConfig.source,
baudRate: this.args.swoConfig.swoFrequency
}));
}
}
}
public debuggerLaunchStarted(): void {}
public debuggerLaunchCompleted(): void {}
}