diff --git a/src/command.c b/src/command.c index e6fe8f6..8b1a11a 100644 --- a/src/command.c +++ b/src/command.c @@ -39,7 +39,7 @@ static bool cmd_help(target *t); static bool cmd_jtag_scan(target *t, int argc, char **argv); static bool cmd_swdp_scan(void); -static bool cmd_targets(target *t); +static bool cmd_targets(void); static bool cmd_morse(void); static bool cmd_connect_srst(target *t, int argc, const char **argv); static bool cmd_hard_srst(void); @@ -183,7 +183,7 @@ static bool cmd_jtag_scan(target *t, int argc, char **argv) gdb_out("JTAG device scan failed!\n"); return false; } - cmd_targets(NULL); + cmd_targets(); return true; } @@ -214,27 +214,26 @@ bool cmd_swdp_scan(void) return false; } - cmd_targets(NULL); + cmd_targets(); return true; } -bool cmd_targets(target *cur_target) +static void display_target(int i, target *t, void *context) { - struct target_s *t; - int i; + (void)context; + gdb_outf("%2d %c %s\n", i, target_attached(t)?'*':' ', target_driver_name(t)); +} - if(!target_list) { +bool cmd_targets(void) +{ + gdb_out("Available Targets:\n"); + gdb_out("No. Att Driver\n"); + if (!target_foreach(display_target, NULL)) { gdb_out("No usable targets found.\n"); return false; } - gdb_out("Available Targets:\n"); - gdb_out("No. Att Driver\n"); - for(t = target_list, i = 1; t; t = t->next, i++) - gdb_outf("%2d %c %s\n", i, t==cur_target?'*':' ', - t->driver); - return true; } diff --git a/src/cortexa.c b/src/cortexa.c index 79c53a6..4baf4e0 100644 --- a/src/cortexa.c +++ b/src/cortexa.c @@ -360,7 +360,7 @@ bool cortexa_probe(ADIv5_AP_t *apb, uint32_t debug_base) { target *t; - t = target_new(sizeof(*t)); + t = target_new(); adiv5_ap_ref(apb); struct cortexa_priv *priv = calloc(1, sizeof(*priv)); t->priv = priv; diff --git a/src/cortexm.c b/src/cortexm.c index 570cdd9..1e538e6 100644 --- a/src/cortexm.c +++ b/src/cortexm.c @@ -229,7 +229,7 @@ bool cortexm_probe(ADIv5_AP_t *ap) { target *t; - t = target_new(sizeof(*t)); + t = target_new(); adiv5_ap_ref(ap); struct cortexm_priv *priv = calloc(1, sizeof(*priv)); t->priv = priv; diff --git a/src/include/target.h b/src/include/target.h index 9436af4..fe47e21 100644 --- a/src/include/target.h +++ b/src/include/target.h @@ -30,6 +30,7 @@ typedef struct target_s target; int adiv5_swdp_scan(void); int jtag_scan(const uint8_t *lrlens); +bool target_foreach(void (*cb)(int i, target *t, void *context), void *context); void target_list_free(void); /* The destroy callback function will be called by target_list_free() just @@ -48,6 +49,7 @@ target *target_attach(target *t, target_destroy_callback destroy_cb); target *target_attach_n(int n, target_destroy_callback destroy_cb); void target_detach(target *t); bool target_check_error(target *t); +bool target_attached(target *t); /* Memory access functions */ void target_mem_read(target *t, void *dest, uint32_t src, size_t len); @@ -69,13 +71,11 @@ int target_clear_hw_bp(target *t, uint32_t addr, uint8_t len); int target_set_hw_wp(target *t, uint8_t type, uint32_t addr, uint8_t len); int target_clear_hw_wp(target *t, uint8_t type, uint32_t addr, uint8_t len); - int target_check_hw_wp(target *t, uint32_t *addr); /* Flash memory access functions */ int target_flash_erase(target *t, uint32_t addr, size_t len); -int target_flash_write(target *t, - uint32_t dest, const void *src, size_t len); +int target_flash_write(target *t, uint32_t dest, const void *src, size_t len); int target_flash_done(target *t); /* Host I/O */ @@ -85,6 +85,7 @@ void target_hostio_reply(target *t, int32_t retcode, uint32_t errcode); int target_regs_size(target *t); const char *target_tdesc(target *t); const char *target_mem_map(target *t); +const char *target_driver_name(target *t); struct target_command_s { const char *specific_name; diff --git a/src/target.c b/src/target.c index 94a24cf..dc2d2b4 100644 --- a/src/target.c +++ b/src/target.c @@ -23,15 +23,24 @@ target *target_list = NULL; -target *target_new(unsigned size) +target *target_new(void) { - target *t = (void*)calloc(1, size); + target *t = (void*)calloc(1, sizeof(*t)); t->next = target_list; target_list = t; return t; } +bool target_foreach(void (*cb)(int, target *t, void *context), void *context) +{ + int i = 1; + target *t = target_list; + for (; t; t = t->next, i++) + cb(i, t, context); + return target_list != NULL; +} + void target_list_free(void) { struct target_command_s *tc; @@ -100,6 +109,7 @@ target *target_attach(target *t, target_destroy_callback destroy_cb) if (!t->attach(t)) return NULL; + t->attached = true; return t; } @@ -265,8 +275,14 @@ int target_flash_done_buffered(struct target_flash *f) } /* Wrapper functions */ -void target_detach(target *t) { t->detach(t); } +void target_detach(target *t) +{ + t->detach(t); + t->attached = false; +} + bool target_check_error(target *t) { return t->check_error(t); } +bool target_attached(target *t) { return t->attached; } /* Memory access functions */ void target_mem_read(target *t, void *dest, uint32_t src, size_t len) @@ -332,6 +348,11 @@ const char *target_tdesc(target *t) return t->tdesc ? t->tdesc : ""; } +const char *target_driver_name(target *t) +{ + return t->driver; +} + uint32_t target_mem_read32(target *t, uint32_t addr) { uint32_t ret; diff --git a/src/target_internal.h b/src/target_internal.h index 408056f..98f6239 100644 --- a/src/target_internal.h +++ b/src/target_internal.h @@ -22,7 +22,7 @@ #define __TARGET_INTERNAL_H extern target *target_list; -target *target_new(unsigned size); +target *target_new(void); struct target_ram { uint32_t start; @@ -55,6 +55,7 @@ struct target_flash { }; struct target_s { + bool attached; /* Notify controlling debugger if target is lost */ target_destroy_callback destroy_callback;