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/>.
23 #include "libjaylink.h"
24 #include "libjaylink-internal.h"
29 * Target related functions.
33 #define CMD_SET_SPEED 0x05
34 #define CMD_SET_TARGET_POWER 0x08
35 #define CMD_GET_SPEEDS 0xc0
36 #define CMD_SELECT_TIF 0xc7
37 #define CMD_CLEAR_RESET 0xdc
38 #define CMD_SET_RESET 0xdd
40 #define TIF_GET_SELECTED 0xfe
41 #define TIF_GET_AVAILABLE 0xff
45 * Set the target interface speed.
47 * @param[in,out] devh Device handle.
48 * @param[in] speed Speed in kHz or #JAYLINK_SPEED_ADAPTIVE_CLOCKING for
49 * adaptive clocking. Speed of 0 kHz is not allowed and
50 * adaptive clocking must only be used if the device has the
51 * #JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING capability.
53 * @retval JAYLINK_OK Success.
54 * @retval JAYLINK_ERR_ARG Invalid arguments.
55 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
56 * @retval JAYLINK_ERR_IO Input/output error.
57 * @retval JAYLINK_ERR Other error conditions.
59 * @see jaylink_get_speeds()
63 JAYLINK_API
int jaylink_set_speed(struct jaylink_device_handle
*devh
,
67 struct jaylink_context
*ctx
;
71 return JAYLINK_ERR_ARG
;
74 ret
= transport_start_write(devh
, 3, true);
76 if (ret
!= JAYLINK_OK
) {
77 log_err(ctx
, "transport_start_write() failed: %s",
78 jaylink_strerror(ret
));
82 buf
[0] = CMD_SET_SPEED
;
83 buffer_set_u16(buf
, speed
, 1);
85 ret
= transport_write(devh
, buf
, 3);
87 if (ret
!= JAYLINK_OK
) {
88 log_err(ctx
, "transport_write() failed: %s",
89 jaylink_strerror(ret
));
97 * Retrieve target interface speeds.
99 * The speeds are applicable for the currently selected target interface only
100 * and calculated as follows:
103 * <tt>speeds = @a freq / n</tt> with <tt>n >= @a div</tt>, where @p n is an
106 * Assuming, for example, a base frequency @a freq of 4 MHz and a minimum
107 * divider @a div of 4 then the highest possible target interface speed is
108 * 4 MHz / 4 = 1 MHz. The next highest speed is 800 kHz for a divider of 5, and
111 * @note This function must only be used if the device has the
112 * #JAYLINK_DEV_CAP_GET_SPEEDS capability.
114 * @param[in,out] devh Device handle.
115 * @param[out] speed Speed information on success, and undefined on failure.
117 * @retval JAYLINK_OK Success.
118 * @retval JAYLINK_ERR_ARG Invalid arguments.
119 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
120 * @retval JAYLINK_ERR_PROTO Protocol violation.
121 * @retval JAYLINK_ERR_IO Input/output error.
122 * @retval JAYLINK_ERR Other error conditions.
124 * @see jaylink_select_interface()
128 JAYLINK_API
int jaylink_get_speeds(struct jaylink_device_handle
*devh
,
129 struct jaylink_speed
*speed
)
132 struct jaylink_context
*ctx
;
137 return JAYLINK_ERR_ARG
;
139 ctx
= devh
->dev
->ctx
;
140 ret
= transport_start_write_read(devh
, 1, 6, true);
142 if (ret
!= JAYLINK_OK
) {
143 log_err(ctx
, "transport_start_write_read() failed: %s",
144 jaylink_strerror(ret
));
148 buf
[0] = CMD_GET_SPEEDS
;
150 ret
= transport_write(devh
, buf
, 1);
152 if (ret
!= JAYLINK_OK
) {
153 log_err(ctx
, "transport_write() failed: %s",
154 jaylink_strerror(ret
));
158 ret
= transport_read(devh
, buf
, 6);
160 if (ret
!= JAYLINK_OK
) {
161 log_err(ctx
, "transport_read() failed: %s",
162 jaylink_strerror(ret
));
166 div
= buffer_get_u16(buf
, 4);
169 log_err(ctx
, "Minimum frequency divider is zero");
170 return JAYLINK_ERR_PROTO
;
173 speed
->freq
= buffer_get_u32(buf
, 0);
180 * Select the target interface.
182 * @note This function must only be used if the device has the
183 * #JAYLINK_DEV_CAP_SELECT_TIF capability.
185 * @warning This function may return a value for @p prev_iface which is not
186 * covered by #jaylink_target_interface.
188 * @param[in,out] devh Device handle.
189 * @param[in] iface Target interface to select.
190 * @param[out] prev_iface Previously selected target interface on success, and
191 * undefined on failure. Can be NULL.
193 * @retval JAYLINK_OK Success.
194 * @retval JAYLINK_ERR_ARG Invalid arguments.
195 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
196 * @retval JAYLINK_ERR_IO Input/output error.
197 * @retval JAYLINK_ERR Other error conditions.
199 * @see jaylink_get_available_interfaces()
203 JAYLINK_API
int jaylink_select_interface(struct jaylink_device_handle
*devh
,
204 enum jaylink_target_interface iface
,
205 enum jaylink_target_interface
*prev_iface
)
208 struct jaylink_context
*ctx
;
212 return JAYLINK_ERR_ARG
;
215 case JAYLINK_TIF_JTAG
:
216 case JAYLINK_TIF_SWD
:
217 case JAYLINK_TIF_BDM3
:
218 case JAYLINK_TIF_FINE
:
219 case JAYLINK_TIF_2W_JTAG_PIC32
:
220 case JAYLINK_TIF_CJTAG
:
223 return JAYLINK_ERR_ARG
;
226 ctx
= devh
->dev
->ctx
;
227 ret
= transport_start_write_read(devh
, 2, 4, true);
229 if (ret
!= JAYLINK_OK
) {
230 log_err(ctx
, "transport_start_write_read() failed: %s",
231 jaylink_strerror(ret
));
235 buf
[0] = CMD_SELECT_TIF
;
238 ret
= transport_write(devh
, buf
, 2);
240 if (ret
!= JAYLINK_OK
) {
241 log_err(ctx
, "transport_write() failed: %s",
242 jaylink_strerror(ret
));
246 ret
= transport_read(devh
, buf
, 4);
248 if (ret
!= JAYLINK_OK
) {
249 log_err(ctx
, "transport_read() failed: %s",
250 jaylink_strerror(ret
));
255 *prev_iface
= buffer_get_u32(buf
, 0);
261 * Retrieve the available target interfaces.
263 * The target interfaces are stored in a 32-bit bit field where each individual
264 * bit represents a target interface. A set bit indicates an available target
265 * interface. See #jaylink_target_interface for a description of the target
266 * interfaces and their bit positions.
268 * @note This function must only be used if the device has the
269 * #JAYLINK_DEV_CAP_SELECT_TIF capability.
271 * @param[in,out] devh Device handle.
272 * @param[out] ifaces Target interfaces on success, and undefined on failure.
274 * @retval JAYLINK_OK Success.
275 * @retval JAYLINK_ERR_ARG Invalid arguments.
276 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
277 * @retval JAYLINK_ERR_IO Input/output error.
278 * @retval JAYLINK_ERR Other error conditions.
280 * @see jaylink_select_interface()
284 JAYLINK_API
int jaylink_get_available_interfaces(
285 struct jaylink_device_handle
*devh
, uint32_t *ifaces
)
288 struct jaylink_context
*ctx
;
291 if (!devh
|| !ifaces
)
292 return JAYLINK_ERR_ARG
;
294 ctx
= devh
->dev
->ctx
;
295 ret
= transport_start_write_read(devh
, 2, 4, true);
297 if (ret
!= JAYLINK_OK
) {
298 log_err(ctx
, "transport_start_write_read() failed: %s",
299 jaylink_strerror(ret
));
303 buf
[0] = CMD_SELECT_TIF
;
304 buf
[1] = TIF_GET_AVAILABLE
;
306 ret
= transport_write(devh
, buf
, 2);
308 if (ret
!= JAYLINK_OK
) {
309 log_err(ctx
, "transport_write() failed: %s",
310 jaylink_strerror(ret
));
314 ret
= transport_read(devh
, buf
, 4);
316 if (ret
!= JAYLINK_OK
) {
317 log_err(ctx
, "transport_read() failed: %s",
318 jaylink_strerror(ret
));
322 *ifaces
= buffer_get_u32(buf
, 0);
328 * Retrieve the selected target interface.
330 * @note This function must only be used if the device has the
331 * #JAYLINK_DEV_CAP_SELECT_TIF capability.
333 * @warning This function may return a value for @p iface which is not covered
334 * by #jaylink_target_interface.
336 * @param[in,out] devh Device handle.
337 * @param[out] iface Selected target interface on success, and undefined on
340 * @retval JAYLINK_OK Success.
341 * @retval JAYLINK_ERR_ARG Invalid arguments.
342 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
343 * @retval JAYLINK_ERR_IO Input/output error.
344 * @retval JAYLINK_ERR Other error conditions.
346 * @see jaylink_select_interface()
350 JAYLINK_API
int jaylink_get_selected_interface(
351 struct jaylink_device_handle
*devh
,
352 enum jaylink_target_interface
*iface
)
355 struct jaylink_context
*ctx
;
359 return JAYLINK_ERR_ARG
;
361 ctx
= devh
->dev
->ctx
;
362 ret
= transport_start_write_read(devh
, 2, 4, true);
364 if (ret
!= JAYLINK_OK
) {
365 log_err(ctx
, "transport_start_write_read() failed: %s",
366 jaylink_strerror(ret
));
370 buf
[0] = CMD_SELECT_TIF
;
371 buf
[1] = TIF_GET_SELECTED
;
373 ret
= transport_write(devh
, buf
, 2);
375 if (ret
!= JAYLINK_OK
) {
376 log_err(ctx
, "transport_write() failed: %s",
377 jaylink_strerror(ret
));
381 ret
= transport_read(devh
, buf
, 4);
383 if (ret
!= JAYLINK_OK
) {
384 log_err(ctx
, "transport_read() failed: %s",
385 jaylink_strerror(ret
));
389 *iface
= buffer_get_u32(buf
, 0);
395 * Clear the target reset signal.
397 * @param[in,out] devh Device handle.
399 * @retval JAYLINK_OK Success.
400 * @retval JAYLINK_ERR_ARG Invalid arguments.
401 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
402 * @retval JAYLINK_ERR_IO Input/output error.
403 * @retval JAYLINK_ERR Other error conditions.
407 JAYLINK_API
int jaylink_clear_reset(struct jaylink_device_handle
*devh
)
410 struct jaylink_context
*ctx
;
414 return JAYLINK_ERR_ARG
;
416 ctx
= devh
->dev
->ctx
;
417 ret
= transport_start_write(devh
, 1, true);
419 if (ret
!= JAYLINK_OK
) {
420 log_err(ctx
, "transport_start_write() failed: %s",
421 jaylink_strerror(ret
));
425 buf
[0] = CMD_CLEAR_RESET
;
427 ret
= transport_write(devh
, buf
, 1);
429 if (ret
!= JAYLINK_OK
) {
430 log_err(ctx
, "transport_write() failed: %s",
431 jaylink_strerror(ret
));
439 * Set the target reset signal.
441 * @param[in,out] devh Device handle.
443 * @retval JAYLINK_OK Success.
444 * @retval JAYLINK_ERR_ARG Invalid arguments.
445 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
446 * @retval JAYLINK_ERR_IO Input/output error.
447 * @retval JAYLINK_ERR Other error conditions.
451 JAYLINK_API
int jaylink_set_reset(struct jaylink_device_handle
*devh
)
454 struct jaylink_context
*ctx
;
458 return JAYLINK_ERR_ARG
;
460 ctx
= devh
->dev
->ctx
;
461 ret
= transport_start_write(devh
, 1, true);
463 if (ret
!= JAYLINK_OK
) {
464 log_err(ctx
, "transport_start_write() failed: %s",
465 jaylink_strerror(ret
));
469 buf
[0] = CMD_SET_RESET
;
471 ret
= transport_write(devh
, buf
, 1);
473 if (ret
!= JAYLINK_OK
) {
474 log_err(ctx
, "transport_write() failed: %s",
475 jaylink_strerror(ret
));
483 * Set the target power supply.
485 * If enabled, the target is supplied with 5 V from pin 19 of the 20-pin
486 * JTAG / SWD connector.
488 * @note This function must only be used if the device has the
489 * #JAYLINK_DEV_CAP_SET_TARGET_POWER capability.
491 * @param[in,out] devh Device handle.
492 * @param[in] enable Determines whether to enable or disable the target power
495 * @retval JAYLINK_OK Success.
496 * @retval JAYLINK_ERR_ARG Invalid arguments.
497 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
498 * @retval JAYLINK_ERR_IO Input/output error.
499 * @retval JAYLINK_ERR Other error conditions.
503 JAYLINK_API
int jaylink_set_target_power(struct jaylink_device_handle
*devh
,
507 struct jaylink_context
*ctx
;
511 return JAYLINK_ERR_ARG
;
513 ctx
= devh
->dev
->ctx
;
514 ret
= transport_start_write(devh
, 2, true);
516 if (ret
!= JAYLINK_OK
) {
517 log_err(ctx
, "transport_start_wrte() failed: %s",
518 jaylink_strerror(ret
));
522 buf
[0] = CMD_SET_TARGET_POWER
;
525 ret
= transport_write(devh
, buf
, 2);
527 if (ret
!= JAYLINK_OK
) {
528 log_err(ctx
, "transport_write() failed: %s",
529 jaylink_strerror(ret
));