OpenOCD
usbtoswd.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2009 - 2010 by Simon Qian <SimonQian@SimonQian.com> *
5  ***************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include "../versaloon_include.h"
15 #include "../versaloon.h"
16 #include "../versaloon_internal.h"
17 #include "usbtoxxx.h"
18 #include "usbtoxxx_internal.h"
19 
20 static RESULT usbtoswd_read_callback(void *p, uint8_t *src, uint8_t *processed)
21 {
22  struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p;
23 
24  if (pending->extra_data)
25  *((uint8_t *)pending->extra_data) = src[0];
26 
27  return ERROR_OK;
28 }
29 
30 static RESULT usbtoswd_write_callback(void *p, uint8_t *src, uint8_t *processed)
31 {
32  struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p;
33 
34  if (pending->extra_data)
35  *((uint8_t *)pending->extra_data) = src[0];
36 
37  /* mark it processed to ignore other input data */
38  *processed = 1;
39  return ERROR_OK;
40 }
41 
42 RESULT usbtoswd_init(uint8_t interface_index)
43 {
44  return usbtoxxx_init_command(USB_TO_SWD, interface_index);
45 }
46 
47 RESULT usbtoswd_fini(uint8_t interface_index)
48 {
49  return usbtoxxx_fini_command(USB_TO_SWD, interface_index);
50 }
51 
52 RESULT usbtoswd_config(uint8_t interface_index, uint8_t trn, uint16_t retry,
53  uint16_t dly)
54 {
55  uint8_t cfg_buf[5];
56 
57 #if PARAM_CHECK
58  if (interface_index > 7) {
59  LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
60  return ERROR_FAIL;
61  }
62 #endif
63 
64  cfg_buf[0] = trn;
65  SET_LE_U16(&cfg_buf[1], retry);
66  SET_LE_U16(&cfg_buf[3], dly);
67 
68  return usbtoxxx_conf_command(USB_TO_SWD, interface_index, cfg_buf, 5);
69 }
70 
71 RESULT usbtoswd_seqout(uint8_t interface_index, const uint8_t *data,
72  uint16_t bitlen)
73 {
74  uint16_t bytelen = (bitlen + 7) >> 3;
75 
76 #if PARAM_CHECK
77  if (interface_index > 7) {
78  LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
79  return ERROR_FAIL;
80  }
81 #endif
82 
83  SET_LE_U16(&versaloon_cmd_buf[0], bitlen);
84  memcpy(versaloon_cmd_buf + 2, data, bytelen);
85 
86  return usbtoxxx_out_command(USB_TO_SWD, interface_index,
87  versaloon_cmd_buf, bytelen + 2, 0);
88 }
89 
90 RESULT usbtoswd_seqin(uint8_t interface_index, uint8_t *data, uint16_t bitlen)
91 {
92  uint16_t bytelen = (bitlen + 7) >> 3;
93  uint8_t buff[2];
94 
95 #if PARAM_CHECK
96  if (interface_index > 7) {
97  LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
98  return ERROR_FAIL;
99  }
100 #endif
101 
102  SET_LE_U16(&buff[0], bitlen);
103 
104  return usbtoxxx_in_command(USB_TO_SWD, interface_index, buff, 2, bytelen,
105  data, 0, bytelen, 0);
106 }
107 
108 RESULT usbtoswd_transact(uint8_t interface_index, uint8_t request,
109  uint32_t *data, uint8_t *ack)
110 {
111  uint8_t parity;
112  uint8_t buff[5];
113 
114 #if PARAM_CHECK
115  if (interface_index > 7) {
116  LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
117  return ERROR_FAIL;
118  }
119 #endif
120 
121  parity = (request >> 1) & 1;
122  parity += (request >> 2) & 1;
123  parity += (request >> 3) & 1;
124  parity += (request >> 4) & 1;
125  parity &= 1;
126  buff[0] = (request | 0x81 | (parity << 5)) & ~0x40;
127  if (data)
128  SET_LE_U32(&buff[1], *data);
129  else
130  memset(buff + 1, 0, 4);
131 
133  if (request & 0x04) {
134  /* read */
136  } else {
137  /* write */
139  }
140 
141  /* Input buffer must be passed even for write operations. Otherwise
142  * the callback function is not called and ack value is not set. */
143  return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5,
144  (uint8_t *)data, 1, 4, 0);
145 }
#define ERROR_FAIL
Definition: log.h:170
#define ERROR_OK
Definition: log.h:164
RESULT usbtoswd_seqin(uint8_t interface_index, uint8_t *data, uint16_t bitlen)
Definition: usbtoswd.c:90
RESULT usbtoswd_transact(uint8_t interface_index, uint8_t request, uint32_t *data, uint8_t *ack)
Definition: usbtoswd.c:108
RESULT usbtoswd_config(uint8_t interface_index, uint8_t trn, uint16_t retry, uint16_t dly)
Definition: usbtoswd.c:52
static RESULT usbtoswd_read_callback(void *p, uint8_t *src, uint8_t *processed)
Definition: usbtoswd.c:20
RESULT usbtoswd_init(uint8_t interface_index)
Definition: usbtoswd.c:42
static RESULT usbtoswd_write_callback(void *p, uint8_t *src, uint8_t *processed)
Definition: usbtoswd.c:30
RESULT usbtoswd_fini(uint8_t interface_index)
Definition: usbtoswd.c:47
RESULT usbtoswd_seqout(uint8_t interface_index, const uint8_t *data, uint16_t bitlen)
Definition: usbtoswd.c:71
#define usbtoxxx_in_command(type, port, cmdbuf, cmdlen, retlen, wantbuf, wantpos, wantlen, c)
#define USB_TO_SWD
#define usbtoxxx_conf_command(type, port, cmdbuf, cmdlen)
#define usbtoxxx_inout_command(type, port, cmdbuf, cmdlen, retlen, wantbuf, wantpos, wantlen, c)
#define usbtoxxx_init_command(type, port)
#define usbtoxxx_out_command(type, port, cmdbuf, cmdlen, c)
#define usbtoxxx_fini_command(type, port)
void versaloon_set_callback(versaloon_callback_t callback)
Definition: versaloon.c:93
void versaloon_set_extra_data(void *p)
Definition: versaloon.c:97
uint8_t * versaloon_cmd_buf
Definition: versaloon.c:22
#define RESULT
#define ERRMSG_INVALID_INTERFACE_NUM
#define LOG_BUG
#define SET_LE_U16(p, v)
#define SET_LE_U32(p, v)
static unsigned int parity(unsigned int v)
Definition: xscale.c:623