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/>.
23 /* Length of expected payload for each token. */
24 static const 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 },
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 },
51 static int find_token_payload_len(unsigned char c)
55 for (i = 0; i < ARRAY_SIZE(token_payloads); i++) {
56 if (token_payloads[i][0] == c)
57 return token_payloads[i][1];
63 /* Process measurement or setting (0xa5 command). */
64 static void process_mset(const struct sr_dev_inst *sdi)
66 struct dev_context *devc;
67 struct sr_datafeed_packet packet;
68 struct sr_datafeed_analog analog;
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);
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]);
83 sr_spew("%s", dbg->str);
84 g_string_free(dbg, TRUE);
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;
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;
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;
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;
105 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MAX;
106 devc->cur_mqflags &= ~SR_MQFLAG_MIN;
109 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MIN;
110 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
112 case TOKEN_HOLD_NONE:
113 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_HOLD);
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;
120 devc->last_spl = fvalue;
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. */
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.channels = sdi->channels;
138 analog.num_samples = 1;
139 analog.data = &devc->last_spl;
140 packet.type = SR_DF_ANALOG;
141 packet.payload = &analog;
142 sr_session_send(devc->cb_data, &packet);
145 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
146 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
149 case TOKEN_RECORDING_ON:
150 devc->recording = TRUE;
152 case TOKEN_RECORDING_OFF:
153 devc->recording = FALSE;
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;
163 case TOKEN_STORE_FULL:
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:
169 /* Not useful, or not expressible in sigrok. */
175 static void send_data(const struct sr_dev_inst *sdi, unsigned char *data,
176 uint64_t num_samples)
178 struct dev_context *devc;
179 struct sr_datafeed_packet packet;
180 struct sr_datafeed_analog analog;
181 float fbuf[SAMPLES_PER_PACKET];
186 for (i = 0; i < num_samples; i ++) {
187 fbuf[i] = ((data[i * 2] & 0xf0) >> 4) * 100;
188 fbuf[i] += (data[i * 2] & 0x0f) * 10;
189 fbuf[i] += ((data[i * 2 + 1] & 0xf0) >> 4);
190 fbuf[i] += (data[i * 2 + 1] & 0x0f) / 10.0;
192 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
193 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
194 analog.mqflags = devc->cur_mqflags;
195 analog.unit = SR_UNIT_DECIBEL_SPL;
196 analog.channels = sdi->channels;
197 analog.num_samples = num_samples;
199 packet.type = SR_DF_ANALOG;
200 packet.payload = &analog;
201 sr_session_send(devc->cb_data, &packet);
203 devc->num_samples += analog.num_samples;
204 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
205 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
211 static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c,
214 struct dev_context *devc;
215 struct sr_datafeed_packet packet;
216 struct sr_datafeed_meta meta;
217 struct sr_config *src;
221 if (!(devc = sdi->priv))
225 /* Device is in hold mode */
226 devc->cur_mqflags |= SR_MQFLAG_HOLD;
228 if (devc->hold_last_sent == 0) {
229 /* First hold notification. */
230 devc->hold_last_sent = g_get_monotonic_time();
231 /* When the device leaves hold mode, it starts from scratch. */
232 devc->state = ST_INIT;
234 cur_time = g_get_monotonic_time();
235 if (cur_time - devc->hold_last_sent > HOLD_REPEAT_INTERVAL) {
236 /* Force the last measurement out again. */
238 devc->token = TOKEN_MEAS_WAS_READOUT;
241 devc->hold_last_sent = cur_time;
247 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
248 devc->hold_last_sent = 0;
250 if (devc->state == ST_INIT) {
254 devc->state = ST_GET_TOKEN;
255 } else if (c == 0xbb) {
258 devc->state = ST_GET_LOG_HEADER;
259 sr_dbg("got command 0xbb");
261 } else if (devc->state == ST_GET_TOKEN) {
264 len = find_token_payload_len(devc->token);
265 if (len == -1 || len > 0) {
267 devc->state = ST_GET_DATA;
271 devc->state = ST_INIT;
273 } else if (devc->state == ST_GET_DATA) {
274 len = find_token_payload_len(devc->token);
276 /* We don't know this token. */
277 sr_dbg("Unknown 0xa5 token 0x%.2x", devc->token);
278 if (c == 0xa5 || c == 0xbb) {
279 /* Looks like a new command however. */
282 devc->state = ST_INIT;
284 devc->buf[devc->buf_len++] = c;
285 if (devc->buf_len > BUF_SIZE) {
286 /* Shouldn't happen, ignore. */
287 devc->state = ST_INIT;
291 devc->buf[devc->buf_len++] = c;
292 if (devc->buf_len == len) {
295 devc->state = ST_INIT;
296 } else if (devc->buf_len > BUF_SIZE) {
297 /* Shouldn't happen, ignore. */
298 devc->state = ST_INIT;
301 } else if (devc->state == ST_GET_LOG_HEADER) {
302 sr_dbg("log header: 0x%.2x", c);
303 if (devc->buf_len < 2)
304 devc->buf[devc->buf_len++] = c;
305 if (devc->buf_len == 2) {
306 sr_dbg("Device says it has %d bytes stored.",
307 ((devc->buf[0] << 8) + devc->buf[1]) - 100);
309 devc->state = ST_GET_LOG_RECORD_META;
311 } else if (devc->state == ST_GET_LOG_RECORD_META) {
312 sr_dbg("log meta: 0x%.2x", c);
313 if (c == RECORD_END) {
314 devc->state = ST_INIT;
315 /* Stop acquisition after transferring all stored
316 * records. Otherwise the frontend would have no
317 * way to tell where stored data ends and live
318 * measurements begin. */
319 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
321 } else if (c == RECORD_DATA) {
323 devc->state = ST_GET_LOG_RECORD_DATA;
325 /* RECORD_DBA/RECORD_DBC + 7 bytes of metadata */
326 devc->buf[devc->buf_len++] = c;
327 if (devc->buf_len < 8)
328 /* Keep filling up the record header. */
330 if (devc->buf[0] == RECORD_DBA)
331 devc->cur_mqflags = SR_MQFLAG_SPL_FREQ_WEIGHT_A;
332 else if (devc->buf[0] == RECORD_DBC)
333 devc->cur_mqflags = SR_MQFLAG_SPL_FREQ_WEIGHT_C;
335 /* Shouldn't happen. */
336 sr_dbg("Unknown record token 0x%.2x", c);
339 packet.type = SR_DF_META;
340 packet.payload = &meta;
341 src = sr_config_new(SR_CONF_SAMPLE_INTERVAL,
342 g_variant_new_uint64(devc->buf[7] * 1000));
343 meta.config = g_slist_append(NULL, src);
344 sr_session_send(devc->cb_data, &packet);
348 } else if (devc->state == ST_GET_LOG_RECORD_DATA) {
349 sr_dbg("log data: 0x%.2x", c);
350 if (c == RECORD_DBA || c == RECORD_DBC || c == RECORD_DATA || c == RECORD_END) {
351 /* Work around off-by-one bug in device firmware. This
352 * happens only on the last record, i.e. before RECORD_END */
353 if (devc->buf_len & 1)
355 /* Done with this set of samples */
356 send_data(sdi, devc->buf, devc->buf_len / 2);
359 /* Process this meta marker in the right state. */
360 devc->state = ST_GET_LOG_RECORD_META;
361 process_byte(sdi, c, handle_packets);
363 devc->buf[devc->buf_len++] = c;
364 if (devc->buf_len == SAMPLES_PER_PACKET * 2) {
365 send_data(sdi, devc->buf, devc->buf_len / 2);
373 SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
375 const struct sr_dev_inst *sdi;
376 struct dev_context *devc;
377 struct sr_serial_dev_inst *serial;
378 unsigned char c, cmd;
382 if (!(sdi = cb_data))
387 if (revents == G_IO_IN) {
388 if (serial_read_nonblocking(serial, &c, 1) != 1)
390 process_byte(sdi, c, TRUE);
392 if (devc->enable_data_source_memory) {
393 if (devc->state == ST_GET_LOG_HEADER) {
394 /* Memory transfer started. */
395 devc->enable_data_source_memory = FALSE;
397 /* Tell device to start transferring from memory. */
398 cmd = CMD_TRANSFER_MEMORY;
399 serial_write_nonblocking(serial, &cmd, 1);
407 static int wait_for_token(const struct sr_dev_inst *sdi, int8_t *tokens, int timeout)
409 struct dev_context *devc;
410 struct sr_serial_dev_inst *serial;
417 devc->state = ST_INIT;
418 start_time = g_get_monotonic_time() / 1000;
420 if (serial_read_nonblocking(serial, &c, 1) != 1)
421 /* Device might have gone away. */
423 process_byte(sdi, c, FALSE);
424 if (devc->state != ST_INIT)
425 /* Wait for a whole packet to get processed. */
427 for (i = 0; tokens[i] != -1; i++) {
428 if (devc->token == tokens[i]) {
429 sr_spew("wait_for_token: got token 0x%.2x", devc->token);
433 if (timeout && g_get_monotonic_time() / 1000 - start_time > timeout)
434 return SR_ERR_TIMEOUT;
440 /* cmd is the command to send, tokens are the tokens that denote the state
441 * which the command affects. The first token is the desired state. */
442 static int cem_dt_885x_toggle(const struct sr_dev_inst *sdi, uint8_t cmd,
443 int8_t *tokens, int timeout)
445 struct dev_context *devc;
446 struct sr_serial_dev_inst *serial;
451 /* The device doesn't respond to commands very well. The
452 * only thing to do is wait for the token that will confirm
453 * whether the command worked or not, and resend if needed. */
455 if (serial_write_nonblocking(serial, (const void *)&cmd, 1) != 1)
457 if (wait_for_token(sdi, tokens, timeout) == SR_ERR)
459 if (devc->token == tokens[0])
467 SR_PRIV gboolean cem_dt_885x_recording_get(const struct sr_dev_inst *sdi,
470 struct dev_context *devc;
474 if (devc->recording == -1) {
475 /* Didn't pick up device state yet. */
476 tokens[0] = TOKEN_RECORDING_ON;
477 tokens[1] = TOKEN_RECORDING_OFF;
479 if (wait_for_token(sdi, tokens, 510) != SR_OK)
482 *state = devc->token == TOKEN_RECORDING_ON;
487 SR_PRIV int cem_dt_885x_recording_set(const struct sr_dev_inst *sdi,
490 struct dev_context *devc;
496 /* The toggle below needs the desired state in first position. */
498 tokens[0] = TOKEN_RECORDING_ON;
499 tokens[1] = TOKEN_RECORDING_OFF;
501 tokens[0] = TOKEN_RECORDING_OFF;
502 tokens[1] = TOKEN_RECORDING_ON;
506 if (devc->recording == -1) {
507 /* Didn't pick up device state yet. */
508 if (wait_for_token(sdi, tokens, 0) != SR_OK)
510 if (devc->token == tokens[0])
513 } else if (devc->recording == state)
517 /* Recording state notifications are sent at 2Hz, so allow just over
518 * that, 510ms, for the state to come in. */
519 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_RECORDING, tokens, 510);
524 SR_PRIV int cem_dt_885x_weight_freq_get(const struct sr_dev_inst *sdi)
526 struct dev_context *devc;
532 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
533 if (cur_setting == 0) {
534 /* Didn't pick up device state yet. */
535 tokens[0] = TOKEN_WEIGHT_FREQ_A;
536 tokens[1] = TOKEN_WEIGHT_FREQ_C;
538 if (wait_for_token(sdi, tokens, 0) != SR_OK)
540 if (devc->token == TOKEN_WEIGHT_FREQ_A)
541 return SR_MQFLAG_SPL_FREQ_WEIGHT_A;
543 return SR_MQFLAG_SPL_FREQ_WEIGHT_C;
548 SR_PRIV int cem_dt_885x_weight_freq_set(const struct sr_dev_inst *sdi, int freqw)
550 struct dev_context *devc;
551 int cur_setting, ret;
556 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
557 if (cur_setting == freqw)
558 /* Already set to this frequency weighting. */
561 /* The toggle below needs the desired state in first position. */
562 if (freqw == SR_MQFLAG_SPL_FREQ_WEIGHT_A) {
563 tokens[0] = TOKEN_WEIGHT_FREQ_A;
564 tokens[1] = TOKEN_WEIGHT_FREQ_C;
566 tokens[0] = TOKEN_WEIGHT_FREQ_C;
567 tokens[1] = TOKEN_WEIGHT_FREQ_A;
571 if (cur_setting == 0) {
572 /* Didn't pick up device state yet. */
573 if (wait_for_token(sdi, tokens, 0) != SR_OK)
575 if (devc->token == tokens[0])
580 /* 10ms timeout seems to work best for this. */
581 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_FREQ, tokens, 10);
586 SR_PRIV int cem_dt_885x_weight_time_get(const struct sr_dev_inst *sdi)
588 struct dev_context *devc;
594 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
595 if (cur_setting == 0) {
596 /* Didn't pick up device state yet. */
597 tokens[0] = TOKEN_WEIGHT_TIME_FAST;
598 tokens[1] = TOKEN_WEIGHT_TIME_SLOW;
600 if (wait_for_token(sdi, tokens, 0) != SR_OK)
602 if (devc->token == TOKEN_WEIGHT_TIME_FAST)
603 return SR_MQFLAG_SPL_TIME_WEIGHT_F;
605 return SR_MQFLAG_SPL_TIME_WEIGHT_S;
610 SR_PRIV int cem_dt_885x_weight_time_set(const struct sr_dev_inst *sdi, int timew)
612 struct dev_context *devc;
613 int cur_setting, ret;
618 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
619 if (cur_setting == timew)
620 /* Already set to this time weighting. */
623 /* The toggle below needs the desired state in first position. */
624 if (timew == SR_MQFLAG_SPL_TIME_WEIGHT_F) {
625 tokens[0] = TOKEN_WEIGHT_TIME_FAST;
626 tokens[1] = TOKEN_WEIGHT_TIME_SLOW;
628 tokens[0] = TOKEN_WEIGHT_TIME_SLOW;
629 tokens[1] = TOKEN_WEIGHT_TIME_FAST;
633 if (cur_setting == 0) {
634 /* Didn't pick up device state yet. */
635 if (wait_for_token(sdi, tokens, 0) != SR_OK)
637 if (devc->token == tokens[0])
642 /* 51ms timeout seems to work best for this. */
643 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_TIME, tokens, 51);
648 SR_PRIV int cem_dt_885x_holdmode_get(const struct sr_dev_inst *sdi,
651 struct dev_context *devc;
656 if (devc->cur_mqflags == 0) {
657 tokens[0] = TOKEN_HOLD_MAX;
658 tokens[1] = TOKEN_HOLD_MIN;
659 tokens[2] = TOKEN_HOLD_NONE;
661 if (wait_for_token(sdi, tokens, 0) != SR_OK)
663 if (devc->token == TOKEN_HOLD_MAX)
664 devc->cur_mqflags = SR_MQFLAG_MAX;
665 else if (devc->token == TOKEN_HOLD_MIN)
666 devc->cur_mqflags = SR_MQFLAG_MIN;
668 *holdmode = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
673 SR_PRIV int cem_dt_885x_holdmode_set(const struct sr_dev_inst *sdi, int holdmode)
675 struct dev_context *devc;
676 int cur_setting, ret;
681 /* The toggle below needs the desired state in first position. */
682 if (holdmode == SR_MQFLAG_MAX) {
683 tokens[0] = TOKEN_HOLD_MAX;
684 tokens[1] = TOKEN_HOLD_MIN;
685 tokens[2] = TOKEN_HOLD_NONE;
686 } else if (holdmode == SR_MQFLAG_MIN) {
687 tokens[0] = TOKEN_HOLD_MIN;
688 tokens[1] = TOKEN_HOLD_MAX;
689 tokens[2] = TOKEN_HOLD_NONE;
691 tokens[0] = TOKEN_HOLD_NONE;
692 tokens[1] = TOKEN_HOLD_MAX;
693 tokens[2] = TOKEN_HOLD_MIN;
697 if (devc->cur_mqflags == 0) {
698 /* Didn't pick up device state yet. */
699 if (wait_for_token(sdi, tokens, 0) != SR_OK)
701 if (devc->token == tokens[0])
705 cur_setting = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
706 if (cur_setting == holdmode)
707 /* Already set correctly. */
711 /* 51ms timeout seems to work best for this. */
712 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_HOLD_MAX_MIN, tokens, 51);
717 SR_PRIV int cem_dt_885x_meas_range_get(const struct sr_dev_inst *sdi,
718 uint64_t *low, uint64_t *high)
720 struct dev_context *devc;
724 if (devc->cur_meas_range == 0) {
725 tokens[0] = TOKEN_MEAS_RANGE_30_130;
726 tokens[1] = TOKEN_MEAS_RANGE_30_80;
727 tokens[2] = TOKEN_MEAS_RANGE_50_100;
728 tokens[3] = TOKEN_MEAS_RANGE_80_130;
730 if (wait_for_token(sdi, tokens, 0) != SR_OK)
732 devc->cur_meas_range = devc->token;
735 switch (devc->cur_meas_range) {
736 case TOKEN_MEAS_RANGE_30_130:
740 case TOKEN_MEAS_RANGE_30_80:
744 case TOKEN_MEAS_RANGE_50_100:
748 case TOKEN_MEAS_RANGE_80_130:
759 SR_PRIV int cem_dt_885x_meas_range_set(const struct sr_dev_inst *sdi,
760 uint64_t low, uint64_t high)
762 struct dev_context *devc;
764 int8_t token, tokens[6];
767 if (low == 30 && high == 130)
768 token = TOKEN_MEAS_RANGE_30_130;
769 else if (low == 30 && high == 80)
770 token = TOKEN_MEAS_RANGE_30_80;
771 else if (low == 50 && high == 100)
772 token = TOKEN_MEAS_RANGE_50_100;
773 else if (low == 80 && high == 130)
774 token = TOKEN_MEAS_RANGE_80_130;
778 sr_dbg("want 0x%.2x", token);
779 /* The toggle below needs the desired state in first position. */
781 tokens[1] = TOKEN_MEAS_RANGE_30_130;
782 tokens[2] = TOKEN_MEAS_RANGE_30_80;
783 tokens[3] = TOKEN_MEAS_RANGE_50_100;
784 tokens[4] = TOKEN_MEAS_RANGE_80_130;
787 if (devc->cur_meas_range == 0) {
788 /* 110ms should be enough for two of these announcements */
789 if (wait_for_token(sdi, tokens, 110) != SR_OK)
791 devc->cur_meas_range = devc->token;
794 if (devc->cur_meas_range == token)
795 /* Already set to this range. */
798 /* For measurement range, it works best to ignore announcements of the
799 * current setting and keep resending the toggle quickly. */
801 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_MEAS_RANGE, tokens, 11);
806 SR_PRIV int cem_dt_885x_power_off(const struct sr_dev_inst *sdi)
808 struct sr_serial_dev_inst *serial;
813 cmd = CMD_TOGGLE_POWER_OFF;
815 serial_flush(serial);
816 if (serial_write_nonblocking(serial, (const void *)&cmd, 1) != 1)
818 /* It never takes more than 23ms for the next token to arrive. */
820 if (serial_read_nonblocking(serial, &c, 1) != 1)
821 /* Device is no longer responding. Good! */
825 /* In case the user manually turns on the device again, reset
826 * the port back to blocking. */
827 serial_close(serial);
828 serial_open(serial, SERIAL_RDWR);