82 static struct ftdi_context
ftdic;
84 #define OPENJTAG_BUFFER_SIZE 504
85 #define OPENJTAG_MAX_PENDING_RESULTS 256
103 static struct libusb_device_handle *
usbh;
106 #define CY7C65215_JTAG_REQUEST 0x40
107 #define CY7C65215_JTAG_ENABLE 0xD0
108 #define CY7C65215_JTAG_DISABLE 0xD1
109 #define CY7C65215_JTAG_READ 0xD2
110 #define CY7C65215_JTAG_WRITE 0xD3
112 #define CY7C65215_USB_TIMEOUT 100
117 #define CY7C65215_JTAG_CLASS 0xff
118 #define CY7C65215_JTAG_SUBCLASS 0x04
122 #ifdef _DEBUG_USB_COMMS_
124 #define DEBUG_TYPE_READ 0
125 #define DEBUG_TYPE_WRITE 1
126 #define DEBUG_TYPE_OCD_READ 2
127 #define DEBUG_TYPE_BUFFER 3
130 static void openjtag_debug_buffer(uint8_t *
buffer,
int length, uint8_t
type)
138 case DEBUG_TYPE_READ:
139 sprintf(line,
"USB READ %d bytes",
length);
141 case DEBUG_TYPE_WRITE:
142 sprintf(line,
"USB WRITE %d bytes",
length);
144 case DEBUG_TYPE_OCD_READ:
145 sprintf(line,
"TO OpenOCD %d bytes",
length);
147 case DEBUG_TYPE_BUFFER:
148 sprintf(line,
"Buffer %d bytes",
length);
154 for (i = 0; i <
length; i += LINE_LEN) {
156 case DEBUG_TYPE_READ:
157 sprintf(line,
"USB READ: %04x", i);
159 case DEBUG_TYPE_WRITE:
160 sprintf(line,
"USB WRITE: %04x", i);
162 case DEBUG_TYPE_OCD_READ:
163 sprintf(line,
"TO OpenOCD: %04x", i);
165 case DEBUG_TYPE_BUFFER:
166 sprintf(line,
"BUFFER: %04x", i);
170 for (j = i; j < i + LINE_LEN && j <
length; j++) {
171 sprintf(s,
" %02x",
buffer[j]);
207 uint8_t *buf,
int size, uint32_t *bytes_written)
210 #ifdef _DEBUG_USB_COMMS_
211 openjtag_debug_buffer(buf,
size, DEBUG_TYPE_WRITE);
214 retval = ftdi_write_data(&
ftdic, buf,
size);
221 *bytes_written = retval;
227 uint8_t *buf,
int size, uint32_t *bytes_written)
231 #ifdef _DEBUG_USB_COMMS_
232 openjtag_debug_buffer(buf,
size, DEBUG_TYPE_WRITE);
253 *bytes_written = ret;
259 uint8_t *buf,
int size, uint32_t *bytes_written)
270 uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
278 while ((*bytes_read < qty) &&
timeout--) {
279 retval = ftdi_read_data(&
ftdic, buf + *bytes_read,
284 ftdi_get_error_string(&
ftdic));
287 *bytes_read += retval;
290 #ifdef _DEBUG_USB_COMMS_
291 openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
298 uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
323 #ifdef _DEBUG_USB_COMMS_
324 openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
376 LOG_WARNING(
"adapter speed not recognized, reverting to 375 kHz");
386 uint8_t latency_timer;
390 LOG_WARNING(
"no openjtag device description specified, "
391 "using default 'Open JTAG Project'");
395 if (ftdi_init(&
ftdic) < 0)
404 if (ftdi_usb_reset(&
ftdic) < 0) {
405 LOG_ERROR(
"unable to reset ftdi device");
409 if (ftdi_set_latency_timer(&
ftdic, 2) < 0) {
410 LOG_ERROR(
"unable to set latency timer");
414 if (ftdi_get_latency_timer(&
ftdic, &latency_timer) < 0) {
415 LOG_ERROR(
"unable to get latency timer");
418 LOG_DEBUG(
"current latency timer: %u", latency_timer);
420 ftdi_disable_bitbang(&
ftdic);
422 if (ftdi_set_baudrate(&
ftdic, 3000000) < 0) {
423 LOG_ERROR(
"Can't set baud rate to max: %s",
424 ftdi_get_error_string(&
ftdic));
443 LOG_ERROR(
"unable to open cy7c65215 device");
451 LOG_ERROR(
"unable to claim JTAG interface");
460 LOG_ERROR(
"could not enable JTAG module");
498 ftdi_usb_close(&
ftdic);
533 uint32_t rx_expected = 0;
540 }
else if ((
usb_tx_buf[tx_offs] & 0x0F) == 2) {
588 #ifdef _DEBUG_USB_COMMS_
589 openjtag_debug_buffer(
buffer,
670 cmd->cmd.reset->trst,
cmd->cmd.reset->srst);
675 if (
cmd->cmd.reset->srst) {
719 int scan_size, old_state;
728 #ifdef _DEBUG_USB_COMMS_
729 openjtag_debug_buffer(
buffer, (scan_size + 7) / 8, DEBUG_TYPE_BUFFER);
761 cmd->cmd.runtest->num_cycles) {
763 unsigned int num_cycles =
cmd->cmd.runtest->num_cycles;
766 const unsigned int num_cycles_cmd =
MIN(num_cycles, 16);
769 command |= ((num_cycles_cmd - 1) & 0x0F) << 4;
772 num_cycles -= num_cycles_cmd;
773 }
while (num_cycles > 0);
805 LOG_ERROR(
"BUG: unknown Open JTAG command type encountered");
834 else if (khz >= 24000)
836 else if (khz >= 12000)
838 else if (khz >= 6000)
840 else if (khz >= 3000)
842 else if (khz >= 1500)
857 LOG_ERROR(
"require exactly one argument to "
858 "openjtag_device_desc <description>");
875 LOG_ERROR(
"require exactly one argument to "
876 "openjtag_variant <variant>");
894 .
name =
"device_desc",
895 .handler = openjtag_handle_device_desc_command,
897 .help =
"set the USB device description of the OpenJTAG",
898 .usage =
"description-string",
902 .handler = openjtag_handle_variant_command,
904 .help =
"set the OpenJTAG variant",
905 .usage =
"variant-string",
909 .handler = openjtag_handle_vid_pid_command,
911 .help =
"USB VID and PID of the adapter",
921 .help =
"perform openjtag management",
const char *const jtag_only[]
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
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,.
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...
void jtag_sleep(uint32_t us)
#define ERROR_JTAG_DEVICE_ERROR
#define ERROR_JTAG_INIT_FAILED
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
static int ftdi_tcioflush(struct ftdi_context *ftdi)
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_control_transfer(struct libusb_device_handle *dev, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, char *bytes, uint16_t size, unsigned int timeout, int *transferred)
void jtag_libusb_close(struct libusb_device_handle *dev)
int jtag_libusb_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_choose_interface(struct libusb_device_handle *devh, unsigned int *usb_read_ep, unsigned int *usb_write_ep, int bclass, int subclass, int protocol, int trans_type)
Find the first interface optionally matching class, subclass and protocol and claim it.
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
static int openjtag_quit_standard(void)
static int openjtag_khz(int khz, int *jtag_speed)
static uint8_t usb_tx_buf[OPENJTAG_BUFFER_SIZE]
static const uint16_t cy7c65215_vids[]
static int8_t openjtag_get_tap_state(int8_t state)
static int openjtag_quit(void)
#define CY7C65215_JTAG_ENABLE
static struct openjtag_scan_result openjtag_scan_result_buffer[OPENJTAG_MAX_PENDING_RESULTS]
static void openjtag_add_byte(char buf)
static int openjtag_buf_read_cy7c65215(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
#define CY7C65215_JTAG_WRITE
static struct jtag_interface openjtag_interface
static int openjtag_buf_read_standard(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
#define CY7C65215_JTAG_DISABLE
static char * openjtag_device_desc
static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
static uint16_t openjtag_vid
static int openjtag_buf_write_standard(uint8_t *buf, int size, uint32_t *bytes_written)
static void openjtag_write_tap_buffer(void)
#define CY7C65215_USB_TIMEOUT
static void openjtag_execute_command(struct jtag_command *cmd)
static const struct command_registration openjtag_command_handlers[]
static const uint16_t cy7c65215_pids[]
#define OPENJTAG_BUFFER_SIZE
#define CY7C65215_JTAG_SUBCLASS
static void openjtag_execute_runtest(struct jtag_command *cmd)
static int openjtag_execute_tap_queue(void)
static uint32_t usb_rx_buf_len
static void openjtag_execute_scan(struct jtag_command *cmd)
static int openjtag_speed_div(int speed, int *khz)
static uint16_t openjtag_pid
static int openjtag_scan_result_count
static int usb_tx_buf_offs
#define CY7C65215_JTAG_READ
static const char *const openjtag_variant_names[]
static int openjtag_buf_write_cy7c65215(uint8_t *buf, int size, uint32_t *bytes_written)
static int openjtag_speed(int speed)
static const struct command_registration openjtag_subcommand_handlers[]
static void openjtag_execute_sleep(struct jtag_command *cmd)
static uint8_t usb_rx_buf[OPENJTAG_BUFFER_SIZE]
static void openjtag_execute_statemove(struct jtag_command *cmd)
struct adapter_driver openjtag_adapter_driver
static struct libusb_device_handle * usbh
static unsigned int ep_out
#define CY7C65215_JTAG_CLASS
static int openjtag_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
static unsigned int ep_in
@ OPENJTAG_VARIANT_STANDARD
@ OPENJTAG_VARIANT_CY7C65215
static int openjtag_execute_queue(struct jtag_command *cmd_queue)
@ OPENJTAG_TAP_CAPTURE_DR
static int openjtag_quit_cy7c65215(void)
static int openjtag_sendcommand(uint8_t cmd)
static struct ftdi_context ftdic
static int openjtag_init_cy7c65215(void)
static int openjtag_init_standard(void)
enum openjtag_tap_state openjtag_tap_state_t
COMMAND_HANDLER(openjtag_handle_device_desc_command)
#define CY7C65215_JTAG_REQUEST
static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
static enum @29 openjtag_variant
static void openjtag_set_state(uint8_t openocd_state)
#define OPENJTAG_MAX_PENDING_RESULTS
static int openjtag_init(void)
static void openjtag_execute_reset(struct jtag_command *cmd)
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
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.
const char * usage
a string listing the options and arguments, required or optional
Represents a driver for a debugging interface.
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
struct scan_command * command
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.