19 #include <sys/ioctl.h>
36 #define CMD_UNKNOWN 0x00
37 #define CMD_PORT_MODE 0x01
38 #define CMD_FEATURE 0x02
39 #define CMD_READ_ADCS 0x03
41 #define CMD_TAP_SHIFT 0x05
42 #define CMD_ENTER_RWIRE 0x05
43 #define CMD_ENTER_OOCD 0x06
44 #define CMD_UART_SPEED 0x07
45 #define CMD_JTAG_SPEED 0x08
46 #define CMD_RAW_PERIPH 0x40
47 #define CMD_RAW_SPEED 0x60
48 #define CMD_RAW_MODE 0x80
50 #define CMD_TAP_SHIFT_HEADER_LEN 3
53 #define CMD_RAW_CONFIG_HIZ 0x00
54 #define CMD_RAW_CONFIG_3V3 0x08
55 #define CMD_RAW_CONFIG_2W 0x00
56 #define CMD_RAW_CONFIG_3W 0x04
57 #define CMD_RAW_CONFIG_MSB 0x00
58 #define CMD_RAW_CONFIG_LSB 0x02
61 #if !defined(B1000000)
62 #define B1000000 0010010
65 #define SHORT_TIMEOUT 1
66 #define NORMAL_TIMEOUT 10
165 cmd->cmd.runtest->num_cycles,
183 cmd->cmd.pathmove->num_states,
185 ->path[
cmd->cmd.pathmove
189 cmd->cmd.pathmove->path);
212 LOG_DEBUG_IO(
"stable clock %u cycles",
cmd->cmd.stableclocks->num_cycles);
216 LOG_ERROR(
"BUG: unknown JTAG command type encountered");
233 bool was_msg_already_printed =
false;
238 const ssize_t read_count = read(fd,
buffer,
sizeof(
buffer));
240 if (read_count == 0) {
245 if (read_count > 0) {
246 if (!was_msg_already_printed) {
247 LOG_INFO(
"Some stale data from a previous connection was discarded.");
248 was_msg_already_printed =
true;
254 assert(read_count == -1);
256 const int errno_code = errno;
258 if (errno_code == EINTR)
261 if (errno_code == EAGAIN ||
262 errno_code == EWOULDBLOCK) {
277 LOG_ERROR(
"You need to specify the serial port!");
301 LOG_ERROR(
"Error configuring the serial port.");
306 LOG_ERROR(
"Error while attempting to discard any stale data right after establishing the connection.");
311 LOG_ERROR(
"Error configuring the serial port.");
337 LOG_INFO(
"Shutting down buspirate.");
377 LOG_ERROR(
"usage: buspirate_vreg <1|0>");
393 LOG_ERROR(
"usage: buspirate_pullup <1|0>");
408 }
else if (atoi(
CMD_ARGV[0]) == 0) {
430 LOG_ERROR(
"usage: buspirate_mode <normal|open-drain>");
446 LOG_ERROR(
"usage: buspirate_speed <normal|fast>");
467 .handler = &buspirate_handle_adc_command,
469 .help =
"reads voltages on adc pins",
475 .handler = &buspirate_handle_vreg_command,
477 .help =
"changes the state of voltage regulators",
482 .handler = &buspirate_handle_pullup_command,
484 .help =
"changes the state of pullup",
489 .handler = &buspirate_handle_led_command,
491 .help =
"changes the state of led",
495 .usage =
"<normal|fast>",
496 .handler = &buspirate_handle_speed_command,
498 .help =
"speed of the interface",
502 .usage =
"<normal|open-drain>",
503 .handler = &buspirate_handle_mode_command,
505 .help =
"pin mode of the interface",
509 .usage =
"/dev/ttyUSB0",
510 .handler = &buspirate_handle_port_command,
512 .help =
"name of the serial port to open",
521 .help =
"perform buspirate management",
573 for (i = 0; i < tms_count; i++) {
574 tms = (tms_scan >> i) & 1;
583 for (
unsigned int i = 0; i < num_states; i++) {
613 for (
unsigned int i = 0; i < num_cycles; i++)
661 for (
unsigned int i = 0; i < num_cycles; i++)
669 #define BUSPIRATE_BUFFER_SIZE 1024
680 #define BUSPIRATE_MAX_PENDING_SCANS 128
716 LOG_DEBUG(
"executing tap num bits = %i scans = %i",
726 for (i = 0; i < bytes_to_send; i++) {
781 if ((have_scans < scans) || (have_bits <
bits))
794 uint8_t
bit = 1 << bit_index;
796 if (bit_index == 0) {
821 LOG_ERROR(
"tap_chain overflow, bad things will happen");
838 for (i = 0; i <
length; i++) {
839 int tms = (i <
length-1 ? 0 : 1);
840 int tdi = (
buffer[i/8] >> (i%8)) & 1;
851 LOG_DEBUG(
"trst: %i, srst: %i", trst, srst);
898 LOG_DEBUG(
"Buspirate speed setting in SWD mode defaults to 400 kHz");
905 LOG_ERROR(
"Buspirate did not answer correctly");
909 LOG_ERROR(
"Buspirate did not reply as expected to the speed change command");
928 LOG_ERROR(
"Buspirate did not answer correctly");
932 LOG_ERROR(
"Buspirate did not reply as expected to the configure command");
944 LOG_DEBUG(
"Buspirate TRST feature not available in SWD mode");
947 LOG_ERROR(
"Buspirate LED feature not available in SWD mode");
959 LOG_DEBUG(
"Buspirate unknown feature %d", feat);
967 LOG_DEBUG(
"Buspirate feature %d not supported in SWD mode", feat);
968 }
else if (tmp[0] != 1) {
969 LOG_ERROR(
"Buspirate did not reply as expected to the configure command");
979 const char *mode_answers[2] = {
"OCD1",
"RAW1" };
980 const char *correct_ans =
NULL;
981 uint8_t tmp[21] = { [0 ... 20] = 0x00 };
987 correct_ans = mode_answers[1];
990 correct_ans = mode_answers[0];
993 LOG_DEBUG(
"Entering binary mode, that is %s", correct_ans);
1002 "/OpenOCD support enabled?");
1005 if (strncmp((
char *)tmp,
"BBIO", 4) == 0) {
1008 LOG_ERROR(
"Buspirate did not answer correctly! "
1009 "Do you have correct firmware?");
1012 if (tmp[0] !=
'1') {
1013 LOG_ERROR(
"Unsupported binary protocol");
1016 if (cmd_sent == 0) {
1025 }
else if (strncmp((
char *)tmp, correct_ans, 4) == 0)
1028 LOG_ERROR(
"Buspirate did not answer correctly! "
1029 "Do you have correct firmware?");
1045 if (strncmp((
char *)tmp,
"BBIO1", 5) == 0) {
1049 LOG_ERROR(
"Unable to restart buspirate!");
1067 LOG_ERROR(
"Error configuring the serial port.");
1074 LOG_ERROR(
"Buspirate did not ack speed change");
1078 LOG_ERROR(
"Buspirate did not reply as expected to the speed change command");
1081 LOG_INFO(
"Buspirate switched to %s mode",
1106 uint16_t a, b, c, d;
1109 a = tmp[2] << 8 | tmp[3];
1110 b = tmp[4] << 8 | tmp[5];
1111 c = tmp[6] << 8 | tmp[7];
1112 d = tmp[8] << 8 | tmp[9];
1114 LOG_INFO(
"ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
1116 ((
float)a)/155.1515, ((
float)b)/155.1515,
1117 ((
float)c)/155.1515, ((
float)d)/155.1515);
1121 uint8_t *
cmd,
int cmdlen)
1134 if (res == cmdlen) {
1147 return (
unsigned char)
cmd[1];
1167 struct termios t_opt;
1171 fcntl(fd, F_SETFL, 0);
1172 if (tcgetattr(fd, &t_opt) != 0)
1175 if (cfsetispeed(&t_opt, baud) != 0)
1178 if (cfsetospeed(&t_opt, baud) != 0)
1181 t_opt.c_cflag |= (CLOCAL | CREAD);
1182 t_opt.c_cflag &= ~PARENB;
1183 t_opt.c_cflag &= ~CSTOPB;
1184 t_opt.c_cflag &= ~CSIZE;
1185 t_opt.c_cflag |= CS8;
1186 t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
1191 t_opt.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL);
1193 t_opt.c_oflag &= ~OPOST;
1194 t_opt.c_cc[VMIN] = 0;
1200 if (tcsetattr(fd, TCSADRAIN, &t_opt) != 0) {
1214 ret = write(fd, buf,
size);
1231 while (len <
size) {
1232 ret = read(fd, buf+len,
size-len);
1248 LOG_DEBUG(
"should have read = %d actual size = %d",
size, len);
1262 #define LINE_SIZE 81
1263 #define BYTES_PER_LINE 16
1272 snprintf(tmp, 5,
"%02x ", (uint8_t)buf[
offset]);
1291 LOG_INFO(
"Buspirate SWD mode enabled");
1301 uint32_t no_bytes, sequence_offset;
1320 LOG_ERROR(
"Sequence %d not supported", seq);
1324 no_bytes = sequence_len;
1325 sequence_offset = 0;
1331 to_send = no_bytes > 16 ? 16 : no_bytes;
1333 tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
1334 memcpy(tmp + 1, &
sequence[sequence_offset], to_send);
1339 no_bytes -= to_send;
1340 sequence_offset += to_send;
1366 return tmp[2] << 2 | tmp[1] << 1 | tmp[0];
1374 no_bytes = (no_bits + 7) / 8;
1375 memset(tmp + 1, 0x00,
sizeof(tmp) - 1);
1384 uint8_t to_send = no_bytes > 16 ? 16 : no_bytes;
1385 tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
1390 no_bytes -= to_send;
1422 uint32_t data = (uint8_t) tmp[0];
1423 data |= (uint8_t) tmp[1] << 8;
1424 data |= (uint8_t) tmp[2] << 16;
1425 data |= (uint8_t) tmp[3] << 24;
1426 int parity = tmp[4] ? 0x01 : 0x00;
1428 LOG_DEBUG(
"%s %s %s reg %X = %08"PRIx32,
1454 LOG_DEBUG(
"No valid acknowledge: ack=%d", ack);
1470 tmp[0] = 0x10 + ((4 + 1 - 1) & 0xF);
1478 LOG_DEBUG(
"%s %s %s reg %X = %08"PRIx32,
1498 LOG_DEBUG(
"No valid acknowledge: ack=%d", ack);
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
static void buspirate_tap_init(void)
static void buspirate_end_state(enum tap_state state)
static const struct command_registration buspirate_subcommand_handlers[]
static enum tap_state last_tap_state
static int buspirate_tap_execute(void)
#define BUSPIRATE_MAX_PENDING_SCANS
static void buspirate_path_move(unsigned int num_states, enum tap_state *path)
static void buspirate_swd_clear_sticky_errors(void)
static void buspirate_state_move(void)
static int buspirate_pinmode
static void buspirate_tap_make_space(int scan, int bits)
static void buspirate_runtest(unsigned int num_cycles)
static const struct swd_driver buspirate_swd
static void buspirate_swd_set_feature(int, char, char)
static const struct command_registration buspirate_command_handlers[]
static char * buspirate_port
struct adapter_driver buspirate_adapter_driver
static void buspirate_jtag_set_feature(int, char, char)
static int buspirate_baudrate
#define CMD_TAP_SHIFT_HEADER_LEN
static void buspirate_print_buffer(uint8_t *buf, int size)
static int buspirate_pullup
static int buspirate_swd_init(void)
static void buspirate_jtag_reset(int)
static struct pending_scan_result tap_pending_scans[BUSPIRATE_MAX_PENDING_SCANS]
static unsigned char buspirate_jtag_command(int, uint8_t *, int)
static int buspirate_vreg
static uint8_t buspirate_swd_write_header(uint8_t cmd)
static uint8_t tms_chain[BUSPIRATE_BUFFER_SIZE]
static void buspirate_set_speed(int, char)
static void buspirate_set_mode(int, char)
static void buspirate_jtag_get_adcs(int)
#define CMD_RAW_CONFIG_LSB
static int buspirate_serial_read(int fd, uint8_t *buf, int size)
static void buspirate_set_feature(int, char, char)
static int buspirate_execute_queue(struct jtag_command *cmd_queue)
static void buspirate_stableclocks(unsigned int num_cycles)
static void buspirate_swd_idle_clocks(uint32_t no_bits)
static uint8_t tdi_chain[BUSPIRATE_BUFFER_SIZE]
static int tap_pending_scans_num
static int buspirate_serial_write(int fd, uint8_t *buf, int size)
static int buspirate_swd_run_queue(void)
static void buspirate_swd_set_speed(int, char)
static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout)
static void buspirate_swd_set_mode(int, char)
static bool read_and_discard_all_data(const int fd)
static void buspirate_jtag_set_speed(int, char)
#define CMD_RAW_CONFIG_3V3
static void buspirate_serial_close(int fd)
COMMAND_HANDLER(buspirate_handle_adc_command)
static struct jtag_interface buspirate_interface
static int buspirate_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
static void buspirate_bbio_enable(int)
static int buspirate_quit(void)
static int buspirate_serial_open(char *port)
static void buspirate_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
static int buspirate_init(void)
static int buspirate_reset(int trst, int srst)
static void buspirate_jtag_set_mode(int, char)
static int tap_chain_index
static int buspirate_swd_switch_seq(enum swd_special_seq seq)
static void buspirate_tap_append(int tms, int tdi)
static void buspirate_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
#define BUSPIRATE_BUFFER_SIZE
static int buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
#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_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)
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...
uint64_t buffer
Pointer to data buffer to send over SPI.
uint32_t size
Size of dw_spi_transaction::buffer.
enum tap_state tap_get_end_state(void)
For more information,.
enum tap_state tap_state_transition(enum tap_state 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(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
int tap_get_tms_path_len(enum tap_state from, enum tap_state to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
bool tap_is_state_stable(enum tap_state astate)
Function tap_is_state_stable returns true if the astate is stable.
int tap_get_tms_path(enum tap_state from, enum tap_state to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
#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 enum tap_state jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf, unsigned int tap_len, enum tap_state start_tap_state)
Prints verbose TAP state transitions for the given TMS/TDI buffers.
void jtag_sleep(uint32_t us)
#define ERROR_JTAG_DEVICE_ERROR
tap_state
Defines JTAG Test Access Port states.
#define ERROR_JTAG_QUEUE_FAILED
#define ERROR_JTAG_INIT_FAILED
static struct scan_blk scan
#define LOG_DEBUG_IO(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static uint32_t bit(uint32_t value, unsigned int b)
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
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.
int first
First bit position in tdo_buffer to read.
int length
Number of bits to read.
uint8_t * buffer
Location to store the result.
struct scan_command * command
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
static uint8_t swd_cmd(bool is_read, bool is_ap, uint8_t regnum)
Construct a "cmd" byte, in lSB bit order, which swd_driver.read_reg() and swd_driver....
static const unsigned int swd_seq_jtag_to_swd_len
static const unsigned int swd_seq_line_reset_len
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
static const unsigned int swd_seq_swd_to_jtag_len
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
static unsigned int parity(unsigned int v)