Skip to content
This repository was archived by the owner on Mar 25, 2019. It is now read-only.

Commit 5e7763d

Browse files
author
Ilia Motornyi
authored
Customize and improve debug running
1 parent 7798245 commit 5e7763d

File tree

4 files changed

+171
-34
lines changed

4 files changed

+171
-34
lines changed

src/xyz/elmot/clion/openocd/OpenOcdComponent.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,28 @@
2828
import org.jetbrains.annotations.Nullable;
2929

3030
import java.io.File;
31+
import java.util.Objects;
3132
import java.util.concurrent.Future;
3233

3334
public class OpenOcdComponent {
3435

36+
@SuppressWarnings("WeakerAccess")
3537
public static final String SCRIPTS_PATH_SHORT = "scripts";
38+
@SuppressWarnings("WeakerAccess")
3639
public static final String SCRIPTS_PATH_LONG = "share/openocd/" + SCRIPTS_PATH_SHORT;
40+
@SuppressWarnings("WeakerAccess")
3741
public static final String BIN_OPENOCD;
42+
private static final Key<Long> UPLOAD_LOAD_COUNT_KEY = new Key<>(OpenOcdConfiguration.class.getName() + "#LAST_DOWNLOAD_MOD_COUNT");
3843
private static final String ERROR_PREFIX = "Error: ";
39-
private static final String[] IGNORED_STRINGS = { //todo take into use
44+
private static final String[] IGNORED_STRINGS = {
4045
"clearing lockup after double fault",
4146
"LIB_USB_NOT_SUPPORTED"};
4247

4348
private final static String[] FAIL_STRINGS = {
4449
"** Programming Failed **", "communication failure", "** OpenOCD init failed **"};
4550
private static final String FLASH_SUCCESS_TEXT = "** Programming Finished **";
4651
private static final Logger LOG = Logger.getInstance(OpenOcdComponent.class);
52+
private static final String ADAPTER_SPEED = "adapter speed";
4753

4854
static {
4955
BIN_OPENOCD = "bin/openocd" + (OS.isWindows() ? ".exe" : "");
@@ -58,7 +64,7 @@ public OpenOcdComponent() {
5864

5965
@SuppressWarnings("WeakerAccess")
6066
@NotNull
61-
public static GeneralCommandLine createOcdCommandLine(OpenOcdConfiguration config, File fileToLoad,@Nullable String additionalCommand, boolean shutdown) throws ConfigurationException {
67+
public static GeneralCommandLine createOcdCommandLine(OpenOcdConfiguration config, File fileToLoad, @Nullable String additionalCommand, boolean shutdown) throws ConfigurationException {
6268
Project project = config.getProject();
6369
OpenOcdSettingsState ocdSettings = project.getComponent(OpenOcdSettingsState.class);
6470
if (StringUtil.isEmpty(config.getBoardConfigFile())) {
@@ -125,7 +131,7 @@ public Future<STATUS> startOpenOcd(OpenOcdConfiguration config, @Nullable File f
125131
LOG.info("openOcd is already run");
126132
return new FutureResult<>(STATUS.FLASH_ERROR);
127133
}
128-
134+
VirtualFile virtualFile = fileToLoad != null ? VfsUtil.findFileByIoFile(fileToLoad, true) : null;
129135
Project project = config.getProject();
130136
try {
131137
process = new OSProcessHandler(commandLine) {
@@ -134,7 +140,7 @@ public boolean isSilentlyDestroyOnClose() {
134140
return true;
135141
}
136142
};
137-
DownloadFollower downloadFollower = new DownloadFollower();
143+
DownloadFollower downloadFollower = new DownloadFollower(virtualFile);
138144
process.addProcessListener(downloadFollower);
139145
RunContentExecutor openOCDConsole = new RunContentExecutor(project, process)
140146
.withTitle("OpenOCD console")
@@ -151,10 +157,6 @@ public boolean isSilentlyDestroyOnClose() {
151157
}
152158
}
153159

154-
public boolean isRun() {
155-
return process != null && !process.isProcessTerminated();
156-
}
157-
158160
public enum STATUS {
159161
FLASH_SUCCESS,
160162
FLASH_WARNING,
@@ -196,6 +198,12 @@ public int getHighlighterLayer() {
196198
}
197199

198200
private class DownloadFollower extends FutureResult<STATUS> implements ProcessListener {
201+
@Nullable
202+
private final VirtualFile vRunFile;
203+
204+
DownloadFollower(@Nullable VirtualFile vRunFile) {
205+
this.vRunFile = vRunFile;
206+
}
199207

200208
@Override
201209
public void startNotified(@NotNull ProcessEvent event) {
@@ -223,9 +231,16 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
223231
if (containsOneOf(text, FAIL_STRINGS)) {
224232
reset();
225233
set(STATUS.FLASH_ERROR);
234+
} else if (vRunFile == null && text.startsWith(ADAPTER_SPEED)) {
235+
reset();
236+
set(STATUS.FLASH_SUCCESS);
226237
} else if (text.equals(FLASH_SUCCESS_TEXT)) {
227238
reset();
239+
if (vRunFile != null) {
240+
UPLOAD_LOAD_COUNT_KEY.set(vRunFile, vRunFile.getModificationCount());
241+
}
228242
set(STATUS.FLASH_SUCCESS);
243+
229244
} else if (text.startsWith(ERROR_PREFIX) && !containsOneOf(text, IGNORED_STRINGS)) {
230245
reset();
231246
set(STATUS.FLASH_WARNING);
@@ -243,4 +258,11 @@ private boolean containsOneOf(String text, String[] sampleStrings) {
243258
return false;
244259

245260
}
261+
262+
@SuppressWarnings("WeakerAccess")
263+
public static boolean isLatestUploaded(File runFile) {
264+
VirtualFile vRunFile = VfsUtil.findFileByIoFile(runFile, true);
265+
Long latestDownloadModCount = UPLOAD_LOAD_COUNT_KEY.get(vRunFile);
266+
return vRunFile != null && Objects.equals(latestDownloadModCount, vRunFile.getModificationCount());
267+
}
246268
}

src/xyz/elmot/clion/openocd/OpenOcdConfiguration.java

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,65 @@ public class OpenOcdConfiguration extends CMakeAppRunConfiguration implements Ci
2727
private static final String ATTR_GDB_PORT = "gdb-port";
2828
private static final String ATTR_TELNET_PORT = "telnet-port";
2929
private static final String ATTR_BOARD_CONFIG = "board-config";
30+
public static final String ATTR_RESET_TYPE = "reset-type";
31+
public static final String ATTR_DOWNLOAD_TYPE = "download-type";
32+
public static final ResetType DEFAULT_RESET = ResetType.INIT;
3033
private int gdbPort = DEF_GDB_PORT;
3134
private int telnetPort = DEF_TELNET_PORT;
3235
private String boardConfigFile;
36+
private DownloadType downloadType = DownloadType.ALWAYS;
37+
private ResetType resetType = DEFAULT_RESET;
38+
39+
public enum DownloadType {
40+
41+
ALWAYS,
42+
UPDATED_ONLY,
43+
NONE;
44+
45+
@Override
46+
public String toString() {
47+
return toBeautyString(super.toString());
48+
}
49+
}
50+
51+
public static String toBeautyString(String obj) {
52+
return StringUtil.toTitleCase(obj.toLowerCase().replace("_", " "));
53+
}
54+
55+
public enum ResetType {
56+
RUN {
57+
@Override
58+
public String getCommand() {
59+
return "init;reset run;";
60+
}
61+
},
62+
INIT {
63+
@Override
64+
public String getCommand() {
65+
return "init;reset init;";
66+
}
67+
},
68+
HALT {
69+
@Override
70+
public String getCommand() {
71+
return "init;reset halt";
72+
}
73+
},
74+
NONE {
75+
@Override
76+
public String getCommand() {
77+
return "";
78+
}
79+
};
80+
81+
@Override
82+
public String toString() {
83+
return toBeautyString(super.toString());
84+
}
85+
86+
public abstract String getCommand();
87+
88+
}
3389

3490

3591
@SuppressWarnings("WeakerAccess")
@@ -47,16 +103,29 @@ public CidrCommandLineState getState(@NotNull Executor executor, @NotNull Execut
47103
public void readExternal(@NotNull Element element) throws InvalidDataException {
48104
super.readExternal(element);
49105
boardConfigFile = element.getAttributeValue(ATTR_BOARD_CONFIG, NAMESPACE);
50-
gdbPort = intAttribute(element, ATTR_GDB_PORT, DEF_GDB_PORT);
51-
telnetPort = intAttribute(element, ATTR_TELNET_PORT, DEF_TELNET_PORT);
106+
gdbPort = readIntAttr(element, ATTR_GDB_PORT, DEF_GDB_PORT);
107+
telnetPort = readIntAttr(element, ATTR_TELNET_PORT, DEF_TELNET_PORT);
108+
resetType = readEnumAttr(element, ATTR_RESET_TYPE, DEFAULT_RESET);
109+
downloadType = readEnumAttr(element, ATTR_DOWNLOAD_TYPE, DownloadType.ALWAYS);
52110
}
53111

54-
private int intAttribute(@NotNull Element element, String name, int def) {
112+
private int readIntAttr(@NotNull Element element, String name, int def) {
55113
String s = element.getAttributeValue(name, NAMESPACE);
56114
if (StringUtil.isEmpty(s)) return def;
57115
return Integer.parseUnsignedInt(s);
58116
}
59117

118+
private <T extends Enum> T readEnumAttr(@NotNull Element element, String name, T def) {
119+
String s = element.getAttributeValue(name, NAMESPACE);
120+
if (StringUtil.isEmpty(s)) return def;
121+
try {
122+
//noinspection unchecked
123+
return (T) Enum.valueOf(def.getClass(), s);
124+
} catch (Throwable t) {
125+
return def;
126+
}
127+
}
128+
60129
@Override
61130
public void writeExternal(@NotNull Element element) throws WriteExternalException {
62131
super.writeExternal(element);
@@ -65,6 +134,8 @@ public void writeExternal(@NotNull Element element) throws WriteExternalExceptio
65134
if (boardConfigFile != null) {
66135
element.setAttribute(ATTR_BOARD_CONFIG, boardConfigFile, NAMESPACE);
67136
}
137+
element.setAttribute(ATTR_RESET_TYPE, resetType.name(), NAMESPACE);
138+
element.setAttribute(ATTR_DOWNLOAD_TYPE, downloadType.name(), NAMESPACE);
68139
}
69140

70141
@Override
@@ -108,4 +179,20 @@ public String getBoardConfigFile() {
108179
public void setBoardConfigFile(String boardConfigFile) {
109180
this.boardConfigFile = boardConfigFile;
110181
}
182+
183+
public DownloadType getDownloadType() {
184+
return downloadType;
185+
}
186+
187+
public void setDownloadType(DownloadType downloadType) {
188+
this.downloadType = downloadType;
189+
}
190+
191+
public ResetType getResetType() {
192+
return resetType;
193+
}
194+
195+
public void setResetType(ResetType resetType) {
196+
this.resetType = resetType;
197+
}
111198
}

src/xyz/elmot/clion/openocd/OpenOcdConfigurationEditor.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import com.intellij.openapi.options.ConfigurationException;
55
import com.intellij.openapi.project.Project;
66
import com.intellij.openapi.vfs.VfsUtil;
7-
import com.intellij.ui.components.JBTextField;
87
import com.intellij.ui.components.fields.IntegerField;
98
import com.intellij.util.ui.GridBag;
109
import com.jetbrains.cidr.cpp.execution.CMakeAppRunConfiguration;
1110
import com.jetbrains.cidr.cpp.execution.CMakeAppRunConfigurationSettingsEditor;
1211
import com.jetbrains.cidr.cpp.execution.CMakeBuildConfigurationHelper;
12+
import org.jdesktop.swingx.JXRadioGroup;
1313
import org.jetbrains.annotations.NotNull;
14+
import xyz.elmot.clion.openocd.OpenOcdConfiguration.DownloadType;
15+
import xyz.elmot.clion.openocd.OpenOcdConfiguration.ResetType;
1416

1517
import javax.swing.*;
1618
import java.awt.*;
@@ -21,7 +23,10 @@ public class OpenOcdConfigurationEditor extends CMakeAppRunConfigurationSettings
2123

2224
private FileChooseInput boardConfigFile;
2325
private String openocdHome;
26+
private JXRadioGroup<DownloadType> downloadGroup;
27+
private JXRadioGroup<ResetType> resetGroup;
2428

29+
@SuppressWarnings("WeakerAccess")
2530
public OpenOcdConfigurationEditor(Project project, @NotNull CMakeBuildConfigurationHelper cMakeBuildConfigurationHelper) {
2631
super(project, cMakeBuildConfigurationHelper);
2732
}
@@ -39,6 +44,8 @@ protected void applyEditorTo(@NotNull CMakeAppRunConfiguration cMakeAppRunConfig
3944
telnetPort.validateContent();
4045
ocdConfiguration.setGdbPort(gdbPort.getValue());
4146
ocdConfiguration.setTelnetPort(telnetPort.getValue());
47+
ocdConfiguration.setDownloadType(downloadGroup.getSelectedValue());
48+
ocdConfiguration.setResetType(resetGroup.getSelectedValue());
4249
}
4350

4451
@Override
@@ -54,15 +61,17 @@ protected void resetEditorFrom(@NotNull CMakeAppRunConfiguration cMakeAppRunConf
5461

5562
gdbPort.setText("" + ocd.getGdbPort());
5663

57-
telnetPort.setText("" + ocd.getTelnetPort());
64+
telnetPort.setText("" + ocd.getTelnetPort());
65+
downloadGroup.setSelectedValue(ocd.getDownloadType());
66+
resetGroup.setSelectedValue(ocd.getResetType());
5867
}
5968

6069
@Override
6170
protected void createEditorInner(JPanel panel, GridBag gridBag) {
6271
super.createEditorInner(panel, gridBag);
6372

64-
for (Component component: panel.getComponents()) {
65-
if(component instanceof CommonProgramParametersPanel) {
73+
for (Component component : panel.getComponents()) {
74+
if (component instanceof CommonProgramParametersPanel) {
6675
component.setVisible(false);//todo get rid of this hack
6776
}
6877
}
@@ -71,23 +80,26 @@ protected void createEditorInner(JPanel panel, GridBag gridBag) {
7180
boardConfigFile = new FileChooseInput.BoardCfg("Board config", VfsUtil.getUserHomeDir(), this::getOpenocdHome);
7281
panel.add(boardConfigFile, gridBag.next().coverLine());
7382

74-
((JBTextField) boardConfigFile.getChildComponent()).getEmptyText().setText("<use project default>");
75-
76-
JPanel portsPanel = new JPanel();
77-
portsPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
83+
JPanel portsPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
7884

7985
gdbPort = addPortInput(portsPanel, "GDB port", OpenOcdConfiguration.DEF_GDB_PORT);
8086
portsPanel.add(Box.createHorizontalStrut(10));
8187

82-
telnetPort = addPortInput(portsPanel,"Telnet port",OpenOcdConfiguration.DEF_TELNET_PORT);
88+
telnetPort = addPortInput(portsPanel, "Telnet port", OpenOcdConfiguration.DEF_TELNET_PORT);
8389

8490
panel.add(portsPanel, gridBag.nextLine().next().coverLine());
8591

92+
panel.add(new JLabel("Download:"), gridBag.nextLine().next());
93+
downloadGroup = new JXRadioGroup<>(DownloadType.values());
94+
panel.add(downloadGroup, gridBag.next().fillCellHorizontally());
95+
panel.add(new JLabel("Reset:"), gridBag.nextLine().next());
96+
resetGroup = new JXRadioGroup<>(ResetType.values());
97+
panel.add(resetGroup, gridBag.next());
8698
}
8799

88100
private IntegerField addPortInput(JPanel portsPanel, String label, int defaultValue) {
89101
portsPanel.add(new JLabel(label + ": "));
90-
IntegerField field = new IntegerField(label, 1024,65535);
102+
IntegerField field = new IntegerField(label, 1024, 65535);
91103
field.setDefaultValue(defaultValue);
92104
field.setColumns(5);
93105
portsPanel.add(field);

0 commit comments

Comments
 (0)