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

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)