Zach Welch <zw@superlucidity.net> refactor

git-svn-id: svn://svn.berlios.de/openocd/trunk@1498 b42882b7-edfa-0310-969c-e2dbd0fdcd60
__archive__
oharboe 2009-04-21 15:39:49 +00:00
parent 97a5e7a608
commit 66db8e7bb4
1 changed files with 159 additions and 182 deletions

View File

@ -230,8 +230,13 @@ static int jlink_speed(int speed)
{ {
int result; int result;
if (speed <= JLINK_MAX_SPEED) if (speed > JLINK_MAX_SPEED)
{ {
LOG_INFO("Ignoring speed request: %dkHz exceeds %dkHz maximum",
speed, JLINK_MAX_SPEED);
return ERROR_OK;
}
/* check for RTCK setting */ /* check for RTCK setting */
if (speed == 0) if (speed == 0)
speed = -1; speed = -1;
@ -241,21 +246,11 @@ static int jlink_speed(int speed)
usb_out_buffer[2] = (speed >> 8) & 0xff; usb_out_buffer[2] = (speed >> 8) & 0xff;
result = jlink_usb_write(jlink_jtag_handle, 3); result = jlink_usb_write(jlink_jtag_handle, 3);
if (result != 3)
if (result == 3)
{
return ERROR_OK;
}
else
{ {
LOG_ERROR("J-Link setting speed failed (%d)", result); LOG_ERROR("J-Link setting speed failed (%d)", result);
return ERROR_JTAG_DEVICE_ERROR; return ERROR_JTAG_DEVICE_ERROR;
} }
}
else
{
LOG_INFO("Requested speed %dkHz exceeds maximum of %dkHz, ignored", speed, JLINK_MAX_SPEED);
}
return ERROR_OK; return ERROR_OK;
} }
@ -485,10 +480,14 @@ static int jlink_get_status(void)
int result; int result;
jlink_simple_command(EMU_CMD_GET_STATE); jlink_simple_command(EMU_CMD_GET_STATE);
result = jlink_usb_read(jlink_jtag_handle, 8);
if (result == 8) result = jlink_usb_read(jlink_jtag_handle, 8);
if (result != 8)
{ {
LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
return ERROR_JTAG_DEVICE_ERROR;
}
int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8); int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \ LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \
vref / 1000, vref % 1000, \ vref / 1000, vref % 1000, \
@ -496,14 +495,7 @@ static int jlink_get_status(void)
usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]); usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
if (vref < 1500) if (vref < 1500)
{
LOG_ERROR("Vref too low. Check Target Power\n"); LOG_ERROR("Vref too low. Check Target Power\n");
}
}
else
{
LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
}
return ERROR_OK; return ERROR_OK;
} }
@ -511,27 +503,32 @@ static int jlink_get_status(void)
static int jlink_get_version_info(void) static int jlink_get_version_info(void)
{ {
int result; int result;
int len = 0; int len;
/* query hardware version */ /* query hardware version */
jlink_simple_command(EMU_CMD_VERSION); jlink_simple_command(EMU_CMD_VERSION);
result = jlink_usb_read(jlink_jtag_handle, 2);
if (result == 2) result = jlink_usb_read(jlink_jtag_handle, 2);
if (2 != result)
{ {
LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n",
result);
return ERROR_JTAG_DEVICE_ERROR;
}
len = buf_get_u32(usb_in_buffer, 0, 16); len = buf_get_u32(usb_in_buffer, 0, 16);
result = jlink_usb_read(jlink_jtag_handle, len); result = jlink_usb_read(jlink_jtag_handle, len);
if (result != len)
if (result == len)
{ {
LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n",
result);
return ERROR_JTAG_DEVICE_ERROR;
}
usb_in_buffer[result] = 0; usb_in_buffer[result] = 0;
LOG_INFO("%s", (char *)usb_in_buffer); LOG_INFO("%s", (char *)usb_in_buffer);
return ERROR_OK;
}
}
LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result); return ERROR_OK;
return ERROR_JTAG_DEVICE_ERROR;
} }
static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
@ -551,7 +548,7 @@ static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, ch
/* 2048 is the max value we can use here */ /* 2048 is the max value we can use here */
#define JLINK_TAP_BUFFER_SIZE 2048 #define JLINK_TAP_BUFFER_SIZE 2048
static int tap_length; static unsigned tap_length;
static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE]; static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE]; static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE]; static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
@ -593,40 +590,32 @@ static void jlink_tap_append_step(int tms, int tdi)
last_tms = tms; last_tms = tms;
int index = tap_length / 8; int index = tap_length / 8;
if (index < JLINK_TAP_BUFFER_SIZE) if (index >= JLINK_TAP_BUFFER_SIZE)
{ {
LOG_ERROR("jlink_tap_append_step: overflow");
exit(-1);
}
int bit_index = tap_length % 8; int bit_index = tap_length % 8;
u8 bit = 1 << bit_index; u8 bit = 1 << bit_index;
if (tms) if (tms)
{
tms_buffer[index] |= bit; tms_buffer[index] |= bit;
}
else else
{
tms_buffer[index] &= ~bit; tms_buffer[index] &= ~bit;
}
if (tdi) if (tdi)
{
tdi_buffer[index] |= bit; tdi_buffer[index] |= bit;
}
else else
{
tdi_buffer[index] &= ~bit; tdi_buffer[index] &= ~bit;
}
tap_length++; tap_length++;
}
else
{
LOG_ERROR("jlink_tap_append_step, overflow");
}
} }
static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command) static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
{ {
pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length]; pending_scan_result_t *pending_scan_result =
&pending_scan_results_buffer[pending_scan_results_length];
int i; int i;
pending_scan_result->first = tap_length; pending_scan_result->first = tap_length;
@ -636,7 +625,9 @@ static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *comman
for (i = 0; i < length; i++) for (i = 0; i < length; i++)
{ {
jlink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1); int tms = i < length - 1 ? 0 : 1;
int tdi = buffer[i / 8] & (1 << (i % 8));
jlink_tap_append_step(tms, tdi);
} }
pending_scan_results_length++; pending_scan_results_length++;
} }
@ -651,8 +642,9 @@ static int jlink_tap_execute(void)
int i; int i;
int result; int result;
if (tap_length > 0) if (!tap_length)
{ return ERROR_OK;
/* Pad last byte so that tap_length is divisible by 8 */ /* Pad last byte so that tap_length is divisible by 8 */
while (tap_length % 8 != 0) while (tap_length % 8 != 0)
{ {
@ -682,13 +674,16 @@ static int jlink_tap_execute(void)
result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length); result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
if (result == byte_length) if (result != byte_length)
{ {
for (i = 0; i < byte_length; i++) LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)",
{ result, byte_length);
tdo_buffer[i] = usb_in_buffer[i]; return ERROR_JTAG_QUEUE_FAILED;
} }
for (i = 0; i < byte_length; i++)
tdo_buffer[i] = usb_in_buffer[i];
for (i = 0; i < pending_scan_results_length; i++) for (i = 0; i < pending_scan_results_length; i++)
{ {
pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i]; pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
@ -717,15 +712,8 @@ static int jlink_tap_execute(void)
free(pending_scan_result->buffer); free(pending_scan_result->buffer);
} }
} }
}
else
{
LOG_ERROR("jlink_tap_execute, wrong result %d, expected %d", result, byte_length);
return ERROR_JTAG_QUEUE_FAILED;
}
jlink_tap_init(); jlink_tap_init();
}
return ERROR_OK; return ERROR_OK;
} }
@ -792,59 +780,48 @@ static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_le
int result2; int result2;
result = jlink_usb_write(jlink_jtag, out_length); result = jlink_usb_write(jlink_jtag, out_length);
if (result == out_length) if (result != out_length)
{ {
LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
out_length, result);
return ERROR_JTAG_DEVICE_ERROR;
}
result = jlink_usb_read(jlink_jtag, in_length); result = jlink_usb_read(jlink_jtag, in_length);
if (result == in_length || result == in_length+1) if ((result != in_length) && (result != in_length + 1))
{ {
LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
in_length, result);
return ERROR_JTAG_DEVICE_ERROR;
}
if (result == in_length) if (result == in_length)
{ {
/* Must read the result from the EMU too */ /* Must read the result from the EMU too */
result2 = jlink_usb_read_emu_result(jlink_jtag); result2 = jlink_usb_read_emu_result(jlink_jtag);
if (1 == result2) if (1 != result2)
{ {
LOG_ERROR("jlink_usb_read_emu_result failed "
"(requested=1, result=%d)", result2);
return ERROR_JTAG_DEVICE_ERROR;
}
/* Check the result itself */ /* Check the result itself */
if (0 == usb_emu_result_buffer[0]) result2 = usb_emu_result_buffer[0];
}
else
{ {
/* Save the result, then remove it from return value */
result2 = usb_in_buffer[result--];
}
if (result2)
{
LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
return ERROR_JTAG_DEVICE_ERROR;
}
return result; return result;
}
else
{
LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_emu_result_buffer[0]);
return -1;
}
}
else
{
LOG_ERROR("jlink_usb_read_emu_result len (requested=1, result=%d)", result2);
return -1;
}
}
else
{
/* Check the result itself */
if (0 == usb_in_buffer[result-1])
{
return result-1;
}
else
{
LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_in_buffer[result]);
return -1;
}
}
}
else
{
LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
return -1;
}
}
else
{
LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
return -1;
}
} }
/* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */ /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */