commit 0559c3666e27b7bed69d58ec5ef2825b87ed796d
parent a60e6844cb0c136a9cd1f7113659c9db840e82d1
Author: Brian Swetland <swetland@frotz.net>
Date: Tue, 7 Mar 2023 20:51:41 -0800
show some status information
- add a status callback to dc_create()
- call it when the connection status changes
- xdebug: adjust the statusbar to indicate active status
Diffstat:
5 files changed, 63 insertions(+), 23 deletions(-)
diff --git a/src/transport-dap.c b/src/transport-dap.c
@@ -20,13 +20,20 @@ uint32_t dc_get_attn_value(DC *dc) {
return dc->attn;
}
+void dc_set_status(DC* dc, uint32_t status) {
+ dc->status = status;
+ if (dc->status_callback) {
+ dc->status_callback(dc->status_cookie, status);
+ }
+}
+
static void usb_failure(DC* dc, int status) {
ERROR("usb_failure status %d usb %p\n", status, dc->usb);
if (dc->usb != NULL) {
usb_close(dc->usb);
dc->usb = NULL;
}
- dc->status = DC_OFFLINE;
+ dc_set_status(dc, DC_OFFLINE);
}
static int dap_get_info(DC* dc, unsigned di, void *out, unsigned minlen, unsigned maxlen) {
@@ -251,7 +258,9 @@ int dc_q_exec(DC* dc) {
int r = _dc_q_exec(dc);
if (r == DC_ERR_SWD_FAULT) {
// clear all sticky errors
- dc_dp_wr(dc, DP_ABORT, DP_ABORT_ALLCLR);
+ if (dc_dp_wr(dc, DP_ABORT, DP_ABORT_ALLCLR) < 0) {
+ dc_set_status(dc, DC_DETACHED);
+ }
}
return r;
}
@@ -498,7 +507,7 @@ int dc_attach(DC* dc, unsigned flags, unsigned tgt, uint32_t* idcode) {
dc->map_csw_keep &= MAP_CSW_KEEP;
- dc->status = DC_ATTACHED;
+ dc_set_status(dc, DC_ATTACHED);
return 0;
}
@@ -619,23 +628,28 @@ static int dap_configure(DC* dc) {
return DC_OK;
}
-static void dc_connect(DC* dc) {
+static int dc_connect(DC* dc) {
if ((dc->usb = usb_connect()) != NULL) {
- dc->status = DC_UNCONFIG;
if (dap_configure(dc) == 0) {
- dc->status = DC_DETACHED;
+ dc_set_status(dc, DC_DETACHED);
+ } else {
+ dc_set_status(dc, DC_UNCONFIG);
}
+ return 0;
}
+ return DC_ERR_FAILED;
}
-int dc_create(DC** out) {
+int dc_create(DC** out, void (*cb)(void *cookie, uint32_t status), void *cookie) {
DC* dc;
if ((dc = calloc(1, sizeof(DC))) == NULL) {
return DC_ERR_FAILED;
}
+ dc->status_callback = cb;
+ dc->status_cookie = cookie;
*out = dc;
- dc->status = DC_OFFLINE;
+ dc_set_status(dc, DC_OFFLINE);
dc_connect(dc);
return 0;
}
@@ -643,19 +657,22 @@ int dc_create(DC** out) {
int dc_periodic(DC* dc) {
switch (dc->status) {
case DC_OFFLINE:
- dc_connect(dc);
- return 1000;
+ if (dc_connect(dc) < 0) {
+ return 500;
+ } else {
+ return 100;
+ }
case DC_ATTACHED: {
uint32_t n;
int r = dc_dp_rd(dc, DP_CS, &n);
if (r == DC_ERR_IO) {
- dc->status = DC_OFFLINE;
+ dc_set_status(dc, DC_OFFLINE);
ERROR("offline\n");
} else if (r < 0) {
- dc->status = DC_DETACHED;
+ dc_set_status(dc, DC_DETACHED);
ERROR("detached\n");
}
- return 250;
+ return 100;
}
case DC_FAILURE:
case DC_UNCONFIG:
@@ -663,7 +680,7 @@ int dc_periodic(DC* dc) {
// ping the probe to see if USB is still connected
uint8_t buf[256 + 2];
dap_get_info(dc, DI_Protocol_Version, buf, 0, 255);
- return 1000;
+ return 500;
}
default:
return 1000;
diff --git a/src/transport-private.h b/src/transport-private.h
@@ -14,6 +14,8 @@ struct debug_context {
unsigned status;
volatile uint32_t attn;
+ void (*status_callback)(void *cookie, uint32_t status);
+ void *status_cookie;
// dap protocol info
uint32_t max_packet_count;
@@ -47,12 +49,6 @@ struct debug_context {
typedef struct debug_context DC;
-#define DC_ATTACHED 0 // attached and ready to do txns
-#define DC_FAILURE 1 // last txn failed, need to re-attach
-#define DC_DETACHED 2 // have not yet attached
-#define DC_UNCONFIG 3 // configure failed
-#define DC_OFFLINE 4 // usb connection not available
-
#define INVALID 0xFFFFFFFFU
diff --git a/src/transport.h b/src/transport.h
@@ -67,7 +67,14 @@ int dc_ap_rd(dctx_t* dc, unsigned apaddr, uint32_t* val);
int dc_ap_wr(dctx_t* dc, unsigned apaddr, uint32_t val);
// create debug connection
-int dc_create(dctx_t** dc);
+int dc_create(dctx_t** dc, void (*cb)(void *cookie, uint32_t status), void *cookie);
+
+// status values
+#define DC_ATTACHED 0 // attached and ready to do txns
+#define DC_FAILURE 1 // last txn failed, need to re-attach
+#define DC_DETACHED 2 // have not yet attached
+#define DC_UNCONFIG 3 // configure failed
+#define DC_OFFLINE 4 // usb connection not available
// attempt to attach to the debug target
int dc_attach(dctx_t* dc, unsigned flags, uint32_t tgt, uint32_t* idcode);
diff --git a/src/xdebug.c b/src/xdebug.c
@@ -190,6 +190,8 @@ static void *work_thread(void* arg) {
exit(-1);
}
if (r == 0) {
+ char statusline[64];
+ statusline[0] = 0;
timeout = dc_periodic(dc);
if (timeout < 100) {
timeout = 100;
@@ -208,6 +210,24 @@ static void *work_thread(void* arg) {
return 0;
}
+const char* status_text(uint32_t status) {
+ switch (status) {
+ case DC_ATTACHED:
+ return "[ATTACHED]";
+ case DC_FAILURE:
+ case DC_DETACHED:
+ case DC_UNCONFIG:
+ return "[DETACHED]";
+ case DC_OFFLINE:
+ default:
+ return "[OFFLINE]";
+ }
+}
+
+void handle_status(void* cookie, uint32_t status) {
+ tui_status_rhs(status_text(status));
+}
+
void handle_line(char *line, unsigned len) {
if (!strcmp(line, "@ESC@")) {
dc_interrupt(dc);
@@ -264,7 +284,7 @@ int main(int argc, char** argv) {
tui_init();
tui_ch_create(&ch, 0);
- dc_create(&dc);
+ dc_create(&dc, handle_status, NULL);
pthread_t t;
if (pthread_create(&t, NULL, work_thread, NULL) != 0) {
diff --git a/src/xtest.c b/src/xtest.c
@@ -37,7 +37,7 @@ int main(int argc, char **argv) {
uint32_t n = 0;
dctx_t* dc;
- if (dc_create(&dc) < 0) {
+ if (dc_create(&dc, 0, 0) < 0) {
return -1;
}