buspirate: ignore return value and fix warning
[openocd.git] / src / jtag / drivers / buspirate.c
1 /***************************************************************************
2 * Copyright (C) 2010 by Michal Demin *
3 * based on usbprog.c and arm-jtag-ew.c *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <jtag/interface.h>
26 #include <jtag/commands.h>
27
28 #include <termios.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31
32 #undef DEBUG_SERIAL
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);
39
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);
46
47
48 #define CMD_UNKNOWN 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
57
58 /* Not all OSes have this speed defined */
59 #if !defined(B1000000)
60 #define B1000000 0010010
61 #endif
62
63 enum {
64 MODE_HIZ = 0,
65 MODE_JTAG = 1, /* push-pull outputs */
66 MODE_JTAG_OD = 2, /* open-drain outputs */
67 };
68
69 enum {
70 FEATURE_LED = 0x01,
71 FEATURE_VREG = 0x02,
72 FEATURE_TRST = 0x04,
73 FEATURE_SRST = 0x08,
74 FEATURE_PULLUP = 0x10
75 };
76
77 enum {
78 ACTION_DISABLE = 0,
79 ACTION_ENABLE = 1
80 };
81
82 enum {
83 SERIAL_NORMAL = 0,
84 SERIAL_FAST = 1
85 };
86
87
88 static int buspirate_fd = -1;
89 static int buspirate_pinmode = MODE_JTAG_OD;
90 static int buspirate_baudrate = SERIAL_NORMAL;
91 static int buspirate_vreg;
92 static int buspirate_pullup;
93 static char *buspirate_port;
94
95
96 /* TAP interface */
97 static void buspirate_tap_init(void);
98 static int buspirate_tap_execute(void);
99 static void buspirate_tap_append(int tms, int tdi);
100 static void buspirate_tap_append_scan(int length, uint8_t *buffer,
101 struct scan_command *command);
102 static void buspirate_tap_make_space(int scan, int bits);
103
104 static void buspirate_reset(int trst, int srst);
105
106 /* low level interface */
107 static void buspirate_jtag_reset(int);
108 static void buspirate_jtag_enable(int);
109 static unsigned char buspirate_jtag_command(int, char *, int);
110 static void buspirate_jtag_set_speed(int, char);
111 static void buspirate_jtag_set_mode(int, char);
112 static void buspirate_jtag_set_feature(int, char, char);
113 static void buspirate_jtag_get_adcs(int);
114
115 /* low level HW communication interface */
116 static int buspirate_serial_open(char *port);
117 static int buspirate_serial_setspeed(int fd, char speed);
118 static int buspirate_serial_write(int fd, char *buf, int size);
119 static int buspirate_serial_read(int fd, char *buf, int size);
120 static void buspirate_serial_close(int fd);
121 static void buspirate_print_buffer(char *buf, int size);
122
123 static int buspirate_speed(int speed)
124 {
125 /* TODO */
126 LOG_INFO("Want to set speed to %dkHz, but not implemented yet", speed);
127 return ERROR_OK;
128 }
129
130 static int buspirate_khz(int khz, int *jtag_speed)
131 {
132 *jtag_speed = khz;
133 return ERROR_OK;
134 }
135
136 static int buspirate_execute_queue(void)
137 {
138 /* currently processed command */
139 struct jtag_command *cmd = jtag_command_queue;
140 int scan_size;
141 enum scan_type type;
142 uint8_t *buffer;
143
144 while (cmd) {
145 switch (cmd->type) {
146 case JTAG_RUNTEST:
147 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
148 cmd->cmd.runtest->num_cycles,
149 tap_state_name(cmd->cmd.runtest
150 ->end_state));
151 buspirate_end_state(cmd->cmd.runtest
152 ->end_state);
153 buspirate_runtest(cmd->cmd.runtest
154 ->num_cycles);
155 break;
156 case JTAG_TLR_RESET:
157 DEBUG_JTAG_IO("statemove end in %s",
158 tap_state_name(cmd->cmd.statemove
159 ->end_state));
160 buspirate_end_state(cmd->cmd.statemove
161 ->end_state);
162 buspirate_state_move();
163 break;
164 case JTAG_PATHMOVE:
165 DEBUG_JTAG_IO("pathmove: %i states, end in %s",
166 cmd->cmd.pathmove->num_states,
167 tap_state_name(cmd->cmd.pathmove
168 ->path[cmd->cmd.pathmove
169 ->num_states - 1]));
170 buspirate_path_move(cmd->cmd.pathmove
171 ->num_states,
172 cmd->cmd.pathmove->path);
173 break;
174 case JTAG_SCAN:
175 DEBUG_JTAG_IO("scan end in %s",
176 tap_state_name(cmd->cmd.scan
177 ->end_state));
178
179 buspirate_end_state(cmd->cmd.scan
180 ->end_state);
181
182 scan_size = jtag_build_buffer(cmd->cmd.scan,
183 &buffer);
184 type = jtag_scan_type(cmd->cmd.scan);
185 buspirate_scan(cmd->cmd.scan->ir_scan, type,
186 buffer, scan_size, cmd->cmd.scan);
187
188 break;
189 case JTAG_RESET:
190 DEBUG_JTAG_IO("reset trst: %i srst %i",
191 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
192
193 /* flush buffers, so we can reset */
194 buspirate_tap_execute();
195
196 if (cmd->cmd.reset->trst == 1)
197 tap_set_state(TAP_RESET);
198 buspirate_reset(cmd->cmd.reset->trst,
199 cmd->cmd.reset->srst);
200 break;
201 case JTAG_SLEEP:
202 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
203 buspirate_tap_execute();
204 jtag_sleep(cmd->cmd.sleep->us);
205 break;
206 default:
207 LOG_ERROR("BUG: unknown JTAG command type encountered");
208 exit(-1);
209 }
210
211 cmd = cmd->next;
212 }
213
214 return buspirate_tap_execute();
215 }
216
217 static int buspirate_init(void)
218 {
219 if (buspirate_port == NULL) {
220 LOG_ERROR("You need to specify the serial port!");
221 return ERROR_JTAG_INIT_FAILED;
222 }
223
224 buspirate_fd = buspirate_serial_open(buspirate_port);
225 if (buspirate_fd == -1) {
226 LOG_ERROR("Could not open serial port");
227 return ERROR_JTAG_INIT_FAILED;
228 }
229
230 buspirate_serial_setspeed(buspirate_fd, SERIAL_NORMAL);
231
232 buspirate_jtag_enable(buspirate_fd);
233
234 if (buspirate_baudrate != SERIAL_NORMAL)
235 buspirate_jtag_set_speed(buspirate_fd, SERIAL_FAST);
236
237 LOG_INFO("Buspirate Interface ready!");
238
239 buspirate_tap_init();
240 buspirate_jtag_set_mode(buspirate_fd, buspirate_pinmode);
241 buspirate_jtag_set_feature(buspirate_fd, FEATURE_VREG,
242 (buspirate_vreg == 1) ? ACTION_ENABLE : ACTION_DISABLE);
243 buspirate_jtag_set_feature(buspirate_fd, FEATURE_PULLUP,
244 (buspirate_pullup == 1) ? ACTION_ENABLE : ACTION_DISABLE);
245 buspirate_reset(0, 0);
246
247 return ERROR_OK;
248 }
249
250 static int buspirate_quit(void)
251 {
252 LOG_INFO("Shutting down buspirate.");
253 buspirate_jtag_set_mode(buspirate_fd, MODE_HIZ);
254
255 buspirate_jtag_set_speed(buspirate_fd, SERIAL_NORMAL);
256 buspirate_jtag_reset(buspirate_fd);
257
258 buspirate_serial_close(buspirate_fd);
259
260 if (buspirate_port) {
261 free(buspirate_port);
262 buspirate_port = NULL;
263 }
264 return ERROR_OK;
265 }
266
267 /* openocd command interface */
268 COMMAND_HANDLER(buspirate_handle_adc_command)
269 {
270 if (buspirate_fd == -1)
271 return ERROR_OK;
272
273 /* send the command */
274 buspirate_jtag_get_adcs(buspirate_fd);
275
276 return ERROR_OK;
277
278 }
279
280 COMMAND_HANDLER(buspirate_handle_vreg_command)
281 {
282 if (CMD_ARGC < 1) {
283 LOG_ERROR("usage: buspirate_vreg <1|0>");
284 return ERROR_OK;
285 }
286
287 if (atoi(CMD_ARGV[0]) == 1)
288 buspirate_vreg = 1;
289 else if (atoi(CMD_ARGV[0]) == 0)
290 buspirate_vreg = 0;
291 else
292 LOG_ERROR("usage: buspirate_vreg <1|0>");
293
294 return ERROR_OK;
295
296 }
297
298 COMMAND_HANDLER(buspirate_handle_pullup_command)
299 {
300 if (CMD_ARGC < 1) {
301 LOG_ERROR("usage: buspirate_pullup <1|0>");
302 return ERROR_OK;
303 }
304
305 if (atoi(CMD_ARGV[0]) == 1)
306 buspirate_pullup = 1;
307 else if (atoi(CMD_ARGV[0]) == 0)
308 buspirate_pullup = 0;
309 else
310 LOG_ERROR("usage: buspirate_pullup <1|0>");
311
312 return ERROR_OK;
313
314 }
315
316 COMMAND_HANDLER(buspirate_handle_led_command)
317 {
318 if (CMD_ARGC < 1) {
319 LOG_ERROR("usage: buspirate_led <1|0>");
320 return ERROR_OK;
321 }
322
323 if (atoi(CMD_ARGV[0]) == 1) {
324 /* enable led */
325 buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
326 ACTION_ENABLE);
327 } else if (atoi(CMD_ARGV[0]) == 0) {
328 /* disable led */
329 buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
330 ACTION_DISABLE);
331 } else {
332 LOG_ERROR("usage: buspirate_led <1|0>");
333 }
334
335 return ERROR_OK;
336
337 }
338
339 COMMAND_HANDLER(buspirate_handle_mode_command)
340 {
341 if (CMD_ARGC < 1) {
342 LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
343 return ERROR_OK;
344 }
345
346 if (CMD_ARGV[0][0] == 'n')
347 buspirate_pinmode = MODE_JTAG;
348 else if (CMD_ARGV[0][0] == 'o')
349 buspirate_pinmode = MODE_JTAG_OD;
350 else
351 LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
352
353 return ERROR_OK;
354
355 }
356
357 COMMAND_HANDLER(buspirate_handle_speed_command)
358 {
359 if (CMD_ARGC < 1) {
360 LOG_ERROR("usage: buspirate_speed <normal|fast>");
361 return ERROR_OK;
362 }
363
364 if (CMD_ARGV[0][0] == 'n')
365 buspirate_baudrate = SERIAL_NORMAL;
366 else if (CMD_ARGV[0][0] == 'f')
367 buspirate_baudrate = SERIAL_FAST;
368 else
369 LOG_ERROR("usage: buspirate_speed <normal|fast>");
370
371 return ERROR_OK;
372
373 }
374
375 COMMAND_HANDLER(buspirate_handle_port_command)
376 {
377 if (CMD_ARGC < 1) {
378 LOG_ERROR("usage: buspirate_port /dev/ttyUSB0");
379 return ERROR_OK;
380 }
381
382 if (buspirate_port == NULL)
383 buspirate_port = strdup(CMD_ARGV[0]);
384
385 return ERROR_OK;
386
387 }
388
389 static const struct command_registration buspirate_command_handlers[] = {
390 {
391 .name = "buspirate_adc",
392 .handler = &buspirate_handle_adc_command,
393 .mode = COMMAND_EXEC,
394 .help = "reads voltages on adc pins",
395 },
396 {
397 .name = "buspirate_vreg",
398 .handler = &buspirate_handle_vreg_command,
399 .mode = COMMAND_CONFIG,
400 .help = "changes the state of voltage regulators",
401 },
402 {
403 .name = "buspirate_pullup",
404 .handler = &buspirate_handle_pullup_command,
405 .mode = COMMAND_CONFIG,
406 .help = "changes the state of pullup",
407 },
408 {
409 .name = "buspirate_led",
410 .handler = &buspirate_handle_led_command,
411 .mode = COMMAND_EXEC,
412 .help = "changes the state of led",
413 },
414 {
415 .name = "buspirate_speed",
416 .handler = &buspirate_handle_speed_command,
417 .mode = COMMAND_CONFIG,
418 .help = "speed of the interface",
419 },
420 {
421 .name = "buspirate_mode",
422 .handler = &buspirate_handle_mode_command,
423 .mode = COMMAND_CONFIG,
424 .help = "pin mode of the interface",
425 },
426 {
427 .name = "buspirate_port",
428 .handler = &buspirate_handle_port_command,
429 .mode = COMMAND_CONFIG,
430 .help = "name of the serial port to open",
431 },
432 COMMAND_REGISTRATION_DONE
433 };
434
435 struct jtag_interface buspirate_interface = {
436 .name = "buspirate",
437 .execute_queue = buspirate_execute_queue,
438 .speed = buspirate_speed,
439 .khz = buspirate_khz,
440 .commands = buspirate_command_handlers,
441 .init = buspirate_init,
442 .quit = buspirate_quit
443 };
444
445 /*************** jtag execute commands **********************/
446 static void buspirate_end_state(tap_state_t state)
447 {
448 if (tap_is_state_stable(state))
449 tap_set_end_state(state);
450 else {
451 LOG_ERROR("BUG: %i is not a valid end state", state);
452 exit(-1);
453 }
454 }
455
456 static void buspirate_state_move(void)
457 {
458 int i = 0, tms = 0;
459 uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
460 tap_get_end_state());
461 int tms_count = tap_get_tms_path_len(tap_get_state(),
462 tap_get_end_state());
463
464 for (i = 0; i < tms_count; i++) {
465 tms = (tms_scan >> i) & 1;
466 buspirate_tap_append(tms, 0);
467 }
468
469 tap_set_state(tap_get_end_state());
470 }
471
472 static void buspirate_path_move(int num_states, tap_state_t *path)
473 {
474 int i;
475
476 for (i = 0; i < num_states; i++) {
477 if (tap_state_transition(tap_get_state(), false) == path[i]) {
478 buspirate_tap_append(0, 0);
479 } else if (tap_state_transition(tap_get_state(), true)
480 == path[i]) {
481 buspirate_tap_append(1, 0);
482 } else {
483 LOG_ERROR("BUG: %s -> %s isn't a valid "
484 "TAP transition",
485 tap_state_name(tap_get_state()),
486 tap_state_name(path[i]));
487 exit(-1);
488 }
489
490 tap_set_state(path[i]);
491 }
492
493 tap_set_end_state(tap_get_state());
494 }
495
496 static void buspirate_runtest(int num_cycles)
497 {
498 int i;
499
500 tap_state_t saved_end_state = tap_get_end_state();
501
502 /* only do a state_move when we're not already in IDLE */
503 if (tap_get_state() != TAP_IDLE) {
504 buspirate_end_state(TAP_IDLE);
505 buspirate_state_move();
506 }
507
508 for (i = 0; i < num_cycles; i++)
509 buspirate_tap_append(0, 0);
510
511 DEBUG_JTAG_IO("runtest: cur_state %s end_state %s",
512 tap_state_name(tap_get_state()),
513 tap_state_name(tap_get_end_state()));
514
515 /* finish in end_state */
516 buspirate_end_state(saved_end_state);
517 if (tap_get_state() != tap_get_end_state())
518 buspirate_state_move();
519 }
520
521 static void buspirate_scan(bool ir_scan, enum scan_type type,
522 uint8_t *buffer, int scan_size, struct scan_command *command)
523 {
524 tap_state_t saved_end_state;
525
526 buspirate_tap_make_space(1, scan_size+8);
527 /* is 8 correct ? (2 moves = 16) */
528
529 saved_end_state = tap_get_end_state();
530
531 buspirate_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
532 buspirate_state_move();
533
534 buspirate_tap_append_scan(scan_size, buffer, command);
535
536 /* move to PAUSE */
537 buspirate_tap_append(0, 0);
538
539 /* restore the saved state */
540 buspirate_end_state(saved_end_state);
541 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
542
543 if (tap_get_state() != tap_get_end_state())
544 buspirate_state_move();
545 }
546
547
548 /************************* TAP related stuff **********/
549
550 #define BUSPIRATE_BUFFER_SIZE 1024
551 #define BUSPIRATE_MAX_PENDING_SCANS 32
552
553 static char tms_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
554 static char tdi_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
555 static int tap_chain_index;
556
557 struct pending_scan_result /* this was stolen from arm-jtag-ew */
558 {
559 int first; /* First bit position in tdo_buffer to read */
560 int length; /* Number of bits to read */
561 struct scan_command *command; /* Corresponding scan command */
562 uint8_t *buffer;
563 };
564
565 static struct pending_scan_result
566 tap_pending_scans[BUSPIRATE_MAX_PENDING_SCANS];
567 static int tap_pending_scans_num;
568
569 static void buspirate_tap_init(void)
570 {
571 tap_chain_index = 0;
572 tap_pending_scans_num = 0;
573 }
574
575 static int buspirate_tap_execute(void)
576 {
577 char tmp[4096];
578 uint8_t *in_buf;
579 int i;
580 int fill_index = 0;
581 int ret;
582 int bytes_to_send;
583
584 if (tap_chain_index <= 0)
585 return ERROR_OK;
586
587 LOG_DEBUG("executing tap num bits = %i scans = %i",
588 tap_chain_index, tap_pending_scans_num);
589
590 bytes_to_send = (tap_chain_index+7) / 8;
591
592 tmp[0] = CMD_TAP_SHIFT; /* this command expects number of bits */
593 tmp[1] = (char)(tap_chain_index >> 8); /* high */
594 tmp[2] = (char)(tap_chain_index); /* low */
595
596 fill_index = 3;
597 for (i = 0; i < bytes_to_send; i++) {
598 tmp[fill_index] = tdi_chain[i];
599 fill_index++;
600 tmp[fill_index] = tms_chain[i];
601 fill_index++;
602 }
603
604 ret = buspirate_serial_write(buspirate_fd, tmp, 3 + bytes_to_send*2);
605 if (ret != bytes_to_send*2+3) {
606 LOG_ERROR("error writing :(");
607 return ERROR_JTAG_DEVICE_ERROR;
608 }
609
610 ret = buspirate_serial_read(buspirate_fd, tmp, bytes_to_send + 3);
611 in_buf = (uint8_t *)(&tmp[3]);
612
613 /* parse the scans */
614 for (i = 0; i < tap_pending_scans_num; i++) {
615 uint8_t *buffer = tap_pending_scans[i].buffer;
616 int length = tap_pending_scans[i].length;
617 int first = tap_pending_scans[i].first;
618 struct scan_command *command = tap_pending_scans[i].command;
619
620 /* copy bits from buffer */
621 buf_set_buf(in_buf, first, buffer, 0, length);
622
623 /* return buffer to higher level */
624 if (jtag_read_buffer(buffer, command) != ERROR_OK) {
625 buspirate_tap_init();
626 return ERROR_JTAG_QUEUE_FAILED;
627 }
628
629 free(buffer);
630 }
631 tap_pending_scans_num = 0;
632 tap_chain_index = 0;
633 return ERROR_OK;
634 }
635
636 static void buspirate_tap_make_space(int scans, int bits)
637 {
638 int have_scans = BUSPIRATE_MAX_PENDING_SCANS - tap_pending_scans_num;
639 int have_bits = BUSPIRATE_BUFFER_SIZE * 8 - tap_chain_index;
640
641 if ((have_scans < scans) || (have_bits < bits))
642 buspirate_tap_execute();
643 }
644
645 static void buspirate_tap_append(int tms, int tdi)
646 {
647 int chain_index;
648
649 buspirate_tap_make_space(0, 1);
650 chain_index = tap_chain_index / 8;
651
652 if (chain_index < BUSPIRATE_BUFFER_SIZE) {
653 int bit_index = tap_chain_index % 8;
654 uint8_t bit = 1 << bit_index;
655
656 if (tms)
657 tms_chain[chain_index] |= bit;
658 else
659 tms_chain[chain_index] &= ~bit;
660
661 if (tdi)
662 tdi_chain[chain_index] |= bit;
663 else
664 tdi_chain[chain_index] &= ~bit;
665
666 tap_chain_index++;
667 } else
668 LOG_ERROR("tap_chain overflow, bad things will happen");
669
670 }
671
672 static void buspirate_tap_append_scan(int length, uint8_t *buffer,
673 struct scan_command *command)
674 {
675 int i;
676 tap_pending_scans[tap_pending_scans_num].length = length;
677 tap_pending_scans[tap_pending_scans_num].buffer = buffer;
678 tap_pending_scans[tap_pending_scans_num].command = command;
679 tap_pending_scans[tap_pending_scans_num].first = tap_chain_index;
680
681 for (i = 0; i < length; i++) {
682 int tms = (i < length-1 ? 0 : 1);
683 int tdi = (buffer[i/8] >> (i%8)) & 1;
684 buspirate_tap_append(tms, tdi);
685 }
686 tap_pending_scans_num++;
687 }
688
689 /*************** jtag wrapper functions *********************/
690
691 /* (1) assert or (0) deassert reset lines */
692 static void buspirate_reset(int trst, int srst)
693 {
694 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
695
696 if (trst)
697 buspirate_jtag_set_feature(buspirate_fd,
698 FEATURE_TRST, ACTION_DISABLE);
699 else
700 buspirate_jtag_set_feature(buspirate_fd,
701 FEATURE_TRST, ACTION_ENABLE);
702
703 if (srst)
704 buspirate_jtag_set_feature(buspirate_fd,
705 FEATURE_SRST, ACTION_DISABLE);
706 else
707 buspirate_jtag_set_feature(buspirate_fd,
708 FEATURE_SRST, ACTION_ENABLE);
709 }
710
711 /*************** jtag lowlevel functions ********************/
712 static void buspirate_jtag_enable(int fd)
713 {
714 int ret;
715 char tmp[21] = { [0 ... 20] = 0x00 };
716 int done = 0;
717 int cmd_sent = 0;
718
719 LOG_DEBUG("Entering binary mode");
720 buspirate_serial_write(fd, tmp, 20);
721 usleep(10000);
722
723 /* reads 1 to n "BBIO1"s and one "OCD1" */
724 while (!done) {
725 ret = buspirate_serial_read(fd, tmp, 4);
726 if (ret != 4) {
727 LOG_ERROR("Buspirate error. Is binary"
728 "/OpenOCD support enabled?");
729 exit(-1);
730 }
731 if (strncmp(tmp, "BBIO", 4) == 0) {
732 ret = buspirate_serial_read(fd, tmp, 1);
733 if (ret != 1) {
734 LOG_ERROR("Buspirate did not answer correctly! "
735 "Do you have correct firmware?");
736 exit(-1);
737 }
738 if (tmp[0] != '1') {
739 LOG_ERROR("Unsupported binary protocol");
740 exit(-1);
741 }
742 if (cmd_sent == 0) {
743 cmd_sent = 1;
744 tmp[0] = CMD_ENTER_OOCD;
745 ret = buspirate_serial_write(fd, tmp, 1);
746 }
747 } else if (strncmp(tmp, "OCD1", 4) == 0)
748 done = 1;
749 else {
750 LOG_ERROR("Buspirate did not answer correctly! "
751 "Do you have correct firmware?");
752 exit(-1);
753 }
754 }
755
756 }
757
758 static void buspirate_jtag_reset(int fd)
759 {
760 char tmp[5];
761
762 tmp[0] = 0x00; /* exit OCD1 mode */
763 buspirate_serial_write(fd, tmp, 1);
764 usleep(10000);
765 /* We ignore the return value here purposly, nothing we can do */
766 buspirate_serial_read(fd, tmp, 5);
767 if (strncmp(tmp, "BBIO1", 5) == 0) {
768 tmp[0] = 0x0F; /* reset BP */
769 buspirate_serial_write(fd, tmp, 1);
770 } else
771 LOG_ERROR("Unable to restart buspirate!");
772 }
773
774 static void buspirate_jtag_set_speed(int fd, char speed)
775 {
776 int ret;
777 char tmp[2];
778 char ack[2];
779
780 ack[0] = 0xAA;
781 ack[1] = 0x55;
782
783 tmp[0] = CMD_UART_SPEED;
784 tmp[1] = speed;
785 buspirate_jtag_command(fd, tmp, 2);
786
787 /* here the adapter changes speed, we need follow */
788 buspirate_serial_setspeed(fd, speed);
789
790 buspirate_serial_write(fd, ack, 2);
791 ret = buspirate_serial_read(fd, tmp, 2);
792 if (ret != 2) {
793 LOG_ERROR("Buspirate did not ack speed change");
794 exit(-1);
795 }
796 if ((tmp[0] != CMD_UART_SPEED) || (tmp[1] != speed)) {
797 LOG_ERROR("Buspirate did not reply as expected");
798 exit(-1);
799 }
800 LOG_INFO("Buspirate switched to %s mode",
801 (speed == SERIAL_NORMAL) ? "normal" : "FAST");
802 }
803
804
805 static void buspirate_jtag_set_mode(int fd, char mode)
806 {
807 char tmp[2];
808 tmp[0] = CMD_PORT_MODE;
809 tmp[1] = mode;
810 buspirate_jtag_command(fd, tmp, 2);
811 }
812
813 static void buspirate_jtag_set_feature(int fd, char feat, char action)
814 {
815 char tmp[3];
816 tmp[0] = CMD_FEATURE;
817 tmp[1] = feat; /* what */
818 tmp[2] = action; /* action */
819 buspirate_jtag_command(fd, tmp, 3);
820 }
821
822 static void buspirate_jtag_get_adcs(int fd)
823 {
824 uint8_t tmp[10];
825 uint16_t a, b, c, d;
826 tmp[0] = CMD_READ_ADCS;
827 buspirate_jtag_command(fd, (char *)tmp, 1);
828 a = tmp[2] << 8 | tmp[3];
829 b = tmp[4] << 8 | tmp[5];
830 c = tmp[6] << 8 | tmp[7];
831 d = tmp[8] << 8 | tmp[9];
832
833 LOG_INFO("ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
834 "V50 = %.02f",
835 ((float)a)/155.1515, ((float)b)/155.1515,
836 ((float)c)/155.1515, ((float)d)/155.1515);
837 }
838
839 static unsigned char buspirate_jtag_command(int fd,
840 char *cmd, int cmdlen)
841 {
842 int res;
843 int len = 0;
844
845 res = buspirate_serial_write(fd, cmd, cmdlen);
846
847 if ((cmd[0] == CMD_UART_SPEED)
848 || (cmd[0] == CMD_PORT_MODE)
849 || (cmd[0] == CMD_FEATURE)
850 || (cmd[0] == CMD_JTAG_SPEED))
851 return 1;
852
853 if (res == cmdlen) {
854 switch (cmd[0]) {
855 case CMD_READ_ADCS:
856 len = 10; /* 2*sizeof(char)+4*sizeof(uint16_t) */
857 break;
858 case CMD_TAP_SHIFT:
859 len = cmdlen;
860 break;
861 default:
862 LOG_INFO("Wrong !");
863 }
864 res = buspirate_serial_read(fd, cmd, len);
865 if (res > 0)
866 return (unsigned char)cmd[1];
867 else
868 return -1;
869 } else
870 return -1;
871 return 0;
872 }
873
874 /* low level serial port */
875 /* TODO add support for WIN32 and others ! */
876 static int buspirate_serial_open(char *port)
877 {
878 int fd;
879 fd = open(buspirate_port, O_RDWR | O_NOCTTY | O_NDELAY);
880 return fd;
881 }
882
883 static int buspirate_serial_setspeed(int fd, char speed)
884 {
885 struct termios t_opt;
886 speed_t baud = (speed == SERIAL_FAST) ? B1000000 : B115200;
887
888 /* set the serial port parameters */
889 fcntl(fd, F_SETFL, 0);
890 tcgetattr(fd, &t_opt);
891 cfsetispeed(&t_opt, baud);
892 cfsetospeed(&t_opt, baud);
893 t_opt.c_cflag |= (CLOCAL | CREAD);
894 t_opt.c_cflag &= ~PARENB;
895 t_opt.c_cflag &= ~CSTOPB;
896 t_opt.c_cflag &= ~CSIZE;
897 t_opt.c_cflag |= CS8;
898 t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
899 t_opt.c_iflag &= ~(IXON | IXOFF | IXANY);
900 t_opt.c_oflag &= ~OPOST;
901 t_opt.c_cc[VMIN] = 0;
902 t_opt.c_cc[VTIME] = 10;
903 tcflush(fd, TCIFLUSH);
904 tcsetattr(fd, TCSANOW, &t_opt);
905
906 return 0;
907 }
908
909 static int buspirate_serial_write(int fd, char *buf, int size)
910 {
911 int ret = 0;
912
913 ret = write(fd, buf, size);
914
915 LOG_DEBUG("size = %d ret = %d", size, ret);
916 buspirate_print_buffer(buf, size);
917
918 if (ret != size)
919 LOG_ERROR("Error sending data");
920
921 return ret;
922 }
923
924 static int buspirate_serial_read(int fd, char *buf, int size)
925 {
926 int len = 0;
927 int ret = 0;
928 int timeout = 0;
929
930 while (len < size) {
931 ret = read(fd, buf+len, size-len);
932 if (ret == -1)
933 return -1;
934
935 if (ret == 0) {
936 timeout++;
937
938 if (timeout >= 10)
939 break;
940
941 continue;
942 }
943
944 len += ret;
945 }
946
947 LOG_DEBUG("should have read = %d actual size = %d", size, len);
948 buspirate_print_buffer(buf, len);
949
950 if (len != size)
951 LOG_ERROR("Error reading data");
952
953 return len;
954 }
955
956 static void buspirate_serial_close(int fd)
957 {
958 close(fd);
959 }
960
961 #define LINE_SIZE 81
962 #define BYTES_PER_LINE 16
963 static void buspirate_print_buffer(char *buf, int size)
964 {
965 char line[LINE_SIZE];
966 char tmp[10];
967 int offset = 0;
968
969 line[0] = 0;
970 while (offset < size) {
971 snprintf(tmp, 5, "%02x ", (uint8_t)buf[offset]);
972 offset++;
973
974 strcat(line, tmp);
975
976 if (offset % BYTES_PER_LINE == 0) {
977 LOG_DEBUG("%s", line);
978 line[0] = 0;
979 }
980 }
981
982 if (line[0] != 0) {
983 LOG_DEBUG("%s", line);
984 }
985 }
986

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)