1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
4 Contents : OpenOCD driver code for NanoXplore USB-JTAG ANGIE *
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 ***************************************************************************/
19 #include "helper/system.h"
20 #include <jtag/interface.h>
21 #include <jtag/commands.h>
22 #include <target/image.h>
24 #include "libusb_helper.h"
25 #include "angie/include/msgtypes.h"
27 /** USB Vendor ID of ANGIE device in unconfigured state (no firmware loaded
28 * yet) or with its firmware. */
29 #define ANGIE_VID 0x584e
31 /** USB Product ID of ANGIE device in unconfigured state (no firmware loaded
32 * yet) or with its firmware. */
33 #define ANGIE_PID 0x424e
34 #define ANGIE_PID_2 0x4255
35 #define ANGIE_PID_3 0x4355
36 #define ANGIE_PID_4 0x4a55
38 /** Address of EZ-USB ANGIE CPU Control & Status register. This register can be
39 * written by issuing a Control EP0 vendor request. */
40 #define CPUCS_REG 0xE600
42 /** USB Control EP0 bRequest: "Firmware Load". */
43 #define REQUEST_FIRMWARE_LOAD 0xA0
45 /** Value to write into CPUCS to put EZ-USB ANGIE into reset. */
46 #define CPU_RESET 0x01
48 /** Value to write into CPUCS to put EZ-USB ANGIE out of reset. */
49 #define CPU_START 0x00
51 /** Base address of firmware in EZ-USB ANGIE code space. */
52 #define FIRMWARE_ADDR 0x0000
54 /** USB interface number */
55 #define USB_INTERFACE 0
57 /** Delay (in microseconds) to wait while EZ-USB performs ReNumeration. */
58 #define ANGIE_RENUMERATION_DELAY_US 1500000
60 /** Default location of ANGIE firmware image. */
61 #define ANGIE_FIRMWARE_FILE PKGDATADIR "/angie/angie_firmware.bin"
63 /** Default location of ANGIE firmware image. */
64 #define ANGIE_BITSTREAM_FILE PKGDATADIR "/angie/angie_bitstream.bit"
66 /** Maximum size of a single firmware section. Entire EZ-USB ANGIE code space = 16kB */
67 #define SECTION_BUFFERSIZE 16384
69 /** Tuning of OpenOCD SCAN commands split into multiple ANGIE commands. */
70 #define SPLIT_SCAN_THRESHOLD 10
72 /** ANGIE hardware type */
77 enum angie_payload_direction
{
78 PAYLOAD_DIRECTION_OUT
,
82 enum angie_delay_type
{
91 * ANGIE command (ANGIE command queue element).
93 * For the OUT direction payload, things are quite easy: Payload is stored
94 * in a rather small array (up to 63 bytes), the payload is always allocated
95 * by the function generating the command and freed by angie_clear_queue().
97 * For the IN direction payload, things get a little bit more complicated:
98 * The maximum IN payload size for a single command is 64 bytes. Assume that
99 * a single OpenOCD command needs to scan 256 bytes. This results in the
100 * generation of four ANGIE commands. The function generating these
101 * commands shall allocate an uint8_t[256] array. Each command's #payload_in
102 * pointer shall point to the corresponding offset where IN data shall be
103 * placed, while #payload_in_start shall point to the first element of the 256
105 * - first command: #payload_in_start + 0
106 * - second command: #payload_in_start + 64
107 * - third command: #payload_in_start + 128
108 * - fourth command: #payload_in_start + 192
110 * The last command sets #needs_postprocessing to true.
113 uint8_t id
; /**< ANGIE command ID */
115 uint8_t *payload_out
; /**< Pointer where OUT payload shall be stored */
116 uint8_t payload_out_size
; /**< OUT direction payload size for this command */
118 uint8_t *payload_in_start
; /**< Pointer to first element of IN payload array */
119 uint8_t *payload_in
; /**< Pointer where IN payload shall be stored */
120 uint8_t payload_in_size
; /**< IN direction payload size for this command */
122 /** Indicates if this command needs post-processing */
123 bool needs_postprocessing
;
125 /** Indicates if angie_clear_queue() should free payload_in_start */
126 bool free_payload_in_start
;
128 /** Pointer to corresponding OpenOCD command for post-processing */
129 struct jtag_command
*cmd_origin
;
131 struct angie_cmd
*next
; /**< Pointer to next command (linked list) */
134 /** Describes one driver instance */
136 struct libusb_context
*libusb_ctx
;
137 struct libusb_device_handle
*usb_device_handle
;
138 enum angie_type type
;
140 unsigned int ep_in
; /**< IN endpoint number */
141 unsigned int ep_out
; /**< OUT endpoint number */
143 /* delay value for "SLOW_CLOCK commands" in [0:255] range in units of 4 us;
144 -1 means no need for delay */
145 int delay_scan_in
; /**< Delay value for SCAN_IN commands */
146 int delay_scan_out
; /**< Delay value for SCAN_OUT commands */
147 int delay_scan_io
; /**< Delay value for SCAN_IO commands */
148 int delay_clock_tck
; /**< Delay value for CLOCK_TMS commands */
149 int delay_clock_tms
; /**< Delay value for CLOCK_TCK commands */
151 int commands_in_queue
; /**< Number of commands in queue */
152 struct angie_cmd
*queue_start
; /**< Pointer to first command in queue */
153 struct angie_cmd
*queue_end
; /**< Pointer to last command in queue */
156 /**************************** Function Prototypes *****************************/
158 /* USB helper functions */
159 static int angie_usb_open(struct angie
*device
);
160 static int angie_usb_close(struct angie
*device
);
162 /* ANGIE MCU (Cypress EZ-USB) specific functions */
163 static int angie_cpu_reset(struct angie
*device
, char reset_bit
);
164 static int angie_load_firmware_and_renumerate(struct angie
*device
, const char *filename
,
166 static int angie_load_firmware(struct angie
*device
, const char *filename
);
167 static int angie_load_bitstream(struct angie
*device
, const char *filename
);
169 static int angie_write_firmware_section(struct angie
*device
,
170 struct image
*firmware_image
, int section_index
);
172 /* Generic helper functions */
173 static void angie_dump_signal_states(uint8_t input_signals
, uint8_t output_signals
);
175 /* ANGIE command generation helper functions */
176 static int angie_allocate_payload(struct angie_cmd
*angie_cmd
, int size
,
177 enum angie_payload_direction direction
);
179 /* ANGIE command queue helper functions */
180 static int angie_get_queue_size(struct angie
*device
,
181 enum angie_payload_direction direction
);
182 static void angie_clear_queue(struct angie
*device
);
183 static int angie_append_queue(struct angie
*device
, struct angie_cmd
*angie_cmd
);
184 static int angie_execute_queued_commands(struct angie
*device
, int timeout_ms
);
186 static void angie_dump_queue(struct angie
*device
);
188 static int angie_append_scan_cmd(struct angie
*device
,
189 enum scan_type scan_type
,
194 uint8_t tms_count_start
,
195 uint8_t tms_sequence_start
,
196 uint8_t tms_count_end
,
197 uint8_t tms_sequence_end
,
198 struct jtag_command
*origin
,
200 static int angie_append_clock_tms_cmd(struct angie
*device
, uint8_t count
,
202 static int angie_append_clock_tck_cmd(struct angie
*device
, uint16_t count
);
203 static int angie_append_get_signals_cmd(struct angie
*device
);
204 static int angie_append_set_signals_cmd(struct angie
*device
, uint8_t low
,
206 static int angie_append_sleep_cmd(struct angie
*device
, uint32_t us
);
207 static int angie_append_configure_tck_cmd(struct angie
*device
,
213 static int angie_append_test_cmd(struct angie
*device
);
215 /* ANGIE TCK frequency helper functions */
216 static int angie_calculate_delay(enum angie_delay_type type
, long f
, int *delay
);
218 /* Interface between ANGIE and OpenOCD */
219 static void angie_set_end_state(tap_state_t endstate
);
220 static int angie_queue_statemove(struct angie
*device
);
222 static int angie_queue_scan(struct angie
*device
, struct jtag_command
*cmd
);
223 static int angie_queue_tlr_reset(struct angie
*device
, struct jtag_command
*cmd
);
224 static int angie_queue_runtest(struct angie
*device
, struct jtag_command
*cmd
);
225 static int angie_queue_pathmove(struct angie
*device
, struct jtag_command
*cmd
);
226 static int angie_queue_sleep(struct angie
*device
, struct jtag_command
*cmd
);
227 static int angie_queue_stableclocks(struct angie
*device
, struct jtag_command
*cmd
);
229 static int angie_post_process_scan(struct angie_cmd
*angie_cmd
);
230 static int angie_post_process_queue(struct angie
*device
);
232 /* adapter driver functions */
233 static int angie_execute_queue(void);
234 static int angie_khz(int khz
, int *jtag_speed
);
235 static int angie_speed(int speed
);
236 static int angie_speed_div(int speed
, int *khz
);
237 static int angie_init(void);
238 static int angie_quit(void);
239 static int angie_reset(int trst
, int srst
);
241 /****************************** Global Variables ******************************/
243 static struct angie
*angie_handle
;
245 /**************************** USB helper functions ****************************/
248 * Opens the ANGIE device
250 * @param device pointer to struct angie identifying ANGIE driver instance.
251 * @return on success: ERROR_OK
252 * @return on failure: ERROR_FAIL
254 static int angie_usb_open(struct angie
*device
)
256 struct libusb_device_handle
*usb_device_handle
;
257 const uint16_t vids
[] = {ANGIE_VID
, ANGIE_VID
, ANGIE_VID
, ANGIE_VID
, 0};
258 const uint16_t pids
[] = {ANGIE_PID
, ANGIE_PID_2
, ANGIE_PID_3
, ANGIE_PID_4
, 0};
260 int ret
= jtag_libusb_open(vids
, pids
, NULL
, &usb_device_handle
, NULL
);
265 device
->usb_device_handle
= usb_device_handle
;
266 device
->type
= ANGIE
;
272 * Releases the ANGIE interface and closes the USB device handle.
274 * @param device pointer to struct angie identifying ANGIE driver instance.
275 * @return on success: ERROR_OK
276 * @return on failure: ERROR_FAIL
278 static int angie_usb_close(struct angie
*device
)
280 if (device
->usb_device_handle
) {
281 if (libusb_release_interface(device
->usb_device_handle
, 0) != 0)
284 jtag_libusb_close(device
->usb_device_handle
);
285 device
->usb_device_handle
= NULL
;
290 /******************* ANGIE CPU (EZ-USB) specific functions ********************/
293 * Writes '0' or '1' to the CPUCS register, putting the EZ-USB CPU into reset
296 * @param device pointer to struct angie identifying ANGIE driver instance.
297 * @param reset_bit 0 to put CPU into reset, 1 to put CPU out of reset.
298 * @return on success: ERROR_OK
299 * @return on failure: ERROR_FAIL
301 static int angie_cpu_reset(struct angie
*device
, char reset_bit
)
303 return jtag_libusb_control_transfer(device
->usb_device_handle
,
304 (LIBUSB_ENDPOINT_OUT
| LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
),
305 REQUEST_FIRMWARE_LOAD
, CPUCS_REG
, 0, &reset_bit
, 1, LIBUSB_TIMEOUT_MS
, NULL
);
309 * Puts the ANGIE's EZ-USB microcontroller into reset state, downloads
310 * the firmware image, resumes the microcontroller and re-enumerates
313 * @param device pointer to struct angie identifying ANGIE driver instance.
314 * The usb_handle member will be modified during re-enumeration.
315 * @param filename path to the Intel HEX file containing the firmware image.
316 * @param delay_us the delay to wait for the device to re-enumerate.
317 * @return on success: ERROR_OK
318 * @return on failure: ERROR_FAIL
320 static int angie_load_firmware_and_renumerate(struct angie
*device
,
321 const char *filename
, uint32_t delay_us
)
325 /* Basic process: After downloading the firmware, the ANGIE will disconnect
326 * itself and re-connect after a short amount of time so we have to close
327 * the handle and re-enumerate USB devices */
329 ret
= angie_load_firmware(device
, filename
);
333 ret
= angie_usb_close(device
);
339 return angie_usb_open(device
);
343 * Downloads a firmware image to the ANGIE's EZ-USB microcontroller
346 * @param device pointer to struct angie identifying ANGIE driver instance.
347 * @param filename an absolute or relative path to the Intel HEX file
348 * containing the firmware image.
349 * @return on success: ERROR_OK
350 * @return on failure: ERROR_FAIL
352 static int angie_load_firmware(struct angie
*device
, const char *filename
)
354 struct image angie_firmware_image
;
357 ret
= angie_cpu_reset(device
, CPU_RESET
);
358 if (ret
!= ERROR_OK
) {
359 LOG_ERROR("Could not halt ANGIE CPU");
363 angie_firmware_image
.base_address
= 0;
364 angie_firmware_image
.base_address_set
= false;
366 ret
= image_open(&angie_firmware_image
, filename
, "bin");
367 if (ret
!= ERROR_OK
) {
368 LOG_ERROR("Could not load firmware image");
372 /* Download all sections in the image to ANGIE */
373 for (unsigned int i
= 0; i
< angie_firmware_image
.num_sections
; i
++) {
374 ret
= angie_write_firmware_section(device
, &angie_firmware_image
, i
);
379 image_close(&angie_firmware_image
);
381 ret
= angie_cpu_reset(device
, CPU_START
);
382 if (ret
!= ERROR_OK
) {
383 LOG_ERROR("Could not restart ANGIE CPU");
391 * Downloads a bitstream file to the ANGIE's FPGA through the EZ-USB microcontroller
394 * @param device pointer to struct angie identifying ANGIE driver instance.
395 * @param filename an absolute or relative path to the Xilinx .bit file
396 * containing the bitstream data.
397 * @return on success: ERROR_OK
398 * @return on failure: ERROR_FAIL
400 static int angie_load_bitstream(struct angie
*device
, const char *filename
)
402 int ret
, transferred
;
403 const char *bitstream_file_path
= filename
;
404 FILE *bitstream_file
= NULL
;
405 char *bitstream_data
= NULL
;
406 size_t bitstream_size
= 0;
409 ret
= jtag_libusb_control_transfer(device
->usb_device_handle
,
410 0x00, 0xB0, 0, 0, NULL
, 0, LIBUSB_TIMEOUT_MS
, &transferred
);
411 if (ret
!= ERROR_OK
) {
412 LOG_ERROR("Failed opencfg");
413 /* Abort if libusb sent less data than requested */
417 /* Open the bitstream file */
418 bitstream_file
= fopen(bitstream_file_path
, "rb");
419 if (!bitstream_file
) {
420 LOG_ERROR("Failed to open bitstream file: %s\n", bitstream_file_path
);
424 /* Get the size of the bitstream file */
425 fseek(bitstream_file
, 0, SEEK_END
);
426 bitstream_size
= ftell(bitstream_file
);
427 fseek(bitstream_file
, 0, SEEK_SET
);
429 /* Allocate memory for the bitstream data */
430 bitstream_data
= malloc(bitstream_size
);
431 if (!bitstream_data
) {
432 LOG_ERROR("Failed to allocate memory for bitstream data.");
433 fclose(bitstream_file
);
437 /* Read the bitstream data from the file */
438 if (fread(bitstream_data
, 1, bitstream_size
, bitstream_file
) != bitstream_size
) {
439 LOG_ERROR("Failed to read bitstream data.");
440 free(bitstream_data
);
441 fclose(bitstream_file
);
445 /* Send the bitstream data to the microcontroller */
446 int actual_length
= 0;
447 ret
= jtag_libusb_bulk_write(device
->usb_device_handle
, 0x02, bitstream_data
, bitstream_size
, 1000, &actual_length
);
448 if (ret
!= ERROR_OK
) {
449 LOG_ERROR("Failed to send bitstream data: %s", libusb_strerror(ret
));
450 free(bitstream_data
);
451 fclose(bitstream_file
);
455 LOG_INFO("Bitstream sent successfully.");
458 free(bitstream_data
);
459 fclose(bitstream_file
);
463 ret
= jtag_libusb_control_transfer(device
->usb_device_handle
,
464 0x00, 0xB1, 0, 0, NULL
, 0, LIBUSB_TIMEOUT_MS
, &transferred
);
465 if (ret
!= ERROR_OK
) {
466 LOG_INFO("error cfgclose");
467 /* Abort if libusb sent less data than requested */
474 * Send one contiguous firmware section to the ANGIE's EZ-USB microcontroller
477 * @param device pointer to struct angie identifying ANGIE driver instance.
478 * @param firmware_image pointer to the firmware image that contains the section
479 * which should be sent to the ANGIE's EZ-USB microcontroller.
480 * @param section_index index of the section within the firmware image.
481 * @return on success: ERROR_OK
482 * @return on failure: ERROR_FAIL
484 static int angie_write_firmware_section(struct angie
*device
,
485 struct image
*firmware_image
, int section_index
)
487 int addr
, bytes_remaining
, chunk_size
;
488 uint8_t data
[SECTION_BUFFERSIZE
];
489 uint8_t *data_ptr
= data
;
492 int ret
, transferred
;
494 size
= (uint16_t)firmware_image
->sections
[section_index
].size
;
495 addr
= (uint16_t)firmware_image
->sections
[section_index
].base_address
;
497 LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04" PRIx16
")", section_index
, addr
,
500 /* Copy section contents to local buffer */
501 ret
= image_read_section(firmware_image
, section_index
, 0, size
, data
,
506 if (size_read
!= size
)
509 bytes_remaining
= size
;
511 /* Send section data in chunks of up to 64 bytes to ANGIE */
512 while (bytes_remaining
> 0) {
513 if (bytes_remaining
> 64)
516 chunk_size
= bytes_remaining
;
518 ret
= jtag_libusb_control_transfer(device
->usb_device_handle
,
519 (LIBUSB_ENDPOINT_OUT
| LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
),
520 REQUEST_FIRMWARE_LOAD
, addr
, FIRMWARE_ADDR
, (char *)data_ptr
,
521 chunk_size
, LIBUSB_TIMEOUT_MS
, &transferred
);
526 if (transferred
!= chunk_size
) {
527 /* Abort if libusb sent less data than requested */
531 bytes_remaining
-= chunk_size
;
533 data_ptr
+= chunk_size
;
539 /************************** Generic helper functions **************************/
542 * Print state of interesting signals via LOG_INFO().
544 * @param input_signals input signal states as returned by CMD_GET_SIGNALS
545 * @param output_signals output signal states as returned by CMD_GET_SIGNALS
547 static void angie_dump_signal_states(uint8_t input_signals
, uint8_t output_signals
)
549 LOG_INFO("ANGIE signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i "
551 (output_signals
& SIGNAL_TDI
? 1 : 0),
552 (input_signals
& SIGNAL_TDO
? 1 : 0),
553 (output_signals
& SIGNAL_TMS
? 1 : 0),
554 (output_signals
& SIGNAL_TCK
? 1 : 0),
555 (output_signals
& SIGNAL_TRST
? 1 : 0),
556 (output_signals
& SIGNAL_SRST
? 1 : 0));
559 /**************** ANGIE command generation helper functions ***************/
562 * Allocate and initialize space in memory for ANGIE command payload.
564 * @param angie_cmd pointer to command whose payload should be allocated.
565 * @param size the amount of memory to allocate (bytes).
566 * @param direction which payload to allocate.
567 * @return on success: ERROR_OK
568 * @return on failure: ERROR_FAIL
570 static int angie_allocate_payload(struct angie_cmd
*angie_cmd
, int size
,
571 enum angie_payload_direction direction
)
575 payload
= calloc(size
, sizeof(uint8_t));
578 LOG_ERROR("Could not allocate ANGIE command payload: out of memory");
583 case PAYLOAD_DIRECTION_OUT
:
584 if (angie_cmd
->payload_out
) {
585 LOG_ERROR("BUG: Duplicate payload allocation for ANGIE command");
589 angie_cmd
->payload_out
= payload
;
590 angie_cmd
->payload_out_size
= size
;
592 case PAYLOAD_DIRECTION_IN
:
593 if (angie_cmd
->payload_in_start
) {
594 LOG_ERROR("BUG: Duplicate payload allocation for ANGIE command");
599 angie_cmd
->payload_in_start
= payload
;
600 angie_cmd
->payload_in
= payload
;
601 angie_cmd
->payload_in_size
= size
;
603 /* By default, free payload_in_start in angie_clear_queue(). Commands
604 * that do not want this behavior (e. g. split scans) must turn it off
606 angie_cmd
->free_payload_in_start
= true;
614 /****************** ANGIE command queue helper functions ******************/
617 * Get the current number of bytes in the queue, including command IDs.
619 * @param device pointer to struct angie identifying ANGIE driver instance.
620 * @param direction the transfer direction for which to get byte count.
621 * @return the number of bytes currently stored in the queue for the specified
624 static int angie_get_queue_size(struct angie
*device
,
625 enum angie_payload_direction direction
)
627 struct angie_cmd
*current
= device
->queue_start
;
632 case PAYLOAD_DIRECTION_OUT
:
633 sum
+= current
->payload_out_size
+ 1; /* + 1 byte for Command ID */
635 case PAYLOAD_DIRECTION_IN
:
636 sum
+= current
->payload_in_size
;
640 current
= current
->next
;
647 * Clear the ANGIE command queue.
649 * @param device pointer to struct angie identifying ANGIE driver instance.
651 static void angie_clear_queue(struct angie
*device
)
653 struct angie_cmd
*current
= device
->queue_start
;
654 struct angie_cmd
*next
= NULL
;
657 /* Save pointer to next element */
658 next
= current
->next
;
660 /* Free payloads: OUT payload can be freed immediately */
661 free(current
->payload_out
);
662 current
->payload_out
= NULL
;
664 /* IN payload MUST be freed ONLY if no other commands use the
665 * payload_in_start buffer */
666 if (current
->free_payload_in_start
) {
667 free(current
->payload_in_start
);
668 current
->payload_in_start
= NULL
;
669 current
->payload_in
= NULL
;
672 /* Free queue element */
675 /* Proceed with next element */
679 device
->commands_in_queue
= 0;
680 device
->queue_start
= NULL
;
681 device
->queue_end
= NULL
;
685 * Add a command to the ANGIE command queue.
687 * @param device pointer to struct angie identifying ANGIE driver instance.
688 * @param angie_cmd pointer to command that shall be appended to the ANGIE
690 * @return on success: ERROR_OK
691 * @return on failure: ERROR_FAIL
693 static int angie_append_queue(struct angie
*device
, struct angie_cmd
*angie_cmd
)
695 int newsize_out
, newsize_in
;
698 newsize_out
= angie_get_queue_size(device
, PAYLOAD_DIRECTION_OUT
) + 1
699 + angie_cmd
->payload_out_size
;
701 newsize_in
= angie_get_queue_size(device
, PAYLOAD_DIRECTION_IN
)
702 + angie_cmd
->payload_in_size
;
704 /* Check if the current command can be appended to the queue */
705 if (newsize_out
> 64 || newsize_in
> 64) {
706 /* New command does not fit. Execute all commands in queue before starting
707 * new queue with the current command as first entry. */
708 ret
= angie_execute_queued_commands(device
, LIBUSB_TIMEOUT_MS
);
711 ret
= angie_post_process_queue(device
);
714 angie_clear_queue(device
);
717 if (!device
->queue_start
) {
718 /* Queue was empty */
719 device
->commands_in_queue
= 1;
721 device
->queue_start
= angie_cmd
;
722 device
->queue_end
= angie_cmd
;
724 /* There are already commands in the queue */
725 device
->commands_in_queue
++;
727 device
->queue_end
->next
= angie_cmd
;
728 device
->queue_end
= angie_cmd
;
732 angie_clear_queue(device
);
738 * Sends all queued ANGIE commands to the ANGIE for execution.
740 * @param device pointer to struct angie identifying ANGIE driver instance.
742 * @return on success: ERROR_OK
743 * @return on failure: ERROR_FAIL
745 static int angie_execute_queued_commands(struct angie
*device
, int timeout_ms
)
747 struct angie_cmd
*current
;
748 int ret
, i
, index_out
, index_in
, count_out
, count_in
, transferred
;
751 if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO
))
752 angie_dump_queue(device
);
758 for (current
= device
->queue_start
; current
; current
= current
->next
) {
759 /* Add command to packet */
760 buffer
[index_out
] = current
->id
;
764 for (i
= 0; i
< current
->payload_out_size
; i
++)
765 buffer
[index_out
+ i
] = current
->payload_out
[i
];
766 index_out
+= current
->payload_out_size
;
767 count_in
+= current
->payload_in_size
;
768 count_out
+= current
->payload_out_size
;
771 /* Send packet to ANGIE */
772 ret
= jtag_libusb_bulk_write(device
->usb_device_handle
, device
->ep_out
,
773 (char *)buffer
, count_out
, timeout_ms
, &transferred
);
776 if (transferred
!= count_out
)
779 /* Wait for response if commands contain IN payload data */
781 ret
= jtag_libusb_bulk_write(device
->usb_device_handle
, device
->ep_in
,
782 (char *)buffer
, count_in
, timeout_ms
, &transferred
);
785 if (transferred
!= count_in
)
788 /* Write back IN payload data */
790 for (current
= device
->queue_start
; current
; current
= current
->next
) {
791 for (i
= 0; i
< current
->payload_in_size
; i
++) {
792 current
->payload_in
[i
] = buffer
[index_in
];
801 * Convert an ANGIE command ID (\a id) to a human-readable string.
803 * @param id the ANGIE command ID.
804 * @return the corresponding human-readable string.
806 static const char *angie_cmd_id_string(uint8_t id
)
810 return "CMD_SCAN_IN";
811 case CMD_SLOW_SCAN_IN
:
812 return "CMD_SLOW_SCAN_IN";
814 return "CMD_SCAN_OUT";
815 case CMD_SLOW_SCAN_OUT
:
816 return "CMD_SLOW_SCAN_OUT";
818 return "CMD_SCAN_IO";
819 case CMD_SLOW_SCAN_IO
:
820 return "CMD_SLOW_SCAN_IO";
822 return "CMD_CLOCK_TMS";
823 case CMD_SLOW_CLOCK_TMS
:
824 return "CMD_SLOW_CLOCK_TMS";
826 return "CMD_CLOCK_TCK";
827 case CMD_SLOW_CLOCK_TCK
:
828 return "CMD_SLOW_CLOCK_TCK";
830 return "CMD_SLEEP_US";
832 return "CMD_SLEEP_MS";
833 case CMD_GET_SIGNALS
:
834 return "CMD_GET_SIGNALS";
835 case CMD_SET_SIGNALS
:
836 return "CMD_SET_SIGNALS";
837 case CMD_CONFIGURE_TCK_FREQ
:
838 return "CMD_CONFIGURE_TCK_FREQ";
840 return "CMD_SET_LEDS";
844 return "CMD_UNKNOWN";
849 * Print one ANGIE command to stdout.
851 * @param angie_cmd pointer to ANGIE command.
853 static void angie_dump_command(struct angie_cmd
*angie_cmd
)
856 for (int i
= 0; i
< angie_cmd
->payload_out_size
; i
++)
857 sprintf(hex
+ 3 * i
, "%02" PRIX8
" ", angie_cmd
->payload_out
[i
]);
859 hex
[3 * angie_cmd
->payload_out_size
- 1] = 0;
860 LOG_DEBUG_IO(" %-22s | OUT size = %" PRIi8
", bytes = %s",
861 angie_cmd_id_string(angie_cmd
->id
), angie_cmd
->payload_out_size
, hex
);
863 LOG_DEBUG_IO("\n | IN size = %" PRIi8
"\n", angie_cmd
->payload_in_size
);
867 * Print the ANGIE command queue to stdout.
869 * @param device pointer to struct angie identifying ANGIE driver instance.
871 static void angie_dump_queue(struct angie
*device
)
873 struct angie_cmd
*current
;
875 LOG_DEBUG_IO("ANGIE command queue:\n");
877 for (current
= device
->queue_start
; current
; current
= current
->next
)
878 angie_dump_command(current
);
884 * Creates and appends a JTAG scan command to the ANGIE command queue.
885 * A JTAG scan consists of three steps:
886 * - Move to the desired SHIFT state, depending on scan type (IR/DR scan).
887 * - Shift TDI data into the JTAG chain, optionally reading the TDO pin.
888 * - Move to the desired end state.
890 * @param device pointer to struct angie identifying ANGIE driver instance.
891 * @param scan_type the type of the scan (IN, OUT, IO (bidirectional)).
892 * @param scan_size_bits number of bits to shift into the JTAG chain.
893 * @param tdi pointer to array containing TDI data.
894 * @param tdo_start pointer to first element of array where TDO data shall be
895 * stored. See #angie_cmd for details.
896 * @param tdo pointer to array where TDO data shall be stored
897 * @param tms_count_start number of TMS state transitions to perform BEFORE
898 * shifting data into the JTAG chain.
899 * @param tms_sequence_start sequence of TMS state transitions that will be
900 * performed BEFORE shifting data into the JTAG chain.
901 * @param tms_count_end number of TMS state transitions to perform AFTER
902 * shifting data into the JTAG chain.
903 * @param tms_sequence_end sequence of TMS state transitions that will be
904 * performed AFTER shifting data into the JTAG chain.
905 * @param origin pointer to OpenOCD command that generated this scan command.
906 * @param postprocess whether this command needs to be post-processed after
908 * @return on success: ERROR_OK
909 * @return on failure: ERROR_FAIL
911 static int angie_append_scan_cmd(struct angie
*device
, enum scan_type scan_type
,
912 int scan_size_bits
, uint8_t *tdi
, uint8_t *tdo_start
, uint8_t *tdo
,
913 uint8_t tms_count_start
, uint8_t tms_sequence_start
, uint8_t tms_count_end
,
914 uint8_t tms_sequence_end
, struct jtag_command
*origin
, bool postprocess
)
916 struct angie_cmd
*cmd
= calloc(1, sizeof(struct angie_cmd
));
917 int ret
, i
, scan_size_bytes
;
918 uint8_t bits_last_byte
;
923 /* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID,
924 * 5 bytes are setup data -> 58 remaining payload bytes for TDI data */
925 if (scan_size_bits
> (58 * 8)) {
926 LOG_ERROR("BUG: Tried to create CMD_SCAN_IO ANGIE command with too"
932 scan_size_bytes
= DIV_ROUND_UP(scan_size_bits
, 8);
934 bits_last_byte
= scan_size_bits
% 8;
935 if (bits_last_byte
== 0)
938 /* Allocate out_payload depending on scan type */
941 if (device
->delay_scan_in
< 0)
942 cmd
->id
= CMD_SCAN_IN
;
944 cmd
->id
= CMD_SLOW_SCAN_IN
;
945 ret
= angie_allocate_payload(cmd
, 5, PAYLOAD_DIRECTION_IN
);
948 if (device
->delay_scan_out
< 0)
949 cmd
->id
= CMD_SCAN_OUT
;
951 cmd
->id
= CMD_SLOW_SCAN_OUT
;
952 ret
= angie_allocate_payload(cmd
, scan_size_bytes
+ 5, PAYLOAD_DIRECTION_OUT
);
955 if (device
->delay_scan_io
< 0)
956 cmd
->id
= CMD_SCAN_IO
;
958 cmd
->id
= CMD_SLOW_SCAN_IO
;
959 ret
= angie_allocate_payload(cmd
, scan_size_bytes
+ 5, PAYLOAD_DIRECTION_OUT
);
962 LOG_ERROR("BUG: 'append scan cmd' encountered an unknown scan type");
967 if (ret
!= ERROR_OK
) {
972 /* Build payload_out that is common to all scan types */
973 cmd
->payload_out
[0] = scan_size_bytes
& 0xFF;
974 cmd
->payload_out
[1] = bits_last_byte
& 0xFF;
975 cmd
->payload_out
[2] = ((tms_count_start
& 0x0F) << 4) | (tms_count_end
& 0x0F);
976 cmd
->payload_out
[3] = tms_sequence_start
;
977 cmd
->payload_out
[4] = tms_sequence_end
;
979 /* Setup payload_out for types with OUT transfer */
980 if (scan_type
== SCAN_OUT
|| scan_type
== SCAN_IO
) {
981 for (i
= 0; i
< scan_size_bytes
; i
++)
982 cmd
->payload_out
[i
+ 5] = tdi
[i
];
985 /* Setup payload_in pointers for types with IN transfer */
986 if (scan_type
== SCAN_IN
|| scan_type
== SCAN_IO
) {
987 cmd
->payload_in_start
= tdo_start
;
988 cmd
->payload_in
= tdo
;
989 cmd
->payload_in_size
= scan_size_bytes
;
992 cmd
->needs_postprocessing
= postprocess
;
993 cmd
->cmd_origin
= origin
;
995 /* For scan commands, we free payload_in_start only when the command is
996 * the last in a series of split commands or a stand-alone command */
997 cmd
->free_payload_in_start
= postprocess
;
999 return angie_append_queue(device
, cmd
);
1003 * Perform TAP state transitions
1005 * @param device pointer to struct angie identifying ANGIE driver instance.
1006 * @param count defines the number of TCK clock cycles generated (up to 8).
1007 * @param sequence defines the TMS pin levels for each state transition. The
1008 * Least-Significant Bit is read first.
1009 * @return on success: ERROR_OK
1010 * @return on failure: ERROR_FAIL
1012 static int angie_append_clock_tms_cmd(struct angie
*device
, uint8_t count
,
1015 struct angie_cmd
*cmd
= calloc(1, sizeof(struct angie_cmd
));
1019 LOG_ERROR("Out of memory");
1023 if (device
->delay_clock_tms
< 0)
1024 cmd
->id
= CMD_CLOCK_TMS
;
1026 cmd
->id
= CMD_SLOW_CLOCK_TMS
;
1028 /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
1029 ret
= angie_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_OUT
);
1030 if (ret
!= ERROR_OK
) {
1035 cmd
->payload_out
[0] = count
;
1036 cmd
->payload_out
[1] = sequence
;
1038 return angie_append_queue(device
, cmd
);
1042 * Generate a defined amount of TCK clock cycles
1044 * All other JTAG signals are left unchanged.
1046 * @param device pointer to struct angie identifying ANGIE driver instance.
1047 * @param count the number of TCK clock cycles to generate.
1048 * @return on success: ERROR_OK
1049 * @return on failure: ERROR_FAIL
1051 static int angie_append_clock_tck_cmd(struct angie
*device
, uint16_t count
)
1053 struct angie_cmd
*cmd
= calloc(1, sizeof(struct angie_cmd
));
1057 LOG_ERROR("Out of memory");
1061 if (device
->delay_clock_tck
< 0)
1062 cmd
->id
= CMD_CLOCK_TCK
;
1064 cmd
->id
= CMD_SLOW_CLOCK_TCK
;
1066 /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */
1067 ret
= angie_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_OUT
);
1068 if (ret
!= ERROR_OK
) {
1073 cmd
->payload_out
[0] = count
& 0xff;
1074 cmd
->payload_out
[1] = (count
>> 8) & 0xff;
1076 return angie_append_queue(device
, cmd
);
1080 * Read JTAG signals.
1082 * @param device pointer to struct angie identifying ANGIE driver instance.
1083 * @return on success: ERROR_OK
1084 * @return on failure: ERROR_FAIL
1086 static int angie_append_get_signals_cmd(struct angie
*device
)
1088 struct angie_cmd
*cmd
= calloc(1, sizeof(struct angie_cmd
));
1092 LOG_ERROR("Out of memory");
1096 cmd
->id
= CMD_GET_SIGNALS
;
1097 cmd
->needs_postprocessing
= true;
1099 /* CMD_GET_SIGNALS has two IN payload bytes */
1100 ret
= angie_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_IN
);
1102 if (ret
!= ERROR_OK
) {
1107 return angie_append_queue(device
, cmd
);
1111 * Arbitrarily set JTAG output signals.
1113 * @param device pointer to struct angie identifying ANGIE driver instance.
1114 * @param low defines which signals will be de-asserted. Each bit corresponds
1123 * @param high defines which signals will be asserted.
1124 * @return on success: ERROR_OK
1125 * @return on failure: ERROR_FAIL
1127 static int angie_append_set_signals_cmd(struct angie
*device
, uint8_t low
,
1130 struct angie_cmd
*cmd
= calloc(1, sizeof(struct angie_cmd
));
1134 LOG_ERROR("Out of memory");
1138 cmd
->id
= CMD_SET_SIGNALS
;
1140 /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */
1141 ret
= angie_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_OUT
);
1143 if (ret
!= ERROR_OK
) {
1148 cmd
->payload_out
[0] = low
;
1149 cmd
->payload_out
[1] = high
;
1151 return angie_append_queue(device
, cmd
);
1155 * Sleep for a pre-defined number of microseconds
1157 * @param device pointer to struct angie identifying ANGIE driver instance.
1158 * @param us the number microseconds to sleep.
1159 * @return on success: ERROR_OK
1160 * @return on failure: ERROR_FAIL
1162 static int angie_append_sleep_cmd(struct angie
*device
, uint32_t us
)
1164 struct angie_cmd
*cmd
= calloc(1, sizeof(struct angie_cmd
));
1168 LOG_ERROR("Out of memory");
1172 cmd
->id
= CMD_SLEEP_US
;
1174 /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */
1175 ret
= angie_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_OUT
);
1177 if (ret
!= ERROR_OK
) {
1182 cmd
->payload_out
[0] = us
& 0x00ff;
1183 cmd
->payload_out
[1] = (us
>> 8) & 0x00ff;
1185 return angie_append_queue(device
, cmd
);
1189 * Set TCK delay counters
1191 * @param device pointer to struct angie identifying ANGIE driver instance.
1192 * @param delay_scan_in delay count top value in jtag_slow_scan_in() function.
1193 * @param delay_scan_out delay count top value in jtag_slow_scan_out() function.
1194 * @param delay_scan_io delay count top value in jtag_slow_scan_io() function.
1195 * @param delay_tck delay count top value in jtag_clock_tck() function.
1196 * @param delay_tms delay count top value in jtag_slow_clock_tms() function.
1197 * @return on success: ERROR_OK
1198 * @return on failure: ERROR_FAIL
1200 static int angie_append_configure_tck_cmd(struct angie
*device
, int delay_scan_in
,
1201 int delay_scan_out
, int delay_scan_io
, int delay_tck
, int delay_tms
)
1203 struct angie_cmd
*cmd
= calloc(1, sizeof(struct angie_cmd
));
1207 LOG_ERROR("Out of memory");
1211 cmd
->id
= CMD_CONFIGURE_TCK_FREQ
;
1213 /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero
1214 * IN payload bytes */
1215 ret
= angie_allocate_payload(cmd
, 5, PAYLOAD_DIRECTION_OUT
);
1216 if (ret
!= ERROR_OK
) {
1221 if (delay_scan_in
< 0)
1222 cmd
->payload_out
[0] = 0;
1224 cmd
->payload_out
[0] = (uint8_t)delay_scan_in
;
1226 if (delay_scan_out
< 0)
1227 cmd
->payload_out
[1] = 0;
1229 cmd
->payload_out
[1] = (uint8_t)delay_scan_out
;
1231 if (delay_scan_io
< 0)
1232 cmd
->payload_out
[2] = 0;
1234 cmd
->payload_out
[2] = (uint8_t)delay_scan_io
;
1237 cmd
->payload_out
[3] = 0;
1239 cmd
->payload_out
[3] = (uint8_t)delay_tck
;
1242 cmd
->payload_out
[4] = 0;
1244 cmd
->payload_out
[4] = (uint8_t)delay_tms
;
1246 return angie_append_queue(device
, cmd
);
1250 * Test command. Used to check if the ANGIE device is ready to accept new
1253 * @param device pointer to struct angie identifying ANGIE driver instance.
1254 * @return on success: ERROR_OK
1255 * @return on failure: ERROR_FAIL
1257 static int angie_append_test_cmd(struct angie
*device
)
1259 struct angie_cmd
*cmd
= calloc(1, sizeof(struct angie_cmd
));
1263 LOG_ERROR("Out of memory");
1269 /* CMD_TEST has one OUT payload byte and zero IN payload bytes */
1270 ret
= angie_allocate_payload(cmd
, 1, PAYLOAD_DIRECTION_OUT
);
1271 if (ret
!= ERROR_OK
) {
1276 cmd
->payload_out
[0] = 0xAA;
1278 return angie_append_queue(device
, cmd
);
1281 /****************** ANGIE TCK frequency helper functions ******************/
1284 * Calculate delay values for a given TCK frequency.
1286 * The ANGIE firmware uses five different speed values for different
1287 * commands. These speed values are calculated in these functions.
1289 * The five different commands which support variable TCK frequency are
1290 * implemented twice in the firmware:
1291 * 1. Maximum possible frequency without any artificial delay
1292 * 2. Variable frequency with artificial linear delay loop
1294 * To set the ANGIE to maximum frequency, it is only necessary to use the
1295 * corresponding command IDs. To set the ANGIE to a lower frequency, the
1296 * delay loop top values have to be calculated first. Then, a
1297 * CMD_CONFIGURE_TCK_FREQ command needs to be sent to the ANGIE device.
1299 * The delay values are described by linear equations:
1301 * (t = period, k = constant, x = delay value, d = constant)
1303 * Thus, the delay can be calculated as in the following equation:
1306 * The constants in these equations have been determined and validated by
1307 * measuring the frequency resulting from different delay values.
1309 * @param type for which command to calculate the delay value.
1310 * @param f TCK frequency for which to calculate the delay value in Hz.
1311 * @param delay where to store resulting delay value.
1312 * @return on success: ERROR_OK
1313 * @return on failure: ERROR_FAIL
1315 static int angie_calculate_delay(enum angie_delay_type type
, long f
, int *delay
)
1317 float t_us
, x
, x_ceil
;
1319 /* Calculate period of requested TCK frequency */
1320 t_us
= 1000000.0 / f
;
1323 case DELAY_CLOCK_TCK
:
1324 x
= (t_us
- 6.0) / 4;
1326 case DELAY_CLOCK_TMS
:
1327 x
= (t_us
- 8.5) / 4;
1330 x
= (t_us
- 8.8308) / 4;
1332 case DELAY_SCAN_OUT
:
1333 x
= (t_us
- 10.527) / 4;
1336 x
= (t_us
- 13.132) / 4;
1343 /* Check if the delay value is negative. This happens when a frequency is
1344 * requested that is too high for the delay loop implementation. In this
1345 * case, set delay value to zero. */
1349 /* We need to convert the exact delay value to an integer. Therefore, we
1350 * round the exact value UP to ensure that the resulting frequency is NOT
1351 * higher than the requested frequency. */
1354 /* Check if the value is within limits */
1358 *delay
= (int)x_ceil
;
1364 * Calculate frequency for a given delay value.
1366 * Similar to the #angie_calculate_delay function, this function calculates the
1367 * TCK frequency for a given delay value by using linear equations of the form:
1369 * (t = period, k = constant, x = delay value, d = constant)
1371 * @param type for which command to calculate the delay value.
1372 * @param delay value for which to calculate the resulting TCK frequency.
1373 * @return the resulting TCK frequency
1375 static long angie_calculate_frequency(enum angie_delay_type type
, int delay
)
1377 float t_us
, f_float
;
1383 case DELAY_CLOCK_TCK
:
1387 t_us
= (4.0 * delay
) + 6.0;
1389 case DELAY_CLOCK_TMS
:
1393 t_us
= (4.0 * delay
) + 8.5;
1399 t_us
= (4.0 * delay
) + 8.8308;
1401 case DELAY_SCAN_OUT
:
1405 t_us
= (4.0 * delay
) + 10.527;
1411 t_us
= (4.0 * delay
) + 13.132;
1417 f_float
= 1000000.0 / t_us
;
1418 return roundf(f_float
);
1421 /******************* Interface between ANGIE and OpenOCD ******************/
1424 * Sets the end state follower (see interface.h) if \a endstate is a stable
1427 * @param endstate the state the end state follower should be set to.
1429 static void angie_set_end_state(tap_state_t endstate
)
1431 if (tap_is_state_stable(endstate
))
1432 tap_set_end_state(endstate
);
1434 LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate
));
1438 * Move from the current TAP state to the current TAP end state.
1440 * @param device pointer to struct angie identifying ANGIE driver instance.
1441 * @return on success: ERROR_OK
1442 * @return on failure: ERROR_FAIL
1444 static int angie_queue_statemove(struct angie
*device
)
1446 uint8_t tms_sequence
, tms_count
;
1449 if (tap_get_state() == tap_get_end_state()) {
1450 /* Do nothing if we are already there */
1454 tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1455 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1457 ret
= angie_append_clock_tms_cmd(device
, tms_count
, tms_sequence
);
1459 if (ret
== ERROR_OK
)
1460 tap_set_state(tap_get_end_state());
1466 * Perform a scan operation on a JTAG register.
1468 * @param device pointer to struct angie identifying ANGIE driver instance.
1469 * @param cmd pointer to the command that shall be executed.
1470 * @return on success: ERROR_OK
1471 * @return on failure: ERROR_FAIL
1473 static int angie_queue_scan(struct angie
*device
, struct jtag_command
*cmd
)
1475 uint32_t scan_size_bits
, scan_size_bytes
, bits_last_scan
;
1476 uint32_t scans_max_payload
, bytecount
;
1477 uint8_t *tdi_buffer_start
= NULL
, *tdi_buffer
= NULL
;
1478 uint8_t *tdo_buffer_start
= NULL
, *tdo_buffer
= NULL
;
1480 uint8_t first_tms_count
, first_tms_sequence
;
1481 uint8_t last_tms_count
, last_tms_sequence
;
1483 uint8_t tms_count_pause
, tms_sequence_pause
;
1484 uint8_t tms_count_resume
, tms_sequence_resume
;
1486 uint8_t tms_count_start
, tms_sequence_start
;
1487 uint8_t tms_count_end
, tms_sequence_end
;
1489 enum scan_type type
;
1492 /* Determine scan size */
1493 scan_size_bits
= jtag_scan_size(cmd
->cmd
.scan
);
1494 scan_size_bytes
= DIV_ROUND_UP(scan_size_bits
, 8);
1496 /* Determine scan type (IN/OUT/IO) */
1497 type
= jtag_scan_type(cmd
->cmd
.scan
);
1499 /* Determine number of scan commands with maximum payload */
1500 scans_max_payload
= scan_size_bytes
/ 58;
1502 /* Determine size of last shift command */
1503 bits_last_scan
= scan_size_bits
- (scans_max_payload
* 58 * 8);
1505 /* Allocate TDO buffer if required */
1506 if (type
== SCAN_IN
|| type
== SCAN_IO
) {
1507 tdo_buffer_start
= calloc(sizeof(uint8_t), scan_size_bytes
);
1509 if (!tdo_buffer_start
)
1512 tdo_buffer
= tdo_buffer_start
;
1515 /* Fill TDI buffer if required */
1516 if (type
== SCAN_OUT
|| type
== SCAN_IO
) {
1517 jtag_build_buffer(cmd
->cmd
.scan
, &tdi_buffer_start
);
1518 tdi_buffer
= tdi_buffer_start
;
1521 /* Get TAP state transitions */
1522 if (cmd
->cmd
.scan
->ir_scan
) {
1523 angie_set_end_state(TAP_IRSHIFT
);
1524 first_tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1525 first_tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1527 tap_set_state(TAP_IRSHIFT
);
1528 tap_set_end_state(cmd
->cmd
.scan
->end_state
);
1529 last_tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1530 last_tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1532 /* TAP state transitions for split scans */
1533 tms_count_pause
= tap_get_tms_path_len(TAP_IRSHIFT
, TAP_IRPAUSE
);
1534 tms_sequence_pause
= tap_get_tms_path(TAP_IRSHIFT
, TAP_IRPAUSE
);
1535 tms_count_resume
= tap_get_tms_path_len(TAP_IRPAUSE
, TAP_IRSHIFT
);
1536 tms_sequence_resume
= tap_get_tms_path(TAP_IRPAUSE
, TAP_IRSHIFT
);
1538 angie_set_end_state(TAP_DRSHIFT
);
1539 first_tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1540 first_tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1542 tap_set_state(TAP_DRSHIFT
);
1543 tap_set_end_state(cmd
->cmd
.scan
->end_state
);
1544 last_tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1545 last_tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1547 /* TAP state transitions for split scans */
1548 tms_count_pause
= tap_get_tms_path_len(TAP_DRSHIFT
, TAP_DRPAUSE
);
1549 tms_sequence_pause
= tap_get_tms_path(TAP_DRSHIFT
, TAP_DRPAUSE
);
1550 tms_count_resume
= tap_get_tms_path_len(TAP_DRPAUSE
, TAP_DRSHIFT
);
1551 tms_sequence_resume
= tap_get_tms_path(TAP_DRPAUSE
, TAP_DRSHIFT
);
1554 /* Generate scan commands */
1555 bytecount
= scan_size_bytes
;
1556 while (bytecount
> 0) {
1557 if (bytecount
== scan_size_bytes
) {
1558 /* This is the first scan */
1559 tms_count_start
= first_tms_count
;
1560 tms_sequence_start
= first_tms_sequence
;
1562 /* Resume from previous scan */
1563 tms_count_start
= tms_count_resume
;
1564 tms_sequence_start
= tms_sequence_resume
;
1567 if (bytecount
> 58) { /* Full scan, at least one scan will follow */
1568 tms_count_end
= tms_count_pause
;
1569 tms_sequence_end
= tms_sequence_pause
;
1571 ret
= angie_append_scan_cmd(device
,
1586 /* Update TDI and TDO buffer pointers */
1587 if (tdi_buffer_start
)
1589 if (tdo_buffer_start
)
1591 } else if (bytecount
== 58) { /* Full scan, no further scans */
1592 tms_count_end
= last_tms_count
;
1593 tms_sequence_end
= last_tms_sequence
;
1595 ret
= angie_append_scan_cmd(device
,
1609 } else {/* Scan with less than maximum payload, no further scans */
1610 tms_count_end
= last_tms_count
;
1611 tms_sequence_end
= last_tms_sequence
;
1613 ret
= angie_append_scan_cmd(device
,
1629 if (ret
!= ERROR_OK
) {
1630 free(tdi_buffer_start
);
1631 free(tdo_buffer_start
);
1636 free(tdi_buffer_start
);
1638 /* Set current state to the end state requested by the command */
1639 tap_set_state(cmd
->cmd
.scan
->end_state
);
1645 * Move the TAP into the Test Logic Reset state.
1647 * @param device pointer to struct angie identifying ANGIE driver instance.
1648 * @param cmd pointer to the command that shall be executed.
1649 * @return on success: ERROR_OK
1650 * @return on failure: ERROR_FAIL
1652 static int angie_queue_tlr_reset(struct angie
*device
, struct jtag_command
*cmd
)
1654 int ret
= angie_append_clock_tms_cmd(device
, 5, 0xff);
1656 if (ret
== ERROR_OK
)
1657 tap_set_state(TAP_RESET
);
1665 * Generate TCK clock cycles while remaining
1666 * in the Run-Test/Idle state.
1668 * @param device pointer to struct angie identifying ANGIE driver instance.
1669 * @param cmd pointer to the command that shall be executed.
1670 * @return on success: ERROR_OK
1671 * @return on failure: ERROR_FAIL
1673 static int angie_queue_runtest(struct angie
*device
, struct jtag_command
*cmd
)
1677 /* Only perform statemove if the TAP currently isn't in the TAP_IDLE state */
1678 if (tap_get_state() != TAP_IDLE
) {
1679 angie_set_end_state(TAP_IDLE
);
1680 angie_queue_statemove(device
);
1683 /* Generate the clock cycles */
1684 ret
= angie_append_clock_tck_cmd(device
, cmd
->cmd
.runtest
->num_cycles
);
1685 if (ret
!= ERROR_OK
)
1688 /* Move to end state specified in command */
1689 if (cmd
->cmd
.runtest
->end_state
!= tap_get_state()) {
1690 tap_set_end_state(cmd
->cmd
.runtest
->end_state
);
1691 angie_queue_statemove(device
);
1698 * Execute a JTAG_RESET command
1701 * @param trst indicate if trst signal is activated.
1702 * @param srst indicate if srst signal is activated.
1703 * @return on success: ERROR_OK
1704 * @return on failure: ERROR_FAIL
1706 static int angie_reset(int trst
, int srst
)
1708 struct angie
*device
= angie_handle
;
1709 uint8_t low
= 0, high
= 0;
1712 tap_set_state(TAP_RESET
);
1715 high
|= SIGNAL_TRST
;
1721 high
|= SIGNAL_SRST
;
1723 int ret
= angie_append_set_signals_cmd(device
, low
, high
);
1724 if (ret
== ERROR_OK
)
1725 angie_clear_queue(device
);
1727 ret
= angie_execute_queued_commands(device
, LIBUSB_TIMEOUT_MS
);
1728 if (ret
== ERROR_OK
)
1729 angie_clear_queue(device
);
1735 * Move to one TAP state or several states in succession.
1737 * @param device pointer to struct angie identifying ANGIE driver instance.
1738 * @param cmd pointer to the command that shall be executed.
1739 * @return on success: ERROR_OK
1740 * @return on failure: ERROR_FAIL
1742 static int angie_queue_pathmove(struct angie
*device
, struct jtag_command
*cmd
)
1744 int ret
, i
, num_states
, batch_size
, state_count
;
1746 uint8_t tms_sequence
;
1748 num_states
= cmd
->cmd
.pathmove
->num_states
;
1749 path
= cmd
->cmd
.pathmove
->path
;
1752 while (num_states
> 0) {
1755 /* Determine batch size */
1756 if (num_states
>= 8)
1759 batch_size
= num_states
;
1761 for (i
= 0; i
< batch_size
; i
++) {
1762 if (tap_state_transition(tap_get_state(), false) == path
[state_count
]) {
1763 /* Append '0' transition: clear bit 'i' in tms_sequence */
1764 buf_set_u32(&tms_sequence
, i
, 1, 0x0);
1765 } else if (tap_state_transition(tap_get_state(), true)
1766 == path
[state_count
]) {
1767 /* Append '1' transition: set bit 'i' in tms_sequence */
1768 buf_set_u32(&tms_sequence
, i
, 1, 0x1);
1770 /* Invalid state transition */
1771 LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
1772 tap_state_name(tap_get_state()),
1773 tap_state_name(path
[state_count
]));
1777 tap_set_state(path
[state_count
]);
1782 /* Append CLOCK_TMS command to ANGIE command queue */
1783 LOG_INFO("pathmove batch: count = %i, sequence = 0x%" PRIx8
"", batch_size
, tms_sequence
);
1784 ret
= angie_append_clock_tms_cmd(angie_handle
, batch_size
, tms_sequence
);
1785 if (ret
!= ERROR_OK
)
1793 * Sleep for a specific amount of time.
1795 * @param device pointer to struct angie identifying ANGIE driver instance.
1796 * @param cmd pointer to the command that shall be executed.
1797 * @return on success: ERROR_OK
1798 * @return on failure: ERROR_FAIL
1800 static int angie_queue_sleep(struct angie
*device
, struct jtag_command
*cmd
)
1802 /* IMPORTANT! Due to the time offset in command execution introduced by
1803 * command queueing, this needs to be implemented in the ANGIE device */
1804 return angie_append_sleep_cmd(device
, cmd
->cmd
.sleep
->us
);
1808 * Generate TCK cycles while remaining in a stable state.
1810 * @param device pointer to struct angie identifying ANGIE driver instance.
1811 * @param cmd pointer to the command that shall be executed.
1813 static int angie_queue_stableclocks(struct angie
*device
, struct jtag_command
*cmd
)
1816 unsigned int num_cycles
;
1818 if (!tap_is_state_stable(tap_get_state())) {
1819 LOG_ERROR("JTAG_STABLECLOCKS: state not stable");
1823 num_cycles
= cmd
->cmd
.stableclocks
->num_cycles
;
1825 /* TMS stays either high (Test Logic Reset state) or low (all other states) */
1826 if (tap_get_state() == TAP_RESET
)
1827 ret
= angie_append_set_signals_cmd(device
, 0, SIGNAL_TMS
);
1829 ret
= angie_append_set_signals_cmd(device
, SIGNAL_TMS
, 0);
1831 if (ret
!= ERROR_OK
)
1834 while (num_cycles
> 0) {
1835 if (num_cycles
> 0xFFFF) {
1836 /* ANGIE CMD_CLOCK_TCK can generate up to 0xFFFF (uint16_t) cycles */
1837 ret
= angie_append_clock_tck_cmd(device
, 0xFFFF);
1838 num_cycles
-= 0xFFFF;
1840 ret
= angie_append_clock_tck_cmd(device
, num_cycles
);
1844 if (ret
!= ERROR_OK
)
1852 * Post-process JTAG_SCAN command
1854 * @param angie_cmd pointer to ANGIE command that shall be processed.
1855 * @return on success: ERROR_OK
1856 * @return on failure: ERROR_FAIL
1858 static int angie_post_process_scan(struct angie_cmd
*angie_cmd
)
1860 struct jtag_command
*cmd
= angie_cmd
->cmd_origin
;
1863 switch (jtag_scan_type(cmd
->cmd
.scan
)) {
1866 ret
= jtag_read_buffer(angie_cmd
->payload_in_start
, cmd
->cmd
.scan
);
1869 /* Nothing to do for OUT scans */
1873 LOG_ERROR("BUG: angie post process scan encountered an unknown JTAG scan type");
1882 * Perform post-processing of commands after ANGIE queue has been executed.
1884 * @param device pointer to struct angie identifying ANGIE driver instance.
1885 * @return on success: ERROR_OK
1886 * @return on failure: ERROR_FAIL
1888 static int angie_post_process_queue(struct angie
*device
)
1890 struct angie_cmd
*current
;
1891 struct jtag_command
*openocd_cmd
;
1894 current
= device
->queue_start
;
1897 openocd_cmd
= current
->cmd_origin
;
1899 /* Check if a corresponding OpenOCD command is stored for this
1901 if (current
->needs_postprocessing
&& openocd_cmd
) {
1902 switch (openocd_cmd
->type
) {
1904 ret
= angie_post_process_scan(current
);
1906 case JTAG_TLR_RESET
:
1910 case JTAG_STABLECLOCKS
:
1911 /* Nothing to do for these commands */
1916 LOG_ERROR("BUG: angie post process queue encountered unknown JTAG "
1921 if (ret
!= ERROR_OK
)
1925 current
= current
->next
;
1931 /**************************** JTAG driver functions ***************************/
1934 * Executes the JTAG Command Queue.
1936 * This is done in three stages: First, all OpenOCD commands are processed into
1937 * queued ANGIE commands. Next, the ANGIE command queue is sent to the
1938 * ANGIE device and data received from the ANGIE device is cached. Finally,
1939 * the post-processing function writes back data to the corresponding OpenOCD
1942 * @return on success: ERROR_OK
1943 * @return on failure: ERROR_FAIL
1945 static int angie_execute_queue(void)
1947 struct jtag_command
*cmd
= jtag_command_queue
;
1951 switch (cmd
->type
) {
1953 ret
= angie_queue_scan(angie_handle
, cmd
);
1955 case JTAG_TLR_RESET
:
1956 ret
= angie_queue_tlr_reset(angie_handle
, cmd
);
1959 ret
= angie_queue_runtest(angie_handle
, cmd
);
1962 ret
= angie_queue_pathmove(angie_handle
, cmd
);
1965 ret
= angie_queue_sleep(angie_handle
, cmd
);
1967 case JTAG_STABLECLOCKS
:
1968 ret
= angie_queue_stableclocks(angie_handle
, cmd
);
1972 LOG_ERROR("BUG: encountered unknown JTAG command type");
1976 if (ret
!= ERROR_OK
)
1982 if (angie_handle
->commands_in_queue
> 0) {
1983 ret
= angie_execute_queued_commands(angie_handle
, LIBUSB_TIMEOUT_MS
);
1984 if (ret
!= ERROR_OK
)
1987 ret
= angie_post_process_queue(angie_handle
);
1988 if (ret
!= ERROR_OK
)
1991 angie_clear_queue(angie_handle
);
1998 * Set the TCK frequency of the ANGIE adapter.
2000 * @param khz desired JTAG TCK frequency.
2001 * @param jtag_speed where to store corresponding adapter-specific speed value.
2002 * @return on success: ERROR_OK
2003 * @return on failure: ERROR_FAIL
2005 static int angie_khz(int khz
, int *jtag_speed
)
2010 LOG_ERROR("RCLK not supported");
2014 /* CLOCK_TCK commands are decoupled from others. Therefore, the frequency
2015 * setting can be done independently from all other commands. */
2017 angie_handle
->delay_clock_tck
= -1;
2019 ret
= angie_calculate_delay(DELAY_CLOCK_TCK
, khz
* 1000,
2020 &angie_handle
->delay_clock_tck
);
2021 if (ret
!= ERROR_OK
)
2025 /* SCAN_{IN,OUT,IO} commands invoke CLOCK_TMS commands. Therefore, if the
2026 * requested frequency goes below the maximum frequency for SLOW_CLOCK_TMS
2027 * commands, all SCAN commands MUST also use the variable frequency
2028 * implementation! */
2030 angie_handle
->delay_clock_tms
= -1;
2031 angie_handle
->delay_scan_in
= -1;
2032 angie_handle
->delay_scan_out
= -1;
2033 angie_handle
->delay_scan_io
= -1;
2035 ret
= angie_calculate_delay(DELAY_CLOCK_TMS
, khz
* 1000,
2036 &angie_handle
->delay_clock_tms
);
2037 if (ret
!= ERROR_OK
)
2040 ret
= angie_calculate_delay(DELAY_SCAN_IN
, khz
* 1000,
2041 &angie_handle
->delay_scan_in
);
2042 if (ret
!= ERROR_OK
)
2045 ret
= angie_calculate_delay(DELAY_SCAN_OUT
, khz
* 1000,
2046 &angie_handle
->delay_scan_out
);
2047 if (ret
!= ERROR_OK
)
2050 ret
= angie_calculate_delay(DELAY_SCAN_IO
, khz
* 1000,
2051 &angie_handle
->delay_scan_io
);
2052 if (ret
!= ERROR_OK
)
2056 LOG_DEBUG_IO("ANGIE TCK setup: delay_tck = %i (%li Hz),",
2057 angie_handle
->delay_clock_tck
,
2058 angie_calculate_frequency(DELAY_CLOCK_TCK
, angie_handle
->delay_clock_tck
));
2059 LOG_DEBUG_IO(" delay_tms = %i (%li Hz),",
2060 angie_handle
->delay_clock_tms
,
2061 angie_calculate_frequency(DELAY_CLOCK_TMS
, angie_handle
->delay_clock_tms
));
2062 LOG_DEBUG_IO(" delay_scan_in = %i (%li Hz),",
2063 angie_handle
->delay_scan_in
,
2064 angie_calculate_frequency(DELAY_SCAN_IN
, angie_handle
->delay_scan_in
));
2065 LOG_DEBUG_IO(" delay_scan_out = %i (%li Hz),",
2066 angie_handle
->delay_scan_out
,
2067 angie_calculate_frequency(DELAY_SCAN_OUT
, angie_handle
->delay_scan_out
));
2068 LOG_DEBUG_IO(" delay_scan_io = %i (%li Hz),",
2069 angie_handle
->delay_scan_io
,
2070 angie_calculate_frequency(DELAY_SCAN_IO
, angie_handle
->delay_scan_io
));
2072 /* Configure the ANGIE device with the new delay values */
2073 ret
= angie_append_configure_tck_cmd(angie_handle
,
2074 angie_handle
->delay_scan_in
,
2075 angie_handle
->delay_scan_out
,
2076 angie_handle
->delay_scan_io
,
2077 angie_handle
->delay_clock_tck
,
2078 angie_handle
->delay_clock_tms
);
2080 if (ret
!= ERROR_OK
)
2089 * Set the TCK frequency of the ANGIE adapter.
2091 * Because of the way the TCK frequency is set up in the ANGIE firmware,
2092 * there are five different speed settings. To simplify things, the
2093 * adapter-specific speed setting value is identical to the TCK frequency in
2096 * @param speed desired adapter-specific speed value.
2097 * @return on success: ERROR_OK
2098 * @return on failure: ERROR_FAIL
2100 static int angie_speed(int speed
)
2104 return angie_khz(speed
, &dummy
);
2108 * Convert adapter-specific speed value to corresponding TCK frequency in kHz.
2110 * Because of the way the TCK frequency is set up in the ANGIE firmware,
2111 * there are five different speed settings. To simplify things, the
2112 * adapter-specific speed setting value is identical to the TCK frequency in
2115 * @param speed adapter-specific speed value.
2116 * @param khz where to store corresponding TCK frequency in kHz.
2117 * @return on success: ERROR_OK
2118 * @return on failure: ERROR_FAIL
2120 static int angie_speed_div(int speed
, int *khz
)
2128 * Initiates the firmware download to the ANGIE adapter and prepares
2131 * @return on success: ERROR_OK
2132 * @return on failure: ERROR_FAIL
2134 static int angie_init(void)
2136 int ret
, transferred
;
2137 char str_manufacturer
[20];
2138 bool download_firmware
= false;
2140 uint8_t input_signals
, output_signals
;
2142 angie_handle
= calloc(1, sizeof(struct angie
));
2144 if (!angie_handle
) {
2145 LOG_ERROR("Out of memory");
2149 ret
= angie_usb_open(angie_handle
);
2150 if (ret
!= ERROR_OK
) {
2151 LOG_ERROR("Could not open ANGIE device");
2153 angie_handle
= NULL
;
2157 /* Get String Descriptor to determine if firmware needs to be loaded */
2158 ret
= libusb_get_string_descriptor_ascii(angie_handle
->usb_device_handle
, 1, (unsigned char *)str_manufacturer
, 20);
2160 /* Could not get descriptor -> Unconfigured or original Keil firmware */
2161 download_firmware
= true;
2163 /* We got a String Descriptor, check if it is the correct one */
2164 if (strncmp(str_manufacturer
, "NanoXplore, SAS.", 16) != 0)
2165 download_firmware
= true;
2168 if (download_firmware
) {
2169 LOG_INFO("Loading ANGIE firmware. This is reversible by power-cycling ANGIE device.");
2171 if (libusb_claim_interface(angie_handle
->usb_device_handle
, 0) != ERROR_OK
)
2172 LOG_ERROR("Could not claim interface");
2174 ret
= angie_load_firmware_and_renumerate(angie_handle
,
2175 ANGIE_FIRMWARE_FILE
, ANGIE_RENUMERATION_DELAY_US
);
2176 if (ret
!= ERROR_OK
) {
2177 LOG_ERROR("Could not download firmware and re-numerate ANGIE");
2181 ret
= angie_load_bitstream(angie_handle
, ANGIE_BITSTREAM_FILE
);
2182 if (ret
!= ERROR_OK
) {
2183 LOG_ERROR("Could not download bitstream");
2188 LOG_INFO("ANGIE device is already running ANGIE firmware");
2191 /* Get ANGIE USB IN/OUT endpoints and claim the interface */
2192 ret
= jtag_libusb_choose_interface(angie_handle
->usb_device_handle
,
2193 &angie_handle
->ep_in
, &angie_handle
->ep_out
, -1, -1, -1, -1);
2194 if (ret
!= ERROR_OK
) {
2199 /* Initialize ANGIE command queue */
2200 angie_clear_queue(angie_handle
);
2202 /* Issue one test command with short timeout */
2203 ret
= angie_append_test_cmd(angie_handle
);
2204 if (ret
!= ERROR_OK
) {
2209 ret
= angie_execute_queued_commands(angie_handle
, 200);
2210 if (ret
!= ERROR_OK
) {
2211 /* Sending test command failed. The ANGIE device may be forever waiting for
2212 * the host to fetch an USB Bulk IN packet (e. g. OpenOCD crashed or was
2213 * shut down by the user via Ctrl-C. Try to retrieve this Bulk IN packet. */
2215 ret
= jtag_libusb_bulk_write(angie_handle
->usb_device_handle
, angie_handle
->ep_in
,
2216 dummy
, 64, 200, &transferred
);
2218 if (ret
!= ERROR_OK
|| transferred
== 0) {
2219 /* Bulk IN transfer failed -> unrecoverable error condition */
2220 LOG_ERROR("Cannot communicate with ANGIE device. Disconnect ANGIE from "
2221 "the USB port and re-connect, then re-run OpenOCD");
2225 /* Successfully received Bulk IN packet -> continue */
2226 LOG_INFO("Recovered from lost Bulk IN packet");
2229 angie_clear_queue(angie_handle
);
2231 ret
= angie_append_get_signals_cmd(angie_handle
);
2232 if (ret
!= ERROR_OK
) {
2237 ret
= angie_execute_queued_commands(angie_handle
, 200);
2238 if (ret
!= ERROR_OK
) {
2243 /* Post-process the single CMD_GET_SIGNALS command */
2244 input_signals
= angie_handle
->queue_start
->payload_in
[0];
2245 output_signals
= angie_handle
->queue_start
->payload_in
[1];
2246 angie_dump_signal_states(input_signals
, output_signals
);
2248 angie_clear_queue(angie_handle
);
2254 * Closes the USB handle for the ANGIE device.
2256 * @return on success: ERROR_OK
2257 * @return on failure: ERROR_FAIL
2259 static int angie_quit(void)
2261 int ret
= angie_usb_close(angie_handle
);
2263 angie_handle
= NULL
;
2268 static struct jtag_interface angie_interface
= {
2269 .execute_queue
= angie_execute_queue
,
2272 struct adapter_driver angie_adapter_driver
= {
2274 .transports
= jtag_only
,
2278 .reset
= angie_reset
,
2279 .speed
= angie_speed
,
2281 .speed_div
= angie_speed_div
,
2283 .jtag_ops
= &angie_interface
,
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)