- switch to synchronous halt during connect. This fixes the bug
  where poll() was not invoked between halt() and servicing the
  'g' register packet
- halt() no longer returns error code when target is already halted, just
  logs a warning. Only the halt() implementation can say anything
  meaningful about why a halt() failed, so error messages are pushed
  up to halt()
- fixed soft_reset_halt infinite loop bug in arm7_9_common.c. The rest
  of the implementations are still busted.
- by using USER() instead of command_print() the log gets the
  source + line #. Nice.
- no longer invoke exit() if soft_reset_halt fails. A reset can often
  fix the problem.
git-svn-id: svn://svn.berlios.de/openocd/trunk@475 
b42882b7-edfa-0310-969c-
e2dbd0fdcd60
         * GDB connection will fail if e.g. register read packets fail,
         * otherwise resetting/halting the target could have been left to GDB init
         * scripts
+        * 
+        * DANGER!!!! 
+        * We need a synchronous halt, lest connect will fail.
+        * Also there is no guarantee that poll() will be invoked
+        * between here and serving the first packet, so the halt()
+        * statement above is *NOT* sufficient
         */
-       if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
-                       (retval != ERROR_TARGET_ALREADY_HALTED))
+       if ((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK)
        {
                ERROR("error(%d) when trying to halt target, falling back to \"reset\"", retval);
                command_run_line(connection->cmd_ctx, "reset");
        }
+       command_run_line(connection->cmd_ctx, "halt");
        
        /* remove the initial ACK from the incoming buffer */
        if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
                        log_add_callback(gdb_log_callback, connection);
                        target_call_timer_callbacks();
                        command_run_line(cmd_ctx, cmd);
+                       target_call_timer_callbacks();
                        log_remove_callback(gdb_log_callback, connection);
                        free(cmd);
                }
 
 
     if (target->state == TARGET_HALTED)
     {
-       WARNING("target was already halted");
-       return ERROR_TARGET_ALREADY_HALTED;
+               WARNING("target was already halted");
+               return ERROR_OK;
     }
 
     if (arm11->trst_active)
 
     if (target->state != TARGET_HALTED)
     {
-       return ERROR_TARGET_NOT_HALTED;
+       ERROR("Target not halted");     
+               return ERROR_TARGET_NOT_HALTED;
     }
        
     *reg_list_size  = ARM11_GDB_REGISTER_COUNT;
 
        arm720t_common_t *arm720t = arm7tdmi->arch_info;
        reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
        
-       if (target->state == TARGET_RUNNING)
-       {
-               target->type->halt(target);
-       }
+       target->type->halt(target);
        
        while (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) == 0)
        {
 
        reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
        reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
        int i;
+       int retval;
        
-       if (target->state == TARGET_RUNNING)
-       {
-               target->type->halt(target);
-       }
+       if ((retval=target->type->halt(target))!=ERROR_OK)
+               return retval;
        
-       while (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) == 0)
+       for (i=0; i<10; i++)
        {
+               if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) != 0)
+                       break;
                embeddedice_read_reg(dbg_stat);
-               jtag_execute_queue();
+               if ((retval=jtag_execute_queue())!=ERROR_OK)
+                       return retval;
+               /* do not eat all CPU, time out after 1 se*/
+               usleep(100*1000);
+               
+       }
+       if (i==10)
+       {
+               ERROR("Failed to halt CPU after 1 sec");
+               return ERROR_TARGET_TIMEOUT;
        }
        target->state = TARGET_HALTED;
        
        if (target->state == TARGET_HALTED)
        {
                WARNING("target was already halted");
-               return ERROR_TARGET_ALREADY_HALTED;
+               return ERROR_OK;
        }
        
        if (target->state == TARGET_UNKNOWN)
 
        arm920t_common_t *arm920t = arm9tdmi->arch_info;
        reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
        
-       if (target->state == TARGET_RUNNING)
-       {
-               target->type->halt(target);
-       }
+       target->type->halt(target);
        
        while (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) == 0)
        {
 
        arm926ejs_common_t *arm926ejs = arm9tdmi->arch_info;
        reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
        
-       if (target->state == TARGET_RUNNING)
-       {
-               target->type->halt(target);
-       }
+       target->type->halt(target);
        
        while (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) == 0)
        {
 
        
        if (target->state != TARGET_HALTED)
        {
+               ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
        
 
        
        if (target->state != TARGET_HALTED)
        {
+               ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
        
 
        if (target->state == TARGET_HALTED)
        {
                WARNING("target was already halted");
-               return ERROR_TARGET_ALREADY_HALTED;
+               return ERROR_OK;
        }
        
        if (target->state == TARGET_UNKNOWN)
 
                                {
                                        if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
                                        {
-                                               command_print(cmd_ctx, "Timed out waiting for reset");
+                                               USER("Timed out waiting for reset");
                                                goto done;
                                        }
                                        /* this will send alive messages on e.g. GDB remote protocol. */
 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
        target_t *target = get_current_target(cmd_ctx);
-       int retval;
        
-       command_print(cmd_ctx, "requesting target halt and executing a soft reset");
+       USER("requesting target halt and executing a soft reset");
        
-       if ((retval = target->type->soft_reset_halt(target)) != ERROR_OK)
-       {       
-               switch (retval)
-               {
-                       case ERROR_TARGET_TIMEOUT:
-                               command_print(cmd_ctx, "target timed out... shutting down");
-                               exit(-1);
-                       default:
-                               command_print(cmd_ctx, "unknown error... shutting down");
-                               exit(-1);
-               }
-       }
+       target->type->soft_reset_halt(target);
        
        return ERROR_OK;
 }
 
        /* target request support */
        int (*target_request_data)(struct target_s *target, u32 size, u8 *buffer);
 
-       /* target execution control */
+       /* halt will log a warning, but return ERROR_OK if the target is already halted. */
        int (*halt)(struct target_s *target);
        int (*resume)(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution);
        int (*step)(struct target_s *target, int current, u32 address, int handle_breakpoints);
 #define ERROR_TARGET_INVALID   (-300)
 #define ERROR_TARGET_INIT_FAILED (-301)
 #define ERROR_TARGET_TIMEOUT   (-302)
-#define ERROR_TARGET_ALREADY_HALTED (-303)
 #define ERROR_TARGET_NOT_HALTED (-304)
 #define ERROR_TARGET_FAILURE   (-305)
 #define ERROR_TARGET_UNALIGNED_ACCESS  (-306)
 
        if (target->state == TARGET_HALTED)
        {
                WARNING("target was already halted");
-               return ERROR_TARGET_ALREADY_HALTED;
+               return ERROR_OK;
        }
        else if (target->state == TARGET_UNKNOWN)
        {