1
2
3
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
82
83
84
85
86
87
88
89
93
97
101
102
103
104
105
106
107
108
113
114
115
124
125
126
127
128
129
130
131
135
136
137
138
139
140
141
142
149
150
151
152
153
154
155
156
157
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
199
200
201
202
203
207
208
221
222
223
224
225
226
227
228
229
230
231
232
233
237
238
239
240
241
242
243
248
249
254
255
256
257
258
259
260
261
262
263
264
265
270
271
276
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
325
326
327
328
335
336
337
341
342
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "versaloon_include.h"
#include <stdio.h>
#include <string.h>
#include <libusb.h>
#include "versaloon.h"
#include "versaloon_internal.h"
#include "usbtoxxx/usbtoxxx.h"
7 includes
uint8_t *versaloon_buf;
uint8_t *versaloon_cmd_buf;
uint16_t versaloon_buf_size;
struct versaloon_pending_t versaloon_pending[VERSALOON_MAX_PENDING_NUMBER];
uint16_t versaloon_pending_idx;
struct libusb_device_handle *versaloon_usb_device_handle;
static uint32_t versaloon_usb_to = VERSALOON_TIMEOUT;
static RESULT versaloon_init(void);
static RESULT versaloon_fini(void);
static RESULT versaloon_get_target_voltage(uint16_t *voltage);
static RESULT versaloon_set_target_voltage(uint16_t voltage);
static RESULT versaloon_delay_ms(uint16_t ms);
static RESULT versaloon_delay_us(uint16_t us);
struct versaloon_interface_t versaloon_interface = {
.init = versaloon_init,
.fini = versaloon_fini,
{
{
.get = versaloon_get_target_voltage,
.set = versaloon_set_target_voltage,
...},
{
.init = usbtogpio_init,
.fini = usbtogpio_fini,
.config = usbtogpio_config,
.out = usbtogpio_out,
.in = usbtogpio_in,
...},
{
.delayms = versaloon_delay_ms,
.delayus = versaloon_delay_us,
...},
{
.init = usbtoswd_init,
.fini = usbtoswd_fini,
.config = usbtoswd_config,
.seqout = usbtoswd_seqout,
.seqin = usbtoswd_seqin,
.transact = usbtoswd_transact,
...},
{
.init = usbtojtagraw_init,
.fini = usbtojtagraw_fini,
.config = usbtojtagraw_config,
.execute = usbtojtagraw_execute,
...},
.peripheral_commit = usbtoxxx_execute_command,
...},
{
.vid = VERSALOON_VID,
.pid = VERSALOON_PID,
.ep_out = VERSALOON_OUTP,
.ep_in = VERSALOON_INP,
.interface = VERSALOON_IFACE,
.buf_size = 256,
...}
...};
static uint32_t versaloon_pending_id;
static versaloon_callback_t versaloon_callback;
static void *versaloon_extra_data;
static struct versaloon_want_pos_t *versaloon_want_pos;
void versaloon_set_pending_id(uint32_t id)
{
versaloon_pending_id = id;
}{ ... }
void versaloon_set_callback(versaloon_callback_t callback)
{
versaloon_callback = callback;
}{ ... }
void versaloon_set_extra_data(void *p)
{
versaloon_extra_data = p;
}{ ... }
void versaloon_free_want_pos(void)
{
uint16_t i;
struct versaloon_want_pos_t *tmp, *free_tmp;
tmp = versaloon_want_pos;
while (tmp) {
free_tmp = tmp;
tmp = tmp->next;
free(free_tmp);
}while (tmp) { ... }
versaloon_want_pos = NULL;
for (i = 0; i < ARRAY_SIZE(versaloon_pending); i++) {
tmp = versaloon_pending[i].pos;
while (tmp) {
free_tmp = tmp;
tmp = tmp->next;
free(free_tmp);
}while (tmp) { ... }
versaloon_pending[i].pos = NULL;
}for (i = 0; i < ARRAY_SIZE(versaloon_pending); i++) { ... }
}{ ... }
RESULT versaloon_add_want_pos(uint16_t offset, uint16_t size, uint8_t *buff)
{
struct versaloon_want_pos_t *new_pos = NULL;
new_pos = malloc(sizeof(*new_pos));
if (!new_pos) {
LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
return ERRCODE_NOT_ENOUGH_MEMORY;
}if (!new_pos) { ... }
new_pos->offset = offset;
new_pos->size = size;
new_pos->buff = buff;
new_pos->next = NULL;
if (!versaloon_want_pos)
versaloon_want_pos = new_pos;
else {
struct versaloon_want_pos_t *tmp = versaloon_want_pos;
while (tmp->next)
tmp = tmp->next;
tmp->next = new_pos;
}else { ... }
return ERROR_OK;
}{ ... }
RESULT versaloon_add_pending(uint8_t type, uint8_t cmd, uint16_t actual_szie,
uint16_t want_pos, uint16_t want_size, uint8_t *buffer, uint8_t collect)
{
#if PARAM_CHECK
if (versaloon_pending_idx >= VERSALOON_MAX_PENDING_NUMBER) {
LOG_BUG(ERRMSG_INVALID_INDEX, versaloon_pending_idx,
"versaloon pending data");
return ERROR_FAIL;
}if (versaloon_pending_idx >= VERSALOON_MAX_PENDING_NUMBER) { ... }
/* ... */#endif
versaloon_pending[versaloon_pending_idx].type = type;
versaloon_pending[versaloon_pending_idx].cmd = cmd;
versaloon_pending[versaloon_pending_idx].actual_data_size = actual_szie;
versaloon_pending[versaloon_pending_idx].want_data_pos = want_pos;
versaloon_pending[versaloon_pending_idx].want_data_size = want_size;
versaloon_pending[versaloon_pending_idx].data_buffer = buffer;
versaloon_pending[versaloon_pending_idx].collect = collect;
versaloon_pending[versaloon_pending_idx].id = versaloon_pending_id;
versaloon_pending_id = 0;
versaloon_pending[versaloon_pending_idx].extra_data = versaloon_extra_data;
versaloon_extra_data = NULL;
versaloon_pending[versaloon_pending_idx].callback = versaloon_callback;
versaloon_callback = NULL;
versaloon_pending[versaloon_pending_idx].pos = versaloon_want_pos;
versaloon_want_pos = NULL;
versaloon_pending_idx++;
return ERROR_OK;
}{ ... }
RESULT versaloon_send_command(uint16_t out_len, uint16_t *inlen)
{
int ret;
int transferred;
#if PARAM_CHECK
if (!versaloon_buf) {
LOG_BUG(ERRMSG_INVALID_BUFFER, TO_STR(versaloon_buf));
return ERRCODE_INVALID_BUFFER;
}if (!versaloon_buf) { ... }
if ((out_len == 0) || (out_len > versaloon_interface.usb_setting.buf_size)) {
LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__);
return ERRCODE_INVALID_PARAMETER;
}if ((out_len == 0) || (out_len > versaloon_interface.usb_setting.buf_size)) { ... }
/* ... */#endif
ret = libusb_bulk_transfer(versaloon_usb_device_handle,
versaloon_interface.usb_setting.ep_out,
versaloon_buf, out_len, &transferred, versaloon_usb_to);
if (ret != 0 || transferred != out_len) {
LOG_ERROR(ERRMSG_FAILURE_OPERATION, "send usb data");
return ERRCODE_FAILURE_OPERATION;
}if (ret != 0 || transferred != out_len) { ... }
if (inlen) {
ret = libusb_bulk_transfer(versaloon_usb_device_handle,
versaloon_interface.usb_setting.ep_in,
versaloon_buf, versaloon_interface.usb_setting.buf_size,
&transferred, versaloon_usb_to);
if (ret == 0) {
*inlen = (uint16_t)transferred;
return ERROR_OK;
}if (ret == 0) { ... } else {
LOG_ERROR(ERRMSG_FAILURE_OPERATION, "receive usb data");
return ERROR_FAIL;
}else { ... }
}if (inlen) { ... } else
return ERROR_OK;
}{ ... }
#define VERSALOON_RETRY_CNT 10
static RESULT versaloon_init(void)
{
uint16_t ret = 0;
uint8_t retry;
uint32_t timeout_tmp;
versaloon_buf = malloc(versaloon_interface.usb_setting.buf_size);
if (!versaloon_buf) {
LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
return ERRCODE_NOT_ENOUGH_MEMORY;
}if (!versaloon_buf) { ... }
timeout_tmp = versaloon_usb_to;
versaloon_usb_to = 100;
for (retry = 0; retry < VERSALOON_RETRY_CNT; retry++) {
versaloon_buf[0] = VERSALOON_GET_INFO;
if ((versaloon_send_command(1, &ret) == ERROR_OK) && (ret >= 3))
break;
}for (retry = 0; retry < VERSALOON_RETRY_CNT; retry++) { ... }
versaloon_usb_to = timeout_tmp;
if (retry == VERSALOON_RETRY_CNT) {
versaloon_fini();
LOG_ERROR(ERRMSG_FAILURE_OPERATION, "communicate with versaloon");
return ERRCODE_FAILURE_OPERATION;
}if (retry == VERSALOON_RETRY_CNT) { ... }
versaloon_buf[ret] = 0;
versaloon_buf_size = versaloon_buf[0] + (versaloon_buf[1] << 8);
versaloon_interface.usb_setting.buf_size = versaloon_buf_size;
LOG_INFO("%s", versaloon_buf + 2);
free(versaloon_buf);
versaloon_buf = NULL;
versaloon_buf = malloc(versaloon_interface.usb_setting.buf_size);
if (!versaloon_buf) {
versaloon_fini();
LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
return ERRCODE_NOT_ENOUGH_MEMORY;
}if (!versaloon_buf) { ... }
versaloon_cmd_buf = malloc(versaloon_interface.usb_setting.buf_size - 3);
if (!versaloon_cmd_buf) {
versaloon_fini();
LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
return ERRCODE_NOT_ENOUGH_MEMORY;
}if (!versaloon_cmd_buf) { ... }
if (usbtoxxx_init() != ERROR_OK) {
LOG_ERROR(ERRMSG_FAILURE_OPERATION, "initialize usbtoxxx");
return ERROR_FAIL;
}if (usbtoxxx_init() != ERROR_OK) { ... }
return versaloon_get_target_voltage(&ret);
}{ ... }
static RESULT versaloon_fini(void)
{
if (versaloon_usb_device_handle) {
usbtoxxx_fini();
versaloon_free_want_pos();
versaloon_usb_device_handle = NULL;
free(versaloon_buf);
versaloon_buf = NULL;
free(versaloon_cmd_buf);
versaloon_cmd_buf = NULL;
}if (versaloon_usb_device_handle) { ... }
return ERROR_OK;
}{ ... }
static RESULT versaloon_set_target_voltage(uint16_t voltage)
{
usbtopwr_init(0);
usbtopwr_config(0);
usbtopwr_output(0, voltage);
usbtopwr_fini(0);
return usbtoxxx_execute_command();
}{ ... }
static RESULT versaloon_get_target_voltage(uint16_t *voltage)
{
uint16_t inlen;
#if PARAM_CHECK
if (!versaloon_buf) {
LOG_BUG(ERRMSG_INVALID_BUFFER, TO_STR(versaloon_buf));
return ERRCODE_INVALID_BUFFER;
}if (!versaloon_buf) { ... }
if (!voltage) {
LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__);
return ERRCODE_INVALID_PARAMETER;
}if (!voltage) { ... }
/* ... */#endif
versaloon_buf[0] = VERSALOON_GET_TVCC;
if ((versaloon_send_command(1, &inlen) != ERROR_OK) || (inlen != 2)) {
LOG_ERROR(ERRMSG_FAILURE_OPERATION, "communicate with versaloon");
return ERRCODE_FAILURE_OPERATION;
}if ((versaloon_send_command(1, &inlen) != ERROR_OK) || (inlen != 2)) { ... } else {
*voltage = versaloon_buf[0] + (versaloon_buf[1] << 8);
return ERROR_OK;
}else { ... }
}{ ... }
static RESULT versaloon_delay_ms(uint16_t ms)
{
return usbtodelay_delay(ms | 0x8000);
}{ ... }
static RESULT versaloon_delay_us(uint16_t us)
{
return usbtodelay_delay(us & 0x7FFF);
}{ ... }