2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2015 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
26 #include <sys/types.h>
31 * Public libjaylink header file to be used by applications.
34 /** Error codes returned by libjaylink functions. */
38 /** Unspecific error. */
40 /** Memory allocation error. */
41 JAYLINK_ERR_MALLOC
= -2,
42 /** Invalid argument. */
44 /** Timeout occurred. */
45 JAYLINK_ERR_TIMEOUT
= -4,
46 /** Protocol violation. */
47 JAYLINK_ERR_PROTO
= -5,
48 /** Entity not available. */
49 JAYLINK_ERR_NOT_AVAILABLE
= -6,
50 /** Operation not supported. */
51 JAYLINK_ERR_NOT_SUPPORTED
= -7,
52 /** Device: unspecified error. */
53 JAYLINK_ERR_DEV
= -1000,
54 /** Device: operation not supported. */
55 JAYLINK_ERR_DEV_NOT_SUPPORTED
= -1001,
56 /** Device: entity not available. */
57 JAYLINK_ERR_DEV_NOT_AVAILABLE
= -1002
60 /** libjaylink log levels. */
61 enum jaylink_log_level
{
62 /** Output no messages. */
63 JAYLINK_LOG_LEVEL_NONE
= 0,
64 /** Output error messages. */
65 JAYLINK_LOG_LEVEL_ERROR
= 1,
66 /** Output warnings. */
67 JAYLINK_LOG_LEVEL_WARNING
= 2,
68 /** Output informational messages. */
69 JAYLINK_LOG_LEVEL_INFO
= 3,
70 /** Output debug messages. */
71 JAYLINK_LOG_LEVEL_DEBUG
= 4
74 /** Default libjaylink log domain. */
75 #define JAYLINK_LOG_DOMAIN_DEFAULT "jaylink: "
77 /** Maximum length of a libjaylink log domain in bytes. */
78 #define JAYLINK_LOG_DOMAIN_MAX_LENGTH 32
80 /** Host interfaces. */
81 enum jaylink_host_interface
{
82 /** Universal Serial Bus (USB). */
83 JAYLINK_HIF_USB
= (1 << 0)
89 * The USB address is a way to identify USB devices and is related to the USB
90 * Product ID (PID) of a device.
92 enum jaylink_usb_address
{
93 /** USB address 0 (Product ID 0x0101). */
94 JAYLINK_USB_ADDRESS_0
= 0,
95 /** USB address 1 (Product ID 0x0102). */
96 JAYLINK_USB_ADDRESS_1
= 1,
97 /** USB address 2 (Product ID 0x0103). */
98 JAYLINK_USB_ADDRESS_2
= 2,
99 /** USB address 3 (Product ID 0x0104). */
100 JAYLINK_USB_ADDRESS_3
= 3
103 /** Device capabilities. */
104 enum jaylink_device_capability
{
105 /** Device supports retrieval of the hardware version. */
106 JAYLINK_DEV_CAP_GET_HW_VERSION
= 1,
107 /** Device supports adaptive clocking. */
108 JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING
= 3,
109 /** Device supports reading configuration data. */
110 JAYLINK_DEV_CAP_READ_CONFIG
= 4,
111 /** Device supports writing configuration data. */
112 JAYLINK_DEV_CAP_WRITE_CONFIG
= 5,
113 /** Device supports retrieval of target interface speeds. */
114 JAYLINK_DEV_CAP_GET_SPEEDS
= 9,
115 /** Device supports retrieval of free memory size. */
116 JAYLINK_DEV_CAP_GET_FREE_MEMORY
= 11,
117 /** Device supports retrieval of hardware information. */
118 JAYLINK_DEV_CAP_GET_HW_INFO
= 12,
119 /** Device supports the setting of the target power supply. */
120 JAYLINK_DEV_CAP_SET_TARGET_POWER
= 13,
121 /** Device supports target interface selection. */
122 JAYLINK_DEV_CAP_SELECT_TIF
= 17,
123 /** Device supports capturing of SWO trace data. */
124 JAYLINK_DEV_CAP_SWO
= 23,
125 /** Device supports file I/O operations. */
126 JAYLINK_DEV_CAP_FILE_IO
= 26,
127 /** Device supports registration of connections. */
128 JAYLINK_DEV_CAP_REGISTER
= 27,
129 /** Device supports retrieval of extended capabilities. */
130 JAYLINK_DEV_CAP_GET_EXT_CAPS
= 31,
131 /** Device supports EMUCOM. */
132 JAYLINK_DEV_CAP_EMUCOM
= 33,
133 /** Device supports ethernet connectivity. */
134 JAYLINK_DEV_CAP_ETHERNET
= 38
137 /** Hardware information. */
138 enum jaylink_hardware_info
{
140 * Status of the target power supply.
142 * This indicates whether the target power supply on pin 19 of the
143 * 20-pin JTAG / SWD connector is enabled or disabled.
145 * @see jaylink_set_target_power() to set the target power supply.
147 JAYLINK_HW_INFO_TARGET_POWER
= (1 << 0),
148 /** Current consumption of the target in mA. */
149 JAYLINK_HW_INFO_ITARGET
= (1 << 2),
150 /** Peak current consumption of the target in mA. */
151 JAYLINK_HW_INFO_ITARGET_PEAK
= (1 << 3)
154 /** Device hardware types. */
155 enum jaylink_hardware_type
{
157 JAYLINK_HW_TYPE_BASE
= 0
160 /** Target interfaces. */
161 enum jaylink_target_interface
{
162 /** Joint Test Action Group, IEEE 1149.1 (JTAG). */
163 JAYLINK_TIF_JTAG
= 0,
164 /** Serial Wire Debug (SWD). */
166 /** Background Debug Mode 3 (BDM3). */
167 JAYLINK_TIF_BDM3
= 2,
168 /** Renesas’ single-wire debug interface (FINE). */
169 JAYLINK_TIF_FINE
= 3,
170 /** 2-wire JTAG for PIC32 compliant devices. */
171 JAYLINK_TIF_2W_JTAG_PIC32
= 4,
173 /** <i>Helper which must be always the last element</i>. */
177 /** Maximum valid target interface number. */
178 #define JAYLINK_TIF_MAX (__JAYLINK_TIF_MAX - 1)
181 * JTAG command versions.
183 * The JTAG command version only affects the device and the communication
184 * protocol. The behaviour of a JTAG operation is not affected at all.
186 enum jaylink_jtag_version
{
188 * JTAG command version 2.
190 * This version is obsolete for major hardware version 5 and above. Use
191 * #JAYLINK_JTAG_V3 for these versions instead.
194 /** JTAG command version 3. */
198 /** Serial Wire Output (SWO) capture modes. */
199 enum jaylink_swo_mode
{
200 /** UART capture mode. */
201 JAYLINK_SWO_MODE_UART
= 0
204 /** Target interface speed information. */
205 struct jaylink_speed
{
206 /** Base frequency in Hz. */
208 /** Minimum frequency divider. */
212 /** Serial Wire Output (SWO) speed information. */
213 struct jaylink_swo_speed
{
214 /** Base frequency in Hz. */
216 /** Minimum frequency divider. */
218 /** Maximum frequency divider. */
220 /** Minimum prescaler. */
221 uint32_t min_prescaler
;
222 /** Maximum prescaler. */
223 uint32_t max_prescaler
;
226 /** Device hardware version. */
227 struct jaylink_hardware_version
{
228 /** Hardware type. */
229 enum jaylink_hardware_type type
;
230 /** Major version. */
232 /** Minor version. */
234 /** Revision number. */
238 /** Device hardware status. */
239 struct jaylink_hardware_status
{
240 /** Target reference voltage in mV. */
241 uint16_t target_voltage
;
242 /** TCK pin state. */
244 /** TDI pin state. */
246 /** TDO pin state. */
248 /** TMS pin state. */
250 /** TRES pin state. */
252 /** TRST pin state. */
256 /** Device connection. */
257 struct jaylink_connection
{
263 * Identification of the client process. Usually this is the
264 * Process ID (PID) of the client process in an arbitrary format.
270 * IP address of the client in network byte order.
278 * Timestamp of the last registration in milliseconds.
280 * The timestamp is relative to the time the device was powered up.
285 /** Target interface speed value for adaptive clocking. */
286 #define JAYLINK_SPEED_ADAPTIVE_CLOCKING 0xffff
288 /** Size of the device configuration data in bytes. */
289 #define JAYLINK_DEV_CONFIG_SIZE 256
291 /** Number of bytes required to store device capabilities. */
292 #define JAYLINK_DEV_CAPS_SIZE 4
294 /** Number of bytes required to store extended device capabilities. */
295 #define JAYLINK_DEV_EXT_CAPS_SIZE 32
297 /** Maximum number of connections that can be registered on a device. */
298 #define JAYLINK_MAX_CONNECTIONS 16
300 /** Maximum length of a filename in bytes. */
301 #define JAYLINK_FILE_NAME_MAX_LENGTH 255
303 /** Maximum transfer size for a file in bytes. */
304 #define JAYLINK_FILE_MAX_TRANSFER_SIZE 0x100000
307 * EMUCOM channel with the system time of the device in milliseconds.
309 * The channel is read-only and the time is encoded in 4 bytes. The byte order
312 #define JAYLINK_EMUCOM_CHANNEL_TIME 0x0
315 * Offset of EMUCOM user channels.
317 * User channels are available to implement vendor and/or device specific
318 * functionalities. All channels below are reserved.
320 #define JAYLINK_EMUCOM_CHANNEL_USER 0x10000
323 * @struct jaylink_context
325 * Opaque structure representing a libjaylink context.
327 struct jaylink_context
;
330 * @struct jaylink_device
332 * Opaque structure representing a device.
334 struct jaylink_device
;
337 * @struct jaylink_device_handle
339 * Opaque structure representing a handle of a device.
341 struct jaylink_device_handle
;
343 /** Macro to mark public libjaylink API symbol. */
347 #define JAYLINK_API __attribute__ ((visibility ("default")))
350 /** Log callback function type. */
351 typedef int (*jaylink_log_callback
)(const struct jaylink_context
*ctx
,
352 enum jaylink_log_level level
, const char *format
, va_list args
,
355 /*--- core.c ----------------------------------------------------------------*/
357 JAYLINK_API
int jaylink_init(struct jaylink_context
**ctx
);
358 JAYLINK_API
void jaylink_exit(struct jaylink_context
*ctx
);
360 /*--- device.c --------------------------------------------------------------*/
362 JAYLINK_API ssize_t
jaylink_get_device_list(struct jaylink_context
*ctx
,
363 struct jaylink_device
***devices
);
364 JAYLINK_API
void jaylink_free_device_list(struct jaylink_device
**devices
,
366 JAYLINK_API
int jaylink_device_get_host_interface(
367 const struct jaylink_device
*dev
,
368 enum jaylink_host_interface
*interface
);
369 JAYLINK_API
int jaylink_device_get_serial_number(
370 const struct jaylink_device
*dev
, uint32_t *serial_number
);
371 JAYLINK_API
int jaylink_device_get_usb_address(const struct jaylink_device
*dev
,
372 enum jaylink_usb_address
*address
);
373 JAYLINK_API
struct jaylink_device
*jaylink_ref_device(
374 struct jaylink_device
*dev
);
375 JAYLINK_API
void jaylink_unref_device(struct jaylink_device
*dev
);
376 JAYLINK_API
int jaylink_open(struct jaylink_device
*dev
,
377 struct jaylink_device_handle
**devh
);
378 JAYLINK_API
void jaylink_close(struct jaylink_device_handle
*devh
);
379 JAYLINK_API
struct jaylink_device
*jaylink_get_device(
380 struct jaylink_device_handle
*devh
);
381 JAYLINK_API
int jaylink_get_firmware_version(struct jaylink_device_handle
*devh
,
382 char **version
, size_t *length
);
383 JAYLINK_API
int jaylink_get_hardware_info(struct jaylink_device_handle
*devh
,
384 uint32_t mask
, uint32_t *info
);
385 JAYLINK_API
int jaylink_get_hardware_version(struct jaylink_device_handle
*devh
,
386 struct jaylink_hardware_version
*version
);
387 JAYLINK_API
int jaylink_get_hardware_status(struct jaylink_device_handle
*devh
,
388 struct jaylink_hardware_status
*status
);
389 JAYLINK_API
int jaylink_get_caps(struct jaylink_device_handle
*devh
,
391 JAYLINK_API
int jaylink_get_extended_caps(struct jaylink_device_handle
*devh
,
393 JAYLINK_API
int jaylink_get_free_memory(struct jaylink_device_handle
*devh
,
395 JAYLINK_API
int jaylink_read_raw_config(struct jaylink_device_handle
*devh
,
397 JAYLINK_API
int jaylink_write_raw_config(struct jaylink_device_handle
*devh
,
398 const uint8_t *config
);
399 JAYLINK_API
int jaylink_register(struct jaylink_device_handle
*devh
,
400 struct jaylink_connection
*connection
,
401 struct jaylink_connection
*connections
, uint8_t *info
,
402 uint16_t *info_size
);
403 JAYLINK_API
int jaylink_unregister(struct jaylink_device_handle
*devh
,
404 const struct jaylink_connection
*connection
,
405 struct jaylink_connection
*connections
, uint8_t *info
,
406 uint16_t *info_size
);
408 /*--- emucom.c --------------------------------------------------------------*/
410 JAYLINK_API
int jaylink_emucom_read(struct jaylink_device_handle
*devh
,
411 uint32_t channel
, uint8_t *buffer
, uint32_t *length
);
412 JAYLINK_API
int jaylink_emucom_write(struct jaylink_device_handle
*devh
,
413 uint32_t channel
, const uint8_t *buffer
, uint32_t *length
);
415 /*--- error.c ---------------------------------------------------------------*/
417 JAYLINK_API
const char *jaylink_strerror(int error_code
);
418 JAYLINK_API
const char *jaylink_strerror_name(int error_code
);
420 /*--- fileio.c --------------------------------------------------------------*/
422 JAYLINK_API
int jaylink_file_read(struct jaylink_device_handle
*devh
,
423 const char *filename
, uint8_t *buffer
, uint32_t offset
,
425 JAYLINK_API
int jaylink_file_write(struct jaylink_device_handle
*devh
,
426 const char *filename
, const uint8_t *buffer
, uint32_t offset
,
428 JAYLINK_API
int jaylink_file_get_size(struct jaylink_device_handle
*devh
,
429 const char *filename
, uint32_t *size
);
430 JAYLINK_API
int jaylink_file_delete(struct jaylink_device_handle
*devh
,
431 const char *filename
);
433 /*--- jtag.c ----------------------------------------------------------------*/
435 JAYLINK_API
int jaylink_jtag_io(struct jaylink_device_handle
*devh
,
436 const uint8_t *tms
, const uint8_t *tdi
, uint8_t *tdo
,
437 uint16_t length
, enum jaylink_jtag_version version
);
438 JAYLINK_API
int jaylink_jtag_clear_trst(struct jaylink_device_handle
*devh
);
439 JAYLINK_API
int jaylink_jtag_set_trst(struct jaylink_device_handle
*devh
);
441 /*--- log.c -----------------------------------------------------------------*/
443 JAYLINK_API
int jaylink_log_set_level(struct jaylink_context
*ctx
,
444 enum jaylink_log_level level
);
445 JAYLINK_API
int jaylink_log_get_level(const struct jaylink_context
*ctx
);
446 JAYLINK_API
int jaylink_log_set_callback(struct jaylink_context
*ctx
,
447 jaylink_log_callback callback
, void *user_data
);
448 JAYLINK_API
int jaylink_log_set_domain(struct jaylink_context
*ctx
,
450 JAYLINK_API
const char *jaylink_log_get_domain(
451 const struct jaylink_context
*ctx
);
453 /*--- swd.c -----------------------------------------------------------------*/
455 JAYLINK_API
int jaylink_swd_io(struct jaylink_device_handle
*devh
,
456 const uint8_t *direction
, const uint8_t *out
, uint8_t *in
,
459 /*--- swo.c -----------------------------------------------------------------*/
461 JAYLINK_API
int jaylink_swo_start(struct jaylink_device_handle
*devh
,
462 enum jaylink_swo_mode mode
, uint32_t baudrate
, uint32_t size
);
463 JAYLINK_API
int jaylink_swo_stop(struct jaylink_device_handle
*devh
);
464 JAYLINK_API
int jaylink_swo_read(struct jaylink_device_handle
*devh
,
465 uint8_t *buffer
, uint32_t *length
);
466 JAYLINK_API
int jaylink_swo_get_speeds(struct jaylink_device_handle
*devh
,
467 enum jaylink_swo_mode mode
, struct jaylink_swo_speed
*speed
);
469 /*--- target.c --------------------------------------------------------------*/
471 JAYLINK_API
int jaylink_set_speed(struct jaylink_device_handle
*devh
,
473 JAYLINK_API
int jaylink_get_speeds(struct jaylink_device_handle
*devh
,
474 struct jaylink_speed
*speed
);
475 JAYLINK_API
int jaylink_select_interface(struct jaylink_device_handle
*devh
,
476 enum jaylink_target_interface interface
,
477 enum jaylink_target_interface
*prev_interface
);
478 JAYLINK_API
int jaylink_get_available_interfaces(
479 struct jaylink_device_handle
*devh
, uint32_t *interfaces
);
480 JAYLINK_API
int jaylink_get_selected_interface(
481 struct jaylink_device_handle
*devh
,
482 enum jaylink_target_interface
*interface
);
483 JAYLINK_API
int jaylink_clear_reset(struct jaylink_device_handle
*devh
);
484 JAYLINK_API
int jaylink_set_reset(struct jaylink_device_handle
*devh
);
485 JAYLINK_API
int jaylink_set_target_power(struct jaylink_device_handle
*devh
,
488 /*--- util.c ----------------------------------------------------------------*/
490 JAYLINK_API
bool jaylink_has_cap(const uint8_t *caps
, uint32_t cap
);
492 /*--- version.c -------------------------------------------------------------*/
494 JAYLINK_API
int jaylink_version_package_get_major(void);
495 JAYLINK_API
int jaylink_version_package_get_minor(void);
496 JAYLINK_API
int jaylink_version_package_get_micro(void);
497 JAYLINK_API
const char *jaylink_version_package_get_string(void);
498 JAYLINK_API
int jaylink_version_lib_get_current(void);
499 JAYLINK_API
int jaylink_version_lib_get_revision(void);
500 JAYLINK_API
int jaylink_version_lib_get_age(void);
501 JAYLINK_API
const char *jaylink_version_lib_get_string(void);
505 #endif /* LIBJAYLINK_LIBJAYLINK_H */