1b0733e92443fe484ee714db9140678e5aad756e
[openocd.git] / src / jtag / drivers / versaloon / usbtoxxx / usbtoswd.c
blob1b0733e92443fe484ee714db9140678e5aad756e
1 /***************************************************************************
2 * Copyright (C) 2009 - 2010 by Simon Qian <SimonQian@SimonQian.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <stdlib.h>
25 #include <string.h>
27 #include "../versaloon_include.h"
28 #include "../versaloon.h"
29 #include "../versaloon_internal.h"
30 #include "usbtoxxx.h"
31 #include "usbtoxxx_internal.h"
33 RESULT usbtoswd_callback(void *p, uint8_t *src, uint8_t *processed)
35 struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p;
37 processed = processed;
39 if (pending->extra_data != NULL)
40 *((uint8_t *)pending->extra_data) = src[0];
42 return ERROR_OK;
45 RESULT usbtoswd_init(uint8_t interface_index)
47 return usbtoxxx_init_command(USB_TO_SWD, interface_index);
50 RESULT usbtoswd_fini(uint8_t interface_index)
52 return usbtoxxx_fini_command(USB_TO_SWD, interface_index);
55 RESULT usbtoswd_config(uint8_t interface_index, uint8_t trn, uint16_t retry,
56 uint16_t dly)
58 uint8_t cfg_buf[5];
60 #if PARAM_CHECK
61 if (interface_index > 7) {
62 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
63 return ERROR_FAIL;
65 #endif
67 cfg_buf[0] = trn;
68 SET_LE_U16(&cfg_buf[1], retry);
69 SET_LE_U16(&cfg_buf[3], dly);
71 return usbtoxxx_conf_command(USB_TO_SWD, interface_index, cfg_buf, 5);
74 RESULT usbtoswd_seqout(uint8_t interface_index, uint8_t *data, uint16_t bitlen)
76 uint16_t bytelen = (bitlen + 7) >> 3;
78 #if PARAM_CHECK
79 if (interface_index > 7) {
80 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
81 return ERROR_FAIL;
83 #endif
85 SET_LE_U16(&versaloon_cmd_buf[0], bitlen);
86 memcpy(versaloon_cmd_buf + 2, data, bytelen);
88 return usbtoxxx_out_command(USB_TO_SWD, interface_index,
89 versaloon_cmd_buf, bytelen + 2, 0);
92 RESULT usbtoswd_seqin(uint8_t interface_index, uint8_t *data, uint16_t bitlen)
94 uint16_t bytelen = (bitlen + 7) >> 3;
95 uint8_t buff[2];
97 #if PARAM_CHECK
98 if (interface_index > 7) {
99 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
100 return ERROR_FAIL;
102 #endif
104 SET_LE_U16(&buff[0], bitlen);
106 return usbtoxxx_in_command(USB_TO_SWD, interface_index, buff, 2, bytelen,
107 data, 0, bytelen, 0);
110 RESULT usbtoswd_transact(uint8_t interface_index, uint8_t request,
111 uint32_t *data, uint8_t *ack)
113 uint8_t parity;
114 uint8_t buff[5];
116 #if PARAM_CHECK
117 if (interface_index > 7) {
118 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
119 return ERROR_FAIL;
121 #endif
123 parity = (request >> 1) & 1;
124 parity += (request >> 2) & 1;
125 parity += (request >> 3) & 1;
126 parity += (request >> 4) & 1;
127 parity &= 1;
128 buff[0] = (request | 0x81 | (parity << 5)) & ~0x40;
129 if (data != NULL)
130 SET_LE_U32(&buff[1], *data);
131 else
132 memset(buff + 1, 0, 4);
134 versaloon_set_extra_data(ack);
135 versaloon_set_callback(usbtoswd_callback);
136 if (request & 0x04) {
137 /* read */
138 return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5,
139 (uint8_t *)data, 1, 4, 0);
140 } else {
141 /* write */
142 return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5,
143 NULL, 0, 0, 0);