jtag/drivers/kitprog: Restructure commands
[openocd.git] / src / jtag / drivers / angie.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3 File : angie.c *
4 Contents : OpenOCD driver code for NanoXplore USB-JTAG ANGIE *
5 adapter hardware. *
6 Based on openULINK driver code by: Martin Schmoelzer. *
7 Copyright 2023, Ahmed Errached BOUDJELIDA, NanoXplore SAS. *
8 <aboudjelida@nanoxplore.com> *
9 <ahmederrachedbjld@gmail.com> *
10 ***************************************************************************/
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <math.h>
19 #include "helper/system.h"
20 #include <jtag/interface.h>
21 #include <jtag/commands.h>
22 #include <target/image.h>
23 #include <libusb.h>
24 #include "libusb_helper.h"
25 #include "angie/include/msgtypes.h"
26
27 /** USB Vendor ID of ANGIE device in unconfigured state (no firmware loaded
28 * yet) or with its firmware. */
29 #define ANGIE_VID 0x584e
30
31 /** USB Product ID of ANGIE device in unconfigured state (no firmware loaded
32 * yet) or with its firmware. */
33 #define ANGIE_PID 0x424e
34 #define ANGIE_PID_2 0x4255
35 #define ANGIE_PID_3 0x4355
36 #define ANGIE_PID_4 0x4a55
37
38 /** Address of EZ-USB ANGIE CPU Control & Status register. This register can be
39 * written by issuing a Control EP0 vendor request. */
40 #define CPUCS_REG 0xE600
41
42 /** USB Control EP0 bRequest: "Firmware Load". */
43 #define REQUEST_FIRMWARE_LOAD 0xA0
44
45 /** Value to write into CPUCS to put EZ-USB ANGIE into reset. */
46 #define CPU_RESET 0x01
47
48 /** Value to write into CPUCS to put EZ-USB ANGIE out of reset. */
49 #define CPU_START 0x00
50
51 /** Base address of firmware in EZ-USB ANGIE code space. */
52 #define FIRMWARE_ADDR 0x0000
53
54 /** USB interface number */
55 #define USB_INTERFACE 0
56
57 /** Delay (in microseconds) to wait while EZ-USB performs ReNumeration. */
58 #define ANGIE_RENUMERATION_DELAY_US 1500000
59
60 /** Default location of ANGIE firmware image. */
61 #define ANGIE_FIRMWARE_FILE PKGDATADIR "/angie/angie_firmware.bin"
62
63 /** Default location of ANGIE firmware image. */
64 #define ANGIE_BITSTREAM_FILE PKGDATADIR "/angie/angie_bitstream.bit"
65
66 /** Maximum size of a single firmware section. Entire EZ-USB ANGIE code space = 16kB */
67 #define SECTION_BUFFERSIZE 16384
68
69 /** Tuning of OpenOCD SCAN commands split into multiple ANGIE commands. */
70 #define SPLIT_SCAN_THRESHOLD 10
71
72 /** ANGIE hardware type */
73 enum angie_type {
74 ANGIE,
75 };
76
77 enum angie_payload_direction {
78 PAYLOAD_DIRECTION_OUT,
79 PAYLOAD_DIRECTION_IN
80 };
81
82 enum angie_delay_type {
83 DELAY_CLOCK_TCK,
84 DELAY_CLOCK_TMS,
85 DELAY_SCAN_IN,
86 DELAY_SCAN_OUT,
87 DELAY_SCAN_IO
88 };
89
90 /**
91 * ANGIE command (ANGIE command queue element).
92 *
93 * For the OUT direction payload, things are quite easy: Payload is stored
94 * in a rather small array (up to 63 bytes), the payload is always allocated
95 * by the function generating the command and freed by angie_clear_queue().
96 *
97 * For the IN direction payload, things get a little bit more complicated:
98 * The maximum IN payload size for a single command is 64 bytes. Assume that
99 * a single OpenOCD command needs to scan 256 bytes. This results in the
100 * generation of four ANGIE commands. The function generating these
101 * commands shall allocate an uint8_t[256] array. Each command's #payload_in
102 * pointer shall point to the corresponding offset where IN data shall be
103 * placed, while #payload_in_start shall point to the first element of the 256
104 * byte array.
105 * - first command: #payload_in_start + 0
106 * - second command: #payload_in_start + 64
107 * - third command: #payload_in_start + 128
108 * - fourth command: #payload_in_start + 192
109 *
110 * The last command sets #needs_postprocessing to true.
111 */
112 struct angie_cmd {
113 uint8_t id; /**< ANGIE command ID */
114
115 uint8_t *payload_out; /**< Pointer where OUT payload shall be stored */
116 uint8_t payload_out_size; /**< OUT direction payload size for this command */
117
118 uint8_t *payload_in_start; /**< Pointer to first element of IN payload array */
119 uint8_t *payload_in; /**< Pointer where IN payload shall be stored */
120 uint8_t payload_in_size; /**< IN direction payload size for this command */
121
122 /** Indicates if this command needs post-processing */
123 bool needs_postprocessing;
124
125 /** Indicates if angie_clear_queue() should free payload_in_start */
126 bool free_payload_in_start;
127
128 /** Pointer to corresponding OpenOCD command for post-processing */
129 struct jtag_command *cmd_origin;
130
131 struct angie_cmd *next; /**< Pointer to next command (linked list) */
132 };
133
134 /** Describes one driver instance */
135 struct angie {
136 struct libusb_context *libusb_ctx;
137 struct libusb_device_handle *usb_device_handle;
138 enum angie_type type;
139
140 unsigned int ep_in; /**< IN endpoint number */
141 unsigned int ep_out; /**< OUT endpoint number */
142
143 /* delay value for "SLOW_CLOCK commands" in [0:255] range in units of 4 us;
144 -1 means no need for delay */
145 int delay_scan_in; /**< Delay value for SCAN_IN commands */
146 int delay_scan_out; /**< Delay value for SCAN_OUT commands */
147 int delay_scan_io; /**< Delay value for SCAN_IO commands */
148 int delay_clock_tck; /**< Delay value for CLOCK_TMS commands */
149 int delay_clock_tms; /**< Delay value for CLOCK_TCK commands */
150
151 int commands_in_queue; /**< Number of commands in queue */
152 struct angie_cmd *queue_start; /**< Pointer to first command in queue */
153 struct angie_cmd *queue_end; /**< Pointer to last command in queue */
154 };
155
156 /**************************** Function Prototypes *****************************/
157
158 /* USB helper functions */
159 static int angie_usb_open(struct angie *device);
160 static int angie_usb_close(struct angie *device);
161
162 /* ANGIE MCU (Cypress EZ-USB) specific functions */
163 static int angie_cpu_reset(struct angie *device, char reset_bit);
164 static int angie_load_firmware_and_renumerate(struct angie *device, const char *filename,
165 uint32_t delay_us);
166 static int angie_load_firmware(struct angie *device, const char *filename);
167 static int angie_load_bitstream(struct angie *device, const char *filename);
168
169 static int angie_write_firmware_section(struct angie *device,
170 struct image *firmware_image, int section_index);
171
172 /* Generic helper functions */
173 static void angie_dump_signal_states(uint8_t input_signals, uint8_t output_signals);
174
175 /* ANGIE command generation helper functions */
176 static int angie_allocate_payload(struct angie_cmd *angie_cmd, int size,
177 enum angie_payload_direction direction);
178
179 /* ANGIE command queue helper functions */
180 static int angie_get_queue_size(struct angie *device,
181 enum angie_payload_direction direction);
182 static void angie_clear_queue(struct angie *device);
183 static int angie_append_queue(struct angie *device, struct angie_cmd *angie_cmd);
184 static int angie_execute_queued_commands(struct angie *device, int timeout_ms);
185
186 static void angie_dump_queue(struct angie *device);
187
188 static int angie_append_scan_cmd(struct angie *device,
189 enum scan_type scan_type,
190 int scan_size_bits,
191 uint8_t *tdi,
192 uint8_t *tdo_start,
193 uint8_t *tdo,
194 uint8_t tms_count_start,
195 uint8_t tms_sequence_start,
196 uint8_t tms_count_end,
197 uint8_t tms_sequence_end,
198 struct jtag_command *origin,
199 bool postprocess);
200 static int angie_append_clock_tms_cmd(struct angie *device, uint8_t count,
201 uint8_t sequence);
202 static int angie_append_clock_tck_cmd(struct angie *device, uint16_t count);
203 static int angie_append_get_signals_cmd(struct angie *device);
204 static int angie_append_set_signals_cmd(struct angie *device, uint8_t low,
205 uint8_t high);
206 static int angie_append_sleep_cmd(struct angie *device, uint32_t us);
207 static int angie_append_configure_tck_cmd(struct angie *device,
208 int delay_scan_in,
209 int delay_scan_out,
210 int delay_scan_io,
211 int delay_tck,
212 int delay_tms);
213 static int angie_append_test_cmd(struct angie *device);
214
215 /* ANGIE TCK frequency helper functions */
216 static int angie_calculate_delay(enum angie_delay_type type, long f, int *delay);
217
218 /* Interface between ANGIE and OpenOCD */
219 static void angie_set_end_state(tap_state_t endstate);
220 static int angie_queue_statemove(struct angie *device);
221
222 static int angie_queue_scan(struct angie *device, struct jtag_command *cmd);
223 static int angie_queue_tlr_reset(struct angie *device, struct jtag_command *cmd);
224 static int angie_queue_runtest(struct angie *device, struct jtag_command *cmd);
225 static int angie_queue_pathmove(struct angie *device, struct jtag_command *cmd);
226 static int angie_queue_sleep(struct angie *device, struct jtag_command *cmd);
227 static int angie_queue_stableclocks(struct angie *device, struct jtag_command *cmd);
228
229 static int angie_post_process_scan(struct angie_cmd *angie_cmd);
230 static int angie_post_process_queue(struct angie *device);
231
232 /* adapter driver functions */
233 static int angie_execute_queue(void);
234 static int angie_khz(int khz, int *jtag_speed);
235 static int angie_speed(int speed);
236 static int angie_speed_div(int speed, int *khz);
237 static int angie_init(void);
238 static int angie_quit(void);
239 static int angie_reset(int trst, int srst);
240
241 /****************************** Global Variables ******************************/
242
243 static struct angie *angie_handle;
244
245 /**************************** USB helper functions ****************************/
246
247 /**
248 * Opens the ANGIE device
249 *
250 * @param device pointer to struct angie identifying ANGIE driver instance.
251 * @return on success: ERROR_OK
252 * @return on failure: ERROR_FAIL
253 */
254 static int angie_usb_open(struct angie *device)
255 {
256 struct libusb_device_handle *usb_device_handle;
257 const uint16_t vids[] = {ANGIE_VID, ANGIE_VID, ANGIE_VID, ANGIE_VID, 0};
258 const uint16_t pids[] = {ANGIE_PID, ANGIE_PID_2, ANGIE_PID_3, ANGIE_PID_4, 0};
259
260 int ret = jtag_libusb_open(vids, pids, NULL, &usb_device_handle, NULL);
261
262 if (ret != ERROR_OK)
263 return ret;
264
265 device->usb_device_handle = usb_device_handle;
266 device->type = ANGIE;
267
268 return ERROR_OK;
269 }
270
271 /**
272 * Releases the ANGIE interface and closes the USB device handle.
273 *
274 * @param device pointer to struct angie identifying ANGIE driver instance.
275 * @return on success: ERROR_OK
276 * @return on failure: ERROR_FAIL
277 */
278 static int angie_usb_close(struct angie *device)
279 {
280 if (device->usb_device_handle) {
281 if (libusb_release_interface(device->usb_device_handle, 0) != 0)
282 return ERROR_FAIL;
283
284 jtag_libusb_close(device->usb_device_handle);
285 device->usb_device_handle = NULL;
286 }
287 return ERROR_OK;
288 }
289
290 /******************* ANGIE CPU (EZ-USB) specific functions ********************/
291
292 /**
293 * Writes '0' or '1' to the CPUCS register, putting the EZ-USB CPU into reset
294 * or out of reset.
295 *
296 * @param device pointer to struct angie identifying ANGIE driver instance.
297 * @param reset_bit 0 to put CPU into reset, 1 to put CPU out of reset.
298 * @return on success: ERROR_OK
299 * @return on failure: ERROR_FAIL
300 */
301 static int angie_cpu_reset(struct angie *device, char reset_bit)
302 {
303 return jtag_libusb_control_transfer(device->usb_device_handle,
304 (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
305 REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, LIBUSB_TIMEOUT_MS, NULL);
306 }
307
308 /**
309 * Puts the ANGIE's EZ-USB microcontroller into reset state, downloads
310 * the firmware image, resumes the microcontroller and re-enumerates
311 * USB devices.
312 *
313 * @param device pointer to struct angie identifying ANGIE driver instance.
314 * The usb_handle member will be modified during re-enumeration.
315 * @param filename path to the Intel HEX file containing the firmware image.
316 * @param delay_us the delay to wait for the device to re-enumerate.
317 * @return on success: ERROR_OK
318 * @return on failure: ERROR_FAIL
319 */
320 static int angie_load_firmware_and_renumerate(struct angie *device,
321 const char *filename, uint32_t delay_us)
322 {
323 int ret;
324
325 /* Basic process: After downloading the firmware, the ANGIE will disconnect
326 * itself and re-connect after a short amount of time so we have to close
327 * the handle and re-enumerate USB devices */
328
329 ret = angie_load_firmware(device, filename);
330 if (ret != ERROR_OK)
331 return ret;
332
333 ret = angie_usb_close(device);
334 if (ret != ERROR_OK)
335 return ret;
336
337 usleep(delay_us);
338
339 return angie_usb_open(device);
340 }
341
342 /**
343 * Downloads a firmware image to the ANGIE's EZ-USB microcontroller
344 * over the USB bus.
345 *
346 * @param device pointer to struct angie identifying ANGIE driver instance.
347 * @param filename an absolute or relative path to the Intel HEX file
348 * containing the firmware image.
349 * @return on success: ERROR_OK
350 * @return on failure: ERROR_FAIL
351 */
352 static int angie_load_firmware(struct angie *device, const char *filename)
353 {
354 struct image angie_firmware_image;
355 int ret;
356
357 ret = angie_cpu_reset(device, CPU_RESET);
358 if (ret != ERROR_OK) {
359 LOG_ERROR("Could not halt ANGIE CPU");
360 return ret;
361 }
362
363 angie_firmware_image.base_address = 0;
364 angie_firmware_image.base_address_set = false;
365
366 ret = image_open(&angie_firmware_image, filename, "bin");
367 if (ret != ERROR_OK) {
368 LOG_ERROR("Could not load firmware image");
369 return ret;
370 }
371
372 /* Download all sections in the image to ANGIE */
373 for (unsigned int i = 0; i < angie_firmware_image.num_sections; i++) {
374 ret = angie_write_firmware_section(device, &angie_firmware_image, i);
375 if (ret != ERROR_OK)
376 return ret;
377 }
378
379 image_close(&angie_firmware_image);
380
381 ret = angie_cpu_reset(device, CPU_START);
382 if (ret != ERROR_OK) {
383 LOG_ERROR("Could not restart ANGIE CPU");
384 return ret;
385 }
386
387 return ERROR_OK;
388 }
389
390 /**
391 * Downloads a bitstream file to the ANGIE's FPGA through the EZ-USB microcontroller
392 * over the USB bus.
393 *
394 * @param device pointer to struct angie identifying ANGIE driver instance.
395 * @param filename an absolute or relative path to the Xilinx .bit file
396 * containing the bitstream data.
397 * @return on success: ERROR_OK
398 * @return on failure: ERROR_FAIL
399 */
400 static int angie_load_bitstream(struct angie *device, const char *filename)
401 {
402 int ret, transferred;
403 const char *bitstream_file_path = filename;
404 FILE *bitstream_file = NULL;
405 char *bitstream_data = NULL;
406 size_t bitstream_size = 0;
407
408 /* CFGopen */
409 ret = jtag_libusb_control_transfer(device->usb_device_handle,
410 0x00, 0xB0, 0, 0, NULL, 0, LIBUSB_TIMEOUT_MS, &transferred);
411 if (ret != ERROR_OK) {
412 LOG_ERROR("Failed opencfg");
413 /* Abort if libusb sent less data than requested */
414 return ERROR_FAIL;
415 }
416
417 /* Open the bitstream file */
418 bitstream_file = fopen(bitstream_file_path, "rb");
419 if (!bitstream_file) {
420 LOG_ERROR("Failed to open bitstream file: %s\n", bitstream_file_path);
421 return ERROR_FAIL;
422 }
423
424 /* Get the size of the bitstream file */
425 fseek(bitstream_file, 0, SEEK_END);
426 bitstream_size = ftell(bitstream_file);
427 fseek(bitstream_file, 0, SEEK_SET);
428
429 /* Allocate memory for the bitstream data */
430 bitstream_data = malloc(bitstream_size);
431 if (!bitstream_data) {
432 LOG_ERROR("Failed to allocate memory for bitstream data.");
433 fclose(bitstream_file);
434 return ERROR_FAIL;
435 }
436
437 /* Read the bitstream data from the file */
438 if (fread(bitstream_data, 1, bitstream_size, bitstream_file) != bitstream_size) {
439 LOG_ERROR("Failed to read bitstream data.");
440 free(bitstream_data);
441 fclose(bitstream_file);
442 return ERROR_FAIL;
443 }
444
445 /* Send the bitstream data to the microcontroller */
446 int actual_length = 0;
447 ret = jtag_libusb_bulk_write(device->usb_device_handle, 0x02, bitstream_data, bitstream_size, 1000, &actual_length);
448 if (ret != ERROR_OK) {
449 LOG_ERROR("Failed to send bitstream data: %s", libusb_strerror(ret));
450 free(bitstream_data);
451 fclose(bitstream_file);
452 return ERROR_FAIL;
453 }
454
455 LOG_INFO("Bitstream sent successfully.");
456
457 /* Clean up */
458 free(bitstream_data);
459 fclose(bitstream_file);
460
461 /* CFGclose */
462 transferred = 0;
463 ret = jtag_libusb_control_transfer(device->usb_device_handle,
464 0x00, 0xB1, 0, 0, NULL, 0, LIBUSB_TIMEOUT_MS, &transferred);
465 if (ret != ERROR_OK) {
466 LOG_INFO("error cfgclose");
467 /* Abort if libusb sent less data than requested */
468 return ERROR_FAIL;
469 }
470 return ERROR_OK;
471 }
472
473 /**
474 * Send one contiguous firmware section to the ANGIE's EZ-USB microcontroller
475 * over the USB bus.
476 *
477 * @param device pointer to struct angie identifying ANGIE driver instance.
478 * @param firmware_image pointer to the firmware image that contains the section
479 * which should be sent to the ANGIE's EZ-USB microcontroller.
480 * @param section_index index of the section within the firmware image.
481 * @return on success: ERROR_OK
482 * @return on failure: ERROR_FAIL
483 */
484 static int angie_write_firmware_section(struct angie *device,
485 struct image *firmware_image, int section_index)
486 {
487 int addr, bytes_remaining, chunk_size;
488 uint8_t data[SECTION_BUFFERSIZE];
489 uint8_t *data_ptr = data;
490 uint16_t size;
491 size_t size_read;
492 int ret, transferred;
493
494 size = (uint16_t)firmware_image->sections[section_index].size;
495 addr = (uint16_t)firmware_image->sections[section_index].base_address;
496
497 LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04" PRIx16 ")", section_index, addr,
498 size);
499
500 /* Copy section contents to local buffer */
501 ret = image_read_section(firmware_image, section_index, 0, size, data,
502 &size_read);
503
504 if (ret != ERROR_OK)
505 return ret;
506 if (size_read != size)
507 return ERROR_FAIL;
508
509 bytes_remaining = size;
510
511 /* Send section data in chunks of up to 64 bytes to ANGIE */
512 while (bytes_remaining > 0) {
513 if (bytes_remaining > 64)
514 chunk_size = 64;
515 else
516 chunk_size = bytes_remaining;
517
518 ret = jtag_libusb_control_transfer(device->usb_device_handle,
519 (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
520 REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (char *)data_ptr,
521 chunk_size, LIBUSB_TIMEOUT_MS, &transferred);
522
523 if (ret != ERROR_OK)
524 return ret;
525
526 if (transferred != chunk_size) {
527 /* Abort if libusb sent less data than requested */
528 return ERROR_FAIL;
529 }
530
531 bytes_remaining -= chunk_size;
532 addr += chunk_size;
533 data_ptr += chunk_size;
534 }
535
536 return ERROR_OK;
537 }
538
539 /************************** Generic helper functions **************************/
540
541 /**
542 * Print state of interesting signals via LOG_INFO().
543 *
544 * @param input_signals input signal states as returned by CMD_GET_SIGNALS
545 * @param output_signals output signal states as returned by CMD_GET_SIGNALS
546 */
547 static void angie_dump_signal_states(uint8_t input_signals, uint8_t output_signals)
548 {
549 LOG_INFO("ANGIE signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i "
550 "SRST: %i",
551 (output_signals & SIGNAL_TDI ? 1 : 0),
552 (input_signals & SIGNAL_TDO ? 1 : 0),
553 (output_signals & SIGNAL_TMS ? 1 : 0),
554 (output_signals & SIGNAL_TCK ? 1 : 0),
555 (output_signals & SIGNAL_TRST ? 1 : 0),
556 (output_signals & SIGNAL_SRST ? 1 : 0));
557 }
558
559 /**************** ANGIE command generation helper functions ***************/
560
561 /**
562 * Allocate and initialize space in memory for ANGIE command payload.
563 *
564 * @param angie_cmd pointer to command whose payload should be allocated.
565 * @param size the amount of memory to allocate (bytes).
566 * @param direction which payload to allocate.
567 * @return on success: ERROR_OK
568 * @return on failure: ERROR_FAIL
569 */
570 static int angie_allocate_payload(struct angie_cmd *angie_cmd, int size,
571 enum angie_payload_direction direction)
572 {
573 uint8_t *payload;
574
575 payload = calloc(size, sizeof(uint8_t));
576
577 if (!payload) {
578 LOG_ERROR("Could not allocate ANGIE command payload: out of memory");
579 return ERROR_FAIL;
580 }
581
582 switch (direction) {
583 case PAYLOAD_DIRECTION_OUT:
584 if (angie_cmd->payload_out) {
585 LOG_ERROR("BUG: Duplicate payload allocation for ANGIE command");
586 free(payload);
587 return ERROR_FAIL;
588 }
589 angie_cmd->payload_out = payload;
590 angie_cmd->payload_out_size = size;
591 break;
592 case PAYLOAD_DIRECTION_IN:
593 if (angie_cmd->payload_in_start) {
594 LOG_ERROR("BUG: Duplicate payload allocation for ANGIE command");
595 free(payload);
596 return ERROR_FAIL;
597 }
598
599 angie_cmd->payload_in_start = payload;
600 angie_cmd->payload_in = payload;
601 angie_cmd->payload_in_size = size;
602
603 /* By default, free payload_in_start in angie_clear_queue(). Commands
604 * that do not want this behavior (e. g. split scans) must turn it off
605 * separately! */
606 angie_cmd->free_payload_in_start = true;
607
608 break;
609 }
610
611 return ERROR_OK;
612 }
613
614 /****************** ANGIE command queue helper functions ******************/
615
616 /**
617 * Get the current number of bytes in the queue, including command IDs.
618 *
619 * @param device pointer to struct angie identifying ANGIE driver instance.
620 * @param direction the transfer direction for which to get byte count.
621 * @return the number of bytes currently stored in the queue for the specified
622 * direction.
623 */
624 static int angie_get_queue_size(struct angie *device,
625 enum angie_payload_direction direction)
626 {
627 struct angie_cmd *current = device->queue_start;
628 int sum = 0;
629
630 while (current) {
631 switch (direction) {
632 case PAYLOAD_DIRECTION_OUT:
633 sum += current->payload_out_size + 1; /* + 1 byte for Command ID */
634 break;
635 case PAYLOAD_DIRECTION_IN:
636 sum += current->payload_in_size;
637 break;
638 }
639
640 current = current->next;
641 }
642
643 return sum;
644 }
645
646 /**
647 * Clear the ANGIE command queue.
648 *
649 * @param device pointer to struct angie identifying ANGIE driver instance.
650 */
651 static void angie_clear_queue(struct angie *device)
652 {
653 struct angie_cmd *current = device->queue_start;
654 struct angie_cmd *next = NULL;
655
656 while (current) {
657 /* Save pointer to next element */
658 next = current->next;
659
660 /* Free payloads: OUT payload can be freed immediately */
661 free(current->payload_out);
662 current->payload_out = NULL;
663
664 /* IN payload MUST be freed ONLY if no other commands use the
665 * payload_in_start buffer */
666 if (current->free_payload_in_start) {
667 free(current->payload_in_start);
668 current->payload_in_start = NULL;
669 current->payload_in = NULL;
670 }
671
672 /* Free queue element */
673 free(current);
674
675 /* Proceed with next element */
676 current = next;
677 }
678
679 device->commands_in_queue = 0;
680 device->queue_start = NULL;
681 device->queue_end = NULL;
682 }
683
684 /**
685 * Add a command to the ANGIE command queue.
686 *
687 * @param device pointer to struct angie identifying ANGIE driver instance.
688 * @param angie_cmd pointer to command that shall be appended to the ANGIE
689 * command queue.
690 * @return on success: ERROR_OK
691 * @return on failure: ERROR_FAIL
692 */
693 static int angie_append_queue(struct angie *device, struct angie_cmd *angie_cmd)
694 {
695 int newsize_out, newsize_in;
696 int ret = ERROR_OK;
697
698 newsize_out = angie_get_queue_size(device, PAYLOAD_DIRECTION_OUT) + 1
699 + angie_cmd->payload_out_size;
700
701 newsize_in = angie_get_queue_size(device, PAYLOAD_DIRECTION_IN)
702 + angie_cmd->payload_in_size;
703
704 /* Check if the current command can be appended to the queue */
705 if (newsize_out > 64 || newsize_in > 64) {
706 /* New command does not fit. Execute all commands in queue before starting
707 * new queue with the current command as first entry. */
708 ret = angie_execute_queued_commands(device, LIBUSB_TIMEOUT_MS);
709
710 if (ret == ERROR_OK)
711 ret = angie_post_process_queue(device);
712
713 if (ret == ERROR_OK)
714 angie_clear_queue(device);
715 }
716
717 if (!device->queue_start) {
718 /* Queue was empty */
719 device->commands_in_queue = 1;
720
721 device->queue_start = angie_cmd;
722 device->queue_end = angie_cmd;
723 } else {
724 /* There are already commands in the queue */
725 device->commands_in_queue++;
726
727 device->queue_end->next = angie_cmd;
728 device->queue_end = angie_cmd;
729 }
730
731 if (ret != ERROR_OK)
732 angie_clear_queue(device);
733
734 return ret;
735 }
736
737 /**
738 * Sends all queued ANGIE commands to the ANGIE for execution.
739 *
740 * @param device pointer to struct angie identifying ANGIE driver instance.
741 * @param timeout_ms
742 * @return on success: ERROR_OK
743 * @return on failure: ERROR_FAIL
744 */
745 static int angie_execute_queued_commands(struct angie *device, int timeout_ms)
746 {
747 struct angie_cmd *current;
748 int ret, i, index_out, index_in, count_out, count_in, transferred;
749 uint8_t buffer[64];
750
751 if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO))
752 angie_dump_queue(device);
753
754 index_out = 0;
755 count_out = 0;
756 count_in = 0;
757
758 for (current = device->queue_start; current; current = current->next) {
759 /* Add command to packet */
760 buffer[index_out] = current->id;
761 index_out++;
762 count_out++;
763
764 for (i = 0; i < current->payload_out_size; i++)
765 buffer[index_out + i] = current->payload_out[i];
766 index_out += current->payload_out_size;
767 count_in += current->payload_in_size;
768 count_out += current->payload_out_size;
769 }
770
771 /* Send packet to ANGIE */
772 ret = jtag_libusb_bulk_write(device->usb_device_handle, device->ep_out,
773 (char *)buffer, count_out, timeout_ms, &transferred);
774 if (ret != ERROR_OK)
775 return ret;
776 if (transferred != count_out)
777 return ERROR_FAIL;
778
779 /* Wait for response if commands contain IN payload data */
780 if (count_in > 0) {
781 ret = jtag_libusb_bulk_write(device->usb_device_handle, device->ep_in,
782 (char *)buffer, count_in, timeout_ms, &transferred);
783 if (ret != ERROR_OK)
784 return ret;
785 if (transferred != count_in)
786 return ERROR_FAIL;
787
788 /* Write back IN payload data */
789 index_in = 0;
790 for (current = device->queue_start; current; current = current->next) {
791 for (i = 0; i < current->payload_in_size; i++) {
792 current->payload_in[i] = buffer[index_in];
793 index_in++;
794 }
795 }
796 }
797 return ERROR_OK;
798 }
799
800 /**
801 * Convert an ANGIE command ID (\a id) to a human-readable string.
802 *
803 * @param id the ANGIE command ID.
804 * @return the corresponding human-readable string.
805 */
806 static const char *angie_cmd_id_string(uint8_t id)
807 {
808 switch (id) {
809 case CMD_SCAN_IN:
810 return "CMD_SCAN_IN";
811 case CMD_SLOW_SCAN_IN:
812 return "CMD_SLOW_SCAN_IN";
813 case CMD_SCAN_OUT:
814 return "CMD_SCAN_OUT";
815 case CMD_SLOW_SCAN_OUT:
816 return "CMD_SLOW_SCAN_OUT";
817 case CMD_SCAN_IO:
818 return "CMD_SCAN_IO";
819 case CMD_SLOW_SCAN_IO:
820 return "CMD_SLOW_SCAN_IO";
821 case CMD_CLOCK_TMS:
822 return "CMD_CLOCK_TMS";
823 case CMD_SLOW_CLOCK_TMS:
824 return "CMD_SLOW_CLOCK_TMS";
825 case CMD_CLOCK_TCK:
826 return "CMD_CLOCK_TCK";
827 case CMD_SLOW_CLOCK_TCK:
828 return "CMD_SLOW_CLOCK_TCK";
829 case CMD_SLEEP_US:
830 return "CMD_SLEEP_US";
831 case CMD_SLEEP_MS:
832 return "CMD_SLEEP_MS";
833 case CMD_GET_SIGNALS:
834 return "CMD_GET_SIGNALS";
835 case CMD_SET_SIGNALS:
836 return "CMD_SET_SIGNALS";
837 case CMD_CONFIGURE_TCK_FREQ:
838 return "CMD_CONFIGURE_TCK_FREQ";
839 case CMD_SET_LEDS:
840 return "CMD_SET_LEDS";
841 case CMD_TEST:
842 return "CMD_TEST";
843 default:
844 return "CMD_UNKNOWN";
845 }
846 }
847
848 /**
849 * Print one ANGIE command to stdout.
850 *
851 * @param angie_cmd pointer to ANGIE command.
852 */
853 static void angie_dump_command(struct angie_cmd *angie_cmd)
854 {
855 char hex[64 * 3];
856 for (int i = 0; i < angie_cmd->payload_out_size; i++)
857 sprintf(hex + 3 * i, "%02" PRIX8 " ", angie_cmd->payload_out[i]);
858
859 hex[3 * angie_cmd->payload_out_size - 1] = 0;
860 LOG_DEBUG_IO(" %-22s | OUT size = %" PRIi8 ", bytes = %s",
861 angie_cmd_id_string(angie_cmd->id), angie_cmd->payload_out_size, hex);
862
863 LOG_DEBUG_IO("\n | IN size = %" PRIi8 "\n", angie_cmd->payload_in_size);
864 }
865
866 /**
867 * Print the ANGIE command queue to stdout.
868 *
869 * @param device pointer to struct angie identifying ANGIE driver instance.
870 */
871 static void angie_dump_queue(struct angie *device)
872 {
873 struct angie_cmd *current;
874
875 LOG_DEBUG_IO("ANGIE command queue:\n");
876
877 for (current = device->queue_start; current; current = current->next)
878 angie_dump_command(current);
879 }
880
881 /**
882 * Perform JTAG scan
883 *
884 * Creates and appends a JTAG scan command to the ANGIE command queue.
885 * A JTAG scan consists of three steps:
886 * - Move to the desired SHIFT state, depending on scan type (IR/DR scan).
887 * - Shift TDI data into the JTAG chain, optionally reading the TDO pin.
888 * - Move to the desired end state.
889 *
890 * @param device pointer to struct angie identifying ANGIE driver instance.
891 * @param scan_type the type of the scan (IN, OUT, IO (bidirectional)).
892 * @param scan_size_bits number of bits to shift into the JTAG chain.
893 * @param tdi pointer to array containing TDI data.
894 * @param tdo_start pointer to first element of array where TDO data shall be
895 * stored. See #angie_cmd for details.
896 * @param tdo pointer to array where TDO data shall be stored
897 * @param tms_count_start number of TMS state transitions to perform BEFORE
898 * shifting data into the JTAG chain.
899 * @param tms_sequence_start sequence of TMS state transitions that will be
900 * performed BEFORE shifting data into the JTAG chain.
901 * @param tms_count_end number of TMS state transitions to perform AFTER
902 * shifting data into the JTAG chain.
903 * @param tms_sequence_end sequence of TMS state transitions that will be
904 * performed AFTER shifting data into the JTAG chain.
905 * @param origin pointer to OpenOCD command that generated this scan command.
906 * @param postprocess whether this command needs to be post-processed after
907 * execution.
908 * @return on success: ERROR_OK
909 * @return on failure: ERROR_FAIL
910 */
911 static int angie_append_scan_cmd(struct angie *device, enum scan_type scan_type,
912 int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo,
913 uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end,
914 uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess)
915 {
916 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
917 int ret, i, scan_size_bytes;
918 uint8_t bits_last_byte;
919
920 if (!cmd)
921 return ERROR_FAIL;
922
923 /* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID,
924 * 5 bytes are setup data -> 58 remaining payload bytes for TDI data */
925 if (scan_size_bits > (58 * 8)) {
926 LOG_ERROR("BUG: Tried to create CMD_SCAN_IO ANGIE command with too"
927 " large payload");
928 free(cmd);
929 return ERROR_FAIL;
930 }
931
932 scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
933
934 bits_last_byte = scan_size_bits % 8;
935 if (bits_last_byte == 0)
936 bits_last_byte = 8;
937
938 /* Allocate out_payload depending on scan type */
939 switch (scan_type) {
940 case SCAN_IN:
941 if (device->delay_scan_in < 0)
942 cmd->id = CMD_SCAN_IN;
943 else
944 cmd->id = CMD_SLOW_SCAN_IN;
945 ret = angie_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_IN);
946 break;
947 case SCAN_OUT:
948 if (device->delay_scan_out < 0)
949 cmd->id = CMD_SCAN_OUT;
950 else
951 cmd->id = CMD_SLOW_SCAN_OUT;
952 ret = angie_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
953 break;
954 case SCAN_IO:
955 if (device->delay_scan_io < 0)
956 cmd->id = CMD_SCAN_IO;
957 else
958 cmd->id = CMD_SLOW_SCAN_IO;
959 ret = angie_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
960 break;
961 default:
962 LOG_ERROR("BUG: 'append scan cmd' encountered an unknown scan type");
963 ret = ERROR_FAIL;
964 break;
965 }
966
967 if (ret != ERROR_OK) {
968 free(cmd);
969 return ret;
970 }
971
972 /* Build payload_out that is common to all scan types */
973 cmd->payload_out[0] = scan_size_bytes & 0xFF;
974 cmd->payload_out[1] = bits_last_byte & 0xFF;
975 cmd->payload_out[2] = ((tms_count_start & 0x0F) << 4) | (tms_count_end & 0x0F);
976 cmd->payload_out[3] = tms_sequence_start;
977 cmd->payload_out[4] = tms_sequence_end;
978
979 /* Setup payload_out for types with OUT transfer */
980 if (scan_type == SCAN_OUT || scan_type == SCAN_IO) {
981 for (i = 0; i < scan_size_bytes; i++)
982 cmd->payload_out[i + 5] = tdi[i];
983 }
984
985 /* Setup payload_in pointers for types with IN transfer */
986 if (scan_type == SCAN_IN || scan_type == SCAN_IO) {
987 cmd->payload_in_start = tdo_start;
988 cmd->payload_in = tdo;
989 cmd->payload_in_size = scan_size_bytes;
990 }
991
992 cmd->needs_postprocessing = postprocess;
993 cmd->cmd_origin = origin;
994
995 /* For scan commands, we free payload_in_start only when the command is
996 * the last in a series of split commands or a stand-alone command */
997 cmd->free_payload_in_start = postprocess;
998
999 return angie_append_queue(device, cmd);
1000 }
1001
1002 /**
1003 * Perform TAP state transitions
1004 *
1005 * @param device pointer to struct angie identifying ANGIE driver instance.
1006 * @param count defines the number of TCK clock cycles generated (up to 8).
1007 * @param sequence defines the TMS pin levels for each state transition. The
1008 * Least-Significant Bit is read first.
1009 * @return on success: ERROR_OK
1010 * @return on failure: ERROR_FAIL
1011 */
1012 static int angie_append_clock_tms_cmd(struct angie *device, uint8_t count,
1013 uint8_t sequence)
1014 {
1015 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1016 int ret;
1017
1018 if (!cmd) {
1019 LOG_ERROR("Out of memory");
1020 return ERROR_FAIL;
1021 }
1022
1023 if (device->delay_clock_tms < 0)
1024 cmd->id = CMD_CLOCK_TMS;
1025 else
1026 cmd->id = CMD_SLOW_CLOCK_TMS;
1027
1028 /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
1029 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1030 if (ret != ERROR_OK) {
1031 free(cmd);
1032 return ret;
1033 }
1034
1035 cmd->payload_out[0] = count;
1036 cmd->payload_out[1] = sequence;
1037
1038 return angie_append_queue(device, cmd);
1039 }
1040
1041 /**
1042 * Generate a defined amount of TCK clock cycles
1043 *
1044 * All other JTAG signals are left unchanged.
1045 *
1046 * @param device pointer to struct angie identifying ANGIE driver instance.
1047 * @param count the number of TCK clock cycles to generate.
1048 * @return on success: ERROR_OK
1049 * @return on failure: ERROR_FAIL
1050 */
1051 static int angie_append_clock_tck_cmd(struct angie *device, uint16_t count)
1052 {
1053 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1054 int ret;
1055
1056 if (!cmd) {
1057 LOG_ERROR("Out of memory");
1058 return ERROR_FAIL;
1059 }
1060
1061 if (device->delay_clock_tck < 0)
1062 cmd->id = CMD_CLOCK_TCK;
1063 else
1064 cmd->id = CMD_SLOW_CLOCK_TCK;
1065
1066 /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */
1067 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1068 if (ret != ERROR_OK) {
1069 free(cmd);
1070 return ret;
1071 }
1072
1073 cmd->payload_out[0] = count & 0xff;
1074 cmd->payload_out[1] = (count >> 8) & 0xff;
1075
1076 return angie_append_queue(device, cmd);
1077 }
1078
1079 /**
1080 * Read JTAG signals.
1081 *
1082 * @param device pointer to struct angie identifying ANGIE driver instance.
1083 * @return on success: ERROR_OK
1084 * @return on failure: ERROR_FAIL
1085 */
1086 static int angie_append_get_signals_cmd(struct angie *device)
1087 {
1088 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1089 int ret;
1090
1091 if (!cmd) {
1092 LOG_ERROR("Out of memory");
1093 return ERROR_FAIL;
1094 }
1095
1096 cmd->id = CMD_GET_SIGNALS;
1097 cmd->needs_postprocessing = true;
1098
1099 /* CMD_GET_SIGNALS has two IN payload bytes */
1100 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_IN);
1101
1102 if (ret != ERROR_OK) {
1103 free(cmd);
1104 return ret;
1105 }
1106
1107 return angie_append_queue(device, cmd);
1108 }
1109
1110 /**
1111 * Arbitrarily set JTAG output signals.
1112 *
1113 * @param device pointer to struct angie identifying ANGIE driver instance.
1114 * @param low defines which signals will be de-asserted. Each bit corresponds
1115 * to a JTAG signal:
1116 * - SIGNAL_TDI
1117 * - SIGNAL_TMS
1118 * - SIGNAL_TCK
1119 * - SIGNAL_TRST
1120 * - SIGNAL_BRKIN
1121 * - SIGNAL_RESET
1122 * - SIGNAL_OCDSE
1123 * @param high defines which signals will be asserted.
1124 * @return on success: ERROR_OK
1125 * @return on failure: ERROR_FAIL
1126 */
1127 static int angie_append_set_signals_cmd(struct angie *device, uint8_t low,
1128 uint8_t high)
1129 {
1130 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1131 int ret;
1132
1133 if (!cmd) {
1134 LOG_ERROR("Out of memory");
1135 return ERROR_FAIL;
1136 }
1137
1138 cmd->id = CMD_SET_SIGNALS;
1139
1140 /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */
1141 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1142
1143 if (ret != ERROR_OK) {
1144 free(cmd);
1145 return ret;
1146 }
1147
1148 cmd->payload_out[0] = low;
1149 cmd->payload_out[1] = high;
1150
1151 return angie_append_queue(device, cmd);
1152 }
1153
1154 /**
1155 * Sleep for a pre-defined number of microseconds
1156 *
1157 * @param device pointer to struct angie identifying ANGIE driver instance.
1158 * @param us the number microseconds to sleep.
1159 * @return on success: ERROR_OK
1160 * @return on failure: ERROR_FAIL
1161 */
1162 static int angie_append_sleep_cmd(struct angie *device, uint32_t us)
1163 {
1164 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1165 int ret;
1166
1167 if (!cmd) {
1168 LOG_ERROR("Out of memory");
1169 return ERROR_FAIL;
1170 }
1171
1172 cmd->id = CMD_SLEEP_US;
1173
1174 /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */
1175 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1176
1177 if (ret != ERROR_OK) {
1178 free(cmd);
1179 return ret;
1180 }
1181
1182 cmd->payload_out[0] = us & 0x00ff;
1183 cmd->payload_out[1] = (us >> 8) & 0x00ff;
1184
1185 return angie_append_queue(device, cmd);
1186 }
1187
1188 /**
1189 * Set TCK delay counters
1190 *
1191 * @param device pointer to struct angie identifying ANGIE driver instance.
1192 * @param delay_scan_in delay count top value in jtag_slow_scan_in() function.
1193 * @param delay_scan_out delay count top value in jtag_slow_scan_out() function.
1194 * @param delay_scan_io delay count top value in jtag_slow_scan_io() function.
1195 * @param delay_tck delay count top value in jtag_clock_tck() function.
1196 * @param delay_tms delay count top value in jtag_slow_clock_tms() function.
1197 * @return on success: ERROR_OK
1198 * @return on failure: ERROR_FAIL
1199 */
1200 static int angie_append_configure_tck_cmd(struct angie *device, int delay_scan_in,
1201 int delay_scan_out, int delay_scan_io, int delay_tck, int delay_tms)
1202 {
1203 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1204 int ret;
1205
1206 if (!cmd) {
1207 LOG_ERROR("Out of memory");
1208 return ERROR_FAIL;
1209 }
1210
1211 cmd->id = CMD_CONFIGURE_TCK_FREQ;
1212
1213 /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero
1214 * IN payload bytes */
1215 ret = angie_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
1216 if (ret != ERROR_OK) {
1217 free(cmd);
1218 return ret;
1219 }
1220
1221 if (delay_scan_in < 0)
1222 cmd->payload_out[0] = 0;
1223 else
1224 cmd->payload_out[0] = (uint8_t)delay_scan_in;
1225
1226 if (delay_scan_out < 0)
1227 cmd->payload_out[1] = 0;
1228 else
1229 cmd->payload_out[1] = (uint8_t)delay_scan_out;
1230
1231 if (delay_scan_io < 0)
1232 cmd->payload_out[2] = 0;
1233 else
1234 cmd->payload_out[2] = (uint8_t)delay_scan_io;
1235
1236 if (delay_tck < 0)
1237 cmd->payload_out[3] = 0;
1238 else
1239 cmd->payload_out[3] = (uint8_t)delay_tck;
1240
1241 if (delay_tms < 0)
1242 cmd->payload_out[4] = 0;
1243 else
1244 cmd->payload_out[4] = (uint8_t)delay_tms;
1245
1246 return angie_append_queue(device, cmd);
1247 }
1248
1249 /**
1250 * Test command. Used to check if the ANGIE device is ready to accept new
1251 * commands.
1252 *
1253 * @param device pointer to struct angie identifying ANGIE driver instance.
1254 * @return on success: ERROR_OK
1255 * @return on failure: ERROR_FAIL
1256 */
1257 static int angie_append_test_cmd(struct angie *device)
1258 {
1259 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1260 int ret;
1261
1262 if (!cmd) {
1263 LOG_ERROR("Out of memory");
1264 return ERROR_FAIL;
1265 }
1266
1267 cmd->id = CMD_TEST;
1268
1269 /* CMD_TEST has one OUT payload byte and zero IN payload bytes */
1270 ret = angie_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
1271 if (ret != ERROR_OK) {
1272 free(cmd);
1273 return ret;
1274 }
1275
1276 cmd->payload_out[0] = 0xAA;
1277
1278 return angie_append_queue(device, cmd);
1279 }
1280
1281 /****************** ANGIE TCK frequency helper functions ******************/
1282
1283 /**
1284 * Calculate delay values for a given TCK frequency.
1285 *
1286 * The ANGIE firmware uses five different speed values for different
1287 * commands. These speed values are calculated in these functions.
1288 *
1289 * The five different commands which support variable TCK frequency are
1290 * implemented twice in the firmware:
1291 * 1. Maximum possible frequency without any artificial delay
1292 * 2. Variable frequency with artificial linear delay loop
1293 *
1294 * To set the ANGIE to maximum frequency, it is only necessary to use the
1295 * corresponding command IDs. To set the ANGIE to a lower frequency, the
1296 * delay loop top values have to be calculated first. Then, a
1297 * CMD_CONFIGURE_TCK_FREQ command needs to be sent to the ANGIE device.
1298 *
1299 * The delay values are described by linear equations:
1300 * t = k * x + d
1301 * (t = period, k = constant, x = delay value, d = constant)
1302 *
1303 * Thus, the delay can be calculated as in the following equation:
1304 * x = (t - d) / k
1305 *
1306 * The constants in these equations have been determined and validated by
1307 * measuring the frequency resulting from different delay values.
1308 *
1309 * @param type for which command to calculate the delay value.
1310 * @param f TCK frequency for which to calculate the delay value in Hz.
1311 * @param delay where to store resulting delay value.
1312 * @return on success: ERROR_OK
1313 * @return on failure: ERROR_FAIL
1314 */
1315 static int angie_calculate_delay(enum angie_delay_type type, long f, int *delay)
1316 {
1317 float t_us, x, x_ceil;
1318
1319 /* Calculate period of requested TCK frequency */
1320 t_us = 1000000.0 / f;
1321
1322 switch (type) {
1323 case DELAY_CLOCK_TCK:
1324 x = (t_us - 6.0) / 4;
1325 break;
1326 case DELAY_CLOCK_TMS:
1327 x = (t_us - 8.5) / 4;
1328 break;
1329 case DELAY_SCAN_IN:
1330 x = (t_us - 8.8308) / 4;
1331 break;
1332 case DELAY_SCAN_OUT:
1333 x = (t_us - 10.527) / 4;
1334 break;
1335 case DELAY_SCAN_IO:
1336 x = (t_us - 13.132) / 4;
1337 break;
1338 default:
1339 return ERROR_FAIL;
1340 break;
1341 }
1342
1343 /* Check if the delay value is negative. This happens when a frequency is
1344 * requested that is too high for the delay loop implementation. In this
1345 * case, set delay value to zero. */
1346 if (x < 0)
1347 x = 0;
1348
1349 /* We need to convert the exact delay value to an integer. Therefore, we
1350 * round the exact value UP to ensure that the resulting frequency is NOT
1351 * higher than the requested frequency. */
1352 x_ceil = ceilf(x);
1353
1354 /* Check if the value is within limits */
1355 if (x_ceil > 255)
1356 return ERROR_FAIL;
1357
1358 *delay = (int)x_ceil;
1359
1360 return ERROR_OK;
1361 }
1362
1363 /**
1364 * Calculate frequency for a given delay value.
1365 *
1366 * Similar to the #angie_calculate_delay function, this function calculates the
1367 * TCK frequency for a given delay value by using linear equations of the form:
1368 * t = k * x + d
1369 * (t = period, k = constant, x = delay value, d = constant)
1370 *
1371 * @param type for which command to calculate the delay value.
1372 * @param delay value for which to calculate the resulting TCK frequency.
1373 * @return the resulting TCK frequency
1374 */
1375 static long angie_calculate_frequency(enum angie_delay_type type, int delay)
1376 {
1377 float t_us, f_float;
1378
1379 if (delay > 255)
1380 return 0;
1381
1382 switch (type) {
1383 case DELAY_CLOCK_TCK:
1384 if (delay < 0)
1385 t_us = 2.666;
1386 else
1387 t_us = (4.0 * delay) + 6.0;
1388 break;
1389 case DELAY_CLOCK_TMS:
1390 if (delay < 0)
1391 t_us = 5.666;
1392 else
1393 t_us = (4.0 * delay) + 8.5;
1394 break;
1395 case DELAY_SCAN_IN:
1396 if (delay < 0)
1397 t_us = 5.5;
1398 else
1399 t_us = (4.0 * delay) + 8.8308;
1400 break;
1401 case DELAY_SCAN_OUT:
1402 if (delay < 0)
1403 t_us = 7.0;
1404 else
1405 t_us = (4.0 * delay) + 10.527;
1406 break;
1407 case DELAY_SCAN_IO:
1408 if (delay < 0)
1409 t_us = 9.926;
1410 else
1411 t_us = (4.0 * delay) + 13.132;
1412 break;
1413 default:
1414 return 0;
1415 }
1416
1417 f_float = 1000000.0 / t_us;
1418 return roundf(f_float);
1419 }
1420
1421 /******************* Interface between ANGIE and OpenOCD ******************/
1422
1423 /**
1424 * Sets the end state follower (see interface.h) if \a endstate is a stable
1425 * state.
1426 *
1427 * @param endstate the state the end state follower should be set to.
1428 */
1429 static void angie_set_end_state(tap_state_t endstate)
1430 {
1431 if (tap_is_state_stable(endstate))
1432 tap_set_end_state(endstate);
1433 else
1434 LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate));
1435 }
1436
1437 /**
1438 * Move from the current TAP state to the current TAP end state.
1439 *
1440 * @param device pointer to struct angie identifying ANGIE driver instance.
1441 * @return on success: ERROR_OK
1442 * @return on failure: ERROR_FAIL
1443 */
1444 static int angie_queue_statemove(struct angie *device)
1445 {
1446 uint8_t tms_sequence, tms_count;
1447 int ret;
1448
1449 if (tap_get_state() == tap_get_end_state()) {
1450 /* Do nothing if we are already there */
1451 return ERROR_OK;
1452 }
1453
1454 tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1455 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1456
1457 ret = angie_append_clock_tms_cmd(device, tms_count, tms_sequence);
1458
1459 if (ret == ERROR_OK)
1460 tap_set_state(tap_get_end_state());
1461
1462 return ret;
1463 }
1464
1465 /**
1466 * Perform a scan operation on a JTAG register.
1467 *
1468 * @param device pointer to struct angie identifying ANGIE driver instance.
1469 * @param cmd pointer to the command that shall be executed.
1470 * @return on success: ERROR_OK
1471 * @return on failure: ERROR_FAIL
1472 */
1473 static int angie_queue_scan(struct angie *device, struct jtag_command *cmd)
1474 {
1475 uint32_t scan_size_bits, scan_size_bytes, bits_last_scan;
1476 uint32_t scans_max_payload, bytecount;
1477 uint8_t *tdi_buffer_start = NULL, *tdi_buffer = NULL;
1478 uint8_t *tdo_buffer_start = NULL, *tdo_buffer = NULL;
1479
1480 uint8_t first_tms_count, first_tms_sequence;
1481 uint8_t last_tms_count, last_tms_sequence;
1482
1483 uint8_t tms_count_pause, tms_sequence_pause;
1484 uint8_t tms_count_resume, tms_sequence_resume;
1485
1486 uint8_t tms_count_start, tms_sequence_start;
1487 uint8_t tms_count_end, tms_sequence_end;
1488
1489 enum scan_type type;
1490 int ret;
1491
1492 /* Determine scan size */
1493 scan_size_bits = jtag_scan_size(cmd->cmd.scan);
1494 scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
1495
1496 /* Determine scan type (IN/OUT/IO) */
1497 type = jtag_scan_type(cmd->cmd.scan);
1498
1499 /* Determine number of scan commands with maximum payload */
1500 scans_max_payload = scan_size_bytes / 58;
1501
1502 /* Determine size of last shift command */
1503 bits_last_scan = scan_size_bits - (scans_max_payload * 58 * 8);
1504
1505 /* Allocate TDO buffer if required */
1506 if (type == SCAN_IN || type == SCAN_IO) {
1507 tdo_buffer_start = calloc(sizeof(uint8_t), scan_size_bytes);
1508
1509 if (!tdo_buffer_start)
1510 return ERROR_FAIL;
1511
1512 tdo_buffer = tdo_buffer_start;
1513 }
1514
1515 /* Fill TDI buffer if required */
1516 if (type == SCAN_OUT || type == SCAN_IO) {
1517 jtag_build_buffer(cmd->cmd.scan, &tdi_buffer_start);
1518 tdi_buffer = tdi_buffer_start;
1519 }
1520
1521 /* Get TAP state transitions */
1522 if (cmd->cmd.scan->ir_scan) {
1523 angie_set_end_state(TAP_IRSHIFT);
1524 first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1525 first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1526
1527 tap_set_state(TAP_IRSHIFT);
1528 tap_set_end_state(cmd->cmd.scan->end_state);
1529 last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1530 last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1531
1532 /* TAP state transitions for split scans */
1533 tms_count_pause = tap_get_tms_path_len(TAP_IRSHIFT, TAP_IRPAUSE);
1534 tms_sequence_pause = tap_get_tms_path(TAP_IRSHIFT, TAP_IRPAUSE);
1535 tms_count_resume = tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRSHIFT);
1536 tms_sequence_resume = tap_get_tms_path(TAP_IRPAUSE, TAP_IRSHIFT);
1537 } else {
1538 angie_set_end_state(TAP_DRSHIFT);
1539 first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1540 first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1541
1542 tap_set_state(TAP_DRSHIFT);
1543 tap_set_end_state(cmd->cmd.scan->end_state);
1544 last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1545 last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1546
1547 /* TAP state transitions for split scans */
1548 tms_count_pause = tap_get_tms_path_len(TAP_DRSHIFT, TAP_DRPAUSE);
1549 tms_sequence_pause = tap_get_tms_path(TAP_DRSHIFT, TAP_DRPAUSE);
1550 tms_count_resume = tap_get_tms_path_len(TAP_DRPAUSE, TAP_DRSHIFT);
1551 tms_sequence_resume = tap_get_tms_path(TAP_DRPAUSE, TAP_DRSHIFT);
1552 }
1553
1554 /* Generate scan commands */
1555 bytecount = scan_size_bytes;
1556 while (bytecount > 0) {
1557 if (bytecount == scan_size_bytes) {
1558 /* This is the first scan */
1559 tms_count_start = first_tms_count;
1560 tms_sequence_start = first_tms_sequence;
1561 } else {
1562 /* Resume from previous scan */
1563 tms_count_start = tms_count_resume;
1564 tms_sequence_start = tms_sequence_resume;
1565 }
1566
1567 if (bytecount > 58) { /* Full scan, at least one scan will follow */
1568 tms_count_end = tms_count_pause;
1569 tms_sequence_end = tms_sequence_pause;
1570
1571 ret = angie_append_scan_cmd(device,
1572 type,
1573 58 * 8,
1574 tdi_buffer,
1575 tdo_buffer_start,
1576 tdo_buffer,
1577 tms_count_start,
1578 tms_sequence_start,
1579 tms_count_end,
1580 tms_sequence_end,
1581 cmd,
1582 false);
1583
1584 bytecount -= 58;
1585
1586 /* Update TDI and TDO buffer pointers */
1587 if (tdi_buffer_start)
1588 tdi_buffer += 58;
1589 if (tdo_buffer_start)
1590 tdo_buffer += 58;
1591 } else if (bytecount == 58) { /* Full scan, no further scans */
1592 tms_count_end = last_tms_count;
1593 tms_sequence_end = last_tms_sequence;
1594
1595 ret = angie_append_scan_cmd(device,
1596 type,
1597 58 * 8,
1598 tdi_buffer,
1599 tdo_buffer_start,
1600 tdo_buffer,
1601 tms_count_start,
1602 tms_sequence_start,
1603 tms_count_end,
1604 tms_sequence_end,
1605 cmd,
1606 true);
1607
1608 bytecount = 0;
1609 } else {/* Scan with less than maximum payload, no further scans */
1610 tms_count_end = last_tms_count;
1611 tms_sequence_end = last_tms_sequence;
1612
1613 ret = angie_append_scan_cmd(device,
1614 type,
1615 bits_last_scan,
1616 tdi_buffer,
1617 tdo_buffer_start,
1618 tdo_buffer,
1619 tms_count_start,
1620 tms_sequence_start,
1621 tms_count_end,
1622 tms_sequence_end,
1623 cmd,
1624 true);
1625
1626 bytecount = 0;
1627 }
1628
1629 if (ret != ERROR_OK) {
1630 free(tdi_buffer_start);
1631 free(tdo_buffer_start);
1632 return ret;
1633 }
1634 }
1635
1636 free(tdi_buffer_start);
1637
1638 /* Set current state to the end state requested by the command */
1639 tap_set_state(cmd->cmd.scan->end_state);
1640
1641 return ERROR_OK;
1642 }
1643
1644 /**
1645 * Move the TAP into the Test Logic Reset state.
1646 *
1647 * @param device pointer to struct angie identifying ANGIE driver instance.
1648 * @param cmd pointer to the command that shall be executed.
1649 * @return on success: ERROR_OK
1650 * @return on failure: ERROR_FAIL
1651 */
1652 static int angie_queue_tlr_reset(struct angie *device, struct jtag_command *cmd)
1653 {
1654 int ret = angie_append_clock_tms_cmd(device, 5, 0xff);
1655
1656 if (ret == ERROR_OK)
1657 tap_set_state(TAP_RESET);
1658
1659 return ret;
1660 }
1661
1662 /**
1663 * Run Test.
1664 *
1665 * Generate TCK clock cycles while remaining
1666 * in the Run-Test/Idle state.
1667 *
1668 * @param device pointer to struct angie identifying ANGIE driver instance.
1669 * @param cmd pointer to the command that shall be executed.
1670 * @return on success: ERROR_OK
1671 * @return on failure: ERROR_FAIL
1672 */
1673 static int angie_queue_runtest(struct angie *device, struct jtag_command *cmd)
1674 {
1675 int ret;
1676
1677 /* Only perform statemove if the TAP currently isn't in the TAP_IDLE state */
1678 if (tap_get_state() != TAP_IDLE) {
1679 angie_set_end_state(TAP_IDLE);
1680 angie_queue_statemove(device);
1681 }
1682
1683 /* Generate the clock cycles */
1684 ret = angie_append_clock_tck_cmd(device, cmd->cmd.runtest->num_cycles);
1685 if (ret != ERROR_OK)
1686 return ret;
1687
1688 /* Move to end state specified in command */
1689 if (cmd->cmd.runtest->end_state != tap_get_state()) {
1690 tap_set_end_state(cmd->cmd.runtest->end_state);
1691 angie_queue_statemove(device);
1692 }
1693
1694 return ERROR_OK;
1695 }
1696
1697 /**
1698 * Execute a JTAG_RESET command
1699 *
1700 * @param device
1701 * @param trst indicate if trst signal is activated.
1702 * @param srst indicate if srst signal is activated.
1703 * @return on success: ERROR_OK
1704 * @return on failure: ERROR_FAIL
1705 */
1706 static int angie_reset(int trst, int srst)
1707 {
1708 struct angie *device = angie_handle;
1709 uint8_t low = 0, high = 0;
1710
1711 if (trst) {
1712 tap_set_state(TAP_RESET);
1713 low |= SIGNAL_TRST;
1714 } else {
1715 high |= SIGNAL_TRST;
1716 }
1717
1718 if (srst)
1719 low |= SIGNAL_SRST;
1720 else
1721 high |= SIGNAL_SRST;
1722
1723 int ret = angie_append_set_signals_cmd(device, low, high);
1724 if (ret == ERROR_OK)
1725 angie_clear_queue(device);
1726
1727 ret = angie_execute_queued_commands(device, LIBUSB_TIMEOUT_MS);
1728 if (ret == ERROR_OK)
1729 angie_clear_queue(device);
1730
1731 return ret;
1732 }
1733
1734 /**
1735 * Move to one TAP state or several states in succession.
1736 *
1737 * @param device pointer to struct angie identifying ANGIE driver instance.
1738 * @param cmd pointer to the command that shall be executed.
1739 * @return on success: ERROR_OK
1740 * @return on failure: ERROR_FAIL
1741 */
1742 static int angie_queue_pathmove(struct angie *device, struct jtag_command *cmd)
1743 {
1744 int ret, i, num_states, batch_size, state_count;
1745 tap_state_t *path;
1746 uint8_t tms_sequence;
1747
1748 num_states = cmd->cmd.pathmove->num_states;
1749 path = cmd->cmd.pathmove->path;
1750 state_count = 0;
1751
1752 while (num_states > 0) {
1753 tms_sequence = 0;
1754
1755 /* Determine batch size */
1756 if (num_states >= 8)
1757 batch_size = 8;
1758 else
1759 batch_size = num_states;
1760
1761 for (i = 0; i < batch_size; i++) {
1762 if (tap_state_transition(tap_get_state(), false) == path[state_count]) {
1763 /* Append '0' transition: clear bit 'i' in tms_sequence */
1764 buf_set_u32(&tms_sequence, i, 1, 0x0);
1765 } else if (tap_state_transition(tap_get_state(), true)
1766 == path[state_count]) {
1767 /* Append '1' transition: set bit 'i' in tms_sequence */
1768 buf_set_u32(&tms_sequence, i, 1, 0x1);
1769 } else {
1770 /* Invalid state transition */
1771 LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
1772 tap_state_name(tap_get_state()),
1773 tap_state_name(path[state_count]));
1774 return ERROR_FAIL;
1775 }
1776
1777 tap_set_state(path[state_count]);
1778 state_count++;
1779 num_states--;
1780 }
1781
1782 /* Append CLOCK_TMS command to ANGIE command queue */
1783 LOG_INFO("pathmove batch: count = %i, sequence = 0x%" PRIx8 "", batch_size, tms_sequence);
1784 ret = angie_append_clock_tms_cmd(angie_handle, batch_size, tms_sequence);
1785 if (ret != ERROR_OK)
1786 return ret;
1787 }
1788
1789 return ERROR_OK;
1790 }
1791
1792 /**
1793 * Sleep for a specific amount of time.
1794 *
1795 * @param device pointer to struct angie identifying ANGIE driver instance.
1796 * @param cmd pointer to the command that shall be executed.
1797 * @return on success: ERROR_OK
1798 * @return on failure: ERROR_FAIL
1799 */
1800 static int angie_queue_sleep(struct angie *device, struct jtag_command *cmd)
1801 {
1802 /* IMPORTANT! Due to the time offset in command execution introduced by
1803 * command queueing, this needs to be implemented in the ANGIE device */
1804 return angie_append_sleep_cmd(device, cmd->cmd.sleep->us);
1805 }
1806
1807 /**
1808 * Generate TCK cycles while remaining in a stable state.
1809 *
1810 * @param device pointer to struct angie identifying ANGIE driver instance.
1811 * @param cmd pointer to the command that shall be executed.
1812 */
1813 static int angie_queue_stableclocks(struct angie *device, struct jtag_command *cmd)
1814 {
1815 int ret;
1816 unsigned int num_cycles;
1817
1818 if (!tap_is_state_stable(tap_get_state())) {
1819 LOG_ERROR("JTAG_STABLECLOCKS: state not stable");
1820 return ERROR_FAIL;
1821 }
1822
1823 num_cycles = cmd->cmd.stableclocks->num_cycles;
1824
1825 /* TMS stays either high (Test Logic Reset state) or low (all other states) */
1826 if (tap_get_state() == TAP_RESET)
1827 ret = angie_append_set_signals_cmd(device, 0, SIGNAL_TMS);
1828 else
1829 ret = angie_append_set_signals_cmd(device, SIGNAL_TMS, 0);
1830
1831 if (ret != ERROR_OK)
1832 return ret;
1833
1834 while (num_cycles > 0) {
1835 if (num_cycles > 0xFFFF) {
1836 /* ANGIE CMD_CLOCK_TCK can generate up to 0xFFFF (uint16_t) cycles */
1837 ret = angie_append_clock_tck_cmd(device, 0xFFFF);
1838 num_cycles -= 0xFFFF;
1839 } else {
1840 ret = angie_append_clock_tck_cmd(device, num_cycles);
1841 num_cycles = 0;
1842 }
1843
1844 if (ret != ERROR_OK)
1845 return ret;
1846 }
1847
1848 return ERROR_OK;
1849 }
1850
1851 /**
1852 * Post-process JTAG_SCAN command
1853 *
1854 * @param angie_cmd pointer to ANGIE command that shall be processed.
1855 * @return on success: ERROR_OK
1856 * @return on failure: ERROR_FAIL
1857 */
1858 static int angie_post_process_scan(struct angie_cmd *angie_cmd)
1859 {
1860 struct jtag_command *cmd = angie_cmd->cmd_origin;
1861 int ret;
1862
1863 switch (jtag_scan_type(cmd->cmd.scan)) {
1864 case SCAN_IN:
1865 case SCAN_IO:
1866 ret = jtag_read_buffer(angie_cmd->payload_in_start, cmd->cmd.scan);
1867 break;
1868 case SCAN_OUT:
1869 /* Nothing to do for OUT scans */
1870 ret = ERROR_OK;
1871 break;
1872 default:
1873 LOG_ERROR("BUG: angie post process scan encountered an unknown JTAG scan type");
1874 ret = ERROR_FAIL;
1875 break;
1876 }
1877
1878 return ret;
1879 }
1880
1881 /**
1882 * Perform post-processing of commands after ANGIE queue has been executed.
1883 *
1884 * @param device pointer to struct angie identifying ANGIE driver instance.
1885 * @return on success: ERROR_OK
1886 * @return on failure: ERROR_FAIL
1887 */
1888 static int angie_post_process_queue(struct angie *device)
1889 {
1890 struct angie_cmd *current;
1891 struct jtag_command *openocd_cmd;
1892 int ret;
1893
1894 current = device->queue_start;
1895
1896 while (current) {
1897 openocd_cmd = current->cmd_origin;
1898
1899 /* Check if a corresponding OpenOCD command is stored for this
1900 * ANGIE command */
1901 if (current->needs_postprocessing && openocd_cmd) {
1902 switch (openocd_cmd->type) {
1903 case JTAG_SCAN:
1904 ret = angie_post_process_scan(current);
1905 break;
1906 case JTAG_TLR_RESET:
1907 case JTAG_RUNTEST:
1908 case JTAG_PATHMOVE:
1909 case JTAG_SLEEP:
1910 case JTAG_STABLECLOCKS:
1911 /* Nothing to do for these commands */
1912 ret = ERROR_OK;
1913 break;
1914 default:
1915 ret = ERROR_FAIL;
1916 LOG_ERROR("BUG: angie post process queue encountered unknown JTAG "
1917 "command type");
1918 break;
1919 }
1920
1921 if (ret != ERROR_OK)
1922 return ret;
1923 }
1924
1925 current = current->next;
1926 }
1927
1928 return ERROR_OK;
1929 }
1930
1931 /**************************** JTAG driver functions ***************************/
1932
1933 /**
1934 * Executes the JTAG Command Queue.
1935 *
1936 * This is done in three stages: First, all OpenOCD commands are processed into
1937 * queued ANGIE commands. Next, the ANGIE command queue is sent to the
1938 * ANGIE device and data received from the ANGIE device is cached. Finally,
1939 * the post-processing function writes back data to the corresponding OpenOCD
1940 * commands.
1941 *
1942 * @return on success: ERROR_OK
1943 * @return on failure: ERROR_FAIL
1944 */
1945 static int angie_execute_queue(void)
1946 {
1947 struct jtag_command *cmd = jtag_command_queue;
1948 int ret;
1949
1950 while (cmd) {
1951 switch (cmd->type) {
1952 case JTAG_SCAN:
1953 ret = angie_queue_scan(angie_handle, cmd);
1954 break;
1955 case JTAG_TLR_RESET:
1956 ret = angie_queue_tlr_reset(angie_handle, cmd);
1957 break;
1958 case JTAG_RUNTEST:
1959 ret = angie_queue_runtest(angie_handle, cmd);
1960 break;
1961 case JTAG_PATHMOVE:
1962 ret = angie_queue_pathmove(angie_handle, cmd);
1963 break;
1964 case JTAG_SLEEP:
1965 ret = angie_queue_sleep(angie_handle, cmd);
1966 break;
1967 case JTAG_STABLECLOCKS:
1968 ret = angie_queue_stableclocks(angie_handle, cmd);
1969 break;
1970 default:
1971 ret = ERROR_FAIL;
1972 LOG_ERROR("BUG: encountered unknown JTAG command type");
1973 break;
1974 }
1975
1976 if (ret != ERROR_OK)
1977 return ret;
1978
1979 cmd = cmd->next;
1980 }
1981
1982 if (angie_handle->commands_in_queue > 0) {
1983 ret = angie_execute_queued_commands(angie_handle, LIBUSB_TIMEOUT_MS);
1984 if (ret != ERROR_OK)
1985 return ret;
1986
1987 ret = angie_post_process_queue(angie_handle);
1988 if (ret != ERROR_OK)
1989 return ret;
1990
1991 angie_clear_queue(angie_handle);
1992 }
1993
1994 return ERROR_OK;
1995 }
1996
1997 /**
1998 * Set the TCK frequency of the ANGIE adapter.
1999 *
2000 * @param khz desired JTAG TCK frequency.
2001 * @param jtag_speed where to store corresponding adapter-specific speed value.
2002 * @return on success: ERROR_OK
2003 * @return on failure: ERROR_FAIL
2004 */
2005 static int angie_khz(int khz, int *jtag_speed)
2006 {
2007 int ret;
2008
2009 if (khz == 0) {
2010 LOG_ERROR("RCLK not supported");
2011 return ERROR_FAIL;
2012 }
2013
2014 /* CLOCK_TCK commands are decoupled from others. Therefore, the frequency
2015 * setting can be done independently from all other commands. */
2016 if (khz >= 375) {
2017 angie_handle->delay_clock_tck = -1;
2018 } else {
2019 ret = angie_calculate_delay(DELAY_CLOCK_TCK, khz * 1000,
2020 &angie_handle->delay_clock_tck);
2021 if (ret != ERROR_OK)
2022 return ret;
2023 }
2024
2025 /* SCAN_{IN,OUT,IO} commands invoke CLOCK_TMS commands. Therefore, if the
2026 * requested frequency goes below the maximum frequency for SLOW_CLOCK_TMS
2027 * commands, all SCAN commands MUST also use the variable frequency
2028 * implementation! */
2029 if (khz >= 176) {
2030 angie_handle->delay_clock_tms = -1;
2031 angie_handle->delay_scan_in = -1;
2032 angie_handle->delay_scan_out = -1;
2033 angie_handle->delay_scan_io = -1;
2034 } else {
2035 ret = angie_calculate_delay(DELAY_CLOCK_TMS, khz * 1000,
2036 &angie_handle->delay_clock_tms);
2037 if (ret != ERROR_OK)
2038 return ret;
2039
2040 ret = angie_calculate_delay(DELAY_SCAN_IN, khz * 1000,
2041 &angie_handle->delay_scan_in);
2042 if (ret != ERROR_OK)
2043 return ret;
2044
2045 ret = angie_calculate_delay(DELAY_SCAN_OUT, khz * 1000,
2046 &angie_handle->delay_scan_out);
2047 if (ret != ERROR_OK)
2048 return ret;
2049
2050 ret = angie_calculate_delay(DELAY_SCAN_IO, khz * 1000,
2051 &angie_handle->delay_scan_io);
2052 if (ret != ERROR_OK)
2053 return ret;
2054 }
2055
2056 LOG_DEBUG_IO("ANGIE TCK setup: delay_tck = %i (%li Hz),",
2057 angie_handle->delay_clock_tck,
2058 angie_calculate_frequency(DELAY_CLOCK_TCK, angie_handle->delay_clock_tck));
2059 LOG_DEBUG_IO(" delay_tms = %i (%li Hz),",
2060 angie_handle->delay_clock_tms,
2061 angie_calculate_frequency(DELAY_CLOCK_TMS, angie_handle->delay_clock_tms));
2062 LOG_DEBUG_IO(" delay_scan_in = %i (%li Hz),",
2063 angie_handle->delay_scan_in,
2064 angie_calculate_frequency(DELAY_SCAN_IN, angie_handle->delay_scan_in));
2065 LOG_DEBUG_IO(" delay_scan_out = %i (%li Hz),",
2066 angie_handle->delay_scan_out,
2067 angie_calculate_frequency(DELAY_SCAN_OUT, angie_handle->delay_scan_out));
2068 LOG_DEBUG_IO(" delay_scan_io = %i (%li Hz),",
2069 angie_handle->delay_scan_io,
2070 angie_calculate_frequency(DELAY_SCAN_IO, angie_handle->delay_scan_io));
2071
2072 /* Configure the ANGIE device with the new delay values */
2073 ret = angie_append_configure_tck_cmd(angie_handle,
2074 angie_handle->delay_scan_in,
2075 angie_handle->delay_scan_out,
2076 angie_handle->delay_scan_io,
2077 angie_handle->delay_clock_tck,
2078 angie_handle->delay_clock_tms);
2079
2080 if (ret != ERROR_OK)
2081 return ret;
2082
2083 *jtag_speed = khz;
2084
2085 return ERROR_OK;
2086 }
2087
2088 /**
2089 * Set the TCK frequency of the ANGIE adapter.
2090 *
2091 * Because of the way the TCK frequency is set up in the ANGIE firmware,
2092 * there are five different speed settings. To simplify things, the
2093 * adapter-specific speed setting value is identical to the TCK frequency in
2094 * khz.
2095 *
2096 * @param speed desired adapter-specific speed value.
2097 * @return on success: ERROR_OK
2098 * @return on failure: ERROR_FAIL
2099 */
2100 static int angie_speed(int speed)
2101 {
2102 int dummy;
2103
2104 return angie_khz(speed, &dummy);
2105 }
2106
2107 /**
2108 * Convert adapter-specific speed value to corresponding TCK frequency in kHz.
2109 *
2110 * Because of the way the TCK frequency is set up in the ANGIE firmware,
2111 * there are five different speed settings. To simplify things, the
2112 * adapter-specific speed setting value is identical to the TCK frequency in
2113 * khz.
2114 *
2115 * @param speed adapter-specific speed value.
2116 * @param khz where to store corresponding TCK frequency in kHz.
2117 * @return on success: ERROR_OK
2118 * @return on failure: ERROR_FAIL
2119 */
2120 static int angie_speed_div(int speed, int *khz)
2121 {
2122 *khz = speed;
2123
2124 return ERROR_OK;
2125 }
2126
2127 /**
2128 * Initiates the firmware download to the ANGIE adapter and prepares
2129 * the USB handle.
2130 *
2131 * @return on success: ERROR_OK
2132 * @return on failure: ERROR_FAIL
2133 */
2134 static int angie_init(void)
2135 {
2136 int ret, transferred;
2137 char str_manufacturer[20];
2138 bool download_firmware = false;
2139 char dummy[64];
2140 uint8_t input_signals, output_signals;
2141
2142 angie_handle = calloc(1, sizeof(struct angie));
2143
2144 if (!angie_handle) {
2145 LOG_ERROR("Out of memory");
2146 return ERROR_FAIL;
2147 }
2148
2149 ret = angie_usb_open(angie_handle);
2150 if (ret != ERROR_OK) {
2151 LOG_ERROR("Could not open ANGIE device");
2152 free(angie_handle);
2153 angie_handle = NULL;
2154 return ret;
2155 }
2156
2157 /* Get String Descriptor to determine if firmware needs to be loaded */
2158 ret = libusb_get_string_descriptor_ascii(angie_handle->usb_device_handle, 1, (unsigned char *)str_manufacturer, 20);
2159 if (ret < 0) {
2160 /* Could not get descriptor -> Unconfigured or original Keil firmware */
2161 download_firmware = true;
2162 } else {
2163 /* We got a String Descriptor, check if it is the correct one */
2164 if (strncmp(str_manufacturer, "NanoXplore, SAS.", 16) != 0)
2165 download_firmware = true;
2166 }
2167
2168 if (download_firmware) {
2169 LOG_INFO("Loading ANGIE firmware. This is reversible by power-cycling ANGIE device.");
2170
2171 if (libusb_claim_interface(angie_handle->usb_device_handle, 0) != ERROR_OK)
2172 LOG_ERROR("Could not claim interface");
2173
2174 ret = angie_load_firmware_and_renumerate(angie_handle,
2175 ANGIE_FIRMWARE_FILE, ANGIE_RENUMERATION_DELAY_US);
2176 if (ret != ERROR_OK) {
2177 LOG_ERROR("Could not download firmware and re-numerate ANGIE");
2178 angie_quit();
2179 return ret;
2180 }
2181 ret = angie_load_bitstream(angie_handle, ANGIE_BITSTREAM_FILE);
2182 if (ret != ERROR_OK) {
2183 LOG_ERROR("Could not download bitstream");
2184 angie_quit();
2185 return ret;
2186 }
2187 } else {
2188 LOG_INFO("ANGIE device is already running ANGIE firmware");
2189 }
2190
2191 /* Get ANGIE USB IN/OUT endpoints and claim the interface */
2192 ret = jtag_libusb_choose_interface(angie_handle->usb_device_handle,
2193 &angie_handle->ep_in, &angie_handle->ep_out, -1, -1, -1, -1);
2194 if (ret != ERROR_OK) {
2195 angie_quit();
2196 return ret;
2197 }
2198
2199 /* Initialize ANGIE command queue */
2200 angie_clear_queue(angie_handle);
2201
2202 /* Issue one test command with short timeout */
2203 ret = angie_append_test_cmd(angie_handle);
2204 if (ret != ERROR_OK) {
2205 angie_quit();
2206 return ret;
2207 }
2208
2209 ret = angie_execute_queued_commands(angie_handle, 200);
2210 if (ret != ERROR_OK) {
2211 /* Sending test command failed. The ANGIE device may be forever waiting for
2212 * the host to fetch an USB Bulk IN packet (e. g. OpenOCD crashed or was
2213 * shut down by the user via Ctrl-C. Try to retrieve this Bulk IN packet. */
2214
2215 ret = jtag_libusb_bulk_write(angie_handle->usb_device_handle, angie_handle->ep_in,
2216 dummy, 64, 200, &transferred);
2217
2218 if (ret != ERROR_OK || transferred == 0) {
2219 /* Bulk IN transfer failed -> unrecoverable error condition */
2220 LOG_ERROR("Cannot communicate with ANGIE device. Disconnect ANGIE from "
2221 "the USB port and re-connect, then re-run OpenOCD");
2222 angie_quit();
2223 return ERROR_FAIL;
2224 }
2225 /* Successfully received Bulk IN packet -> continue */
2226 LOG_INFO("Recovered from lost Bulk IN packet");
2227 }
2228
2229 angie_clear_queue(angie_handle);
2230
2231 ret = angie_append_get_signals_cmd(angie_handle);
2232 if (ret != ERROR_OK) {
2233 angie_quit();
2234 return ret;
2235 }
2236
2237 ret = angie_execute_queued_commands(angie_handle, 200);
2238 if (ret != ERROR_OK) {
2239 angie_quit();
2240 return ret;
2241 }
2242
2243 /* Post-process the single CMD_GET_SIGNALS command */
2244 input_signals = angie_handle->queue_start->payload_in[0];
2245 output_signals = angie_handle->queue_start->payload_in[1];
2246 angie_dump_signal_states(input_signals, output_signals);
2247
2248 angie_clear_queue(angie_handle);
2249
2250 return ERROR_OK;
2251 }
2252
2253 /**
2254 * Closes the USB handle for the ANGIE device.
2255 *
2256 * @return on success: ERROR_OK
2257 * @return on failure: ERROR_FAIL
2258 */
2259 static int angie_quit(void)
2260 {
2261 int ret = angie_usb_close(angie_handle);
2262 free(angie_handle);
2263 angie_handle = NULL;
2264
2265 return ret;
2266 }
2267
2268 static struct jtag_interface angie_interface = {
2269 .execute_queue = angie_execute_queue,
2270 };
2271
2272 struct adapter_driver angie_adapter_driver = {
2273 .name = "angie",
2274 .transports = jtag_only,
2275
2276 .init = angie_init,
2277 .quit = angie_quit,
2278 .reset = angie_reset,
2279 .speed = angie_speed,
2280 .khz = angie_khz,
2281 .speed_div = angie_speed_div,
2282
2283 .jtag_ops = &angie_interface,
2284 };

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)