]> sigrok.org Git - libsigrok.git/blame - hardware/cem-dt-885x/protocol.c
cem-dt-885x: Support for powering off the device
[libsigrok.git] / hardware / cem-dt-885x / protocol.c
CommitLineData
8fa9368e
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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
e37c4b39 20#include <string.h>
8fa9368e
BV
21#include "protocol.h"
22
e37c4b39
BV
23/* Length of expected payload for each token. */
24static int token_payloads[][2] = {
25 { TOKEN_WEIGHT_TIME_FAST, 0 },
26 { TOKEN_WEIGHT_TIME_SLOW, 0 },
27 { TOKEN_HOLD_MAX, 0 },
28 { TOKEN_HOLD_MIN, 0 },
29 { TOKEN_TIME, 3 },
30 { TOKEN_MEAS_RANGE_OVER, 0 },
31 { TOKEN_MEAS_RANGE_UNDER, 0 },
32 { TOKEN_STORE_FULL, 0 },
33 { TOKEN_RECORDING_ON, 0 },
34 { TOKEN_MEAS_WAS_READOUT, 1 },
35 { TOKEN_MEAS_WAS_BARGRAPH, 0 },
36 { TOKEN_MEASUREMENT, 2 },
37 { TOKEN_HOLD_NONE, 0 },
38 { TOKEN_BATTERY_LOW, 0 },
39 { TOKEN_MEAS_RANGE_OK, 0 },
40 { TOKEN_STORE_OK, 0 },
41 { TOKEN_RECORDING_OFF, 0 },
42 { TOKEN_WEIGHT_FREQ_A, 1 },
43 { TOKEN_WEIGHT_FREQ_C, 1 },
44 { TOKEN_BATTERY_OK, 0 },
45 { TOKEN_MEAS_RANGE_30_80, 0 },
46 { TOKEN_MEAS_RANGE_30_130, 0 },
47 { TOKEN_MEAS_RANGE_50_100, 0 },
48 { TOKEN_MEAS_RANGE_80_130, 0 },
49};
50
51static int find_token_payload_len(unsigned char c)
8fa9368e 52{
e37c4b39 53 unsigned int i;
8fa9368e 54
e37c4b39
BV
55 for (i = 0; i < ARRAY_SIZE(token_payloads); i++) {
56 if (token_payloads[i][0] == c)
57 return token_payloads[i][1];
58 }
59
60 return -1;
61}
62
63/* Process measurement or setting (0xa5 command). */
64static void process_mset(const struct sr_dev_inst *sdi)
65{
8fa9368e 66 struct dev_context *devc;
e37c4b39
BV
67 struct sr_datafeed_packet packet;
68 struct sr_datafeed_analog analog;
69 GString *dbg;
70 float fvalue;
71 int i;
8fa9368e 72
e37c4b39
BV
73 devc = sdi->priv;
74 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
75 dbg = g_string_sized_new(128);
76 g_string_printf(dbg, "got command 0x%.2x token 0x%.2x",
77 devc->cmd, devc->token);
78 if (devc->buf_len) {
79 g_string_append_printf(dbg, " payload");
80 for (i = 0; i < devc->buf_len; i++)
81 g_string_append_printf(dbg, " %.2x", devc->buf[i]);
82 }
83 sr_spew("%s", dbg->str);
84 g_string_free(dbg, TRUE);
85 }
86
87 switch(devc->token) {
88 case TOKEN_WEIGHT_TIME_FAST:
89 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F;
90 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_S;
91 break;
92 case TOKEN_WEIGHT_TIME_SLOW:
93 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S;
94 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_F;
95 break;
96 case TOKEN_WEIGHT_FREQ_A:
97 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A;
98 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_C;
99 break;
100 case TOKEN_WEIGHT_FREQ_C:
101 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C;
102 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_A;
103 break;
104 case TOKEN_HOLD_MAX:
105 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MAX;
106 devc->cur_mqflags &= ~SR_MQFLAG_MIN;
107 break;
108 case TOKEN_HOLD_MIN:
109 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MIN;
110 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
111 break;
112 case TOKEN_HOLD_NONE:
113 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_HOLD);
114 break;
115 case TOKEN_MEASUREMENT:
116 fvalue = ((devc->buf[0] & 0xf0) >> 4) * 100;
117 fvalue += (devc->buf[0] & 0x0f) * 10;
118 fvalue += ((devc->buf[1] & 0xf0) >> 4);
119 fvalue += (devc->buf[1] & 0x0f) / 10.0;
bc114328
BV
120 devc->last_spl = fvalue;
121 break;
122 case TOKEN_MEAS_WAS_READOUT:
123 case TOKEN_MEAS_WAS_BARGRAPH:
124 if (devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN)) {
125 if (devc->token == TOKEN_MEAS_WAS_BARGRAPH) {
126 /* The device still sends bargraph measurements even
127 * when in max/min hold mode. Suppress them here, unless
128 * they're readout values. This duplicates the behavior
129 * of the device display exactly. */
130 break;
131 }
132 }
e37c4b39
BV
133 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
134 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
135 analog.mqflags = devc->cur_mqflags;
136 analog.unit = SR_UNIT_DECIBEL_SPL;
137 analog.probes = sdi->probes;
138 analog.num_samples = 1;
bc114328 139 analog.data = &devc->last_spl;
e37c4b39
BV
140 packet.type = SR_DF_ANALOG;
141 packet.payload = &analog;
142 sr_session_send(devc->cb_data, &packet);
143
144 devc->num_samples++;
145 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
146 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
147 devc->cb_data);
148 break;
e1af0e85
BV
149 case TOKEN_RECORDING_ON:
150 devc->recording = TRUE;
151 break;
152 case TOKEN_RECORDING_OFF:
153 devc->recording = FALSE;
154 break;
f157b2ee
BV
155 case TOKEN_MEAS_RANGE_30_80:
156 case TOKEN_MEAS_RANGE_30_130:
157 case TOKEN_MEAS_RANGE_50_100:
158 case TOKEN_MEAS_RANGE_80_130:
159 devc->cur_meas_range = devc->token;
160 break;
e37c4b39
BV
161 case TOKEN_TIME:
162 case TOKEN_STORE_OK:
163 case TOKEN_STORE_FULL:
e37c4b39
BV
164 case TOKEN_BATTERY_OK:
165 case TOKEN_BATTERY_LOW:
166 case TOKEN_MEAS_RANGE_OK:
167 case TOKEN_MEAS_RANGE_OVER:
168 case TOKEN_MEAS_RANGE_UNDER:
e37c4b39
BV
169 /* Not useful, or not expressable in sigrok. */
170 break;
171 }
172
173}
174
e1af0e85
BV
175static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c,
176 int handle_packets)
e37c4b39
BV
177{
178 struct dev_context *devc;
14cf708f 179 gint64 cur_time;
e37c4b39 180 int len;
8fa9368e
BV
181
182 if (!(devc = sdi->priv))
e37c4b39
BV
183 return;
184
185 if (c == 0xff) {
186 /* Device is in hold mode */
187 devc->cur_mqflags |= SR_MQFLAG_HOLD;
188
14cf708f
BV
189 if (devc->hold_last_sent == 0) {
190 /* First hold notification. */
191 devc->hold_last_sent = g_get_monotonic_time();
192 /* When the device leaves hold mode, it starts from scratch. */
193 devc->state = ST_INIT;
194 } else {
195 cur_time = g_get_monotonic_time();
196 if (cur_time - devc->hold_last_sent > HOLD_REPEAT_INTERVAL) {
197 /* Force the last measurement out again. */
198 devc->cmd = 0xa5;
199 devc->token = TOKEN_MEAS_WAS_READOUT;
e1af0e85
BV
200 if (handle_packets)
201 process_mset(sdi);
14cf708f
BV
202 devc->hold_last_sent = cur_time;
203 }
204 }
e37c4b39 205
e37c4b39
BV
206 return;
207 }
208 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
14cf708f 209 devc->hold_last_sent = 0;
e37c4b39
BV
210
211 if (devc->state == ST_INIT) {
212 if (c == 0xa5) {
213 devc->cmd = c;
214 devc->token = 0x00;
215 devc->state = ST_GET_TOKEN;
216 } else if (c == 0xbb) {
217 devc->cmd = c;
218 devc->buf_len = 0;
219 devc->state = ST_GET_LOG;
220 }
221 } else if (devc->state == ST_GET_TOKEN) {
222 devc->token = c;
223 devc->buf_len = 0;
224 len = find_token_payload_len(devc->token);
225 if (len == -1 || len > 0) {
226 devc->buf_len = 0;
227 devc->state = ST_GET_DATA;
228 } else {
e1af0e85
BV
229 if (handle_packets)
230 process_mset(sdi);
e37c4b39
BV
231 devc->state = ST_INIT;
232 }
233 } else if (devc->state == ST_GET_DATA) {
234 len = find_token_payload_len(devc->token);
235 if (len == -1) {
236 /* We don't know this token. */
237 sr_dbg("Unknown 0xa5 token 0x%.2x", devc->token);
238 if (c == 0xa5 || c == 0xbb) {
239 /* Looks like a new command however. */
e1af0e85
BV
240 if (handle_packets)
241 process_mset(sdi);
e37c4b39
BV
242 devc->state = ST_INIT;
243 } else {
244 devc->buf[devc->buf_len++] = c;
245 if (devc->buf_len > BUF_SIZE) {
246 /* Shouldn't happen, ignore. */
247 devc->state = ST_INIT;
248 }
249 }
250 } else {
251 devc->buf[devc->buf_len++] = c;
252 if (devc->buf_len == len) {
e1af0e85
BV
253 if (handle_packets)
254 process_mset(sdi);
e37c4b39
BV
255 devc->state = ST_INIT;
256 } else if (devc->buf_len > BUF_SIZE) {
257 /* Shouldn't happen, ignore. */
258 devc->state = ST_INIT;
259 }
260 }
261 } else if (devc->state == ST_GET_LOG) {
262 }
263
264}
265
266SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
267{
268 const struct sr_dev_inst *sdi;
269 struct sr_serial_dev_inst *serial;
270 unsigned char c;
271
272 (void)fd;
273
274 if (!(sdi = cb_data))
8fa9368e
BV
275 return TRUE;
276
e37c4b39 277 serial = sdi->conn;
8fa9368e 278 if (revents == G_IO_IN) {
e37c4b39
BV
279 if (serial_read(serial, &c, 1) != 1)
280 return TRUE;
e1af0e85 281 process_byte(sdi, c, TRUE);
8fa9368e
BV
282 }
283
284 return TRUE;
285}
e1af0e85
BV
286
287
288static int wait_for_token(const struct sr_dev_inst *sdi, char *tokens, int timeout)
289{
290 struct dev_context *devc;
291 struct sr_serial_dev_inst *serial;
292 gint64 start_time;
293 int i;
294 unsigned char c;
295
296 serial = sdi->conn;
297 devc = sdi->priv;
298 devc->state = ST_INIT;
299 start_time = g_get_monotonic_time() / 1000;
300 while (TRUE) {
301 if (serial_read(serial, &c, 1) != 1)
302 /* Device might have gone away. */
303 return SR_ERR;
304 process_byte(sdi, c, FALSE);
305 if (devc->state != ST_INIT)
306 /* Wait for a whole packet to get processed. */
307 continue;
308 for (i = 0; tokens[i] != -1; i++) {
309 if (devc->token == tokens[i]) {
310 sr_spew("wait_for_token: got token 0x%.2x", devc->token);
311 return SR_OK;
312 }
313 }
314 if (timeout && g_get_monotonic_time() / 1000 - start_time > timeout)
315 return SR_ERR_TIMEOUT;
316 }
317
318 return SR_OK;
319}
320
321/* cmd is the command to send, tokens are the tokens that denote the state
322 * which the command affects. The first token is the desired state. */
be733919
BV
323SR_PRIV int cem_dt_885x_toggle(const struct sr_dev_inst *sdi, uint8_t cmd,
324 char *tokens, int timeout)
e1af0e85
BV
325{
326 struct dev_context *devc;
327 struct sr_serial_dev_inst *serial;
328
329 serial = sdi->conn;
330 devc = sdi->priv;
331
332 /* The device doesn't respond to commands very well. The
333 * only thing to do is wait for the token that will confirm
334 * whether the command worked or not, and resend if needed. */
335 while (TRUE) {
336 if (serial_write(serial, (const void *)&cmd, 1) != 1)
337 return SR_ERR;
be733919 338 if (wait_for_token(sdi, tokens, timeout) == SR_ERR)
e1af0e85
BV
339 return SR_ERR;
340 if (devc->token == tokens[0])
341 /* It worked. */
342 break;
343 }
344
345 return SR_OK;
346}
347
0cd9107d
BV
348SR_PRIV gboolean cem_dt_885x_recording_get(const struct sr_dev_inst *sdi,
349 int *state)
e1af0e85
BV
350{
351 struct dev_context *devc;
352 char tokens[5];
353
354 devc = sdi->priv;
e1af0e85
BV
355 if (devc->recording == -1) {
356 /* Didn't pick up device state yet. */
357 tokens[0] = TOKEN_RECORDING_ON;
358 tokens[1] = TOKEN_RECORDING_OFF;
359 tokens[2] = -1;
0cd9107d 360 if (wait_for_token(sdi, tokens, 510) != SR_OK)
e1af0e85
BV
361 return SR_ERR;
362 }
0cd9107d 363 *state = devc->token == TOKEN_RECORDING_ON;
e1af0e85 364
0cd9107d 365 return SR_OK;
e1af0e85
BV
366}
367
0cd9107d
BV
368SR_PRIV int cem_dt_885x_recording_set(const struct sr_dev_inst *sdi,
369 gboolean state)
e1af0e85
BV
370{
371 struct dev_context *devc;
372 int ret;
373 char tokens[5];
374
375 devc = sdi->priv;
376
377 /* The toggle below needs the desired state in first position. */
0cd9107d 378 if (state) {
e1af0e85
BV
379 tokens[0] = TOKEN_RECORDING_ON;
380 tokens[1] = TOKEN_RECORDING_OFF;
381 } else {
382 tokens[0] = TOKEN_RECORDING_OFF;
383 tokens[1] = TOKEN_RECORDING_ON;
384 }
385 tokens[2] = -1;
386
387 if (devc->recording == -1) {
388 /* Didn't pick up device state yet. */
389 if (wait_for_token(sdi, tokens, 0) != SR_OK)
390 return SR_ERR;
391 if (devc->token == tokens[0])
392 /* Nothing to do. */
393 return SR_OK;
0cd9107d 394 } else if (devc->recording == state)
e1af0e85
BV
395 /* Nothing to do. */
396 return SR_OK;
397
be733919
BV
398 /* Recording state notifications are sent at 2Hz, so allow just over
399 * that, 510ms, for the state to come in. */
400 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_RECORDING, tokens, 510);
401
402 return ret;
403}
404
405SR_PRIV int cem_dt_885x_weight_freq_get(const struct sr_dev_inst *sdi)
406{
407 struct dev_context *devc;
408 int cur_setting;
409 char tokens[5];
410
411 devc = sdi->priv;
412
413 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
414 if (cur_setting == 0) {
415 /* Didn't pick up device state yet. */
416 tokens[0] = TOKEN_WEIGHT_FREQ_A;
417 tokens[1] = TOKEN_WEIGHT_FREQ_C;
418 tokens[2] = -1;
419 if (wait_for_token(sdi, tokens, 0) != SR_OK)
420 return SR_ERR;
421 if (devc->token == TOKEN_WEIGHT_FREQ_A)
422 return SR_MQFLAG_SPL_FREQ_WEIGHT_A;
423 else
424 return SR_MQFLAG_SPL_FREQ_WEIGHT_C;
425 } else
426 return cur_setting;
427
428}
429
430SR_PRIV int cem_dt_885x_weight_freq_set(const struct sr_dev_inst *sdi, int freqw)
431{
432 struct dev_context *devc;
433 int cur_setting, ret;
434 char tokens[5];
435
436 devc = sdi->priv;
437
438 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
439 if (cur_setting == freqw)
440 /* Already set to this frequency weighting. */
441 return SR_OK;
442
443 /* The toggle below needs the desired state in first position. */
444 if (freqw == SR_MQFLAG_SPL_FREQ_WEIGHT_A) {
445 tokens[0] = TOKEN_WEIGHT_FREQ_A;
446 tokens[1] = TOKEN_WEIGHT_FREQ_C;
447 } else {
448 tokens[0] = TOKEN_WEIGHT_FREQ_C;
449 tokens[1] = TOKEN_WEIGHT_FREQ_A;
450 }
451 tokens[2] = -1;
452
453 if (cur_setting == 0) {
454 /* Didn't pick up device state yet. */
455 if (wait_for_token(sdi, tokens, 0) != SR_OK)
456 return SR_ERR;
457 if (devc->token == tokens[0])
458 /* Nothing to do. */
459 return SR_OK;
460 }
461
462 /* 10ms timeout seems to work best for this. */
463 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_FREQ, tokens, 10);
e1af0e85
BV
464
465 return ret;
466}
1487ce4f
BV
467
468SR_PRIV int cem_dt_885x_weight_time_get(const struct sr_dev_inst *sdi)
469{
470 struct dev_context *devc;
471 int cur_setting;
472 char tokens[5];
473
474 devc = sdi->priv;
475
476 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
477 if (cur_setting == 0) {
478 /* Didn't pick up device state yet. */
479 tokens[0] = TOKEN_WEIGHT_TIME_FAST;
480 tokens[1] = TOKEN_WEIGHT_TIME_SLOW;
481 tokens[2] = -1;
482 if (wait_for_token(sdi, tokens, 0) != SR_OK)
483 return SR_ERR;
484 if (devc->token == TOKEN_WEIGHT_TIME_FAST)
485 return SR_MQFLAG_SPL_TIME_WEIGHT_F;
486 else
487 return SR_MQFLAG_SPL_TIME_WEIGHT_S;
488 } else
489 return cur_setting;
490
491}
492
493SR_PRIV int cem_dt_885x_weight_time_set(const struct sr_dev_inst *sdi, int timew)
494{
495 struct dev_context *devc;
496 int cur_setting, ret;
497 char tokens[5];
498
499 devc = sdi->priv;
500
501 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
502 if (cur_setting == timew)
503 /* Already set to this time weighting. */
504 return SR_OK;
505
506 /* The toggle below needs the desired state in first position. */
507 if (timew == SR_MQFLAG_SPL_TIME_WEIGHT_F) {
508 tokens[0] = TOKEN_WEIGHT_TIME_FAST;
509 tokens[1] = TOKEN_WEIGHT_TIME_SLOW;
510 } else {
511 tokens[0] = TOKEN_WEIGHT_TIME_SLOW;
512 tokens[1] = TOKEN_WEIGHT_TIME_FAST;
513 }
514 tokens[2] = -1;
515
516 if (cur_setting == 0) {
517 /* Didn't pick up device state yet. */
518 if (wait_for_token(sdi, tokens, 0) != SR_OK)
519 return SR_ERR;
520 if (devc->token == tokens[0])
521 /* Nothing to do. */
522 return SR_OK;
523 }
524
525 /* 51ms timeout seems to work best for this. */
526 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_TIME, tokens, 51);
527
528 return ret;
529}
a90e480c
BV
530
531SR_PRIV int cem_dt_885x_holdmode_get(const struct sr_dev_inst *sdi,
532 gboolean *holdmode)
533{
534 struct dev_context *devc;
535 char tokens[5];
536
537 devc = sdi->priv;
538
539 if (devc->cur_mqflags == 0) {
540 tokens[0] = TOKEN_HOLD_MAX;
541 tokens[1] = TOKEN_HOLD_MIN;
542 tokens[2] = TOKEN_HOLD_NONE;
543 tokens[3] = -1;
544 if (wait_for_token(sdi, tokens, 0) != SR_OK)
545 return SR_ERR;
546 if (devc->token == TOKEN_HOLD_MAX)
547 devc->cur_mqflags = SR_MQFLAG_MAX;
548 else if (devc->token == TOKEN_HOLD_MIN)
549 devc->cur_mqflags = SR_MQFLAG_MIN;
550 }
551 *holdmode = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
552
553 return SR_OK;
554}
555
556SR_PRIV int cem_dt_885x_holdmode_set(const struct sr_dev_inst *sdi, int holdmode)
557{
558 struct dev_context *devc;
559 int cur_setting, ret;
560 char tokens[5];
561
562 devc = sdi->priv;
563
564 /* The toggle below needs the desired state in first position. */
565 if (holdmode == SR_MQFLAG_MAX) {
566 tokens[0] = TOKEN_HOLD_MAX;
567 tokens[1] = TOKEN_HOLD_MIN;
568 tokens[2] = TOKEN_HOLD_NONE;
569 } else if (holdmode == SR_MQFLAG_MIN) {
570 tokens[0] = TOKEN_HOLD_MIN;
571 tokens[1] = TOKEN_HOLD_MAX;
572 tokens[2] = TOKEN_HOLD_NONE;
573 } else {
574 tokens[0] = TOKEN_HOLD_NONE;
575 tokens[1] = TOKEN_HOLD_MAX;
576 tokens[2] = TOKEN_HOLD_MIN;
577 }
578 tokens[3] = -1;
579
580 if (devc->cur_mqflags == 0) {
581 /* Didn't pick up device state yet. */
582 if (wait_for_token(sdi, tokens, 0) != SR_OK)
583 return SR_ERR;
584 if (devc->token == tokens[0])
585 /* Nothing to do. */
586 return SR_OK;
587 } else {
588 cur_setting = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
589 if (cur_setting == holdmode)
590 /* Already set correctly. */
591 return SR_OK;
592 }
593
594 /* 51ms timeout seems to work best for this. */
595 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_HOLD_MAX_MIN, tokens, 51);
596
597 return ret;
598}
f157b2ee
BV
599
600SR_PRIV int cem_dt_885x_meas_range_get(const struct sr_dev_inst *sdi,
601 uint64_t *low, uint64_t *high)
602{
603 struct dev_context *devc;
604 char tokens[5];
605
606 devc = sdi->priv;
607 if (devc->cur_meas_range == 0) {
608 tokens[0] = TOKEN_MEAS_RANGE_30_130;
609 tokens[1] = TOKEN_MEAS_RANGE_30_80;
610 tokens[2] = TOKEN_MEAS_RANGE_50_100;
611 tokens[3] = TOKEN_MEAS_RANGE_80_130;
612 tokens[4] = -1;
613 if (wait_for_token(sdi, tokens, 0) != SR_OK)
614 return SR_ERR;
615 devc->cur_meas_range = devc->token;
616 }
617
618 switch (devc->cur_meas_range) {
619 case TOKEN_MEAS_RANGE_30_130:
620 *low = 30;
621 *high = 130;
622 break;
623 case TOKEN_MEAS_RANGE_30_80:
624 *low = 30;
625 *high = 80;
626 break;
627 case TOKEN_MEAS_RANGE_50_100:
628 *low = 50;
629 *high = 100;
630 break;
631 case TOKEN_MEAS_RANGE_80_130:
632 *low = 80;
633 *high = 130;
634 break;
635 default:
636 return SR_ERR;
637 }
638
639 return SR_OK;
640}
641
642SR_PRIV int cem_dt_885x_meas_range_set(const struct sr_dev_inst *sdi,
643 uint64_t low, uint64_t high)
644{
645 struct dev_context *devc;
646 int ret;
647 char token, tokens[6];
648
649 devc = sdi->priv;
650 if (low == 30 && high == 130)
651 token = TOKEN_MEAS_RANGE_30_130;
652 else if (low == 30 && high == 80)
653 token = TOKEN_MEAS_RANGE_30_80;
654 else if (low == 50 && high == 100)
655 token = TOKEN_MEAS_RANGE_50_100;
656 else if (low == 80 && high == 130)
657 token = TOKEN_MEAS_RANGE_80_130;
658 else
659 return SR_ERR;
660
661 sr_dbg("want 0x%.2x", token);
662 /* The toggle below needs the desired state in first position. */
663 tokens[0] = token;
664 tokens[1] = TOKEN_MEAS_RANGE_30_130;
665 tokens[2] = TOKEN_MEAS_RANGE_30_80;
666 tokens[3] = TOKEN_MEAS_RANGE_50_100;
667 tokens[4] = TOKEN_MEAS_RANGE_80_130;
668 tokens[5] = -1;
669
670 if (devc->cur_meas_range == 0) {
671 /* 110ms should be enough for two of these announcements */
672 if (wait_for_token(sdi, tokens, 110) != SR_OK)
673 return SR_ERR;
674 devc->cur_meas_range = devc->token;
675 }
676
677 if (devc->cur_meas_range == token)
678 /* Already set to this range. */
679 return SR_OK;
680
681 /* For measurement range, it works best to ignore announcements of the
682 * current setting and keep resending the toggle quickly. */
683 tokens[1] = -1;
684 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_MEAS_RANGE, tokens, 11);
685
686 return ret;
687}
4c22355f
BV
688
689SR_PRIV int cem_dt_885x_power_off(const struct sr_dev_inst *sdi)
690{
691 struct sr_serial_dev_inst *serial;
692 char c, cmd;
693
694 serial = sdi->conn;
695
696 /* Reopen the port in non-blocking mode, so we can properly
697 * detect when the device stops communicating. */
698 serial_close(serial);
699 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
700 return SR_ERR;
701
702 cmd = CMD_TOGGLE_POWER_OFF;
703 while (TRUE) {
704 serial_flush(serial);
705 if (serial_write(serial, (const void *)&cmd, 1) != 1)
706 return SR_ERR;
707 /* It never takes more than 23ms for the next token to arrive. */
708 g_usleep(25 * 1000);
709 if (serial_read(serial, &c, 1) != 1)
710 /* Device is no longer responding. Good! */
711 break;
712 }
713
714 /* In case the user manually turns on the device again, reset
715 * the port back to blocking. */
716 serial_close(serial);
717 serial_open(serial, SERIAL_RDWR);
718
719 return SR_OK;
720}