diff --git a/src/arm7tdmi.c b/src/arm7tdmi.c index e5f7acb..805f995 100644 --- a/src/arm7tdmi.c +++ b/src/arm7tdmi.c @@ -108,12 +108,12 @@ static void do_nothing(void) { } -static void arm7_attach(struct target_s *target); +static bool arm7_attach(struct target_s *target); static int arm7_regs_read(struct target_s *target, void *data); static int arm7_regs_write(struct target_s *target, const void *data); static void arm7_halt_request(struct target_s *target); static int arm7_halt_wait(struct target_s *target); -static void arm7_halt_resume(struct target_s *target, uint8_t step); +static void arm7_halt_resume(struct target_s *target, bool step); void arm7tdmi_jtag_handler(jtag_dev_t *dev) { @@ -242,7 +242,7 @@ static int arm7_halt_wait(struct target_s *target) return 1; } -static void arm7_halt_resume(struct target_s *target, uint8_t step) +static void arm7_halt_resume(struct target_s *target, bool step) { struct target_arm7_s *t = (struct target_arm7_s *)target; @@ -270,10 +270,15 @@ static void arm7_halt_resume(struct target_s *target, uint8_t step) jtag_dev_write_ir(t->jtag, ARM7_IR_RESTART); } -static void arm7_attach(struct target_s *target) +static bool arm7_attach(struct target_s *target) { + int tries = 0; target_halt_request(target); - while(!target_halt_wait(target)); + while(!target_halt_wait(target) && --tries) + platform_delay(2); + if(!tries) + return false; + return true; } static int arm7_regs_read(struct target_s *target, void *data) diff --git a/src/cortexm.c b/src/cortexm.c index 229a044..b403c83 100644 --- a/src/cortexm.c +++ b/src/cortexm.c @@ -180,7 +180,7 @@ const struct command_s cortexm_cmd_list[] = { #define SIGTRAP 5 #define SIGSEGV 11 -static void cortexm_attach(struct target_s *target); +static bool cortexm_attach(struct target_s *target); static void cortexm_detach(struct target_s *target); static int cortexm_regs_read(struct target_s *target, void *data); @@ -333,7 +333,7 @@ cortexm_probe(struct target_s *target) target->halt_request = cortexm_halt_request; target->halt_wait = cortexm_halt_wait; target->halt_resume = cortexm_halt_resume; - target->regs_size = sizeof(regnum_cortex_m); /* XXX: detect FP extension */ + target->regs_size = sizeof(regnum_cortex_m); target_add_commands(target, cortexm_cmd_list, cortexm_driver_str); @@ -371,19 +371,24 @@ cortexm_probe(struct target_s *target) return 0; } -static void +static bool cortexm_attach(struct target_s *target) { ADIv5_AP_t *ap = adiv5_target_ap(target); struct cortexm_priv *priv = ap->priv; unsigned i; uint32_t r; + int tries; /* Clear any pending fault condition */ target_check_error(target); target_halt_request(target); - while(!target_halt_wait(target)); + tries = 10; + while(!target_halt_wait(target) && --tries) + platform_delay(2); + if(!tries) + return false; /* Request halt on reset */ adiv5_ap_mem_write(ap, CORTEXM_DEMCR, priv->demcr); @@ -423,6 +428,8 @@ cortexm_attach(struct target_s *target) target->set_hw_wp = cortexm_set_hw_wp; target->clear_hw_wp = cortexm_clear_hw_wp; target->check_hw_wp = cortexm_check_hw_wp; + + return true; } static void diff --git a/src/gdb_main.c b/src/gdb_main.c index 58ad223..8c5f6d4 100644 --- a/src/gdb_main.c +++ b/src/gdb_main.c @@ -396,14 +396,15 @@ handle_v_packet(char *packet, int plen) /* Attach to remote target processor */ target *t; uint32_t i; - for(t = target_list, i = 1; t; t = t->next, i++) + for(t = target_list, i = 1; t; t = t->next, i++) if(i == addr) { - cur_target = target_attach(t, + cur_target = target_attach(t, gdb_target_destroy_callback); - gdb_putpacketz("T05"); break; } - if(!cur_target) /* Failed to attach */ + if(cur_target) + gdb_putpacketz("T05"); + else gdb_putpacketz("E01"); } else if (!strcmp(packet, "vRun;")) { diff --git a/src/include/target.h b/src/include/target.h index 6fd2beb..6daf350 100644 --- a/src/include/target.h +++ b/src/include/target.h @@ -120,7 +120,7 @@ struct target_s { target_destroy_callback destroy_callback; /* Attach/Detach funcitons */ - void (*attach)(struct target_s *target); + bool (*attach)(struct target_s *target); void (*detach)(struct target_s *target); int (*check_error)(struct target_s *target); diff --git a/src/target.c b/src/target.c index a28c55f..b46f9e3 100644 --- a/src/target.c +++ b/src/target.c @@ -75,7 +75,8 @@ target *target_attach(target *t, target_destroy_callback destroy_cb) t->destroy_callback = destroy_cb; - t->attach(t); + if (!t->attach(t)) + return NULL; return t; }