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"
33 #define CMD_JTAG_IO_V2 0xce
34 #define CMD_JTAG_IO_V3 0xcf
35 #define CMD_JTAG_CLEAR_TRST 0xde
36 #define CMD_JTAG_SET_TRST 0xdf
39 * Error code indicating that there is not enough free memory on the device to
40 * perform the JTAG I/O operation.
42 #define JTAG_IO_ERR_NO_MEMORY 0x06
46 * Perform a JTAG I/O operation.
48 * @note This function must only be used if the #JAYLINK_TIF_JTAG interface is
49 * available and selected. Nevertheless, this function can be used if the
50 * device doesn't have the #JAYLINK_DEV_CAP_SELECT_TIF capability.
52 * @param[in,out] devh Device handle.
53 * @param[in] tms Buffer to read TMS data from.
54 * @param[in] tdi Buffer to read TDI data from.
55 * @param[out] tdo Buffer to store TDO data on success. Its content is
56 * undefined on failure. The buffer must be large enough to
57 * contain at least the specified number of bits to transfer.
58 * @param[in] length Number of bits to transfer.
59 * @param[in] version Version of the JTAG command to use.
61 * @retval JAYLINK_OK Success.
62 * @retval JAYLINK_ERR_ARG Invalid arguments.
63 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
64 * @retval JAYLINK_ERR_IO Input/output error.
65 * @retval JAYLINK_ERR_DEV_NO_MEMORY Not enough memory on the device to perform
67 * @retval JAYLINK_ERR_DEV Unspecified device error.
68 * @retval JAYLINK_ERR Other error conditions.
70 * @see jaylink_select_interface()
71 * @see jaylink_set_speed()
75 JAYLINK_API
int jaylink_jtag_io(struct jaylink_device_handle
*devh
,
76 const uint8_t *tms
, const uint8_t *tdi
, uint8_t *tdo
,
77 uint16_t length
, enum jaylink_jtag_version version
)
80 struct jaylink_context
*ctx
;
87 if (!devh
|| !tms
|| !tdi
|| !tdo
|| !length
)
88 return JAYLINK_ERR_ARG
;
90 num_bytes
= (length
+ 7) / 8;
91 read_length
= num_bytes
;
94 case JAYLINK_JTAG_VERSION_2
:
97 case JAYLINK_JTAG_VERSION_3
:
99 /* In this version, the response includes a status byte. */
103 return JAYLINK_ERR_ARG
;
106 ctx
= devh
->dev
->ctx
;
107 ret
= transport_start_write_read(devh
, 4 + 2 * num_bytes
,
110 if (ret
!= JAYLINK_OK
) {
111 log_err(ctx
, "transport_start_write_read() failed: %s.",
112 jaylink_strerror(ret
));
118 buffer_set_u16(buf
, length
, 2);
120 ret
= transport_write(devh
, buf
, 4);
122 if (ret
!= JAYLINK_OK
) {
123 log_err(ctx
, "transport_write() failed: %s.",
124 jaylink_strerror(ret
));
128 ret
= transport_write(devh
, tms
, num_bytes
);
130 if (ret
!= JAYLINK_OK
) {
131 log_err(ctx
, "transport_write() failed: %s.",
132 jaylink_strerror(ret
));
136 ret
= transport_write(devh
, tdi
, num_bytes
);
138 if (ret
!= JAYLINK_OK
) {
139 log_err(ctx
, "transport_write() failed: %s.",
140 jaylink_strerror(ret
));
144 ret
= transport_read(devh
, tdo
, num_bytes
);
146 if (ret
!= JAYLINK_OK
) {
147 log_err(ctx
, "transport_read() failed: %s.",
148 jaylink_strerror(ret
));
152 if (version
== JAYLINK_JTAG_VERSION_2
)
155 ret
= transport_read(devh
, &status
, 1);
157 if (ret
!= JAYLINK_OK
) {
158 log_err(ctx
, "transport_read() failed: %s.",
159 jaylink_strerror(ret
));
163 if (status
== JTAG_IO_ERR_NO_MEMORY
) {
164 return JAYLINK_ERR_DEV_NO_MEMORY
;
165 } else if (status
> 0) {
166 log_err(ctx
, "JTAG I/O operation failed: 0x%x.", status
);
167 return JAYLINK_ERR_DEV
;
174 * Clear the JTAG test reset (TRST) signal.
176 * @param[in,out] devh Device handle.
178 * @retval JAYLINK_OK Success.
179 * @retval JAYLINK_ERR_ARG Invalid arguments.
180 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
181 * @retval JAYLINK_ERR_IO Input/output error.
182 * @retval JAYLINK_ERR Other error conditions.
186 JAYLINK_API
int jaylink_jtag_clear_trst(struct jaylink_device_handle
*devh
)
189 struct jaylink_context
*ctx
;
193 return JAYLINK_ERR_ARG
;
195 ctx
= devh
->dev
->ctx
;
196 ret
= transport_start_write(devh
, 1, true);
198 if (ret
!= JAYLINK_OK
) {
199 log_err(ctx
, "transport_start_write() failed: %s.",
200 jaylink_strerror(ret
));
204 buf
[0] = CMD_JTAG_CLEAR_TRST
;
206 ret
= transport_write(devh
, buf
, 1);
208 if (ret
!= JAYLINK_OK
) {
209 log_err(ctx
, "transport_write() failed: %s.",
210 jaylink_strerror(ret
));
218 * Set the JTAG test reset (TRST) signal.
220 * @param[in,out] devh Device handle.
222 * @retval JAYLINK_OK Success.
223 * @retval JAYLINK_ERR_ARG Invalid arguments.
224 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
225 * @retval JAYLINK_ERR_IO Input/output error.
226 * @retval JAYLINK_ERR Other error conditions.
230 JAYLINK_API
int jaylink_jtag_set_trst(struct jaylink_device_handle
*devh
)
233 struct jaylink_context
*ctx
;
237 return JAYLINK_ERR_ARG
;
239 ctx
= devh
->dev
->ctx
;
240 ret
= transport_start_write(devh
, 1, true);
242 if (ret
!= JAYLINK_OK
) {
243 log_err(ctx
, "transport_start_write() failed: %s.",
244 jaylink_strerror(ret
));
248 buf
[0] = CMD_JTAG_SET_TRST
;
250 ret
= transport_write(devh
, buf
, 1);
252 if (ret
!= JAYLINK_OK
) {
253 log_err(ctx
, "transport_write() failed: %s.",
254 jaylink_strerror(ret
));