1 /***************************************************************************
2 * Copyright (C) 2010 by Michal Demin *
3 * based on usbprog.c and arm-jtag-ew.c *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include <jtag/interface.h>
26 #include <jtag/commands.h>
30 #include <sys/ioctl.h>
33 /*#define DEBUG_SERIAL */
34 static int buspirate_execute_queue(void);
35 static int buspirate_speed(int speed
);
36 static int buspirate_khz(int khz
, int *jtag_speed
);
37 static int buspirate_init(void);
38 static int buspirate_quit(void);
40 static void buspirate_end_state(tap_state_t state
);
41 static void buspirate_state_move(void);
42 static void buspirate_path_move(int num_states
, tap_state_t
*path
);
43 static void buspirate_runtest(int num_cycles
);
44 static void buspirate_scan(bool ir_scan
, enum scan_type type
,
45 uint8_t *buffer
, int scan_size
, struct scan_command
*command
);
48 #define CMD_UNKOWN 0x00
49 #define CMD_PORT_MODE 0x01
50 #define CMD_FEATURE 0x02
51 #define CMD_READ_ADCS 0x03
52 /*#define CMD_TAP_SHIFT 0x04 // old protocol */
53 #define CMD_TAP_SHIFT 0x05
54 #define CMD_ENTER_OOCD 0x06
55 #define CMD_UART_SPEED 0x07
56 #define CMD_JTAG_SPEED 0x08
60 MODE_JTAG
= 1, /* push-pull outputs */
61 MODE_JTAG_OD
= 2, /* open-drain outputs */
83 static int buspirate_fd
= -1;
84 static int buspirate_pinmode
= MODE_JTAG_OD
;
85 static int buspirate_baudrate
= SERIAL_NORMAL
;
86 static int buspirate_vreg
;
87 static int buspirate_pullup
;
88 static char *buspirate_port
;
92 static void buspirate_tap_init(void);
93 static int buspirate_tap_execute(void);
94 static void buspirate_tap_append(int tms
, int tdi
);
95 static void buspirate_tap_append_scan(int length
, uint8_t *buffer
,
96 struct scan_command
*command
);
97 static void buspirate_tap_make_space(int scan
, int bits
);
99 static void buspirate_reset(int trst
, int srst
);
101 /* low level interface */
102 static void buspirate_jtag_reset(int);
103 static void buspirate_jtag_enable(int);
104 static unsigned char buspirate_jtag_command(int, char *, int);
105 static void buspirate_jtag_set_speed(int, char);
106 static void buspirate_jtag_set_mode(int, char);
107 static void buspirate_jtag_set_feature(int, char, char);
108 static void buspirate_jtag_get_adcs(int);
110 /* low level HW communication interface */
111 static int buspirate_serial_open(char *port
);
112 static int buspirate_serial_setspeed(int fd
, char speed
);
113 static int buspirate_serial_write(int fd
, char *buf
, int size
);
114 static int buspirate_serial_read(int fd
, char *buf
, int size
);
115 static void buspirate_serial_close(int fd
);
116 static void buspirate_print_buffer(char *buf
, int size
);
118 static int buspirate_speed(int speed
)
121 LOG_INFO("Want to set speed to %dkHz, but not implemented yet", speed
);
125 static int buspirate_khz(int khz
, int *jtag_speed
)
131 static int buspirate_execute_queue(void)
133 /* currently processed command */
134 struct jtag_command
*cmd
= jtag_command_queue
;
142 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
143 cmd
->cmd
.runtest
->num_cycles
,
144 tap_state_name(cmd
->cmd
.runtest
146 buspirate_end_state(cmd
->cmd
.runtest
148 buspirate_runtest(cmd
->cmd
.runtest
152 DEBUG_JTAG_IO("statemove end in %s",
153 tap_state_name(cmd
->cmd
.statemove
155 buspirate_end_state(cmd
->cmd
.statemove
157 buspirate_state_move();
160 DEBUG_JTAG_IO("pathmove: %i states, end in %s",
161 cmd
->cmd
.pathmove
->num_states
,
162 tap_state_name(cmd
->cmd
.pathmove
163 ->path
[cmd
->cmd
.pathmove
165 buspirate_path_move(cmd
->cmd
.pathmove
167 cmd
->cmd
.pathmove
->path
);
170 DEBUG_JTAG_IO("scan end in %s",
171 tap_state_name(cmd
->cmd
.scan
174 buspirate_end_state(cmd
->cmd
.scan
177 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
,
179 type
= jtag_scan_type(cmd
->cmd
.scan
);
180 buspirate_scan(cmd
->cmd
.scan
->ir_scan
, type
,
181 buffer
, scan_size
, cmd
->cmd
.scan
);
185 DEBUG_JTAG_IO("reset trst: %i srst %i",
186 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
188 /* flush buffers, so we can reset */
189 buspirate_tap_execute();
191 if (cmd
->cmd
.reset
->trst
== 1)
192 tap_set_state(TAP_RESET
);
193 buspirate_reset(cmd
->cmd
.reset
->trst
,
194 cmd
->cmd
.reset
->srst
);
197 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
198 buspirate_tap_execute();
199 jtag_sleep(cmd
->cmd
.sleep
->us
);
202 LOG_ERROR("BUG: unknown JTAG command type encountered");
209 return buspirate_tap_execute();
212 static int buspirate_init(void)
214 if (buspirate_port
== NULL
) {
215 LOG_ERROR("You need to specify port !");
216 return ERROR_JTAG_INIT_FAILED
;
219 buspirate_fd
= buspirate_serial_open(buspirate_port
);
220 if (buspirate_fd
== -1) {
221 LOG_ERROR("Could not open serial port.");
222 return ERROR_JTAG_INIT_FAILED
;
225 buspirate_serial_setspeed(buspirate_fd
, SERIAL_NORMAL
);
227 buspirate_jtag_enable(buspirate_fd
);
229 if (buspirate_baudrate
!= SERIAL_NORMAL
)
230 buspirate_jtag_set_speed(buspirate_fd
, SERIAL_FAST
);
232 LOG_INFO("Buspirate Interface ready!");
234 buspirate_tap_init();
235 buspirate_jtag_set_mode(buspirate_fd
, buspirate_pinmode
);
236 buspirate_jtag_set_feature(buspirate_fd
, FEATURE_VREG
,
237 (buspirate_vreg
== 1) ? ACTION_ENABLE
: ACTION_DISABLE
);
238 buspirate_jtag_set_feature(buspirate_fd
, FEATURE_PULLUP
,
239 (buspirate_pullup
== 1) ? ACTION_ENABLE
: ACTION_DISABLE
);
240 buspirate_reset(0, 0);
245 static int buspirate_quit(void)
247 LOG_INFO("Shuting down buspirate ");
248 buspirate_jtag_set_mode(buspirate_fd
, MODE_HIZ
);
250 buspirate_jtag_set_speed(buspirate_fd
, SERIAL_NORMAL
);
251 buspirate_jtag_reset(buspirate_fd
);
253 buspirate_serial_close(buspirate_fd
);
255 if (buspirate_port
) {
256 free(buspirate_port
);
257 buspirate_port
= NULL
;
262 /* openocd command interface */
263 COMMAND_HANDLER(buspirate_handle_adc_command
)
266 LOG_ERROR("usage: buspirate_adc");
270 if (buspirate_fd
== -1)
273 /* send the command */
274 buspirate_jtag_get_adcs(buspirate_fd
);
280 COMMAND_HANDLER(buspirate_handle_vreg_command
)
283 LOG_ERROR("usage: buspirate_vreg <1|0>");
287 if (atoi(CMD_ARGV
[0]) == 1)
296 COMMAND_HANDLER(buspirate_handle_pullup_command
)
299 LOG_ERROR("usage: buspirate_pullup <1|0>");
303 if (atoi(CMD_ARGV
[0]) == 1)
304 buspirate_pullup
= 1;
306 buspirate_pullup
= 0;
312 COMMAND_HANDLER(buspirate_handle_led_command
)
315 LOG_ERROR("usage: buspirate_led <1|0>");
319 if (atoi(CMD_ARGV
[0]) == 1) {
321 buspirate_jtag_set_feature(buspirate_fd
, FEATURE_LED
,
325 buspirate_jtag_set_feature(buspirate_fd
, FEATURE_LED
,
333 COMMAND_HANDLER(buspirate_handle_mode_command
)
336 LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
340 if (CMD_ARGV
[0][0] == 'n')
341 buspirate_pinmode
= MODE_JTAG
;
342 else if (CMD_ARGV
[0][0] == 'o')
343 buspirate_pinmode
= MODE_JTAG_OD
;
345 LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
351 COMMAND_HANDLER(buspirate_handle_speed_command
)
354 LOG_ERROR("usage: buspirate_speed <normal|fast>");
358 if (CMD_ARGV
[0][0] == 'n')
359 buspirate_baudrate
= SERIAL_NORMAL
;
360 else if (CMD_ARGV
[0][0] == 'f')
361 buspirate_baudrate
= SERIAL_FAST
;
363 LOG_ERROR("usage: buspirate_speed <normal|fast>");
369 COMMAND_HANDLER(buspirate_handle_port_command
)
372 LOG_ERROR("usage: buspirate_port /dev/ttyUSB0");
376 if (buspirate_port
== 0)
377 buspirate_port
= strdup(CMD_ARGV
[0]);
383 static const struct command_registration buspirate_command_handlers
[] = {
385 .name
= "buspirate_adc",
386 .handler
= &buspirate_handle_adc_command
,
387 .mode
= COMMAND_EXEC
,
388 .help
= "reads voltages on adc pins",
391 .name
= "buspirate_vreg",
392 .handler
= &buspirate_handle_vreg_command
,
393 .mode
= COMMAND_CONFIG
,
394 .help
= "changes the state of voltage regulators",
397 .name
= "buspirate_pullup",
398 .handler
= &buspirate_handle_pullup_command
,
399 .mode
= COMMAND_CONFIG
,
400 .help
= "changes the state of pullup",
403 .name
= "buspirate_led",
404 .handler
= &buspirate_handle_led_command
,
405 .mode
= COMMAND_EXEC
,
406 .help
= "changes the state of led",
409 .name
= "buspirate_speed",
410 .handler
= &buspirate_handle_speed_command
,
411 .mode
= COMMAND_CONFIG
,
412 .help
= "speed of the interface",
415 .name
= "buspirate_mode",
416 .handler
= &buspirate_handle_mode_command
,
417 .mode
= COMMAND_CONFIG
,
418 .help
= "pin mode of the interface",
421 .name
= "buspirate_port",
422 .handler
= &buspirate_handle_port_command
,
423 .mode
= COMMAND_CONFIG
,
424 .help
= "name of the serial port to open",
426 COMMAND_REGISTRATION_DONE
429 struct jtag_interface buspirate_interface
= {
431 .execute_queue
= buspirate_execute_queue
,
432 .speed
= buspirate_speed
,
433 .khz
= buspirate_khz
,
434 .commands
= buspirate_command_handlers
,
435 .init
= buspirate_init
,
436 .quit
= buspirate_quit
439 /*************** jtag execute commands **********************/
440 static void buspirate_end_state(tap_state_t state
)
442 if (tap_is_state_stable(state
))
443 tap_set_end_state(state
);
445 LOG_ERROR("BUG: %i is not a valid end state", state
);
450 static void buspirate_state_move(void)
453 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(),
454 tap_get_end_state());
455 int tms_count
= tap_get_tms_path_len(tap_get_state(),
456 tap_get_end_state());
458 for (i
= 0; i
< tms_count
; i
++) {
459 tms
= (tms_scan
>> i
) & 1;
460 buspirate_tap_append(tms
, 0);
463 tap_set_state(tap_get_end_state());
466 static void buspirate_path_move(int num_states
, tap_state_t
*path
)
470 for (i
= 0; i
< num_states
; i
++) {
471 if (tap_state_transition(tap_get_state(), false) == path
[i
]) {
472 buspirate_tap_append(0, 0);
473 } else if (tap_state_transition(tap_get_state(), true)
475 buspirate_tap_append(1, 0);
477 LOG_ERROR("BUG: %s -> %s isn't a valid "
479 tap_state_name(tap_get_state()),
480 tap_state_name(path
[i
]));
484 tap_set_state(path
[i
]);
487 tap_set_end_state(tap_get_state());
490 static void buspirate_runtest(int num_cycles
)
494 tap_state_t saved_end_state
= tap_get_end_state();
496 /* only do a state_move when we're not already in IDLE */
497 if (tap_get_state() != TAP_IDLE
) {
498 buspirate_end_state(TAP_IDLE
);
499 buspirate_state_move();
502 for (i
= 0; i
< num_cycles
; i
++)
503 buspirate_tap_append(0, 0);
505 DEBUG_JTAG_IO("runtest: cur_state %s end_state %s",
506 tap_state_name(tap_get_state()),
507 tap_state_name(tap_get_end_state()));
509 /* finish in end_state */
510 buspirate_end_state(saved_end_state
);
511 if (tap_get_state() != tap_get_end_state())
512 buspirate_state_move();
515 static void buspirate_scan(bool ir_scan
, enum scan_type type
,
516 uint8_t *buffer
, int scan_size
, struct scan_command
*command
)
518 tap_state_t saved_end_state
;
520 buspirate_tap_make_space(1, scan_size
+8);
521 /* is 8 correct ? (2 moves = 16) */
523 saved_end_state
= tap_get_end_state();
525 buspirate_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
526 buspirate_state_move();
528 buspirate_tap_append_scan(scan_size
, buffer
, command
);
531 buspirate_tap_append(0, 0);
533 /* restore the saved state */
534 buspirate_end_state(saved_end_state
);
535 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
537 if (tap_get_state() != tap_get_end_state())
538 buspirate_state_move();
542 /************************* TAP related stuff **********/
544 #define BUSPIRATE_BUFFER_SIZE 1024
545 #define BUSPIRATE_MAX_PENDING_SCANS 32
547 static char tms_chain
[BUSPIRATE_BUFFER_SIZE
]; /* send */
548 static char tdi_chain
[BUSPIRATE_BUFFER_SIZE
]; /* send */
549 static int tap_chain_index
;
551 struct pending_scan_result
/* this was stolen from arm-jtag-ew */
553 int first
; /* First bit position in tdo_buffer to read */
554 int length
; /* Number of bits to read */
555 struct scan_command
*command
; /* Corresponding scan command */
559 static struct pending_scan_result
560 tap_pending_scans
[BUSPIRATE_MAX_PENDING_SCANS
];
561 static int tap_pending_scans_num
;
563 static void buspirate_tap_init(void)
566 tap_pending_scans_num
= 0;
569 static int buspirate_tap_execute(void)
578 if (tap_chain_index
<= 0)
581 LOG_DEBUG("executing tap num bits = %i scans = %i",
582 tap_chain_index
, tap_pending_scans_num
);
584 bytes_to_send
= (tap_chain_index
+7) / 8;
586 tmp
[0] = CMD_TAP_SHIFT
; /* this command expects number of bits */
587 tmp
[1] = (char)(tap_chain_index
>> 8); /* high */
588 tmp
[2] = (char)(tap_chain_index
); /* low */
591 for (i
= 0; i
< bytes_to_send
; i
++) {
592 tmp
[fill_index
] = tdi_chain
[i
];
594 tmp
[fill_index
] = tms_chain
[i
];
598 ret
= buspirate_serial_write(buspirate_fd
, tmp
, 3 + bytes_to_send
*2);
599 if (ret
!= bytes_to_send
*2+3) {
600 LOG_ERROR("error writing :(");
601 return ERROR_JTAG_DEVICE_ERROR
;
604 ret
= buspirate_serial_read(buspirate_fd
, tmp
, bytes_to_send
+ 3);
605 in_buf
= (uint8_t *)(&tmp
[3]);
607 /* parse the scans */
608 for (i
= 0; i
< tap_pending_scans_num
; i
++) {
609 uint8_t *buffer
= tap_pending_scans
[i
].buffer
;
610 int length
= tap_pending_scans
[i
].length
;
611 int first
= tap_pending_scans
[i
].first
;
612 struct scan_command
*command
= tap_pending_scans
[i
].command
;
614 /* copy bits from buffer */
615 buf_set_buf(in_buf
, first
, buffer
, 0, length
);
617 /* return buffer to higher level */
618 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
) {
619 buspirate_tap_init();
620 return ERROR_JTAG_QUEUE_FAILED
;
625 tap_pending_scans_num
= 0;
630 static void buspirate_tap_make_space(int scans
, int bits
)
632 int have_scans
= BUSPIRATE_MAX_PENDING_SCANS
- tap_pending_scans_num
;
633 int have_bits
= BUSPIRATE_BUFFER_SIZE
* 8 - tap_chain_index
;
635 if ((have_scans
< scans
) || (have_bits
< bits
))
636 buspirate_tap_execute();
639 static void buspirate_tap_append(int tms
, int tdi
)
643 buspirate_tap_make_space(0, 1);
644 chain_index
= tap_chain_index
/ 8;
646 if (chain_index
< BUSPIRATE_BUFFER_SIZE
) {
647 int bit_index
= tap_chain_index
% 8;
648 uint8_t bit
= 1 << bit_index
;
651 tms_chain
[chain_index
] |= bit
;
653 tms_chain
[chain_index
] &= ~bit
;
656 tdi_chain
[chain_index
] |= bit
;
658 tdi_chain
[chain_index
] &= ~bit
;
662 LOG_ERROR("tap_chain overflow, Bad things will happen");
666 static void buspirate_tap_append_scan(int length
, uint8_t *buffer
,
667 struct scan_command
*command
)
670 tap_pending_scans
[tap_pending_scans_num
].length
= length
;
671 tap_pending_scans
[tap_pending_scans_num
].buffer
= buffer
;
672 tap_pending_scans
[tap_pending_scans_num
].command
= command
;
673 tap_pending_scans
[tap_pending_scans_num
].first
= tap_chain_index
;
675 for (i
= 0; i
< length
; i
++) {
676 int tms
= (i
< length
-1 ? 0 : 1);
677 int tdi
= (buffer
[i
/8] >> (i
%8)) & 1;
678 buspirate_tap_append(tms
, tdi
);
680 tap_pending_scans_num
++;
683 /*************** jtag wrapper functions *********************/
685 /* (1) assert or (0) deassert reset lines */
686 static void buspirate_reset(int trst
, int srst
)
688 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
691 buspirate_jtag_set_feature(buspirate_fd
,
692 FEATURE_TRST
, ACTION_DISABLE
);
694 buspirate_jtag_set_feature(buspirate_fd
,
695 FEATURE_TRST
, ACTION_ENABLE
);
698 buspirate_jtag_set_feature(buspirate_fd
,
699 FEATURE_SRST
, ACTION_DISABLE
);
701 buspirate_jtag_set_feature(buspirate_fd
,
702 FEATURE_SRST
, ACTION_ENABLE
);
705 /*************** jtag lowlevel functions ********************/
706 static void buspirate_jtag_enable(int fd
)
709 char tmp
[21] = { [0 ... 20] = 0x00 };
713 LOG_DEBUG("Entering binary mode");
714 buspirate_serial_write(fd
, tmp
, 20);
717 /* reads 1 to n "BBIO1"s and one "OCD1" */
719 ret
= buspirate_serial_read(fd
, tmp
, 4);
721 LOG_ERROR("Buspirate did not respond :"
722 "( restart everything");
726 if (strncmp(tmp
, "BBIO", 4) == 0) {
727 ret
= buspirate_serial_read(fd
, tmp
, 1);
729 LOG_ERROR("Buspirate did not respond well :"
730 "( restart everything");
734 LOG_ERROR("Unsupported binary protocol ");
739 tmp
[0] = CMD_ENTER_OOCD
;
740 ret
= buspirate_serial_write(fd
, tmp
, 1);
742 } else if (strncmp(tmp
, "OCD1", 4) == 0)
745 LOG_ERROR("Buspirate did not respond :"
746 "( restart everything");
753 static void buspirate_jtag_reset(int fd
)
758 tmp
[0] = 0x00; /* exit OCD1 mode */
759 buspirate_serial_write(fd
, tmp
, 1);
761 ret
= buspirate_serial_read(fd
, tmp
, 5);
762 if (strncmp(tmp
, "BBIO1", 5) == 0) {
763 tmp
[0] = 0x0F; /* reset BP */
764 buspirate_serial_write(fd
, tmp
, 1);
766 LOG_ERROR("Bad reply :( Please restart manually");
769 static void buspirate_jtag_set_speed(int fd
, char speed
)
778 tmp
[0] = CMD_UART_SPEED
;
780 buspirate_jtag_command(fd
, tmp
, 2);
782 /* here the adapter changes speed, we need follow */
783 buspirate_serial_setspeed(fd
, speed
);
785 buspirate_serial_write(fd
, ack
, 2);
786 ret
= buspirate_serial_read(fd
, tmp
, 2);
788 LOG_ERROR("Buspirate did not respond :"
789 "( restart everything");
792 if ((tmp
[0] != CMD_UART_SPEED
) || (tmp
[1] != speed
)) {
793 LOG_ERROR("Buspirate didn't reply as expected :"
794 "( restart everything");
797 LOG_INFO("Buspirate switched to %s mode",
798 (speed
== SERIAL_NORMAL
) ? "normal" : "FAST");
802 static void buspirate_jtag_set_mode(int fd
, char mode
)
805 tmp
[0] = CMD_PORT_MODE
;
807 buspirate_jtag_command(fd
, tmp
, 2);
810 static void buspirate_jtag_set_feature(int fd
, char feat
, char action
)
813 tmp
[0] = CMD_FEATURE
;
814 tmp
[1] = feat
; /* what */
815 tmp
[2] = action
; /* action */
816 buspirate_jtag_command(fd
, tmp
, 3);
819 static void buspirate_jtag_get_adcs(int fd
)
823 tmp
[0] = CMD_READ_ADCS
;
824 buspirate_jtag_command(fd
, (char *)tmp
, 1);
825 a
= tmp
[2] << 8 | tmp
[3];
826 b
= tmp
[4] << 8 | tmp
[5];
827 c
= tmp
[6] << 8 | tmp
[7];
828 d
= tmp
[8] << 8 | tmp
[9];
830 LOG_INFO("ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
832 ((float)a
)/155.1515, ((float)b
)/155.1515,
833 ((float)c
)/155.1515, ((float)d
)/155.1515);
836 static unsigned char buspirate_jtag_command(int fd
,
837 char *cmd
, int cmdlen
)
842 res
= buspirate_serial_write(fd
, cmd
, cmdlen
);
844 if ((cmd
[0] == CMD_UART_SPEED
)
845 || (cmd
[0] == CMD_PORT_MODE
)
846 || (cmd
[0] == CMD_FEATURE
)
847 || (cmd
[0] == CMD_JTAG_SPEED
))
853 len
= 10; /* 2*sizeof(char)+4*sizeof(uint16_t) */
861 res
= buspirate_serial_read(fd
, cmd
, len
);
863 return (unsigned char)cmd
[1];
871 /* low level serial port */
872 /* TODO add support for WIN32 and others ! */
873 static int buspirate_serial_open(char *port
)
876 fd
= open(buspirate_port
, O_RDWR
| O_NOCTTY
| O_NDELAY
);
880 static int buspirate_serial_setspeed(int fd
, char speed
)
882 struct termios t_opt
;
883 speed_t baud
= (speed
== SERIAL_FAST
) ? B1000000
: B115200
;
885 /* set the serial port parameters */
886 fcntl(fd
, F_SETFL
, 0);
887 tcgetattr(fd
, &t_opt
);
888 cfsetispeed(&t_opt
, baud
);
889 cfsetospeed(&t_opt
, baud
);
890 t_opt
.c_cflag
|= (CLOCAL
| CREAD
);
891 t_opt
.c_cflag
&= ~PARENB
;
892 t_opt
.c_cflag
&= ~CSTOPB
;
893 t_opt
.c_cflag
&= ~CSIZE
;
894 t_opt
.c_cflag
|= CS8
;
895 t_opt
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ISIG
);
896 t_opt
.c_iflag
&= ~(IXON
| IXOFF
| IXANY
);
897 t_opt
.c_oflag
&= ~OPOST
;
898 t_opt
.c_cc
[VMIN
] = 0;
899 t_opt
.c_cc
[VTIME
] = 10;
900 tcflush(fd
, TCIFLUSH
);
901 tcsetattr(fd
, TCSANOW
, &t_opt
);
906 static int buspirate_serial_write(int fd
, char *buf
, int size
)
910 ret
= write(fd
, buf
, size
);
912 LOG_DEBUG("size = %d ret = %d", size
, ret
);
913 buspirate_print_buffer(buf
, size
);
916 LOG_ERROR("Error sending data");
921 static int buspirate_serial_read(int fd
, char *buf
, int size
)
928 ret
= read(fd
, buf
+len
, size
-len
);
944 LOG_DEBUG("should have read = %d actual size = %d", size
, len
);
945 buspirate_print_buffer(buf
, len
);
948 LOG_ERROR("Error sending data");
953 static void buspirate_serial_close(int fd
)
959 #define BYTES_PER_LINE 16
960 static void buspirate_print_buffer(char *buf
, int size
)
962 char line
[LINE_SIZE
];
967 while (offset
< size
) {
968 snprintf(tmp
, 5, "%02x ", (uint8_t)buf
[offset
]);
973 if (offset
% BYTES_PER_LINE
== 0) {
974 LOG_DEBUG("%s", line
);
980 LOG_DEBUG("%s", line
);