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 %i 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;
587 for (i = 0; i < num_states; i++) {
619 for (i = 0; i < num_cycles; i++)
668 for (i = 0; i < num_cycles; i++)
676 #define BUSPIRATE_BUFFER_SIZE 1024
687 #define BUSPIRATE_MAX_PENDING_SCANS 128
723 LOG_DEBUG(
"executing tap num bits = %i scans = %i",
733 for (i = 0; i < bytes_to_send; i++) {
788 if ((have_scans < scans) || (have_bits <
bits))
801 uint8_t
bit = 1 << bit_index;
803 if (bit_index == 0) {
828 LOG_ERROR(
"tap_chain overflow, bad things will happen");
845 for (i = 0; i <
length; i++) {
846 int tms = (i <
length-1 ? 0 : 1);
847 int tdi = (
buffer[i/8] >> (i%8)) & 1;
858 LOG_DEBUG(
"trst: %i, srst: %i", trst, srst);
905 LOG_DEBUG(
"Buspirate speed setting in SWD mode defaults to 400 kHz");
912 LOG_ERROR(
"Buspirate did not answer correctly");
916 LOG_ERROR(
"Buspirate did not reply as expected to the speed change command");
935 LOG_ERROR(
"Buspirate did not answer correctly");
939 LOG_ERROR(
"Buspirate did not reply as expected to the configure command");
951 LOG_DEBUG(
"Buspirate TRST feature not available in SWD mode");
954 LOG_ERROR(
"Buspirate LED feature not available in SWD mode");
966 LOG_DEBUG(
"Buspirate unknown feature %d", feat);
974 LOG_DEBUG(
"Buspirate feature %d not supported in SWD mode", feat);
975 }
else if (tmp[0] != 1) {
976 LOG_ERROR(
"Buspirate did not reply as expected to the configure command");
986 const char *mode_answers[2] = {
"OCD1",
"RAW1" };
987 const char *correct_ans =
NULL;
988 uint8_t tmp[21] = { [0 ... 20] = 0x00 };
994 correct_ans = mode_answers[1];
997 correct_ans = mode_answers[0];
1000 LOG_DEBUG(
"Entering binary mode, that is %s", correct_ans);
1009 "/OpenOCD support enabled?");
1012 if (strncmp((
char *)tmp,
"BBIO", 4) == 0) {
1015 LOG_ERROR(
"Buspirate did not answer correctly! "
1016 "Do you have correct firmware?");
1019 if (tmp[0] !=
'1') {
1020 LOG_ERROR(
"Unsupported binary protocol");
1023 if (cmd_sent == 0) {
1032 }
else if (strncmp((
char *)tmp, correct_ans, 4) == 0)
1035 LOG_ERROR(
"Buspirate did not answer correctly! "
1036 "Do you have correct firmware?");
1052 if (strncmp((
char *)tmp,
"BBIO1", 5) == 0) {
1056 LOG_ERROR(
"Unable to restart buspirate!");
1074 LOG_ERROR(
"Error configuring the serial port.");
1081 LOG_ERROR(
"Buspirate did not ack speed change");
1085 LOG_ERROR(
"Buspirate did not reply as expected to the speed change command");
1088 LOG_INFO(
"Buspirate switched to %s mode",
1113 uint16_t a, b, c, d;
1116 a = tmp[2] << 8 | tmp[3];
1117 b = tmp[4] << 8 | tmp[5];
1118 c = tmp[6] << 8 | tmp[7];
1119 d = tmp[8] << 8 | tmp[9];
1121 LOG_INFO(
"ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
1123 ((
float)a)/155.1515, ((
float)b)/155.1515,
1124 ((
float)c)/155.1515, ((
float)d)/155.1515);
1128 uint8_t *
cmd,
int cmdlen)
1141 if (res == cmdlen) {
1154 return (
unsigned char)
cmd[1];
1176 struct termios t_opt;
1180 fcntl(fd, F_SETFL, 0);
1181 if (tcgetattr(fd, &t_opt) != 0)
1184 if (cfsetispeed(&t_opt, baud) != 0)
1187 if (cfsetospeed(&t_opt, baud) != 0)
1190 t_opt.c_cflag |= (CLOCAL | CREAD);
1191 t_opt.c_cflag &= ~PARENB;
1192 t_opt.c_cflag &= ~CSTOPB;
1193 t_opt.c_cflag &= ~CSIZE;
1194 t_opt.c_cflag |= CS8;
1195 t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
1200 t_opt.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL);
1202 t_opt.c_oflag &= ~OPOST;
1203 t_opt.c_cc[VMIN] = 0;
1209 if (tcsetattr(fd, TCSADRAIN, &t_opt) != 0) {
1223 ret = write(fd, buf,
size);
1240 while (len <
size) {
1241 ret = read(fd, buf+len,
size-len);
1257 LOG_DEBUG(
"should have read = %d actual size = %d",
size, len);
1271 #define LINE_SIZE 81
1272 #define BYTES_PER_LINE 16
1281 snprintf(tmp, 5,
"%02x ", (uint8_t)buf[
offset]);
1300 LOG_INFO(
"Buspirate SWD mode enabled");
1310 uint32_t no_bytes, sequence_offset;
1329 LOG_ERROR(
"Sequence %d not supported", seq);
1333 no_bytes = sequence_len;
1334 sequence_offset = 0;
1340 to_send = no_bytes > 16 ? 16 : no_bytes;
1342 tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
1343 memcpy(tmp + 1, &
sequence[sequence_offset], to_send);
1348 no_bytes -= to_send;
1349 sequence_offset += to_send;
1375 return tmp[2] << 2 | tmp[1] << 1 | tmp[0];
1383 no_bytes = (no_bits + 7) / 8;
1384 memset(tmp + 1, 0x00,
sizeof(tmp) - 1);
1393 uint8_t to_send = no_bytes > 16 ? 16 : no_bytes;
1394 tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
1399 no_bytes -= to_send;
1436 uint32_t data = (uint8_t) tmp[0];
1437 data |= (uint8_t) tmp[1] << 8;
1438 data |= (uint8_t) tmp[2] << 16;
1439 data |= (uint8_t) tmp[3] << 24;
1440 int parity = tmp[4] ? 0x01 : 0x00;
1442 LOG_DEBUG(
"%s %s %s reg %X = %08"PRIx32,
1470 LOG_DEBUG(
"No valid acknowledge: ack=%d", ack);
1492 tmp[0] = 0x10 + ((4 + 1 - 1) & 0xF);
1500 LOG_DEBUG(
"%s %s %s reg %X = %08"PRIx32,
1521 LOG_DEBUG(
"No valid acknowledge: ack=%d", ack);
1536 LOG_DEBUG(
"SWD queue return value: %02x", retval);
void * buf_set_buf(const void *_src, unsigned src_start, void *_dst, unsigned dst_start, unsigned len)
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned 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 const struct swd_driver buspirate_swd
static int buspirate_execute_queue(void)
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 void buspirate_runtest(int num_cycles)
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 void buspirate_stableclocks(int num_cycles)
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 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)
static void buspirate_path_move(int num_states, tap_state_t *path)
#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.
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 ...
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 ...
static tap_state_t jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf, unsigned tap_len, tap_state_t start_tap_state)
Prints verbose TAP state transitions for the given TMS/TDI buffers.
#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
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)
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)(void)
Execute queued commands.
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_s structures that should be sca...
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
static const unsigned swd_seq_swd_to_jtag_len
static const unsigned swd_seq_jtag_to_swd_len
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 uint8_t swd_seq_line_reset[]
SWD Line reset.
static const unsigned swd_seq_line_reset_len
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.
#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)