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 *header;
296 uint8_t lut_start = 0xc0;
309 if (
length <
sizeof(*header)) {
314 header = (
struct header *)
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 unsigned int num_states =
cmd->num_states;
883 LOG_ERROR(
"BUG: %s -> %s isn't a valid TAP transition",
910 for (
unsigned int i = 0; i < num_cycles; i++)
935 LOG_ERROR(
"%s", libusb_error_name(usb_err));
945 if (usb_err !=
ERROR_OK || transferred < 1) {
946 LOG_ERROR(
"%s", libusb_error_name(usb_err));
971 LOG_ERROR(
"%s", libusb_error_name(usb_err));
981 if (usb_err !=
ERROR_OK || transferred < 1) {
982 LOG_ERROR(
"%s", libusb_error_name(usb_err));
1002 LOG_ERROR(
"%s", libusb_error_name(usb_err));
1012 if (usb_err !=
ERROR_OK || transferred < 1) {
1013 LOG_ERROR(
"%s", libusb_error_name(usb_err));
1019 uint8_t *
buffer,
int scan_size)
1030 uint8_t tdi_mask, *tdi_p;
1033 if (scan_size < 1) {
1034 LOG_ERROR(
"scan_size cannot be less than 1 bit");
1038 ir_scan =
cmd->cmd.scan->ir_scan;
1053 printf(
"scan_size = %d, type = 0x%x\n", scan_size,
type);
1059 buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1061 printf(
"before scan:");
1062 for (i = 0; i < (scan_size + 7) / 8; i++)
1063 printf(
" %02x",
buffer[i]);
1069 byte_bits = (int)(scan_size - 1) / 8 * 8;
1071 extra_bits = (scan_size - 1) - byte_bits;
1086 dtc_mask = 1 << (extra_bits - 1);
1088 while (extra_bits--) {
1089 if (*tdi_p & tdi_mask)
1095 if (tdi_mask == 0) {
1113 chunk_bits = byte_bits;
1115 if (chunk_bits > (16 * 8))
1116 chunk_bits = (16 * 8);
1133 byte_bits -= chunk_bits;
1138 LOG_ERROR(
"enqueuing DTC reply entry: %s", strerror(errno));
1141 dtc_queue.reply_index += (chunk_bits + 7) / 8;
1143 tdi_bit_offset += chunk_bits;
1147 chunk_bytes = chunk_bits / 8;
1164 dtc_mask = 1 << (8 - 1);
1166 while (chunk_bits--) {
1167 if (*tdi_p & tdi_mask)
1171 if (dtc_mask == 0) {
1174 dtc_mask = 1 << (8 - 1);
1178 if (tdi_mask == 0) {
1195 LOG_ERROR(
"enqueuing DTC reply entry: %s", strerror(errno));
1201 tdi_bit_offset += extra_bits;
1212 dtc_mask = 1 << (8 - 1);
1214 while (extra_bits--) {
1215 if (*tdi_p & tdi_mask)
1221 if (tdi_mask == 0) {
1244 LOG_ERROR(
"enqueuing DTC reply entry: %s", strerror(errno));
1269 int retval, tmp_retval;
1274 #ifndef AUTOMATIC_BUSY_LED
1283 switch (
cmd->type) {
1298 switch (
cmd->type) {
1301 cmd->cmd.reset->trst,
1302 cmd->cmd.reset->srst);
1303 if ((
cmd->cmd.reset->trst == 1) ||
1304 (
cmd->cmd.reset->srst &&
1311 cmd->cmd.runtest->num_cycles,
1312 cmd->cmd.runtest->end_state);
1313 if (
cmd->cmd.runtest->end_state != -1)
1319 if (
cmd->cmd.statemove->end_state != -1)
1325 cmd->cmd.pathmove->num_states,
1326 cmd->cmd.pathmove->path[
cmd->cmd.pathmove->num_states - 1]);
1331 (
cmd->cmd.scan->ir_scan) ?
"IR" :
"DR",
1332 cmd->cmd.scan->end_state);
1333 if (
cmd->cmd.scan->end_state != -1)
1345 LOG_ERROR(
"BUG: unknown JTAG command type encountered");
1356 retval = tmp_retval;
1358 #ifndef AUTOMATIC_BUSY_LED
1386 "An error occurred while trying to load DTC code for speed \"%d\".",
1393 LOG_ERROR(
"starting DTC: %s", libusb_error_name(ret));
1401 LOG_ERROR(
"%d is not a supported speed", speed);
1416 LOG_ERROR(
"%d is not a supported speed", speed);
1452 struct libusb_device_descriptor descriptor;
1453 struct libusb_device *usb_dev = libusb_get_device(
hdev);
1454 int r = libusb_get_device_descriptor(usb_dev, &descriptor);
1456 LOG_ERROR(
"error %d getting device descriptor", r);
1460 if (descriptor.bNumConfigurations > 1) {
1461 LOG_ERROR(
"Whoops! NumConfigurations is not 1, don't know what to do...");
1464 struct libusb_config_descriptor *
config;
1465 libusb_get_config_descriptor(usb_dev, 0, &
config);
1466 if (
config->bNumInterfaces > 1) {
1467 LOG_ERROR(
"Whoops! NumInterfaces is not 1, don't know what to do...");
1474 libusb_set_configuration(
hdev,
config->bConfigurationValue);
1478 i = libusb_claim_interface(
hdev, 0);
1479 if (i != LIBUSB_SUCCESS) {
1480 LOG_ERROR(
"usb_claim_interface: %s", libusb_error_name(i));
1481 j = libusb_detach_kernel_driver(
hdev, 0);
1482 if (j != LIBUSB_SUCCESS)
1483 LOG_ERROR(
"detach kernel driver: %s", libusb_error_name(j));
1488 }
while (--retries);
1490 if (i != LIBUSB_SUCCESS) {
1494 if (libusb_set_interface_alt_setting(
hdev, 0, 0) != LIBUSB_SUCCESS) {
1508 for (i = 0; i < 5; i++) {
1514 LOG_ERROR(
"USB write error: %s", libusb_error_name(j));
1519 (
char *)reply_buffer,
sizeof(reply_buffer),
1523 if (j != LIBUSB_ERROR_TIMEOUT)
1527 if (j !=
ERROR_OK || transferred != (
int)
sizeof(reply_buffer)) {
1528 LOG_ERROR(
"USB read error: %s", libusb_error_name(j));
1536 if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3))
1538 "The rlink device is not of the version that the developers have played with. It may or may not work.");
1563 (
char *)reply_buffer, 1,
1588 (
char *)reply_buffer, 1,
1637 #ifdef AUTOMATIC_BUSY_LED
1666 libusb_release_interface(
hdev, 0);
const char *const jtag_only[]
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 structure, indicating whether the command has the host scan in fr...
@ 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_open(const uint16_t vids[], const uint16_t pids[], const char *product, struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
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)
#define LIBUSB_TIMEOUT_MS
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
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 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 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)
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 struct @31 tap_state_queue
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)
static struct @30 dtc_queue
struct adapter_driver rlink_adapter_driver
static int rlink_execute_queue(struct jtag_command *cmd_queue)
struct dtc_reply_queue_entry * rq_tail
static void rlink_state_move(void)
static void rlink_runtest(unsigned int num_cycles)
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
target_addr_t addr
Start address to search for the control block.
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::@32 scan
union jtag_command_container cmd
Represents a driver for a debugging interface.
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
struct scan_command * scan