X-Git-Url: https://review.openocd.org/gitweb?a=blobdiff_plain;f=src%2Fjtag%2Fzy1000%2Fzy1000.c;h=092e3cd25ce013132b0c1a50f0b469556b6c9b2e;hb=32e647acf40bc11858a524e5ee73183ce0d9449b;hp=391d8f2076201d9383aee80dec7a3a1f3c2c7249;hpb=57d7743639d5092770d79f7c4b12ae694c482750;p=openocd.git diff --git a/src/jtag/zy1000/zy1000.c b/src/jtag/zy1000/zy1000.c index 391d8f2076..092e3cd25c 100644 --- a/src/jtag/zy1000/zy1000.c +++ b/src/jtag/zy1000/zy1000.c @@ -48,13 +48,17 @@ #include #include #include +#include +#include + +#include + +#if BUILD_ECOSBOARD #include "zy1000_version.h" #include // low level i/o #include -#include - #ifdef CYGPKG_HAL_NIOS2 #include #include @@ -66,6 +70,7 @@ #define ZYLIN_OPENOCD GIT_OPENOCD_VERSION #define ZYLIN_OPENOCD_VERSION "ZY1000 " ZYLIN_VERSION " " ZYLIN_DATE +#endif static int zy1000_khz(int khz, int *jtag_speed) { @@ -96,7 +101,7 @@ static int zy1000_speed_div(int speed, int *khz) static bool readPowerDropout(void) { - cyg_uint32 state; + uint32_t state; // sample and clear power dropout ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x80); ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, state); @@ -108,7 +113,7 @@ static bool readPowerDropout(void) static bool readSRST(void) { - cyg_uint32 state; + uint32_t state; // sample and clear SRST sensing ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000040); ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, state); @@ -172,28 +177,41 @@ void zy1000_reset(int trst, int srst) } /* wait for srst to float back up */ - if (!srst) + if ((!srst && ((jtag_get_reset_config() & RESET_TRST_PULLS_SRST) == 0))|| + (!srst && !trst && (jtag_get_reset_config() & RESET_TRST_PULLS_SRST))) { - int i; - for (i = 0; i < 1000; i++) - { + bool first = true; + long long start; + long total = 0; + for (;;) + { // We don't want to sense our own reset, so we clear here. // There is of course a timing hole where we could loose // a "real" reset. if (!readSRST()) + { + if (total > 1) + { + LOG_USER("SRST took %dms to deassert", (int)total); + } break; + } - /* wait 1ms */ - alive_sleep(1); - } + if (first) + { + first = false; + start = timeval_ms(); + } - if (i == 1000) - { - LOG_USER("SRST didn't deassert after %dms", i); - } else if (i > 1) - { - LOG_USER("SRST took %dms to deassert", i); + total = timeval_ms() - start; + + if (total > 5000) + { + LOG_ERROR("SRST took too long to deassert: %dms", (int)total); + break; + } } + } } @@ -259,7 +277,20 @@ COMMAND_HANDLER(handle_power_command) return ERROR_OK; } +#if !BUILD_ECOSBOARD +static char *tcp_server = "notspecified"; +static int jim_zy1000_server(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 2) + return JIM_ERR; + + tcp_server = strdup(Jim_GetString(argv[1], NULL)); + + return JIM_OK; +} +#endif +#if BUILD_ECOSBOARD /* Give TELNET a way to find out what version this is */ static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { @@ -308,7 +339,7 @@ static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv * and actual FPGA */ static char *fpga_id = "0x12345678 0x12345678 0x12345678 0x12345678"; - cyg_uint32 id, timestamp; + uint32_t id, timestamp; HAL_READ_UINT32(SYSID_BASE, id); HAL_READ_UINT32(SYSID_BASE+4, timestamp); sprintf(fpga_id, "0x%08x 0x%08x 0x%08x 0x%08x", id, timestamp, SYSID_ID, SYSID_TIMESTAMP); @@ -333,7 +364,7 @@ static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv return JIM_OK; } - +#endif #ifdef CYGPKG_HAL_NIOS2 @@ -353,7 +384,7 @@ static void report_info(void *data, const char * format, va_list args) struct cyg_upgrade_info firmware_info = { - (cyg_uint8 *)0x84000000, + (uint8_t *)0x84000000, "/ram/firmware.phi", "Firmware", 0x0300000, @@ -401,7 +432,7 @@ zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp, return JIM_ERR; } - cyg_uint32 status; + uint32_t status; ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, status); Jim_SetResult(interp, Jim_NewIntObj(interp, (status&0x80) != 0)); @@ -411,23 +442,6 @@ zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp, - -int zy1000_init(void) -{ - LOG_USER("%s", ZYLIN_OPENOCD_VERSION); - - ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x30); // Turn on LED1 & LED2 - - setPower(true); // on by default - - - /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */ - zy1000_reset(0, 0); - zy1000_speed(jtag_get_speed()); - - return ERROR_OK; -} - int zy1000_quit(void) { @@ -438,7 +452,7 @@ int zy1000_quit(void) int interface_jtag_execute_queue(void) { - cyg_uint32 empty; + uint32_t empty; waitIdle(); ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, empty); @@ -460,18 +474,18 @@ int interface_jtag_execute_queue(void) -static cyg_uint32 getShiftValue(void) +static uint32_t getShiftValue(void) { - cyg_uint32 value; + uint32_t value; waitIdle(); ZY1000_PEEK(ZY1000_JTAG_BASE + 0xc, value); VERBOSE(LOG_INFO("getShiftValue %08x", value)); return value; } #if 0 -static cyg_uint32 getShiftValueFlip(void) +static uint32_t getShiftValueFlip(void) { - cyg_uint32 value; + uint32_t value; waitIdle(); ZY1000_PEEK(ZY1000_JTAG_BASE + 0x18, value); VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value)); @@ -480,10 +494,10 @@ static cyg_uint32 getShiftValueFlip(void) #endif #if 0 -static void shiftValueInnerFlip(const tap_state_t state, const tap_state_t endState, int repeat, cyg_uint32 value) +static void shiftValueInnerFlip(const tap_state_t state, const tap_state_t endState, int repeat, uint32_t value) { VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_name(state), tap_state_name(endState), repeat, value)); - cyg_uint32 a,b; + uint32_t a,b; a = state; b = endState; ZY1000_POKE(ZY1000_JTAG_BASE + 0xc, value); @@ -492,103 +506,93 @@ static void shiftValueInnerFlip(const tap_state_t state, const tap_state_t endSt } #endif -static void gotoEndState(tap_state_t end_state) +// here we shuffle N bits out/in +static __inline void scanBits(const uint8_t *out_value, uint8_t *in_value, int num_bits, bool pause, tap_state_t shiftState, tap_state_t end_state) { - setCurrentState(end_state); -} - -static __inline void scanFields(int num_fields, const struct scan_field *fields, tap_state_t shiftState, int pause) -{ - int i; - int j; - int k; - - for (i = 0; i < num_fields; i++) + tap_state_t pause_state = shiftState; + for (int j = 0; j < num_bits; j += 32) { - cyg_uint32 value; - - uint8_t *inBuffer = NULL; - - - // figure out where to store the input data - int num_bits = fields[i].num_bits; - if (fields[i].in_value != NULL) + int k = num_bits - j; + if (k > 32) { - inBuffer = fields[i].in_value; + k = 32; + /* we have more to shift out */ + } else if (pause) + { + /* this was the last to shift out this time */ + pause_state = end_state; } - // here we shuffle N bits out/in - j = 0; - while (j < num_bits) + // we have (num_bits + 7)/8 bytes of bits to toggle out. + // bits are pushed out LSB to MSB + uint32_t value; + value = 0; + if (out_value != NULL) { - tap_state_t pause_state; - int l; - k = num_bits-j; - pause_state = (shiftState == TAP_DRSHIFT)?TAP_DRSHIFT:TAP_IRSHIFT; - if (k > 32) + for (int l = 0; l < k; l += 8) { - k = 32; - /* we have more to shift out */ - } else if (pause&&(i == num_fields-1)) - { - /* this was the last to shift out this time */ - pause_state = (shiftState==TAP_DRSHIFT)?TAP_DRPAUSE:TAP_IRPAUSE; + value|=out_value[(j + l)/8]<= 32 is not defined by the C standard + * and will in fact shift by &0x1f bits on nios */ + } - // we have (num_bits + 7)/8 bytes of bits to toggle out. - // bits are pushed out LSB to MSB - value = 0; - if (fields[i].out_value != NULL) - { - for (l = 0; l < k; l += 8) - { - value|=fields[i].out_value[(j + l)/8]<= 32 is not defined by the C standard - * and will in fact shift by &0x1f bits on nios */ - } + shiftValueInner(shiftState, pause_state, k, value); - shiftValueInner(shiftState, pause_state, k, value); + if (in_value != NULL) + { + // data in, LSB to MSB + value = getShiftValue(); + // we're shifting in data to MSB, shift data to be aligned for returning the value + value >>= 32-k; - if (inBuffer != NULL) + for (int l = 0; l < k; l += 8) { - // data in, LSB to MSB - value = getShiftValue(); - // we're shifting in data to MSB, shift data to be aligned for returning the value - value >>= 32-k; - - for (l = 0; l < k; l += 8) - { - inBuffer[(j + l)/8]=(value >> l)&0xff; - } + in_value[(j + l)/8]=(value >> l)&0xff; } - j += k; } } } +static __inline void scanFields(int num_fields, const struct scan_field *fields, tap_state_t shiftState, tap_state_t end_state) +{ + for (int i = 0; i < num_fields; i++) + { + scanBits(fields[i].out_value, + fields[i].in_value, + fields[i].num_bits, + (i == num_fields-1), + shiftState, + end_state); + } +} + int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field *fields, tap_state_t state) { int scan_size = 0; struct jtag_tap *tap, *nextTap; + tap_state_t pause_state = TAP_IRSHIFT; for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap) { nextTap = jtag_tap_next_enabled(tap); - bool pause = (nextTap==NULL); + if (nextTap==NULL) + { + pause_state = state; + } scan_size = tap->ir_length; /* search the list */ if (tap == active) { - scanFields(1, fields, TAP_IRSHIFT, pause); + scanFields(1, fields, TAP_IRSHIFT, pause_state); /* update device information */ buf_cpy(fields[0].out_value, tap->cur_instr, scan_size); @@ -597,12 +601,11 @@ int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field { /* if a device isn't listed, set it to BYPASS */ assert(scan_size <= 32); - shiftValueInner(TAP_IRSHIFT, pause?TAP_IRPAUSE:TAP_IRSHIFT, scan_size, 0xffffffff); + shiftValueInner(TAP_IRSHIFT, pause_state, scan_size, 0xffffffff); tap->bypass = 1; } } - gotoEndState(state); return ERROR_OK; } @@ -611,43 +614,43 @@ int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field -int interface_jtag_add_plain_ir_scan(int num_fields, const struct scan_field *fields, tap_state_t state) +int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state) { - scanFields(num_fields, fields, TAP_IRSHIFT, 1); - gotoEndState(state); - + scanBits(out_bits, in_bits, num_bits, true, TAP_IRSHIFT, state); return ERROR_OK; } int interface_jtag_add_dr_scan(struct jtag_tap *active, int num_fields, const struct scan_field *fields, tap_state_t state) { struct jtag_tap *tap, *nextTap; + tap_state_t pause_state = TAP_DRSHIFT; for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap) { nextTap = jtag_tap_next_enabled(tap); - bool pause = (nextTap==NULL); + if (nextTap==NULL) + { + pause_state = state; + } /* Find a range of fields to write to this tap */ if (tap == active) { assert(!tap->bypass); - scanFields(num_fields, fields, TAP_DRSHIFT, pause); + scanFields(num_fields, fields, TAP_DRSHIFT, pause_state); } else { /* Shift out a 0 for disabled tap's */ assert(tap->bypass); - shiftValueInner(TAP_DRSHIFT, pause?TAP_DRPAUSE:TAP_DRSHIFT, 1, 0); + shiftValueInner(TAP_DRSHIFT, pause_state, 1, 0); } } - gotoEndState(state); return ERROR_OK; } -int interface_jtag_add_plain_dr_scan(int num_fields, const struct scan_field *fields, tap_state_t state) +int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state) { - scanFields(num_fields, fields, TAP_DRSHIFT, 1); - gotoEndState(state); + scanBits(out_bits, in_bits, num_bits, true, TAP_DRSHIFT, state); return ERROR_OK; } @@ -715,12 +718,6 @@ int interface_jtag_add_clocks(int num_cycles) return zy1000_jtag_add_clocks(num_cycles, cmd_queue_cur_state, cmd_queue_cur_state); } -int interface_jtag_add_sleep(uint32_t us) -{ - jtag_sleep(us); - return ERROR_OK; -} - int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state state) { /*wait for the fifo to be empty*/ @@ -765,7 +762,7 @@ int interface_jtag_add_pathmove(int num_states, const tap_state_t *path) uint8_t seq[16]; memset(seq, 0, sizeof(seq)); - assert(num_states < (sizeof(seq) * 8)); + assert(num_states < (int)((sizeof(seq) * 8))); while (num_states) { @@ -793,34 +790,43 @@ int interface_jtag_add_pathmove(int num_states, const tap_state_t *path) return interface_add_tms_seq(state_count, seq, cur_state); } -void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, uint8_t *buffer, int little, int count) +static void jtag_pre_post_bits(struct jtag_tap *tap, int *pre, int *post) { -// static int const reg_addr = 0x5; - tap_state_t end_state = jtag_get_end_state(); - if (jtag_tap_next_enabled(jtag_tap_next_enabled(NULL)) == NULL) + /* bypass bits before and after */ + int pre_bits = 0; + int post_bits = 0; + + bool found = false; + struct jtag_tap *cur_tap, *nextTap; + for (cur_tap = jtag_tap_next_enabled(NULL); cur_tap!= NULL; cur_tap = nextTap) { - /* better performance via code duplication */ - if (little) + nextTap = jtag_tap_next_enabled(cur_tap); + if (cur_tap == tap) { - int i; - for (i = 0; i < count; i++) - { - shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 1)); - shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr | (1 << 5)); - buffer += 4; - } + found = true; } else { - int i; - for (i = 0; i < count; i++) + if (found) { - shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 0)); - shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr | (1 << 5)); - buffer += 4; + post_bits++; + } else + { + pre_bits++; } } } - else + *pre = pre_bits; + *post = post_bits; +} + +void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, uint8_t *buffer, int little, int count) +{ + + int pre_bits; + int post_bits; + jtag_pre_post_bits(tap, &pre_bits, &post_bits); + + if (pre_bits + post_bits + 6 > 32) { int i; for (i = 0; i < count; i++) @@ -828,6 +834,19 @@ void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, uint8_t *buffer, embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little)); buffer += 4; } + } else + { + shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, pre_bits, 0); + int i; + for (i = 0; i < count - 1; i++) + { + /* Fewer pokes means we get to use the FIFO more efficiently */ + shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, little)); + shiftValueInner(TAP_DRSHIFT, TAP_IDLE, 6 + post_bits + pre_bits, (reg_addr | (1 << 5))); + buffer += 4; + } + shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, little)); + shiftValueInner(TAP_DRSHIFT, TAP_IDLE, 6 + post_bits, (reg_addr | (1 << 5))); } } @@ -848,8 +867,9 @@ int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opc /* bypass bits before and after */ - int pre_bits = 0; - int post_bits = 0; + int pre_bits; + int post_bits; + jtag_pre_post_bits(tap, &pre_bits, &post_bits); bool found = false; struct jtag_tap *cur_tap, *nextTap; @@ -885,6 +905,7 @@ int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opc value |= (*t++<<24); shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, value); + /* minimum 2 bits */ shiftValueInner(TAP_DRSHIFT, TAP_DRPAUSE, post_bits, 0); #if 1 @@ -940,6 +961,7 @@ static const struct command_registration zy1000_commands[] = { "With no arguments, prints status.", .usage = "('on'|'off)", }, +#if BUILD_ECOSBOARD { .name = "zy1000_version", .mode = COMMAND_ANY, @@ -947,6 +969,15 @@ static const struct command_registration zy1000_commands[] = { .help = "Print version info for zy1000.", .usage = "['openocd'|'zy1000'|'date'|'time'|'pcb'|'fpga']", }, +#else + { + .name = "zy1000_server", + .mode = COMMAND_ANY, + .jim_handler = jim_zy1000_server, + .help = "Tcpip address for ZY1000 server.", + .usage = "address", + }, +#endif { .name = "powerstatus", .mode = COMMAND_ANY, @@ -966,6 +997,320 @@ static const struct command_registration zy1000_commands[] = { }; +static int tcp_ip = -1; + +/* Write large packets if we can */ +static size_t out_pos; +static uint8_t out_buffer[16384]; +static size_t in_pos; +static size_t in_write; +static uint8_t in_buffer[16384]; + +static bool flush_writes(void) +{ + bool ok = (write(tcp_ip, out_buffer, out_pos) == (int)out_pos); + out_pos = 0; + return ok; +} + +static bool writeLong(uint32_t l) +{ + int i; + for (i = 0; i < 4; i++) + { + uint8_t c = (l >> (i*8))&0xff; + out_buffer[out_pos++] = c; + if (out_pos >= sizeof(out_buffer)) + { + if (!flush_writes()) + { + return false; + } + } + } + return true; +} + +static bool readLong(uint32_t *out_data) +{ + if (out_pos > 0) + { + if (!flush_writes()) + { + return false; + } + } + + uint32_t data = 0; + int i; + for (i = 0; i < 4; i++) + { + uint8_t c; + if (in_pos == in_write) + { + /* read more */ + int t; + t = read(tcp_ip, in_buffer, sizeof(in_buffer)); + if (t < 1) + { + return false; + } + in_write = (size_t) t; + in_pos = 0; + } + c = in_buffer[in_pos++]; + + data |= (c << (i*8)); + } + *out_data = data; + return true; +} + +enum ZY1000_CMD +{ + ZY1000_CMD_POKE = 0x0, + ZY1000_CMD_PEEK = 0x8, + ZY1000_CMD_SLEEP = 0x1, +}; + + +#if !BUILD_ECOSBOARD + +#include /* for socket(), connect(), send(), and recv() */ +#include /* for sockaddr_in and inet_addr() */ + +/* We initialize this late since we need to know the server address + * first. + */ +static void tcpip_open(void) +{ + if (tcp_ip >= 0) + return; + + struct sockaddr_in echoServAddr; /* Echo server address */ + + /* Create a reliable, stream socket using TCP */ + if ((tcp_ip = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) + { + fprintf(stderr, "Failed to connect to zy1000 server\n"); + exit(-1); + } + + /* Construct the server address structure */ + memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ + echoServAddr.sin_family = AF_INET; /* Internet address family */ + echoServAddr.sin_addr.s_addr = inet_addr(tcp_server); /* Server IP address */ + echoServAddr.sin_port = htons(7777); /* Server port */ + + /* Establish the connection to the echo server */ + if (connect(tcp_ip, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) + { + fprintf(stderr, "Failed to connect to zy1000 server\n"); + exit(-1); + } + + int flag = 1; + setsockopt(tcp_ip, /* socket affected */ + IPPROTO_TCP, /* set option at TCP level */ + TCP_NODELAY, /* name of option */ + (char *)&flag, /* the cast is historical cruft */ + sizeof(int)); /* length of option value */ + +} + + +/* send a poke */ +void zy1000_tcpout(uint32_t address, uint32_t data) +{ + tcpip_open(); + if (!writeLong((ZY1000_CMD_POKE << 24) | address)|| + !writeLong(data)) + { + fprintf(stderr, "Could not write to zy1000 server\n"); + exit(-1); + } +} + +uint32_t zy1000_tcpin(uint32_t address) +{ + tcpip_open(); + uint32_t data; + if (!writeLong((ZY1000_CMD_PEEK << 24) | address)|| + !readLong(&data)) + { + fprintf(stderr, "Could not read from zy1000 server\n"); + exit(-1); + } + return data; +} + +int interface_jtag_add_sleep(uint32_t us) +{ + tcpip_open(); + if (!writeLong((ZY1000_CMD_SLEEP << 24))|| + !writeLong(us)) + { + fprintf(stderr, "Could not read from zy1000 server\n"); + exit(-1); + } + return ERROR_OK; +} + + +#endif + +#if BUILD_ECOSBOARD +static char tcpip_stack[2048]; + +static cyg_thread tcpip_thread_object; +static cyg_handle_t tcpip_thread_handle; + +/* Infinite loop peeking & poking */ +static void tcpipserver(void) +{ + for (;;) + { + uint32_t address; + if (!readLong(&address)) + return; + enum ZY1000_CMD c = (address >> 24) & 0xff; + address &= 0xffffff; + switch (c) + { + case ZY1000_CMD_POKE: + { + uint32_t data; + if (!readLong(&data)) + return; + address &= ~0x80000000; + ZY1000_POKE(address + ZY1000_JTAG_BASE, data); + break; + } + case ZY1000_CMD_PEEK: + { + uint32_t data; + ZY1000_PEEK(address + ZY1000_JTAG_BASE, data); + if (!writeLong(data)) + return; + break; + } + case ZY1000_CMD_SLEEP: + { + uint32_t data; + if (!readLong(&data)) + return; + jtag_sleep(data); + break; + } + default: + return; + } + } +} + + +static void tcpip_server(cyg_addrword_t data) +{ + int so_reuseaddr_option = 1; + + int fd; + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + LOG_ERROR("error creating socket: %s", strerror(errno)); + exit(-1); + } + + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option, + sizeof(int)); + + struct sockaddr_in sin; + unsigned int address_size; + address_size = sizeof(sin); + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = INADDR_ANY; + sin.sin_port = htons(7777); + + if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1) + { + LOG_ERROR("couldn't bind to socket: %s", strerror(errno)); + exit(-1); + } + + if (listen(fd, 1) == -1) + { + LOG_ERROR("couldn't listen on socket: %s", strerror(errno)); + exit(-1); + } + + + for (;;) + { + tcp_ip = accept(fd, (struct sockaddr *) &sin, &address_size); + if (tcp_ip < 0) + { + continue; + } + + int flag = 1; + setsockopt(tcp_ip, /* socket affected */ + IPPROTO_TCP, /* set option at TCP level */ + TCP_NODELAY, /* name of option */ + (char *)&flag, /* the cast is historical cruft */ + sizeof(int)); /* length of option value */ + + bool save_poll = jtag_poll_get_enabled(); + + /* polling will screw up the "connection" */ + jtag_poll_set_enabled(false); + + tcpipserver(); + + jtag_poll_set_enabled(save_poll); + + close(tcp_ip); + + } + close(fd); + +} + +int interface_jtag_add_sleep(uint32_t us) +{ + jtag_sleep(us); + return ERROR_OK; +} + +#endif + + +int zy1000_init(void) +{ +#if BUILD_ECOSBOARD + LOG_USER("%s", ZYLIN_OPENOCD_VERSION); +#endif + + ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x30); // Turn on LED1 & LED2 + + setPower(true); // on by default + + + /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */ + zy1000_reset(0, 0); + zy1000_speed(jtag_get_speed()); + + +#if BUILD_ECOSBOARD + cyg_thread_create(1, tcpip_server, (cyg_addrword_t) 0, "tcip/ip server", + (void *) tcpip_stack, sizeof(tcpip_stack), + &tcpip_thread_handle, &tcpip_thread_object); + cyg_thread_resume(tcpip_thread_handle); +#endif + + return ERROR_OK; +} + + struct jtag_interface zy1000_interface = {