2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 /* Length of expected payload for each token. */
25 static const int token_payloads[][2] = {
26 { TOKEN_WEIGHT_TIME_FAST, 0 },
27 { TOKEN_WEIGHT_TIME_SLOW, 0 },
28 { TOKEN_HOLD_MAX, 0 },
29 { TOKEN_HOLD_MIN, 0 },
31 { TOKEN_MEAS_RANGE_OVER, 0 },
32 { TOKEN_MEAS_RANGE_UNDER, 0 },
33 { TOKEN_STORE_FULL, 0 },
34 { TOKEN_RECORDING_ON, 0 },
35 { TOKEN_MEAS_WAS_READOUT, 1 },
36 { TOKEN_MEAS_WAS_BARGRAPH, 0 },
37 { TOKEN_MEASUREMENT, 2 },
38 { TOKEN_HOLD_NONE, 0 },
39 { TOKEN_BATTERY_LOW, 0 },
40 { TOKEN_MEAS_RANGE_OK, 0 },
41 { TOKEN_STORE_OK, 0 },
42 { TOKEN_RECORDING_OFF, 0 },
43 { TOKEN_WEIGHT_FREQ_A, 1 },
44 { TOKEN_WEIGHT_FREQ_C, 1 },
45 { TOKEN_BATTERY_OK, 0 },
46 { TOKEN_MEAS_RANGE_30_80, 0 },
47 { TOKEN_MEAS_RANGE_30_130, 0 },
48 { TOKEN_MEAS_RANGE_50_100, 0 },
49 { TOKEN_MEAS_RANGE_80_130, 0 },
52 static int find_token_payload_len(unsigned char c)
56 for (i = 0; i < ARRAY_SIZE(token_payloads); i++) {
57 if (token_payloads[i][0] == c)
58 return token_payloads[i][1];
64 /* Process measurement or setting (0xa5 command). */
65 static void process_mset(const struct sr_dev_inst *sdi)
67 struct dev_context *devc;
68 struct sr_datafeed_packet packet;
69 struct sr_datafeed_analog_old analog;
75 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
76 dbg = g_string_sized_new(128);
77 g_string_printf(dbg, "got command 0x%.2x token 0x%.2x",
78 devc->cmd, devc->token);
80 g_string_append_printf(dbg, " payload");
81 for (i = 0; i < devc->buf_len; i++)
82 g_string_append_printf(dbg, " %.2x", devc->buf[i]);
84 sr_spew("%s", dbg->str);
85 g_string_free(dbg, TRUE);
88 switch (devc->token) {
89 case TOKEN_WEIGHT_TIME_FAST:
90 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F;
91 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_S;
93 case TOKEN_WEIGHT_TIME_SLOW:
94 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S;
95 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_F;
97 case TOKEN_WEIGHT_FREQ_A:
98 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A;
99 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_C;
101 case TOKEN_WEIGHT_FREQ_C:
102 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C;
103 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_A;
106 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MAX;
107 devc->cur_mqflags &= ~SR_MQFLAG_MIN;
110 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MIN;
111 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
113 case TOKEN_HOLD_NONE:
114 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_HOLD);
116 case TOKEN_MEASUREMENT:
117 fvalue = ((devc->buf[0] & 0xf0) >> 4) * 100;
118 fvalue += (devc->buf[0] & 0x0f) * 10;
119 fvalue += ((devc->buf[1] & 0xf0) >> 4);
120 fvalue += (devc->buf[1] & 0x0f) / 10.0;
121 devc->last_spl = fvalue;
123 case TOKEN_MEAS_WAS_READOUT:
124 case TOKEN_MEAS_WAS_BARGRAPH:
125 if (devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN)) {
126 if (devc->token == TOKEN_MEAS_WAS_BARGRAPH) {
127 /* The device still sends bargraph measurements even
128 * when in max/min hold mode. Suppress them here, unless
129 * they're readout values. This duplicates the behavior
130 * of the device display exactly. */
134 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
135 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
136 analog.mqflags = devc->cur_mqflags;
137 analog.unit = SR_UNIT_DECIBEL_SPL;
138 analog.channels = sdi->channels;
139 analog.num_samples = 1;
140 analog.data = &devc->last_spl;
141 packet.type = SR_DF_ANALOG_OLD;
142 packet.payload = &analog;
143 sr_session_send(devc->cb_data, &packet);
146 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
147 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
150 case TOKEN_RECORDING_ON:
151 devc->recording = TRUE;
153 case TOKEN_RECORDING_OFF:
154 devc->recording = FALSE;
156 case TOKEN_MEAS_RANGE_30_80:
157 case TOKEN_MEAS_RANGE_30_130:
158 case TOKEN_MEAS_RANGE_50_100:
159 case TOKEN_MEAS_RANGE_80_130:
160 devc->cur_meas_range = devc->token;
164 case TOKEN_STORE_FULL:
165 case TOKEN_BATTERY_OK:
166 case TOKEN_BATTERY_LOW:
167 case TOKEN_MEAS_RANGE_OK:
168 case TOKEN_MEAS_RANGE_OVER:
169 case TOKEN_MEAS_RANGE_UNDER:
170 /* Not useful, or not expressible in sigrok. */
176 static void send_data(const struct sr_dev_inst *sdi, unsigned char *data,
177 uint64_t num_samples)
179 struct dev_context *devc;
180 struct sr_datafeed_packet packet;
181 struct sr_datafeed_analog_old analog;
182 float fbuf[SAMPLES_PER_PACKET];
187 for (i = 0; i < num_samples; i ++) {
188 fbuf[i] = ((data[i * 2] & 0xf0) >> 4) * 100;
189 fbuf[i] += (data[i * 2] & 0x0f) * 10;
190 fbuf[i] += ((data[i * 2 + 1] & 0xf0) >> 4);
191 fbuf[i] += (data[i * 2 + 1] & 0x0f) / 10.0;
193 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
194 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
195 analog.mqflags = devc->cur_mqflags;
196 analog.unit = SR_UNIT_DECIBEL_SPL;
197 analog.channels = sdi->channels;
198 analog.num_samples = num_samples;
200 packet.type = SR_DF_ANALOG_OLD;
201 packet.payload = &analog;
202 sr_session_send(devc->cb_data, &packet);
204 devc->num_samples += analog.num_samples;
205 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
206 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
212 static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c,
215 struct dev_context *devc;
216 struct sr_datafeed_packet packet;
217 struct sr_datafeed_meta meta;
218 struct sr_config *src;
222 if (!(devc = sdi->priv))
226 /* Device is in hold mode */
227 devc->cur_mqflags |= SR_MQFLAG_HOLD;
229 if (devc->hold_last_sent == 0) {
230 /* First hold notification. */
231 devc->hold_last_sent = g_get_monotonic_time();
232 /* When the device leaves hold mode, it starts from scratch. */
233 devc->state = ST_INIT;
235 cur_time = g_get_monotonic_time();
236 if (cur_time - devc->hold_last_sent > HOLD_REPEAT_INTERVAL) {
237 /* Force the last measurement out again. */
239 devc->token = TOKEN_MEAS_WAS_READOUT;
242 devc->hold_last_sent = cur_time;
248 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
249 devc->hold_last_sent = 0;
251 if (devc->state == ST_INIT) {
255 devc->state = ST_GET_TOKEN;
256 } else if (c == 0xbb) {
259 devc->state = ST_GET_LOG_HEADER;
260 sr_dbg("got command 0xbb");
262 } else if (devc->state == ST_GET_TOKEN) {
265 len = find_token_payload_len(devc->token);
266 if (len == -1 || len > 0) {
268 devc->state = ST_GET_DATA;
272 devc->state = ST_INIT;
274 } else if (devc->state == ST_GET_DATA) {
275 len = find_token_payload_len(devc->token);
277 /* We don't know this token. */
278 sr_dbg("Unknown 0xa5 token 0x%.2x", devc->token);
279 if (c == 0xa5 || c == 0xbb) {
280 /* Looks like a new command however. */
283 devc->state = ST_INIT;
285 devc->buf[devc->buf_len++] = c;
286 if (devc->buf_len > BUF_SIZE) {
287 /* Shouldn't happen, ignore. */
288 devc->state = ST_INIT;
292 devc->buf[devc->buf_len++] = c;
293 if (devc->buf_len == len) {
296 devc->state = ST_INIT;
297 } else if (devc->buf_len > BUF_SIZE) {
298 /* Shouldn't happen, ignore. */
299 devc->state = ST_INIT;
302 } else if (devc->state == ST_GET_LOG_HEADER) {
303 sr_dbg("log header: 0x%.2x", c);
304 if (devc->buf_len < 2)
305 devc->buf[devc->buf_len++] = c;
306 if (devc->buf_len == 2) {
307 sr_dbg("Device says it has %d bytes stored.",
308 ((devc->buf[0] << 8) + devc->buf[1]) - 100);
310 devc->state = ST_GET_LOG_RECORD_META;
312 } else if (devc->state == ST_GET_LOG_RECORD_META) {
313 sr_dbg("log meta: 0x%.2x", c);
314 if (c == RECORD_END) {
315 devc->state = ST_INIT;
316 /* Stop acquisition after transferring all stored
317 * records. Otherwise the frontend would have no
318 * way to tell where stored data ends and live
319 * measurements begin. */
320 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
322 } else if (c == RECORD_DATA) {
324 devc->state = ST_GET_LOG_RECORD_DATA;
326 /* RECORD_DBA/RECORD_DBC + 7 bytes of metadata */
327 devc->buf[devc->buf_len++] = c;
328 if (devc->buf_len < 8)
329 /* Keep filling up the record header. */
331 if (devc->buf[0] == RECORD_DBA)
332 devc->cur_mqflags = SR_MQFLAG_SPL_FREQ_WEIGHT_A;
333 else if (devc->buf[0] == RECORD_DBC)
334 devc->cur_mqflags = SR_MQFLAG_SPL_FREQ_WEIGHT_C;
336 /* Shouldn't happen. */
337 sr_dbg("Unknown record token 0x%.2x", c);
340 packet.type = SR_DF_META;
341 packet.payload = &meta;
342 src = sr_config_new(SR_CONF_SAMPLE_INTERVAL,
343 g_variant_new_uint64(devc->buf[7] * 1000));
344 meta.config = g_slist_append(NULL, src);
345 sr_session_send(devc->cb_data, &packet);
349 } else if (devc->state == ST_GET_LOG_RECORD_DATA) {
350 sr_dbg("log data: 0x%.2x", c);
351 if (c == RECORD_DBA || c == RECORD_DBC || c == RECORD_DATA || c == RECORD_END) {
352 /* Work around off-by-one bug in device firmware. This
353 * happens only on the last record, i.e. before RECORD_END */
354 if (devc->buf_len & 1)
356 /* Done with this set of samples */
357 send_data(sdi, devc->buf, devc->buf_len / 2);
360 /* Process this meta marker in the right state. */
361 devc->state = ST_GET_LOG_RECORD_META;
362 process_byte(sdi, c, handle_packets);
364 devc->buf[devc->buf_len++] = c;
365 if (devc->buf_len == SAMPLES_PER_PACKET * 2) {
366 send_data(sdi, devc->buf, devc->buf_len / 2);
374 SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
376 const struct sr_dev_inst *sdi;
377 struct dev_context *devc;
378 struct sr_serial_dev_inst *serial;
379 unsigned char c, cmd;
383 if (!(sdi = cb_data))
388 if (revents == G_IO_IN) {
389 if (serial_read_nonblocking(serial, &c, 1) != 1)
391 process_byte(sdi, c, TRUE);
393 if (devc->enable_data_source_memory) {
394 if (devc->state == ST_GET_LOG_HEADER) {
395 /* Memory transfer started. */
396 devc->enable_data_source_memory = FALSE;
398 /* Tell device to start transferring from memory. */
399 cmd = CMD_TRANSFER_MEMORY;
400 serial_write_nonblocking(serial, &cmd, 1);
408 static int wait_for_token(const struct sr_dev_inst *sdi, int8_t *tokens, int timeout)
410 struct dev_context *devc;
411 struct sr_serial_dev_inst *serial;
418 devc->state = ST_INIT;
419 start_time = g_get_monotonic_time() / 1000;
421 if (serial_read_nonblocking(serial, &c, 1) != 1)
422 /* Device might have gone away. */
424 process_byte(sdi, c, FALSE);
425 if (devc->state != ST_INIT)
426 /* Wait for a whole packet to get processed. */
428 for (i = 0; tokens[i] != -1; i++) {
429 if (devc->token == tokens[i]) {
430 sr_spew("wait_for_token: got token 0x%.2x", devc->token);
434 if (timeout && g_get_monotonic_time() / 1000 - start_time > timeout)
435 return SR_ERR_TIMEOUT;
441 /* cmd is the command to send, tokens are the tokens that denote the state
442 * which the command affects. The first token is the desired state. */
443 static int cem_dt_885x_toggle(const struct sr_dev_inst *sdi, uint8_t cmd,
444 int8_t *tokens, int timeout)
446 struct dev_context *devc;
447 struct sr_serial_dev_inst *serial;
452 /* The device doesn't respond to commands very well. The
453 * only thing to do is wait for the token that will confirm
454 * whether the command worked or not, and resend if needed. */
456 if (serial_write_nonblocking(serial, (const void *)&cmd, 1) != 1)
458 if (wait_for_token(sdi, tokens, timeout) == SR_ERR)
460 if (devc->token == tokens[0])
468 SR_PRIV gboolean cem_dt_885x_recording_get(const struct sr_dev_inst *sdi,
471 struct dev_context *devc;
475 if (devc->recording == -1) {
476 /* Didn't pick up device state yet. */
477 tokens[0] = TOKEN_RECORDING_ON;
478 tokens[1] = TOKEN_RECORDING_OFF;
480 if (wait_for_token(sdi, tokens, 510) != SR_OK)
483 *state = devc->token == TOKEN_RECORDING_ON;
488 SR_PRIV int cem_dt_885x_recording_set(const struct sr_dev_inst *sdi,
491 struct dev_context *devc;
497 /* The toggle below needs the desired state in first position. */
499 tokens[0] = TOKEN_RECORDING_ON;
500 tokens[1] = TOKEN_RECORDING_OFF;
502 tokens[0] = TOKEN_RECORDING_OFF;
503 tokens[1] = TOKEN_RECORDING_ON;
507 if (devc->recording == -1) {
508 /* Didn't pick up device state yet. */
509 if (wait_for_token(sdi, tokens, 0) != SR_OK)
511 if (devc->token == tokens[0])
514 } else if (devc->recording == state)
518 /* Recording state notifications are sent at 2Hz, so allow just over
519 * that, 510ms, for the state to come in. */
520 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_RECORDING, tokens, 510);
525 SR_PRIV int cem_dt_885x_weight_freq_get(const struct sr_dev_inst *sdi)
527 struct dev_context *devc;
533 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
534 if (cur_setting == 0) {
535 /* Didn't pick up device state yet. */
536 tokens[0] = TOKEN_WEIGHT_FREQ_A;
537 tokens[1] = TOKEN_WEIGHT_FREQ_C;
539 if (wait_for_token(sdi, tokens, 0) != SR_OK)
541 if (devc->token == TOKEN_WEIGHT_FREQ_A)
542 return SR_MQFLAG_SPL_FREQ_WEIGHT_A;
544 return SR_MQFLAG_SPL_FREQ_WEIGHT_C;
549 SR_PRIV int cem_dt_885x_weight_freq_set(const struct sr_dev_inst *sdi, int freqw)
551 struct dev_context *devc;
552 int cur_setting, ret;
557 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
558 if (cur_setting == freqw)
559 /* Already set to this frequency weighting. */
562 /* The toggle below needs the desired state in first position. */
563 if (freqw == SR_MQFLAG_SPL_FREQ_WEIGHT_A) {
564 tokens[0] = TOKEN_WEIGHT_FREQ_A;
565 tokens[1] = TOKEN_WEIGHT_FREQ_C;
567 tokens[0] = TOKEN_WEIGHT_FREQ_C;
568 tokens[1] = TOKEN_WEIGHT_FREQ_A;
572 if (cur_setting == 0) {
573 /* Didn't pick up device state yet. */
574 if (wait_for_token(sdi, tokens, 0) != SR_OK)
576 if (devc->token == tokens[0])
581 /* 10ms timeout seems to work best for this. */
582 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_FREQ, tokens, 10);
587 SR_PRIV int cem_dt_885x_weight_time_get(const struct sr_dev_inst *sdi)
589 struct dev_context *devc;
595 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
596 if (cur_setting == 0) {
597 /* Didn't pick up device state yet. */
598 tokens[0] = TOKEN_WEIGHT_TIME_FAST;
599 tokens[1] = TOKEN_WEIGHT_TIME_SLOW;
601 if (wait_for_token(sdi, tokens, 0) != SR_OK)
603 if (devc->token == TOKEN_WEIGHT_TIME_FAST)
604 return SR_MQFLAG_SPL_TIME_WEIGHT_F;
606 return SR_MQFLAG_SPL_TIME_WEIGHT_S;
611 SR_PRIV int cem_dt_885x_weight_time_set(const struct sr_dev_inst *sdi, int timew)
613 struct dev_context *devc;
614 int cur_setting, ret;
619 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
620 if (cur_setting == timew)
621 /* Already set to this time weighting. */
624 /* The toggle below needs the desired state in first position. */
625 if (timew == SR_MQFLAG_SPL_TIME_WEIGHT_F) {
626 tokens[0] = TOKEN_WEIGHT_TIME_FAST;
627 tokens[1] = TOKEN_WEIGHT_TIME_SLOW;
629 tokens[0] = TOKEN_WEIGHT_TIME_SLOW;
630 tokens[1] = TOKEN_WEIGHT_TIME_FAST;
634 if (cur_setting == 0) {
635 /* Didn't pick up device state yet. */
636 if (wait_for_token(sdi, tokens, 0) != SR_OK)
638 if (devc->token == tokens[0])
643 /* 51ms timeout seems to work best for this. */
644 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_TIME, tokens, 51);
649 SR_PRIV int cem_dt_885x_holdmode_get(const struct sr_dev_inst *sdi,
652 struct dev_context *devc;
657 if (devc->cur_mqflags == 0) {
658 tokens[0] = TOKEN_HOLD_MAX;
659 tokens[1] = TOKEN_HOLD_MIN;
660 tokens[2] = TOKEN_HOLD_NONE;
662 if (wait_for_token(sdi, tokens, 0) != SR_OK)
664 if (devc->token == TOKEN_HOLD_MAX)
665 devc->cur_mqflags = SR_MQFLAG_MAX;
666 else if (devc->token == TOKEN_HOLD_MIN)
667 devc->cur_mqflags = SR_MQFLAG_MIN;
669 *holdmode = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
674 SR_PRIV int cem_dt_885x_holdmode_set(const struct sr_dev_inst *sdi, int holdmode)
676 struct dev_context *devc;
677 int cur_setting, ret;
682 /* The toggle below needs the desired state in first position. */
683 if (holdmode == SR_MQFLAG_MAX) {
684 tokens[0] = TOKEN_HOLD_MAX;
685 tokens[1] = TOKEN_HOLD_MIN;
686 tokens[2] = TOKEN_HOLD_NONE;
687 } else if (holdmode == SR_MQFLAG_MIN) {
688 tokens[0] = TOKEN_HOLD_MIN;
689 tokens[1] = TOKEN_HOLD_MAX;
690 tokens[2] = TOKEN_HOLD_NONE;
692 tokens[0] = TOKEN_HOLD_NONE;
693 tokens[1] = TOKEN_HOLD_MAX;
694 tokens[2] = TOKEN_HOLD_MIN;
698 if (devc->cur_mqflags == 0) {
699 /* Didn't pick up device state yet. */
700 if (wait_for_token(sdi, tokens, 0) != SR_OK)
702 if (devc->token == tokens[0])
706 cur_setting = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
707 if (cur_setting == holdmode)
708 /* Already set correctly. */
712 /* 51ms timeout seems to work best for this. */
713 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_HOLD_MAX_MIN, tokens, 51);
718 SR_PRIV int cem_dt_885x_meas_range_get(const struct sr_dev_inst *sdi,
719 uint64_t *low, uint64_t *high)
721 struct dev_context *devc;
725 if (devc->cur_meas_range == 0) {
726 tokens[0] = TOKEN_MEAS_RANGE_30_130;
727 tokens[1] = TOKEN_MEAS_RANGE_30_80;
728 tokens[2] = TOKEN_MEAS_RANGE_50_100;
729 tokens[3] = TOKEN_MEAS_RANGE_80_130;
731 if (wait_for_token(sdi, tokens, 0) != SR_OK)
733 devc->cur_meas_range = devc->token;
736 switch (devc->cur_meas_range) {
737 case TOKEN_MEAS_RANGE_30_130:
741 case TOKEN_MEAS_RANGE_30_80:
745 case TOKEN_MEAS_RANGE_50_100:
749 case TOKEN_MEAS_RANGE_80_130:
760 SR_PRIV int cem_dt_885x_meas_range_set(const struct sr_dev_inst *sdi,
761 uint64_t low, uint64_t high)
763 struct dev_context *devc;
765 int8_t token, tokens[6];
768 if (low == 30 && high == 130)
769 token = TOKEN_MEAS_RANGE_30_130;
770 else if (low == 30 && high == 80)
771 token = TOKEN_MEAS_RANGE_30_80;
772 else if (low == 50 && high == 100)
773 token = TOKEN_MEAS_RANGE_50_100;
774 else if (low == 80 && high == 130)
775 token = TOKEN_MEAS_RANGE_80_130;
779 sr_dbg("want 0x%.2x", token);
780 /* The toggle below needs the desired state in first position. */
782 tokens[1] = TOKEN_MEAS_RANGE_30_130;
783 tokens[2] = TOKEN_MEAS_RANGE_30_80;
784 tokens[3] = TOKEN_MEAS_RANGE_50_100;
785 tokens[4] = TOKEN_MEAS_RANGE_80_130;
788 if (devc->cur_meas_range == 0) {
789 /* 110ms should be enough for two of these announcements */
790 if (wait_for_token(sdi, tokens, 110) != SR_OK)
792 devc->cur_meas_range = devc->token;
795 if (devc->cur_meas_range == token)
796 /* Already set to this range. */
799 /* For measurement range, it works best to ignore announcements of the
800 * current setting and keep resending the toggle quickly. */
802 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_MEAS_RANGE, tokens, 11);
807 SR_PRIV int cem_dt_885x_power_off(const struct sr_dev_inst *sdi)
809 struct sr_serial_dev_inst *serial;
814 cmd = CMD_TOGGLE_POWER_OFF;
816 serial_flush(serial);
817 if (serial_write_nonblocking(serial, (const void *)&cmd, 1) != 1)
819 /* It never takes more than 23ms for the next token to arrive. */
821 if (serial_read_nonblocking(serial, &c, 1) != 1)
822 /* Device is no longer responding. Good! */
826 /* In case the user manually turns on the device again, reset
827 * the port back to blocking. */
828 serial_close(serial);
829 serial_open(serial, SERIAL_RDWR);