2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2016 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef LIBJAYLINK_LIBJAYLINK_H
21 #define LIBJAYLINK_LIBJAYLINK_H
30 #include <arpa/inet.h>
40 * Public libjaylink header file to be used by applications.
43 /** Error codes returned by libjaylink functions. */
47 /** Unspecified error. */
49 /** Invalid argument. */
51 /** Memory allocation error. */
52 JAYLINK_ERR_MALLOC
= -3,
53 /** Timeout occurred. */
54 JAYLINK_ERR_TIMEOUT
= -4,
55 /** Protocol violation. */
56 JAYLINK_ERR_PROTO
= -5,
57 /** Entity not available. */
58 JAYLINK_ERR_NOT_AVAILABLE
= -6,
59 /** Operation not supported. */
60 JAYLINK_ERR_NOT_SUPPORTED
= -7,
61 /** Input/output error. */
63 /** Device: unspecified error. */
64 JAYLINK_ERR_DEV
= -1000,
65 /** Device: operation not supported. */
66 JAYLINK_ERR_DEV_NOT_SUPPORTED
= -1001,
67 /** Device: entity not available. */
68 JAYLINK_ERR_DEV_NOT_AVAILABLE
= -1002,
69 /** Device: not enough memory to perform operation. */
70 JAYLINK_ERR_DEV_NO_MEMORY
= -1003
73 /** libjaylink log levels. */
74 enum jaylink_log_level
{
75 /** Output no messages. */
76 JAYLINK_LOG_LEVEL_NONE
= 0,
77 /** Output error messages. */
78 JAYLINK_LOG_LEVEL_ERROR
= 1,
79 /** Output warnings. */
80 JAYLINK_LOG_LEVEL_WARNING
= 2,
81 /** Output informational messages. */
82 JAYLINK_LOG_LEVEL_INFO
= 3,
83 /** Output debug messages. */
84 JAYLINK_LOG_LEVEL_DEBUG
= 4,
85 /** Output I/O debug messages. */
86 JAYLINK_LOG_LEVEL_DEBUG_IO
= 5
89 /** Default libjaylink log domain. */
90 #define JAYLINK_LOG_DOMAIN_DEFAULT "jaylink: "
92 /** Maximum length of a libjaylink log domain in bytes. */
93 #define JAYLINK_LOG_DOMAIN_MAX_LENGTH 32
95 /** libjaylink capabilities. */
96 enum jaylink_capability
{
97 /** Library supports USB as host interface. */
98 JAYLINK_CAP_HIF_USB
= 0
101 /** Host interfaces. */
102 enum jaylink_host_interface
{
103 /** Universal Serial Bus (USB). */
104 JAYLINK_HIF_USB
= (1 << 0),
105 /** Transmission Control Protocol (TCP). */
106 JAYLINK_HIF_TCP
= (1 << 1)
112 * The USB address is a way to identify USB devices and is related to the USB
113 * Product ID (PID) of a device.
115 enum jaylink_usb_address
{
116 /** USB address 0 (Product ID 0x0101). */
117 JAYLINK_USB_ADDRESS_0
= 0,
118 /** USB address 1 (Product ID 0x0102). */
119 JAYLINK_USB_ADDRESS_1
= 1,
120 /** USB address 2 (Product ID 0x0103). */
121 JAYLINK_USB_ADDRESS_2
= 2,
122 /** USB address 3 (Product ID 0x0104). */
123 JAYLINK_USB_ADDRESS_3
= 3
126 /** Device capabilities. */
127 enum jaylink_device_capability
{
128 /** Device supports retrieval of the hardware version. */
129 JAYLINK_DEV_CAP_GET_HW_VERSION
= 1,
130 /** Device supports adaptive clocking. */
131 JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING
= 3,
132 /** Device supports reading configuration data. */
133 JAYLINK_DEV_CAP_READ_CONFIG
= 4,
134 /** Device supports writing configuration data. */
135 JAYLINK_DEV_CAP_WRITE_CONFIG
= 5,
136 /** Device supports retrieval of target interface speeds. */
137 JAYLINK_DEV_CAP_GET_SPEEDS
= 9,
138 /** Device supports retrieval of free memory size. */
139 JAYLINK_DEV_CAP_GET_FREE_MEMORY
= 11,
140 /** Device supports retrieval of hardware information. */
141 JAYLINK_DEV_CAP_GET_HW_INFO
= 12,
142 /** Device supports the setting of the target power supply. */
143 JAYLINK_DEV_CAP_SET_TARGET_POWER
= 13,
144 /** Device supports target interface selection. */
145 JAYLINK_DEV_CAP_SELECT_TIF
= 17,
146 /** Device supports retrieval of counter values. */
147 JAYLINK_DEV_CAP_GET_COUNTERS
= 19,
148 /** Device supports capturing of SWO trace data. */
149 JAYLINK_DEV_CAP_SWO
= 23,
150 /** Device supports file I/O operations. */
151 JAYLINK_DEV_CAP_FILE_IO
= 26,
152 /** Device supports registration of connections. */
153 JAYLINK_DEV_CAP_REGISTER
= 27,
154 /** Device supports retrieval of extended capabilities. */
155 JAYLINK_DEV_CAP_GET_EXT_CAPS
= 31,
156 /** Device supports EMUCOM. */
157 JAYLINK_DEV_CAP_EMUCOM
= 33,
158 /** Device supports ethernet connectivity. */
159 JAYLINK_DEV_CAP_ETHERNET
= 38
162 /** Hardware information. */
163 enum jaylink_hardware_info
{
165 * Status of the target power supply.
167 * This indicates whether the target power supply on pin 19 of the
168 * 20-pin JTAG / SWD connector is enabled or disabled.
170 * @see jaylink_set_target_power()
172 JAYLINK_HW_INFO_TARGET_POWER
= (1 << 0),
173 /** Current consumption of the target in mA. */
174 JAYLINK_HW_INFO_ITARGET
= (1 << 2),
175 /** Peak current consumption of the target in mA. */
176 JAYLINK_HW_INFO_ITARGET_PEAK
= (1 << 3),
178 * Device's IPv4 address in network byte order.
180 * If the address is 0.0.0.0 and DHCP is enabled, no address is
183 * @note The value is valid only if the device is configured in DHCP
186 JAYLINK_HW_INFO_IPV4_ADDRESS
= (1 << 16),
188 * IPv4 netmask in network byte order.
190 * @note The value is valid only if the device is configured in DHCP
193 JAYLINK_HW_INFO_IPV4_NETMASK
= (1 << 17),
195 * Gateway IPv4 address in network byte order.
197 * @note The value is valid only if the device is configured in DHCP
200 JAYLINK_HW_INFO_IPV4_GATEWAY
= (1 << 18),
202 * DNS server IPv4 address in network byte order.
204 * @note The value is valid only if the device is configured in DHCP
207 JAYLINK_HW_INFO_IPV4_DNS
= (1 << 19)
210 /** Device counters. */
211 enum jaylink_counter
{
212 /** Time the device is connected to a target in milliseconds. */
213 JAYLINK_COUNTER_TARGET_TIME
= (1 << 0),
215 * Number of times the device was connected or disconnected from a
218 JAYLINK_COUNTER_TARGET_CONNECTIONS
= (1 << 1)
221 /** Device hardware types. */
222 enum jaylink_hardware_type
{
224 JAYLINK_HW_TYPE_JLINK
= 0,
226 JAYLINK_HW_TYPE_FLASHER
= 2,
228 JAYLINK_HW_TYPE_JLINK_PRO
= 3
231 /** Target interfaces. */
232 enum jaylink_target_interface
{
233 /** Joint Test Action Group, IEEE 1149.1 (JTAG). */
234 JAYLINK_TIF_JTAG
= 0,
235 /** Serial Wire Debug (SWD). */
237 /** Background Debug Mode 3 (BDM3). */
238 JAYLINK_TIF_BDM3
= 2,
239 /** Renesas’ single-wire debug interface (FINE). */
240 JAYLINK_TIF_FINE
= 3,
241 /** 2-wire JTAG for PIC32 compliant devices. */
242 JAYLINK_TIF_2W_JTAG_PIC32
= 4,
243 /** Compact JTAG (cJTAG). **/
244 JAYLINK_TIF_CJTAG
= 7,
248 * JTAG command versions.
250 * The JTAG command version only affects the device and the communication
251 * protocol. The behaviour of a JTAG operation is not affected at all.
253 enum jaylink_jtag_version
{
255 * JTAG command version 2.
257 * This version is obsolete for major hardware version 5 and above. Use
258 * #JAYLINK_JTAG_VERSION_3 for these versions instead.
260 JAYLINK_JTAG_VERSION_2
= 1,
261 /** JTAG command version 3. */
262 JAYLINK_JTAG_VERSION_3
= 2
265 /** Serial Wire Output (SWO) capture modes. */
266 enum jaylink_swo_mode
{
267 /** Universal Asynchronous Receiver Transmitter (UART). */
268 JAYLINK_SWO_MODE_UART
= 0
271 /** Target interface speed information. */
272 struct jaylink_speed
{
273 /** Base frequency in Hz. */
275 /** Minimum frequency divider. */
279 /** Serial Wire Output (SWO) speed information. */
280 struct jaylink_swo_speed
{
281 /** Base frequency in Hz. */
283 /** Minimum frequency divider. */
285 /** Maximum frequency divider. */
287 /** Minimum prescaler. */
288 uint32_t min_prescaler
;
289 /** Maximum prescaler. */
290 uint32_t max_prescaler
;
293 /** Device hardware version. */
294 struct jaylink_hardware_version
{
295 /** Hardware type. */
296 enum jaylink_hardware_type type
;
297 /** Major version. */
299 /** Minor version. */
301 /** Revision number. */
305 /** Device hardware status. */
306 struct jaylink_hardware_status
{
307 /** Target reference voltage in mV. */
308 uint16_t target_voltage
;
309 /** TCK pin state. */
311 /** TDI pin state. */
313 /** TDO pin state. */
315 /** TMS pin state. */
317 /** TRES pin state. */
319 /** TRST pin state. */
323 /** Device connection. */
324 struct jaylink_connection
{
330 * Identification of the client process. Usually this is the
331 * Process ID (PID) of the client process in an arbitrary format.
337 * IPv4 address string of the client in quad-dotted decimal format
338 * (e.g. 192.0.2.235). The address 0.0.0.0 should be used for the
339 * registration of an USB connection.
341 char hid
[INET_ADDRSTRLEN
];
347 * Timestamp of the last registration in milliseconds.
349 * The timestamp is relative to the time the device was powered up.
354 /** Target interface speed value for adaptive clocking. */
355 #define JAYLINK_SPEED_ADAPTIVE_CLOCKING 0xffff
357 /** Size of the device configuration data in bytes. */
358 #define JAYLINK_DEV_CONFIG_SIZE 256
360 /** Number of bytes required to store device capabilities. */
361 #define JAYLINK_DEV_CAPS_SIZE 4
363 /** Number of bytes required to store extended device capabilities. */
364 #define JAYLINK_DEV_EXT_CAPS_SIZE 32
366 /** Maximum number of connections that can be registered on a device. */
367 #define JAYLINK_MAX_CONNECTIONS 16
369 /** Media Access Control (MAC) address length in bytes. */
370 #define JAYLINK_MAC_ADDRESS_LENGTH 6
373 * Maximum length of a device's nickname including trailing null-terminator in
376 #define JAYLINK_NICKNAME_MAX_LENGTH 32
379 * Maximum length of a device's product name including trailing null-terminator
382 #define JAYLINK_PRODUCT_NAME_MAX_LENGTH 32
384 /** Maximum length of a filename in bytes. */
385 #define JAYLINK_FILE_NAME_MAX_LENGTH 255
387 /** Maximum transfer size for a file in bytes. */
388 #define JAYLINK_FILE_MAX_TRANSFER_SIZE 0x100000
391 * EMUCOM channel with the system time of the device in milliseconds.
393 * The channel is read-only and the time is encoded in 4 bytes. The byte order
396 #define JAYLINK_EMUCOM_CHANNEL_TIME 0x0
399 * Offset of EMUCOM user channels.
401 * User channels are available to implement vendor and/or device specific
402 * functionalities. All channels below are reserved.
404 #define JAYLINK_EMUCOM_CHANNEL_USER 0x10000
407 * @struct jaylink_context
409 * Opaque structure representing a libjaylink context.
411 struct jaylink_context
;
414 * @struct jaylink_device
416 * Opaque structure representing a device.
418 struct jaylink_device
;
421 * @struct jaylink_device_handle
423 * Opaque structure representing a handle of a device.
425 struct jaylink_device_handle
;
427 /** Macro to mark public libjaylink API symbol. */
431 #define JAYLINK_API __attribute__ ((visibility ("default")))
435 * Log callback function type.
437 * @param[in] ctx libjaylink context.
438 * @param[in] level Log level.
439 * @param[in] format Message format in printf()-style.
440 * @param[in] args Message arguments.
441 * @param[in,out] user_data User data passed to the callback function.
443 * @return Number of characters printed on success, or a negative error code on
446 typedef int (*jaylink_log_callback
)(const struct jaylink_context
*ctx
,
447 enum jaylink_log_level level
, const char *format
, va_list args
,
450 /*--- core.c ----------------------------------------------------------------*/
452 JAYLINK_API
int jaylink_init(struct jaylink_context
**ctx
);
453 JAYLINK_API
int jaylink_exit(struct jaylink_context
*ctx
);
454 JAYLINK_API
bool jaylink_library_has_cap(enum jaylink_capability cap
);
456 /*--- device.c --------------------------------------------------------------*/
458 JAYLINK_API
int jaylink_get_devices(struct jaylink_context
*ctx
,
459 struct jaylink_device
***devs
, size_t *count
);
460 JAYLINK_API
void jaylink_free_devices(struct jaylink_device
**devs
,
462 JAYLINK_API
int jaylink_device_get_host_interface(
463 const struct jaylink_device
*dev
,
464 enum jaylink_host_interface
*iface
);
465 JAYLINK_API
int jaylink_device_get_serial_number(
466 const struct jaylink_device
*dev
, uint32_t *serial_number
);
467 JAYLINK_API
int jaylink_device_get_usb_address(
468 const struct jaylink_device
*dev
,
469 enum jaylink_usb_address
*address
);
470 JAYLINK_API
int jaylink_device_get_usb_bus_ports(
471 const struct jaylink_device
*dev
, uint8_t *bus
,
472 uint8_t **ports
, size_t *length
);
473 JAYLINK_API
int jaylink_device_get_ipv4_address(
474 const struct jaylink_device
*dev
, char *address
);
475 JAYLINK_API
int jaylink_device_get_mac_address(
476 const struct jaylink_device
*dev
, uint8_t *address
);
477 JAYLINK_API
int jaylink_device_get_hardware_version(
478 const struct jaylink_device
*dev
,
479 struct jaylink_hardware_version
*version
);
480 JAYLINK_API
int jaylink_device_get_product_name(
481 const struct jaylink_device
*dev
, char *name
);
482 JAYLINK_API
int jaylink_device_get_nickname(const struct jaylink_device
*dev
,
484 JAYLINK_API
struct jaylink_device
*jaylink_ref_device(
485 struct jaylink_device
*dev
);
486 JAYLINK_API
void jaylink_unref_device(struct jaylink_device
*dev
);
487 JAYLINK_API
int jaylink_open(struct jaylink_device
*dev
,
488 struct jaylink_device_handle
**devh
);
489 JAYLINK_API
int jaylink_close(struct jaylink_device_handle
*devh
);
490 JAYLINK_API
struct jaylink_device
*jaylink_get_device(
491 struct jaylink_device_handle
*devh
);
492 JAYLINK_API
int jaylink_get_firmware_version(
493 struct jaylink_device_handle
*devh
, char **version
,
495 JAYLINK_API
int jaylink_get_hardware_info(struct jaylink_device_handle
*devh
,
496 uint32_t mask
, uint32_t *info
);
497 JAYLINK_API
int jaylink_get_counters(struct jaylink_device_handle
*devh
,
498 uint32_t mask
, uint32_t *values
);
499 JAYLINK_API
int jaylink_get_hardware_version(
500 struct jaylink_device_handle
*devh
,
501 struct jaylink_hardware_version
*version
);
502 JAYLINK_API
int jaylink_get_hardware_status(struct jaylink_device_handle
*devh
,
503 struct jaylink_hardware_status
*status
);
504 JAYLINK_API
int jaylink_get_caps(struct jaylink_device_handle
*devh
,
506 JAYLINK_API
int jaylink_get_extended_caps(struct jaylink_device_handle
*devh
,
508 JAYLINK_API
int jaylink_get_free_memory(struct jaylink_device_handle
*devh
,
510 JAYLINK_API
int jaylink_read_raw_config(struct jaylink_device_handle
*devh
,
512 JAYLINK_API
int jaylink_write_raw_config(struct jaylink_device_handle
*devh
,
513 const uint8_t *config
);
514 JAYLINK_API
int jaylink_register(struct jaylink_device_handle
*devh
,
515 struct jaylink_connection
*connection
,
516 struct jaylink_connection
*connections
, size_t *count
);
517 JAYLINK_API
int jaylink_unregister(struct jaylink_device_handle
*devh
,
518 const struct jaylink_connection
*connection
,
519 struct jaylink_connection
*connections
, size_t *count
);
521 /*--- discovery.c -----------------------------------------------------------*/
523 JAYLINK_API
int jaylink_discovery_scan(struct jaylink_context
*ctx
,
526 /*--- emucom.c --------------------------------------------------------------*/
528 JAYLINK_API
int jaylink_emucom_read(struct jaylink_device_handle
*devh
,
529 uint32_t channel
, uint8_t *buffer
, uint32_t *length
);
530 JAYLINK_API
int jaylink_emucom_write(struct jaylink_device_handle
*devh
,
531 uint32_t channel
, const uint8_t *buffer
, uint32_t *length
);
533 /*--- error.c ---------------------------------------------------------------*/
535 JAYLINK_API
const char *jaylink_strerror(int error_code
);
536 JAYLINK_API
const char *jaylink_strerror_name(int error_code
);
538 /*--- fileio.c --------------------------------------------------------------*/
540 JAYLINK_API
int jaylink_file_read(struct jaylink_device_handle
*devh
,
541 const char *filename
, uint8_t *buffer
, uint32_t offset
,
543 JAYLINK_API
int jaylink_file_write(struct jaylink_device_handle
*devh
,
544 const char *filename
, const uint8_t *buffer
, uint32_t offset
,
546 JAYLINK_API
int jaylink_file_get_size(struct jaylink_device_handle
*devh
,
547 const char *filename
, uint32_t *size
);
548 JAYLINK_API
int jaylink_file_delete(struct jaylink_device_handle
*devh
,
549 const char *filename
);
551 /*--- jtag.c ----------------------------------------------------------------*/
553 JAYLINK_API
int jaylink_jtag_io(struct jaylink_device_handle
*devh
,
554 const uint8_t *tms
, const uint8_t *tdi
, uint8_t *tdo
,
555 uint16_t length
, enum jaylink_jtag_version version
);
556 JAYLINK_API
int jaylink_jtag_clear_trst(struct jaylink_device_handle
*devh
);
557 JAYLINK_API
int jaylink_jtag_set_trst(struct jaylink_device_handle
*devh
);
559 /*--- log.c -----------------------------------------------------------------*/
561 JAYLINK_API
int jaylink_log_set_level(struct jaylink_context
*ctx
,
562 enum jaylink_log_level level
);
563 JAYLINK_API
int jaylink_log_get_level(const struct jaylink_context
*ctx
,
564 enum jaylink_log_level
*level
);
565 JAYLINK_API
int jaylink_log_set_callback(struct jaylink_context
*ctx
,
566 jaylink_log_callback callback
, void *user_data
);
567 JAYLINK_API
int jaylink_log_set_domain(struct jaylink_context
*ctx
,
569 JAYLINK_API
const char *jaylink_log_get_domain(
570 const struct jaylink_context
*ctx
);
572 /*--- strutil.c -------------------------------------------------------------*/
574 JAYLINK_API
int jaylink_parse_serial_number(const char *str
,
575 uint32_t *serial_number
);
576 JAYLINK_API
const char *jaylink_hardware_type_string(
577 enum jaylink_hardware_type type
);
578 JAYLINK_API
const char *jaylink_target_interface_string(
579 enum jaylink_target_interface iface
);
581 /*--- swd.c -----------------------------------------------------------------*/
583 JAYLINK_API
int jaylink_swd_io(struct jaylink_device_handle
*devh
,
584 const uint8_t *direction
, const uint8_t *out
, uint8_t *in
,
587 /*--- swo.c -----------------------------------------------------------------*/
589 JAYLINK_API
int jaylink_swo_start(struct jaylink_device_handle
*devh
,
590 enum jaylink_swo_mode mode
, uint32_t baudrate
, uint32_t size
);
591 JAYLINK_API
int jaylink_swo_stop(struct jaylink_device_handle
*devh
);
592 JAYLINK_API
int jaylink_swo_read(struct jaylink_device_handle
*devh
,
593 uint8_t *buffer
, uint32_t *length
);
594 JAYLINK_API
int jaylink_swo_get_speeds(struct jaylink_device_handle
*devh
,
595 enum jaylink_swo_mode mode
, struct jaylink_swo_speed
*speed
);
597 /*--- target.c --------------------------------------------------------------*/
599 JAYLINK_API
int jaylink_set_speed(struct jaylink_device_handle
*devh
,
601 JAYLINK_API
int jaylink_get_speeds(struct jaylink_device_handle
*devh
,
602 struct jaylink_speed
*speed
);
603 JAYLINK_API
int jaylink_select_interface(struct jaylink_device_handle
*devh
,
604 enum jaylink_target_interface iface
,
605 enum jaylink_target_interface
*prev_iface
);
606 JAYLINK_API
int jaylink_get_available_interfaces(
607 struct jaylink_device_handle
*devh
, uint32_t *ifaces
);
608 JAYLINK_API
int jaylink_get_selected_interface(
609 struct jaylink_device_handle
*devh
,
610 enum jaylink_target_interface
*iface
);
611 JAYLINK_API
int jaylink_clear_reset(struct jaylink_device_handle
*devh
);
612 JAYLINK_API
int jaylink_set_reset(struct jaylink_device_handle
*devh
);
613 JAYLINK_API
int jaylink_set_target_power(struct jaylink_device_handle
*devh
,
616 /*--- util.c ----------------------------------------------------------------*/
618 JAYLINK_API
bool jaylink_has_cap(const uint8_t *caps
, uint32_t cap
);
620 /*--- version.c -------------------------------------------------------------*/
622 JAYLINK_API
int jaylink_version_package_get_major(void);
623 JAYLINK_API
int jaylink_version_package_get_minor(void);
624 JAYLINK_API
int jaylink_version_package_get_micro(void);
625 JAYLINK_API
const char *jaylink_version_package_get_string(void);
626 JAYLINK_API
int jaylink_version_library_get_current(void);
627 JAYLINK_API
int jaylink_version_library_get_revision(void);
628 JAYLINK_API
int jaylink_version_library_get_age(void);
629 JAYLINK_API
const char *jaylink_version_library_get_string(void);
637 #endif /* LIBJAYLINK_LIBJAYLINK_H */