]> sigrok.org Git - libsigrok.git/blame - hardware/brymen-bm86x/protocol.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / hardware / brymen-bm86x / protocol.c
CommitLineData
8d9c8554
AJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
5 *
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 3 of the License, or
9 * (at your option) any later version.
10 *
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.
15 *
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/>.
18 */
19
ecaa89af
AJ
20#include <string.h>
21#include <math.h>
8d9c8554
AJ
22#include "protocol.h"
23
ecaa89af
AJ
24#define USB_TIMEOUT 500
25
26static char char_map[128] = {
27 [0x20] = '-',
28 [0x5F] = '0',
29 [0x50] = '1',
30 [0x6D] = '2',
31 [0x7C] = '3',
32 [0x72] = '4',
33 [0x3E] = '5',
34 [0x3F] = '6',
35 [0x54] = '7',
36 [0x7F] = '8',
37 [0x7E] = '9',
38 [0x0F] = 'C',
39 [0x27] = 'F',
40 [0x0B] = 'L',
41 [0x79] = 'd',
42 [0x10] = 'i',
43 [0x39] = 'o',
44};
45
46static int brymen_bm86x_parse_digits(const unsigned char *buf, int length,
47 char *str, float *floatval,
48 char *temp_unit, int flag)
49{
50 char c, *p = str;
51 int i, ret;
52
53 if (buf[0] & flag)
54 *p++ = '-';
55 for (i = 0; i < length; i++) {
56 if (i && i < 5 && buf[i+1] & 0x01)
57 *p++ = '.';
58 c = char_map[buf[i+1] >> 1];
59 if (i == 5 && (c == 'C' || c == 'F'))
60 *temp_unit = c;
61 else if (c)
62 *p++ = c;
63 }
64 *p = 0;
65
66 if ((ret = sr_atof_ascii(str, floatval))) {
67 sr_dbg("invalid float string: '%s'", str);
68 return ret;
69 }
70
71 return SR_OK;
72}
73
74static void brymen_bm86x_parse(unsigned char *buf, float *floatval,
75 struct sr_datafeed_analog *analog)
76{
77 char str[16], temp_unit;
78 int ret1, ret2, over_limit;
79
80 ret1 = brymen_bm86x_parse_digits(buf+2, 6, str, &floatval[0],
81 &temp_unit, 0x80);
82 over_limit = strstr(str, "0L") || strstr(str, "0.L");
83 ret2 = brymen_bm86x_parse_digits(buf+9, 4, str, &floatval[1],
84 &temp_unit, 0x10);
85
86 /* main display */
87 if (ret1 == SR_OK || over_limit) {
88 /* SI unit */
89 if (buf[8] & 0x01) {
90 analog[0].mq = SR_MQ_VOLTAGE;
91 analog[0].unit = SR_UNIT_VOLT;
92 if (!strcmp(str, "diod"))
93 analog[0].mqflags |= SR_MQFLAG_DIODE;
94 } else if (buf[14] & 0x80) {
95 analog[0].mq = SR_MQ_CURRENT;
96 analog[0].unit = SR_UNIT_AMPERE;
97 } else if (buf[14] & 0x20) {
98 analog[0].mq = SR_MQ_CAPACITANCE;
99 analog[0].unit = SR_UNIT_FARAD;
100 } else if (buf[14] & 0x10) {
101 analog[0].mq = SR_MQ_CONDUCTANCE;
102 analog[0].unit = SR_UNIT_SIEMENS;
103 } else if (buf[15] & 0x01) {
104 analog[0].mq = SR_MQ_FREQUENCY;
105 analog[0].unit = SR_UNIT_HERTZ;
106 } else if (buf[10] & 0x01) {
107 analog[0].mq = SR_MQ_CONTINUITY;
108 analog[0].unit = SR_UNIT_OHM;
109 } else if (buf[15] & 0x10) {
110 analog[0].mq = SR_MQ_RESISTANCE;
111 analog[0].unit = SR_UNIT_OHM;
112 } else if (buf[15] & 0x02) {
113 analog[0].mq = SR_MQ_POWER;
114 analog[0].unit = SR_UNIT_DECIBEL_MW;
115 } else if (buf[15] & 0x80) {
116 analog[0].mq = SR_MQ_DUTY_CYCLE;
117 analog[0].unit = SR_UNIT_PERCENTAGE;
118 } else if (buf[ 2] & 0x0A) {
119 analog[0].mq = SR_MQ_TEMPERATURE;
120 if (temp_unit == 'F')
121 analog[0].unit = SR_UNIT_FAHRENHEIT;
122 else
123 analog[0].unit = SR_UNIT_CELSIUS;
124 }
125
126 /* when MIN MAX and AVG are displayed at the same time, remove them */
127 if ((buf[1] & 0xE0) == 0xE0)
128 buf[1] &= ~0xE0;
129
130 /* AC/DC/Auto flags */
131 if (buf[1] & 0x10) analog[0].mqflags |= SR_MQFLAG_DC;
132 if (buf[2] & 0x01) analog[0].mqflags |= SR_MQFLAG_AC;
133 if (buf[1] & 0x01) analog[0].mqflags |= SR_MQFLAG_AUTORANGE;
134 if (buf[1] & 0x08) analog[0].mqflags |= SR_MQFLAG_HOLD;
135 if (buf[1] & 0x20) analog[0].mqflags |= SR_MQFLAG_MAX;
136 if (buf[1] & 0x40) analog[0].mqflags |= SR_MQFLAG_MIN;
137 if (buf[1] & 0x80) analog[0].mqflags |= SR_MQFLAG_AVG;
138 if (buf[3] & 0x01) analog[0].mqflags |= SR_MQFLAG_RELATIVE;
139
140 /* when dBm is displayed, remove the m suffix so that it is
141 not considered as the 10e-3 SI prefix */
142 if (buf[15] & 0x02)
143 buf[15] &= ~0x04;
144
145 /* SI prefix */
146 if (buf[14] & 0x40) floatval[0] *= 1e-9; /* n */
147 if (buf[15] & 0x08) floatval[0] *= 1e-6; /* µ */
148 if (buf[15] & 0x04) floatval[0] *= 1e-3; /* m */
149 if (buf[15] & 0x40) floatval[0] *= 1e3; /* k */
150 if (buf[15] & 0x20) floatval[0] *= 1e6; /* M */
151
152 if (over_limit) floatval[0] = INFINITY;
153 }
154
155 /* secondary display */
156 if (ret2 == SR_OK) {
157 /* SI unit */
158 if (buf[14] & 0x08) {
159 analog[1].mq = SR_MQ_VOLTAGE;
160 analog[1].unit = SR_UNIT_VOLT;
161 } else if (buf[9] & 0x04) {
162 analog[1].mq = SR_MQ_CURRENT;
163 analog[1].unit = SR_UNIT_AMPERE;
164 } else if (buf[14] & 0x04) {
165 analog[1].mq = SR_MQ_FREQUENCY;
166 analog[1].unit = SR_UNIT_HERTZ;
167 } else if (buf[9] & 0x40) {
168 analog[1].mq = SR_MQ_TEMPERATURE;
169 if (temp_unit == 'F')
170 analog[1].unit = SR_UNIT_FAHRENHEIT;
171 else
172 analog[1].unit = SR_UNIT_CELSIUS;
173 }
174
175 /* AC flag */
176 if (buf[9] & 0x20) analog[1].mqflags |= SR_MQFLAG_AC;
177
178 /* SI prefix */
179 if (buf[ 9] & 0x01) floatval[1] *= 1e-6; /* µ */
180 if (buf[ 9] & 0x02) floatval[1] *= 1e-3; /* m */
181 if (buf[14] & 0x02) floatval[1] *= 1e3; /* k */
182 if (buf[14] & 0x01) floatval[1] *= 1e6; /* M */
183 }
184
185 if (buf[9] & 0x80)
186 sr_spew("Battery is low.");
187}
188
189static void brymen_bm86x_handle_packet(const struct sr_dev_inst *sdi,
190 unsigned char *buf)
191{
192 struct dev_context *devc;
193 struct sr_datafeed_packet packet;
194 struct sr_datafeed_analog analog[2];
195 float floatval[2];
196
197 devc = sdi->priv;
198
199 analog[0].mq = -1;
200 analog[0].mqflags = 0;
201
202 analog[1].mq = -1;
203 analog[1].mqflags = 0;
204
205 brymen_bm86x_parse(buf, floatval, analog);
206
207 if (analog[0].mq != -1) {
208 /* Got a measurement. */
209 analog[0].num_samples = 1;
210 analog[0].data = &floatval[0];
ba7dd8bb 211 analog[0].channels = g_slist_append(NULL, sdi->channels->data);
ecaa89af
AJ
212 packet.type = SR_DF_ANALOG;
213 packet.payload = &analog[0];
214 sr_session_send(devc->session_cb_data, &packet);
ba7dd8bb 215 g_slist_free(analog[0].channels);
ecaa89af
AJ
216 }
217
218 if (analog[1].mq != -1) {
219 /* Got a measurement. */
220 analog[1].num_samples = 1;
221 analog[1].data = &floatval[1];
ba7dd8bb 222 analog[1].channels = g_slist_append(NULL, sdi->channels->next->data);
ecaa89af
AJ
223 packet.type = SR_DF_ANALOG;
224 packet.payload = &analog[1];
225 sr_session_send(devc->session_cb_data, &packet);
ba7dd8bb 226 g_slist_free(analog[1].channels);
ecaa89af
AJ
227 }
228
229 if (analog[0].mq != -1 || analog[1].mq != -1)
230 devc->num_samples++;
231}
232
233static int brymen_bm86x_send_command(const struct sr_dev_inst *sdi)
234{
235 struct sr_usb_dev_inst *usb;
236 unsigned char buf[] = { 0x00, 0x86, 0x66 };
237 int ret;
238
239 usb = sdi->conn;
240
241 sr_dbg("Sending HID set report.");
242 ret = libusb_control_transfer(usb->devhdl,
243 LIBUSB_REQUEST_TYPE_CLASS |
244 LIBUSB_RECIPIENT_INTERFACE |
245 LIBUSB_ENDPOINT_OUT,
246 9, /* bRequest: HID set_report */
247 0x300, /* wValue: HID feature, report num 0 */
248 0, /* wIndex: interface 0 */
249 buf, sizeof(buf), USB_TIMEOUT);
250
251 if (ret < 0) {
252 sr_err("HID feature report error: %s.", libusb_error_name(ret));
253 return SR_ERR;
254 }
255
256 if (ret != sizeof(buf)) {
257 sr_err("Short packet: sent %d/%ld bytes.", ret, sizeof(buf));
258 return SR_ERR;
259 }
260
261 return SR_OK;
262}
263
264static int brymen_bm86x_read_interrupt(const struct sr_dev_inst *sdi)
265{
266 struct dev_context *devc;
267 struct sr_usb_dev_inst *usb;
268 unsigned char buf[24];
269 int ret, transferred;
270
271 devc = sdi->priv;
272 usb = sdi->conn;
273
274 sr_dbg("Reading HID interrupt report.");
275 /* Get data from EP1 using an interrupt transfer. */
276 ret = libusb_interrupt_transfer(usb->devhdl,
277 LIBUSB_ENDPOINT_IN | 1, /* EP1, IN */
278 buf, sizeof(buf),
279 &transferred, USB_TIMEOUT);
280
281 if (ret == LIBUSB_ERROR_TIMEOUT) {
282 if (++devc->interrupt_pending > 3)
283 devc->interrupt_pending = 0;
284 return SR_OK;
285 }
286
287 if (ret < 0) {
288 sr_err("USB receive error: %s.", libusb_error_name(ret));
289 return SR_ERR;
290 }
291
292 if (transferred != sizeof(buf)) {
293 sr_err("Short packet: received %d/%d bytes.", transferred, sizeof(buf));
294 return SR_ERR;
295 }
296
297 devc->interrupt_pending = 0;
298 brymen_bm86x_handle_packet(sdi, buf);
299
300 return SR_OK;
301}
302
8d9c8554
AJ
303SR_PRIV int brymen_bm86x_receive_data(int fd, int revents, void *cb_data)
304{
ecaa89af 305 struct sr_dev_inst *sdi;
8d9c8554 306 struct dev_context *devc;
ecaa89af 307 int64_t time;
8d9c8554
AJ
308
309 (void)fd;
ecaa89af 310 (void)revents;
8d9c8554
AJ
311
312 if (!(sdi = cb_data))
313 return TRUE;
314
315 if (!(devc = sdi->priv))
316 return TRUE;
317
ecaa89af
AJ
318 if (!devc->interrupt_pending) {
319 if (brymen_bm86x_send_command(sdi))
320 return FALSE;
321 devc->interrupt_pending = 1;
322 }
323
324 if (brymen_bm86x_read_interrupt(sdi))
325 return FALSE;
326
327 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
328 sr_info("Requested number of samples reached, stopping.");
329 sdi->driver->dev_acquisition_stop(sdi, cb_data);
330 return TRUE;
331 }
332
333 if (devc->limit_msec) {
334 time = (g_get_monotonic_time() - devc->start_time) / 1000;
335 if (time > (int64_t)devc->limit_msec) {
336 sr_info("Requested time limit reached, stopping.");
337 sdi->driver->dev_acquisition_stop(sdi, cb_data);
338 return TRUE;
339 }
8d9c8554
AJ
340 }
341
342 return TRUE;
343}