Skip to content

Commit ca726af

Browse files
committed
Flash erase button in GUI
1 parent 664f3d6 commit ca726af

File tree

3 files changed

+165
-9
lines changed

3 files changed

+165
-9
lines changed

src/stlink-gui/gui.c

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ static void stlink_gui_set_sensitivity(STlinkGUI *gui, gboolean sensitivity) {
101101
gtk_widget_set_sensitive(GTK_WIDGET(gui->reset_button), sensitivity && (gui->sl != NULL));
102102

103103
gtk_widget_set_sensitive(GTK_WIDGET(gui->export_button), sensitivity && (gui->sl != NULL));
104+
105+
gtk_widget_set_sensitive(GTK_WIDGET(gui->erase_button), sensitivity && (gui->sl != NULL));
104106
}
105107

106108
static void mem_view_init_headers(GtkTreeView *view) {
@@ -531,11 +533,12 @@ static void stlink_gui_set_disconnected(STlinkGUI *gui) {
531533
gtk_statusbar_push(gui->statusbar, gtk_statusbar_get_context_id(gui->statusbar, "conn"), "Disconnected");
532534

533535
gtk_widget_set_sensitive(GTK_WIDGET(gui->device_frame), FALSE);
534-
gtk_widget_set_sensitive(GTK_WIDGET(gui->flash_button), FALSE);
535-
gtk_widget_set_sensitive(GTK_WIDGET(gui->export_button), FALSE);
536-
gtk_widget_set_sensitive(GTK_WIDGET(gui->disconnect_button), FALSE);
537536
gtk_widget_set_sensitive(GTK_WIDGET(gui->connect_button), TRUE);
537+
gtk_widget_set_sensitive(GTK_WIDGET(gui->disconnect_button), FALSE);
538+
gtk_widget_set_sensitive(GTK_WIDGET(gui->flash_button), FALSE);
538539
gtk_widget_set_sensitive(GTK_WIDGET(gui->reset_button), FALSE);
540+
gtk_widget_set_sensitive(GTK_WIDGET(gui->export_button), FALSE);
541+
gtk_widget_set_sensitive(GTK_WIDGET(gui->erase_button), FALSE);
539542
}
540543

541544
static void disconnect_button_cb(GtkWidget *widget, gpointer data) {
@@ -614,7 +617,7 @@ static void open_button_cb(GtkWidget *widget, gpointer data) {
614617
stlink_gui_open_file(gui);
615618
}
616619

617-
static gboolean stlink_gui_write_flash_update(STlinkGUI *gui) {
620+
static gboolean stlink_gui_restore_after_background_thread(STlinkGUI *gui) {
618621
stlink_gui_set_sensitivity(gui, TRUE);
619622
gui->progress.activity_mode = FALSE;
620623
gtk_widget_hide(GTK_WIDGET(gui->progress.bar));
@@ -634,7 +637,7 @@ static gpointer stlink_gui_write_flash(gpointer data) {
634637
stlink_gui_set_info_error_message(gui, "Failed to write to flash");
635638
}
636639

637-
g_idle_add((GSourceFunc)stlink_gui_write_flash_update, gui);
640+
g_idle_add((GSourceFunc)stlink_gui_restore_after_background_thread, gui);
638641
return (NULL);
639642
}
640643

@@ -738,6 +741,42 @@ static void export_button_cb(GtkWidget *widget, gpointer data) {
738741
gtk_widget_destroy(dialog);
739742
}
740743

744+
static gpointer stlink_gui_erase_flash(gpointer data) {
745+
g_return_val_if_fail(STLINK_IS_GUI(data), NULL);
746+
STlinkGUI *gui = STLINK_GUI(data);
747+
748+
g_return_val_if_fail((gui->sl != NULL), NULL);
749+
750+
if(stlink_erase_flash_mass(gui->sl) < 0) {
751+
stlink_gui_set_info_error_message(gui, "Failed to mass erase flash");
752+
} else {
753+
stlink_gui_set_info_error_message(gui, "Mass erased flash");
754+
}
755+
756+
g_idle_add((GSourceFunc)stlink_gui_restore_after_background_thread, gui);
757+
return (NULL);
758+
}
759+
760+
static void erase_button_cb(GtkWidget *widget, gpointer data) {
761+
(void)widget;
762+
STlinkGUI *gui;
763+
gint result;
764+
765+
gui = STLINK_GUI(data);
766+
g_return_if_fail(gui->sl != NULL);
767+
768+
result = gtk_dialog_run(gui->erase_dialog);
769+
770+
if(result == GTK_RESPONSE_OK) {
771+
stlink_gui_set_sensitivity(gui, FALSE);
772+
gtk_progress_bar_set_text(gui->progress.bar, "Mass erasing flash");
773+
gui->progress.activity_mode = TRUE;
774+
gtk_widget_show(GTK_WIDGET(gui->progress.bar));
775+
776+
g_thread_new("erase_flash", (GThreadFunc)stlink_gui_erase_flash, gui);
777+
}
778+
}
779+
741780
static gboolean progress_pulse_timeout(STlinkGUI *gui) {
742781
if(gui->progress.activity_mode) {
743782
gtk_progress_bar_pulse(gui->progress.bar);
@@ -872,6 +911,9 @@ static void stlink_gui_build_ui(STlinkGUI *gui) {
872911
gui->export_button = GTK_TOOL_BUTTON(gtk_builder_get_object(builder, "export_button"));
873912
g_signal_connect(G_OBJECT(gui->export_button), "clicked", G_CALLBACK(export_button_cb), gui);
874913

914+
gui->erase_button = GTK_TOOL_BUTTON(gtk_builder_get_object(builder, "erase_button"));
915+
g_signal_connect(G_OBJECT(gui->erase_button), "clicked", G_CALLBACK(erase_button_cb), gui);
916+
875917
gui->devmem_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "devmem_treeview"));
876918
mem_view_init_headers(gui->devmem_treeview);
877919
devmem_store = gtk_list_store_new(5,
@@ -932,6 +974,12 @@ static void stlink_gui_build_ui(STlinkGUI *gui) {
932974
gui->flash_dialog_cancel = GTK_BUTTON(gtk_builder_get_object(builder, "flash_dialog_cancel_button"));
933975
gui->flash_dialog_entry = GTK_ENTRY(gtk_builder_get_object(builder, "flash_dialog_entry"));
934976

977+
/* Erase dialog */
978+
gui->erase_dialog = GTK_DIALOG(gtk_builder_get_object(builder, "erase_dialog"));
979+
g_signal_connect_swapped(gui->erase_dialog, "response", G_CALLBACK(gtk_widget_hide), gui->erase_dialog);
980+
gui->erase_dialog_ok = GTK_BUTTON(gtk_builder_get_object(builder, "erase_dialog_ok_button"));
981+
gui->erase_dialog_cancel = GTK_BUTTON(gtk_builder_get_object(builder, "erase_dialog_cancel_button"));
982+
935983
// make it so
936984
gtk_widget_show_all(GTK_WIDGET(gui->window));
937985
gtk_widget_hide(GTK_WIDGET(gui->infobar));
@@ -964,7 +1012,7 @@ int32_t main(int32_t argc, char **argv) {
9641012
}
9651013
if(argc == 1 && g_file_test(*argv, G_FILE_TEST_IS_REGULAR)){
9661014
/* Open hex file at app startup */
967-
gui->filename = g_strdup(*argv);
1015+
gui->filename = g_strdup(*argv);
9681016
g_idle_add((GSourceFunc)open_file_from_args, gui);
9691017
}
9701018
argc--;

src/stlink-gui/gui.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,25 @@ struct _STlinkGUI {
6060
GtkEntry *devmem_jmp_entry;
6161
GtkBox *filemem_box;
6262
GtkEntry *filemem_jmp_entry;
63+
GtkToolButton *open_button;
6364
GtkToolButton *connect_button;
6465
GtkToolButton *disconnect_button;
6566
GtkToolButton *flash_button;
66-
GtkToolButton *export_button;
67-
GtkToolButton *open_button;
6867
GtkToolButton *reset_button;
68+
GtkToolButton *export_button;
69+
GtkToolButton *erase_button;
6970

70-
/* flash dialog */
71+
/* Flash dialog */
7172
GtkDialog *flash_dialog;
7273
GtkButton *flash_dialog_ok;
7374
GtkButton *flash_dialog_cancel;
7475
GtkEntry *flash_dialog_entry;
7576

77+
/* Erase dialog */
78+
GtkDialog *erase_dialog;
79+
GtkButton *erase_dialog_ok;
80+
GtkButton *erase_dialog_cancel;
81+
7682
struct progress_t progress;
7783
struct mem_t flash_mem;
7884
struct mem_t file_mem;

src/stlink-gui/stlink-gui.ui

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,94 @@
103103
<action-widget response="-5">flash_dialog_ok_button</action-widget>
104104
</action-widgets>
105105
</object>
106+
<object class="GtkDialog" id="erase_dialog">
107+
<property name="can_focus">False</property>
108+
<property name="border_width">5</property>
109+
<property name="title" translatable="yes">Mass erase flash</property>
110+
<property name="type_hint">dialog</property>
111+
<child internal-child="vbox">
112+
<object class="GtkBox" id="erase_dialog-vbox">
113+
<property name="can_focus">False</property>
114+
<property name="orientation">vertical</property>
115+
<property name="spacing">2</property>
116+
<child internal-child="action_area">
117+
<object class="GtkButtonBox" id="erase_dialog-action_area1">
118+
<property name="can_focus">False</property>
119+
<property name="layout_style">end</property>
120+
<child>
121+
<object class="GtkButton" id="erase_dialog_cancel_button">
122+
<property name="label">gtk-cancel</property>
123+
<property name="visible">True</property>
124+
<property name="can_focus">False</property>
125+
<property name="receives_default">True</property>
126+
<property name="use_stock">True</property>
127+
</object>
128+
<packing>
129+
<property name="expand">False</property>
130+
<property name="fill">True</property>
131+
<property name="position">0</property>
132+
</packing>
133+
</child>
134+
<child>
135+
<object class="GtkButton" id="erase_dialog_ok_button">
136+
<property name="label">gtk-ok</property>
137+
<property name="visible">True</property>
138+
<property name="can_focus">True</property>
139+
<property name="has_focus">True</property>
140+
<property name="receives_default">True</property>
141+
<property name="use_stock">True</property>
142+
</object>
143+
<packing>
144+
<property name="expand">False</property>
145+
<property name="fill">True</property>
146+
<property name="position">1</property>
147+
</packing>
148+
</child>
149+
</object>
150+
<packing>
151+
<property name="expand">False</property>
152+
<property name="fill">True</property>
153+
<property name="pack_type">end</property>
154+
<property name="position">0</property>
155+
</packing>
156+
</child>
157+
<child>
158+
<object class="GtkGrid" id="erase_dialog_grid">
159+
<property name="visible">True</property>
160+
<property name="can_focus">False</property>
161+
<property name="margin_left">5</property>
162+
<property name="margin_right">5</property>
163+
<property name="margin_top">5</property>
164+
<property name="margin_bottom">5</property>
165+
<property name="column_spacing">5</property>
166+
<property name="column_homogeneous">True</property>
167+
<child>
168+
<object class="GtkLabel" id="erase_dialog_label">
169+
<property name="visible">True</property>
170+
<property name="can_focus">False</property>
171+
<property name="label" translatable="yes">This will permanently delete all code and data on the chip. This action cannot be undone.</property>
172+
</object>
173+
<packing>
174+
<property name="left_attach">0</property>
175+
<property name="top_attach">0</property>
176+
<property name="width">1</property>
177+
<property name="height">1</property>
178+
</packing>
179+
</child>
180+
</object>
181+
<packing>
182+
<property name="expand">False</property>
183+
<property name="fill">True</property>
184+
<property name="position">2</property>
185+
</packing>
186+
</child>
187+
</object>
188+
</child>
189+
<action-widgets>
190+
<action-widget response="0">erase_dialog_cancel_button</action-widget>
191+
<action-widget response="-5">erase_dialog_ok_button</action-widget>
192+
</action-widgets>
193+
</object>
106194
<object class="GtkWindow" id="window">
107195
<property name="can_focus">False</property>
108196
<property name="title" translatable="yes">STlink GUI</property>
@@ -203,6 +291,20 @@
203291
<property name="homogeneous">True</property>
204292
</packing>
205293
</child>
294+
<child>
295+
<object class="GtkToolButton" id="erase_button">
296+
<property name="visible">True</property>
297+
<property name="can_focus">False</property>
298+
<property name="tooltip_text" translatable="yes">Erase device memory</property>
299+
<property name="label" translatable="yes">Erase device memory</property>
300+
<property name="use_underline">True</property>
301+
<property name="stock_id">gtk-delete</property>
302+
</object>
303+
<packing>
304+
<property name="expand">False</property>
305+
<property name="homogeneous">True</property>
306+
</packing>
307+
</child>
206308
<child>
207309
<object class="GtkToolItem" id="toolProgressbar">
208310
<property name="visible">True</property>

0 commit comments

Comments
 (0)