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