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

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)