30 #undef AUTOMATIC_BUSY_LED
33 #undef USE_HARDWARE_SHIFTER_FOR_TMS
35 #define INTERFACE_NAME "RLink"
37 #define USB_IDVENDOR (0x138e)
38 #define USB_IDPRODUCT (0x9000)
40 #define USB_EP1OUT_ADDR (0x01)
41 #define USB_EP1OUT_SIZE (16)
42 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
43 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
45 #define USB_EP2OUT_ADDR (0x02)
46 #define USB_EP2OUT_SIZE (64)
47 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
48 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
49 #define USB_EP2BANK_SIZE (512)
51 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
53 #define ST7_PD_NBUSY_LED ST7_PD0
54 #define ST7_PD_NRUN_LED ST7_PD1
56 #define ST7_PD_VPP_SEL ST7_PD6
58 #define ST7_PD_VPP_SHDN ST7_PD7
61 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
62 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
65 #define ST7_PA_IO1 ST7_PA1
66 #define ST7_PA_IO2 ST7_PA2
67 #define ST7_PA_IO4 ST7_PA4
68 #define ST7_PA_IO8 ST7_PA6
69 #define ST7_PA_IO10 ST7_PA7
70 #define ST7_PB_IO5 ST7_PB5
71 #define ST7_PC_IO9 ST7_PC1
72 #define ST7_PC_IO3 ST7_PC2
73 #define ST7_PC_IO7 ST7_PC3
74 #define ST7_PE_IO6 ST7_PE5
77 #define ST7_PA_RTCK ST7_PA_IO0
78 #define ST7_PA_NTRST ST7_PA_IO1
79 #define ST7_PC_TDI ST7_PC_IO3
80 #define ST7_PA_DBGRQ ST7_PA_IO4
81 #define ST7_PB_NSRST ST7_PB_IO5
82 #define ST7_PE_TMS ST7_PE_IO6
83 #define ST7_PC_TCK ST7_PC_IO7
84 #define ST7_PC_TDO ST7_PC_IO9
85 #define ST7_PA_DBGACK ST7_PA_IO10
87 static struct libusb_device_handle *
hdev;
97 uint8_t *usb_buffer_p;
102 if (
length >
sizeof(usb_buffer))
103 length =
sizeof(usb_buffer);
105 usb_buffer_p = usb_buffer;
109 *usb_buffer_p++ = va_arg(ap,
int);
116 sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
122 (
char *)usb_buffer,
sizeof(usb_buffer),
133 static ssize_t ep1_memory_read(
134 struct libusb_device_handle *hdev_param, uint16_t
addr,
147 sizeof(usb_buffer) - 4
154 if (remain >
sizeof(usb_buffer))
155 length =
sizeof(usb_buffer);
159 usb_buffer[1] =
addr >> 8;
160 usb_buffer[2] =
addr;
165 (
char *)usb_buffer,
sizeof(usb_buffer),
170 if (usb_ret !=
ERROR_OK || transferred < (
int)
sizeof(usb_buffer))
207 if (remain > (
sizeof(usb_buffer) - 4))
208 length = (
sizeof(usb_buffer) - 4);
212 usb_buffer[1] =
addr >> 8;
213 usb_buffer[2] =
addr;
223 sizeof(usb_buffer) - 4 -
length
230 (
char *)usb_buffer,
sizeof(usb_buffer),
235 if (usb_ret !=
ERROR_OK || transferred < (
int)
sizeof(usb_buffer))
249 static ssize_t ep1_memory_writel(
struct libusb_device_handle *hdev_param, uint16_t
addr,
265 *buffer_p++ = va_arg(ap,
int);
273 #define DTCLOAD_COMMENT (0)
274 #define DTCLOAD_ENTRY (1)
275 #define DTCLOAD_LOAD (2)
276 #define DTCLOAD_RUN (3)
277 #define DTCLOAD_LUT_START (4)
278 #define DTCLOAD_LUT (5)
280 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
295 struct header_s *header;
296 uint8_t lut_start = 0xc0;
309 if (
length <
sizeof(*header)) {
314 header = (
struct header_s *)
buffer;
315 buffer +=
sizeof(*header);
316 length -=
sizeof(*header);
318 if (
length < (
size_t)header->length + 1) {
323 switch (header->type) {
329 if (!strncmp(
"download", (
char *)
buffer + 1, 8))
338 header->length + 1,
buffer
375 header->length + 1,
buffer
382 LOG_ERROR(
"Invalid DTC image record type: 0x%02x", header->type);
387 buffer += (header->length + 1);
388 length -= (header->length + 1);
460 struct libusb_device_handle *hdev_param,
461 uint8_t *command_buffer,
462 int command_buffer_size,
463 uint8_t *reply_buffer,
464 int reply_buffer_size
472 LOG_DEBUG(
"%d/%d", command_buffer_size, reply_buffer_size);
508 if (dtc_status & 0x01)
512 LOG_ERROR(
"too many retries waiting for DTC status");
513 return LIBUSB_ERROR_TIMEOUT;
518 if (reply_buffer && reply_buffer_size) {
522 (
char *)reply_buffer, reply_buffer_size,
527 if (usb_err !=
ERROR_OK || transferred < reply_buffer_size) {
528 LOG_ERROR(
"Read of endpoint 2 returned %d, expected %d",
529 usb_err, reply_buffer_size
631 uint8_t *dtc_p, *tdo_p;
632 uint8_t dtc_mask, tdo_mask;
648 reply_buffer,
sizeof(reply_buffer)
651 LOG_ERROR(
"dtc_run_download: %s", libusb_error_name(usb_err));
657 dtc_p = reply_buffer;
678 dtc_mask = 1 << (8 - 1);
685 if (*dtc_p & dtc_mask)
693 dtc_mask = 1 << (8 - 1);
711 dtc_mask = 1 << (8 - 1);
713 dtc_mask = 1 << (bit_cnt - 1);
744 rq_next = rq_p->
next;
796 if ((
bits >= 8) || !i) {
797 byte_param <<= (8 -
bits);
802 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
810 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
862 for (i = 0; i < tms_count; i++) {
863 tms = (tms_scan >> i) & 1;
872 int num_states =
cmd->num_states;
883 LOG_ERROR(
"BUG: %s -> %s isn't a valid TAP transition",
912 for (i = 0; i < num_cycles; i++)
937 LOG_ERROR(
"%s", libusb_error_name(usb_err));
947 if (usb_err !=
ERROR_OK || transferred < 1) {
948 LOG_ERROR(
"%s", libusb_error_name(usb_err));
973 LOG_ERROR(
"%s", libusb_error_name(usb_err));
983 if (usb_err !=
ERROR_OK || transferred < 1) {
984 LOG_ERROR(
"%s", libusb_error_name(usb_err));
1004 LOG_ERROR(
"%s", libusb_error_name(usb_err));
1014 if (usb_err !=
ERROR_OK || transferred < 1) {
1015 LOG_ERROR(
"%s", libusb_error_name(usb_err));
1021 uint8_t *
buffer,
int scan_size)
1032 uint8_t tdi_mask, *tdi_p;
1035 if (scan_size < 1) {
1036 LOG_ERROR(
"scan_size cannot be less than 1 bit");
1040 ir_scan =
cmd->cmd.scan->ir_scan;
1055 printf(
"scan_size = %d, type = 0x%x\n", scan_size,
type);
1061 buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1063 printf(
"before scan:");
1064 for (i = 0; i < (scan_size + 7) / 8; i++)
1065 printf(
" %02x",
buffer[i]);
1071 byte_bits = (int)(scan_size - 1) / 8 * 8;
1073 extra_bits = (scan_size - 1) - byte_bits;
1088 dtc_mask = 1 << (extra_bits - 1);
1090 while (extra_bits--) {
1091 if (*tdi_p & tdi_mask)
1097 if (tdi_mask == 0) {
1115 chunk_bits = byte_bits;
1117 if (chunk_bits > (16 * 8))
1118 chunk_bits = (16 * 8);
1135 byte_bits -= chunk_bits;
1140 LOG_ERROR(
"enqueuing DTC reply entry: %s", strerror(errno));
1143 dtc_queue.reply_index += (chunk_bits + 7) / 8;
1145 tdi_bit_offset += chunk_bits;
1149 chunk_bytes = chunk_bits / 8;
1166 dtc_mask = 1 << (8 - 1);
1168 while (chunk_bits--) {
1169 if (*tdi_p & tdi_mask)
1173 if (dtc_mask == 0) {
1176 dtc_mask = 1 << (8 - 1);
1180 if (tdi_mask == 0) {
1197 LOG_ERROR(
"enqueuing DTC reply entry: %s", strerror(errno));
1203 tdi_bit_offset += extra_bits;
1214 dtc_mask = 1 << (8 - 1);
1216 while (extra_bits--) {
1217 if (*tdi_p & tdi_mask)
1223 if (tdi_mask == 0) {
1246 LOG_ERROR(
"enqueuing DTC reply entry: %s", strerror(errno));
1271 int retval, tmp_retval;
1276 #ifndef AUTOMATIC_BUSY_LED
1285 switch (
cmd->type) {
1300 switch (
cmd->type) {
1303 cmd->cmd.reset->trst,
1304 cmd->cmd.reset->srst);
1305 if ((
cmd->cmd.reset->trst == 1) ||
1306 (
cmd->cmd.reset->srst &&
1313 cmd->cmd.runtest->num_cycles,
1314 cmd->cmd.runtest->end_state);
1315 if (
cmd->cmd.runtest->end_state != -1)
1321 if (
cmd->cmd.statemove->end_state != -1)
1327 cmd->cmd.pathmove->num_states,
1328 cmd->cmd.pathmove->path[
cmd->cmd.pathmove->num_states - 1]);
1333 (
cmd->cmd.scan->ir_scan) ?
"IR" :
"DR",
1334 cmd->cmd.scan->end_state);
1335 if (
cmd->cmd.scan->end_state != -1)
1347 LOG_ERROR(
"BUG: unknown JTAG command type encountered");
1358 retval = tmp_retval;
1360 #ifndef AUTOMATIC_BUSY_LED
1388 "An error occurred while trying to load DTC code for speed \"%d\".",
1395 LOG_ERROR(
"starting DTC: %s", libusb_error_name(ret));
1403 LOG_ERROR(
"%d is not a supported speed", speed);
1418 LOG_ERROR(
"%d is not a supported speed", speed);
1454 struct libusb_device_descriptor descriptor;
1455 struct libusb_device *usb_dev = libusb_get_device(
hdev);
1456 int r = libusb_get_device_descriptor(usb_dev, &descriptor);
1458 LOG_ERROR(
"error %d getting device descriptor", r);
1462 if (descriptor.bNumConfigurations > 1) {
1463 LOG_ERROR(
"Whoops! NumConfigurations is not 1, don't know what to do...");
1466 struct libusb_config_descriptor *
config;
1467 libusb_get_config_descriptor(usb_dev, 0, &
config);
1468 if (
config->bNumInterfaces > 1) {
1469 LOG_ERROR(
"Whoops! NumInterfaces is not 1, don't know what to do...");
1476 libusb_set_configuration(
hdev,
config->bConfigurationValue);
1480 i = libusb_claim_interface(
hdev, 0);
1481 if (i != LIBUSB_SUCCESS) {
1482 LOG_ERROR(
"usb_claim_interface: %s", libusb_error_name(i));
1483 j = libusb_detach_kernel_driver(
hdev, 0);
1484 if (j != LIBUSB_SUCCESS)
1485 LOG_ERROR(
"detach kernel driver: %s", libusb_error_name(j));
1490 }
while (--retries);
1492 if (i != LIBUSB_SUCCESS) {
1496 if (libusb_set_interface_alt_setting(
hdev, 0, 0) != LIBUSB_SUCCESS) {
1510 for (i = 0; i < 5; i++) {
1516 LOG_ERROR(
"USB write error: %s", libusb_error_name(j));
1521 (
char *)reply_buffer,
sizeof(reply_buffer),
1525 if (j != LIBUSB_ERROR_TIMEOUT)
1529 if (j !=
ERROR_OK || transferred != (
int)
sizeof(reply_buffer)) {
1530 LOG_ERROR(
"USB read error: %s", libusb_error_name(j));
1538 if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3))
1540 "The rlink device is not of the version that the developers have played with. It may or may not work.");
1565 (
char *)reply_buffer, 1,
1590 (
char *)reply_buffer, 1,
1639 #ifdef AUTOMATIC_BUSY_LED
1668 libusb_release_interface(
hdev, 0);
const char *const jtag_only[]
struct jtag_command * jtag_command_queue
The current queue of jtag_command_s structures.
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
enum scan_type jtag_scan_type(const struct scan_command *cmd)
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
scan_type
The inferred type of a scan_command_s structure, indicating whether the command has the host scan in ...
@ SCAN_IN
From device to host,.
@ SCAN_OUT
From host to device,.
bool tap_is_state_stable(tap_state_t astate)
Function tap_is_state_stable returns true if the astate is stable.
tap_state_t tap_state_transition(tap_state_t cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
const char * tap_state_name(tap_state_t state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
void tap_set_end_state(tap_state_t new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
tap_state_t tap_get_end_state(void)
For more information,.
int tap_get_tms_path(tap_state_t from, tap_state_t to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
int tap_get_tms_path_len(tap_state_t from, tap_state_t to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
tap_state_t tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
static struct device_config config
void jtag_sleep(uint32_t us)
enum reset_types jtag_get_reset_config(void)
#define ERROR_JTAG_QUEUE_FAILED
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
int jtag_libusb_bulk_write(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
#define LIBUSB_TIMEOUT_MS
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
static int dtc_load_from_buffer(struct libusb_device_handle *hdev_param, const uint8_t *buffer, size_t length)
static int dtc_queue_run(void)
static struct dtc_reply_queue_entry * dtc_queue_enqueue_reply(enum scan_type type, uint8_t *buffer, int size, int offset, int length, struct jtag_command *cmd)
static int rlink_execute_queue(void)
static int dtc_queue_init(void)
static struct libusb_device_handle * hdev
static int tap_state_queue_append(uint8_t tms)
#define ST7_PE_ADAPTER_SENSE_IN
static struct @26 dtc_queue
static int rlink_khz(int khz, int *speed)
static ssize_t ep1_memory_write(struct libusb_device_handle *hdev_param, uint16_t addr, size_t length, uint8_t const *buffer)
static struct jtag_interface rlink_interface
static int dtc_run_download(struct libusb_device_handle *hdev_param, uint8_t *command_buffer, int command_buffer_size, uint8_t *reply_buffer, int reply_buffer_size)
static void rlink_path_move(struct pathmove_command *cmd)
static int rlink_init(void)
#define ST7_PE_ADAPTER_SENSE_OUT
static int ep1_generic_commandl(struct libusb_device_handle *hdev_param, size_t length,...)
static void rlink_end_state(tap_state_t state)
static void rlink_runtest(int num_cycles)
uint8_t cmd_buffer[USB_EP2BANK_SIZE]
static int dtc_start_download(void)
static int rlink_scan(struct jtag_command *cmd, enum scan_type type, uint8_t *buffer, int scan_size)
static int rlink_speed(int speed)
#define DTCLOAD_LUT_START
struct dtc_reply_queue_entry * rq_head
static uint8_t dtc_entry_download
static int tap_state_queue_run(void)
static int rlink_quit(void)
static int tap_state_queue_init(void)
struct adapter_driver rlink_adapter_driver
static struct @27 tap_state_queue
struct dtc_reply_queue_entry * rq_tail
static void rlink_state_move(void)
static int dtc_queue_run_if_full(int reserved_cmd, int reserved_reply)
static void rlink_reset(int trst, int srst)
static int rlink_speed_div(int speed, int *khz)
#define DTC_STATUS_POLL_BYTE
const size_t rlink_speed_table_size
#define DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(tms, tdi, tdo)
#define DTC_CMD_SHIFT_TMS_BITS(bits)
#define DTC_CMD_SHIFT_TDO_BYTES(bytes)
#define DTC_CMD_SHIFT_TDIO_BYTES(bytes)
#define DTC_CMD_SHIFT_TMS_BYTES(bytes)
#define DTC_CMD_SHIFT_TDI_BYTES(bytes)
#define DTC_CMD_SHIFT_TDIO_BITS(bits)
#define EP1_CMD_MEMORY_READ
#define EP1_CMD_LEDUE_BUSY
#define EP1_CMD_SET_PORTD_LEDS
#define EP1_CMD_DTC_GET_CACHED_STATUS
#define EP1_CMD_SET_PORTD_VPP
#define EP1_CMD_SET_UPLOAD
#define EP1_CMD_LEDUE_NONE
#define EP1_CMD_MEMORY_WRITE
#define EP1_CMD_SET_DOWNLOAD
#define EP1_CMD_GET_FWREV
#define ST7_EP2TXR_DTOG_TX
#define ST7_USB_BUF_EP0OUT
#define ST7_EP2TXR_STAT_VALID
size_t size
Size of the control block search area.
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
struct dtc_reply_queue_entry * next
struct jtag_command * cmd
struct dtc_reply_queue_entry::@28 scan
union jtag_command_container cmd
Represents a driver for a debugging interface.
int(* execute_queue)(void)
Execute queued commands.
struct scan_command * scan