forked from mini-box/dcdc-usb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcdc-usb-proto.c
130 lines (104 loc) · 3.12 KB
/
dcdc-usb-proto.c
1
2
3
4
5
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2011 by Mini-Box.com, iTuner Networks Inc.
* Written by Nicu Pavel <[email protected]>
* All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <usb.h>
#include "dcdc-usb.h"
/* transforms user value to a value understood by device */
static int vout2dev(double vout)
{
double rpot = (double)0.8 * CT_R1 / (vout - (double)0.8) - CT_R2;
double result = (257 * (rpot-CT_RW) / CT_RP);
if (result<0) result = 0;
if (result>255) result = 255;
return (unsigned char)result;
}
static int dcdc_send_command(struct usb_dev_handle *h, unsigned char cmd, unsigned char val)
{
unsigned char c[3];
c[0] = DCDCUSB_CMD_OUT;
c[1] = cmd;
c[2] = val;
return dcdc_send(h, c, 3);
}
int dcdc_get_status(struct usb_dev_handle *h, unsigned char *buf, int buflen)
{
unsigned char c[2];
int ret = 0;
if (buflen < MAX_TRANSFER_SIZE)
return -1;
c[0] = DCDCUSB_GET_ALL_VALUES;
c[1] = 0;
if (dcdc_send(h, c, 2) < 0)
{
fprintf(stderr, "Cannot send command to device\n");
return -2;
}
if ((ret = dcdc_recv(h, buf, MAX_TRANSFER_SIZE, 1000)) < 0)
{
fprintf(stderr, "Cannot get device status\n");
return -3;
}
return ret;
}
int dcdc_set_vout(struct usb_dev_handle *h, double vout)
{
if (vout < 5) vout = 5;
if (vout > 24) vout = 24;
return dcdc_send_command(h, CMD_WRITE_VOUT, vout2dev(vout));
}
/* value will be shown on dcdc_parse_data() */
int dcdc_get_vout(struct usb_dev_handle *h, unsigned char *buf, int buflen)
{
int ret = 0;
if (buflen < MAX_TRANSFER_SIZE)
return -1;
if ((ret = dcdc_send_command(h, CMD_READ_VOUT, 0)) < 0)
return ret;
ret = dcdc_recv(h, buf, MAX_TRANSFER_SIZE, 1000);
return ret;
}
int dcdc_parse_data(unsigned char *data, int size)
{
if (data == NULL)
return -1;
#ifdef DEBUG
int i;
for (i = 0; i < size; i++)
{
if (i % 8 == 0) fprintf(stderr, "\n");
fprintf(stderr, "[%02d] = 0x%02x ", i, data[i]);
}
fprintf(stderr, "\n");
#endif
switch(data[0])
{
case DCDCUSB_RECV_ALL_VALUES: dcdc_parse_values(data);break;
case DCDCUSB_CMD_IN: dcdc_parse_cmd(data); break;
case INTERNAL_MESG: dcdc_parse_internal_msg(data); break;
case DCDCUSB_MEM_READ_IN: dcdc_parse_mem(data); break;
default:
fprintf(stderr, "Unknown message\n");
}
return 0;
}