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
166 cmd->cmd.runtest->num_cycles,
184 cmd->cmd.pathmove->num_states,
186 ->path[
cmd->cmd.pathmove
190 cmd->cmd.pathmove->path);
213 LOG_DEBUG_IO(
"stable clock %u cycles",
cmd->cmd.stableclocks->num_cycles);
217 LOG_ERROR(
"BUG: unknown JTAG command type encountered");
234 bool was_msg_already_printed =
false;
239 const ssize_t read_count = read(fd,
buffer,
sizeof(
buffer));
241 if (read_count == 0) {
246 if (read_count > 0) {
247 if (!was_msg_already_printed) {
248 LOG_INFO(
"Some stale data from a previous connection was discarded.");
249 was_msg_already_printed =
true;
255 assert(read_count == -1);
257 const int errno_code = errno;
259 if (errno_code == EINTR)
262 if (errno_code == EAGAIN ||
263 errno_code == EWOULDBLOCK) {
278 LOG_ERROR(
"You need to specify the serial port!");
302 LOG_ERROR(
"Error configuring the serial port.");
307 LOG_ERROR(
"Error while attempting to discard any stale data right after establishing the connection.");
312 LOG_ERROR(
"Error configuring the serial port.");
338 LOG_INFO(
"Shutting down buspirate.");
378 LOG_ERROR(
"usage: buspirate_vreg <1|0>");
394 LOG_ERROR(
"usage: buspirate_pullup <1|0>");
409 }
else if (atoi(
CMD_ARGV[0]) == 0) {
431 LOG_ERROR(
"usage: buspirate_mode <normal|open-drain>");
447 LOG_ERROR(
"usage: buspirate_speed <normal|fast>");
468 .handler = &buspirate_handle_adc_command,
470 .help =
"reads voltages on adc pins",
476 .handler = &buspirate_handle_vreg_command,
478 .help =
"changes the state of voltage regulators",
483 .handler = &buspirate_handle_pullup_command,
485 .help =
"changes the state of pullup",
490 .handler = &buspirate_handle_led_command,
492 .help =
"changes the state of led",
496 .usage =
"<normal|fast>",
497 .handler = &buspirate_handle_speed_command,
499 .help =
"speed of the interface",
503 .usage =
"<normal|open-drain>",
504 .handler = &buspirate_handle_mode_command,
506 .help =
"pin mode of the interface",
510 .usage =
"/dev/ttyUSB0",
511 .handler = &buspirate_handle_port_command,
513 .help =
"name of the serial port to open",
522 .help =
"perform buspirate management",
575 for (i = 0; i < tms_count; i++) {
576 tms = (tms_scan >> i) & 1;
585 for (
unsigned int i = 0; i < num_states; i++) {
615 for (
unsigned int i = 0; i < num_cycles; i++)
663 for (
unsigned int i = 0; i < num_cycles; i++)
671 #define BUSPIRATE_BUFFER_SIZE 1024
682 #define BUSPIRATE_MAX_PENDING_SCANS 128
718 LOG_DEBUG(
"executing tap num bits = %i scans = %i",
728 for (i = 0; i < bytes_to_send; i++) {
783 if ((have_scans < scans) || (have_bits <
bits))
796 uint8_t
bit = 1 << bit_index;
798 if (bit_index == 0) {
823 LOG_ERROR(
"tap_chain overflow, bad things will happen");
840 for (i = 0; i <
length; i++) {
841 int tms = (i <
length-1 ? 0 : 1);
842 int tdi = (
buffer[i/8] >> (i%8)) & 1;
853 LOG_DEBUG(
"trst: %i, srst: %i", trst, srst);
900 LOG_DEBUG(
"Buspirate speed setting in SWD mode defaults to 400 kHz");
907 LOG_ERROR(
"Buspirate did not answer correctly");
911 LOG_ERROR(
"Buspirate did not reply as expected to the speed change command");
930 LOG_ERROR(
"Buspirate did not answer correctly");
934 LOG_ERROR(
"Buspirate did not reply as expected to the configure command");
946 LOG_DEBUG(
"Buspirate TRST feature not available in SWD mode");
949 LOG_ERROR(
"Buspirate LED feature not available in SWD mode");
961 LOG_DEBUG(
"Buspirate unknown feature %d", feat);
969 LOG_DEBUG(
"Buspirate feature %d not supported in SWD mode", feat);
970 }
else if (tmp[0] != 1) {
971 LOG_ERROR(
"Buspirate did not reply as expected to the configure command");
981 const char *mode_answers[2] = {
"OCD1",
"RAW1" };
982 const char *correct_ans =
NULL;
983 uint8_t tmp[21] = { [0 ... 20] = 0x00 };
989 correct_ans = mode_answers[1];
992 correct_ans = mode_answers[0];
995 LOG_DEBUG(
"Entering binary mode, that is %s", correct_ans);
1004 "/OpenOCD support enabled?");
1007 if (strncmp((
char *)tmp,
"BBIO", 4) == 0) {
1010 LOG_ERROR(
"Buspirate did not answer correctly! "
1011 "Do you have correct firmware?");
1014 if (tmp[0] !=
'1') {
1015 LOG_ERROR(
"Unsupported binary protocol");
1018 if (cmd_sent == 0) {
1027 }
else if (strncmp((
char *)tmp, correct_ans, 4) == 0)
1030 LOG_ERROR(
"Buspirate did not answer correctly! "
1031 "Do you have correct firmware?");
1047 if (strncmp((
char *)tmp,
"BBIO1", 5) == 0) {
1051 LOG_ERROR(
"Unable to restart buspirate!");
1069 LOG_ERROR(
"Error configuring the serial port.");
1076 LOG_ERROR(
"Buspirate did not ack speed change");
1080 LOG_ERROR(
"Buspirate did not reply as expected to the speed change command");
1083 LOG_INFO(
"Buspirate switched to %s mode",
1108 uint16_t a, b, c, d;
1111 a = tmp[2] << 8 | tmp[3];
1112 b = tmp[4] << 8 | tmp[5];
1113 c = tmp[6] << 8 | tmp[7];
1114 d = tmp[8] << 8 | tmp[9];
1116 LOG_INFO(
"ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
1118 ((
float)a)/155.1515, ((
float)b)/155.1515,
1119 ((
float)c)/155.1515, ((
float)d)/155.1515);
1123 uint8_t *
cmd,
int cmdlen)
1136 if (res == cmdlen) {
1149 return (
unsigned char)
cmd[1];
1171 struct termios t_opt;
1175 fcntl(fd, F_SETFL, 0);
1176 if (tcgetattr(fd, &t_opt) != 0)
1179 if (cfsetispeed(&t_opt, baud) != 0)
1182 if (cfsetospeed(&t_opt, baud) != 0)
1185 t_opt.c_cflag |= (CLOCAL | CREAD);
1186 t_opt.c_cflag &= ~PARENB;
1187 t_opt.c_cflag &= ~CSTOPB;
1188 t_opt.c_cflag &= ~CSIZE;
1189 t_opt.c_cflag |= CS8;
1190 t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
1195 t_opt.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL);
1197 t_opt.c_oflag &= ~OPOST;
1198 t_opt.c_cc[VMIN] = 0;
1204 if (tcsetattr(fd, TCSADRAIN, &t_opt) != 0) {
1218 ret = write(fd, buf,
size);
1235 while (len <
size) {
1236 ret = read(fd, buf+len,
size-len);
1252 LOG_DEBUG(
"should have read = %d actual size = %d",
size, len);
1266 #define LINE_SIZE 81
1267 #define BYTES_PER_LINE 16
1276 snprintf(tmp, 5,
"%02x ", (uint8_t)buf[
offset]);
1295 LOG_INFO(
"Buspirate SWD mode enabled");
1305 uint32_t no_bytes, sequence_offset;
1324 LOG_ERROR(
"Sequence %d not supported", seq);
1328 no_bytes = sequence_len;
1329 sequence_offset = 0;
1335 to_send = no_bytes > 16 ? 16 : no_bytes;
1337 tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
1338 memcpy(tmp + 1, &
sequence[sequence_offset], to_send);
1343 no_bytes -= to_send;
1344 sequence_offset += to_send;
1370 return tmp[2] << 2 | tmp[1] << 1 | tmp[0];
1378 no_bytes = (no_bits + 7) / 8;
1379 memset(tmp + 1, 0x00,
sizeof(tmp) - 1);
1388 uint8_t to_send = no_bytes > 16 ? 16 : no_bytes;
1389 tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
1394 no_bytes -= to_send;
1431 uint32_t data = (uint8_t) tmp[0];
1432 data |= (uint8_t) tmp[1] << 8;
1433 data |= (uint8_t) tmp[2] << 16;
1434 data |= (uint8_t) tmp[3] << 24;
1435 int parity = tmp[4] ? 0x01 : 0x00;
1437 LOG_DEBUG(
"%s %s %s reg %X = %08"PRIx32,
1465 LOG_DEBUG(
"No valid acknowledge: ack=%d", ack);
1487 tmp[0] = 0x10 + ((4 + 1 - 1) & 0xF);
1495 LOG_DEBUG(
"%s %s %s reg %X = %08"PRIx32,
1516 LOG_DEBUG(
"No valid acknowledge: ack=%d", ack);
1531 LOG_DEBUG(
"SWD queue return value: %02x", retval);
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 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_end_state(tap_state_t state)
static const char *const buspirate_transports[]
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_path_move(unsigned int num_states, tap_state_t *path)
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_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
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 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 void buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
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
#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...
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 tap_state_t jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf, unsigned int tap_len, tap_state_t 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
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
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]
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.
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)