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 analog;
70 struct sr_analog_encoding encoding;
71 struct sr_analog_meaning meaning;
72 struct sr_analog_spec spec;
78 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
79 dbg = g_string_sized_new(128);
80 g_string_printf(dbg, "got command 0x%.2x token 0x%.2x",
81 devc->cmd, devc->token);
83 g_string_append_printf(dbg, " payload");
84 for (i = 0; i < devc->buf_len; i++)
85 g_string_append_printf(dbg, " %.2x", devc->buf[i]);
87 sr_spew("%s", dbg->str);
88 g_string_free(dbg, TRUE);
91 switch (devc->token) {
92 case TOKEN_WEIGHT_TIME_FAST:
93 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F;
94 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_S;
96 case TOKEN_WEIGHT_TIME_SLOW:
97 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S;
98 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_F;
100 case TOKEN_WEIGHT_FREQ_A:
101 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A;
102 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_C;
104 case TOKEN_WEIGHT_FREQ_C:
105 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C;
106 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_A;
109 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MAX;
110 devc->cur_mqflags &= ~SR_MQFLAG_MIN;
113 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MIN;
114 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
116 case TOKEN_HOLD_NONE:
117 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_HOLD);
119 case TOKEN_MEASUREMENT:
120 fvalue = ((devc->buf[0] & 0xf0) >> 4) * 100;
121 fvalue += (devc->buf[0] & 0x0f) * 10;
122 fvalue += ((devc->buf[1] & 0xf0) >> 4);
123 fvalue += (devc->buf[1] & 0x0f) / 10.0;
124 devc->last_spl = fvalue;
126 case TOKEN_MEAS_WAS_READOUT:
127 case TOKEN_MEAS_WAS_BARGRAPH:
128 if (devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN)) {
129 if (devc->token == TOKEN_MEAS_WAS_BARGRAPH) {
130 /* The device still sends bargraph measurements even
131 * when in max/min hold mode. Suppress them here, unless
132 * they're readout values. This duplicates the behavior
133 * of the device display exactly. */
137 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
138 analog.meaning->mq = SR_MQ_SOUND_PRESSURE_LEVEL;
139 analog.meaning->mqflags = devc->cur_mqflags;
140 analog.meaning->unit = SR_UNIT_DECIBEL_SPL;
141 analog.meaning->channels = sdi->channels;
142 analog.num_samples = 1;
143 analog.data = &devc->last_spl;
144 packet.type = SR_DF_ANALOG;
145 packet.payload = &analog;
146 sr_session_send(sdi, &packet);
149 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
150 sr_dev_acquisition_stop((struct sr_dev_inst *)sdi);
152 case TOKEN_RECORDING_ON:
153 devc->recording = TRUE;
155 case TOKEN_RECORDING_OFF:
156 devc->recording = FALSE;
158 case TOKEN_MEAS_RANGE_30_80:
159 case TOKEN_MEAS_RANGE_30_130:
160 case TOKEN_MEAS_RANGE_50_100:
161 case TOKEN_MEAS_RANGE_80_130:
162 devc->cur_meas_range = devc->token;
166 case TOKEN_STORE_FULL:
167 case TOKEN_BATTERY_OK:
168 case TOKEN_BATTERY_LOW:
169 case TOKEN_MEAS_RANGE_OK:
170 case TOKEN_MEAS_RANGE_OVER:
171 case TOKEN_MEAS_RANGE_UNDER:
172 /* Not useful, or not expressible in sigrok. */
178 static void send_data(const struct sr_dev_inst *sdi, unsigned char *data,
179 uint64_t num_samples)
181 struct dev_context *devc;
182 struct sr_datafeed_packet packet;
183 struct sr_datafeed_analog analog;
184 struct sr_analog_encoding encoding;
185 struct sr_analog_meaning meaning;
186 struct sr_analog_spec spec;
187 float fbuf[SAMPLES_PER_PACKET];
192 for (i = 0; i < num_samples; i++) {
193 fbuf[i] = ((data[i * 2] & 0xf0) >> 4) * 100;
194 fbuf[i] += (data[i * 2] & 0x0f) * 10;
195 fbuf[i] += ((data[i * 2 + 1] & 0xf0) >> 4);
196 fbuf[i] += (data[i * 2 + 1] & 0x0f) / 10.0;
198 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
199 analog.meaning->mq = SR_MQ_SOUND_PRESSURE_LEVEL;
200 analog.meaning->mqflags = devc->cur_mqflags;
201 analog.meaning->unit = SR_UNIT_DECIBEL_SPL;
202 analog.meaning->channels = sdi->channels;
203 analog.num_samples = num_samples;
205 packet.type = SR_DF_ANALOG;
206 packet.payload = &analog;
207 sr_session_send(sdi, &packet);
209 devc->num_samples += analog.num_samples;
210 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
211 sr_dev_acquisition_stop((struct sr_dev_inst *)sdi);
216 static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c,
219 struct dev_context *devc;
220 struct sr_datafeed_packet packet;
221 struct sr_datafeed_meta meta;
222 struct sr_config *src;
226 if (!(devc = sdi->priv))
230 /* Device is in hold mode */
231 devc->cur_mqflags |= SR_MQFLAG_HOLD;
233 if (devc->hold_last_sent == 0) {
234 /* First hold notification. */
235 devc->hold_last_sent = g_get_monotonic_time();
236 /* When the device leaves hold mode, it starts from scratch. */
237 devc->state = ST_INIT;
239 cur_time = g_get_monotonic_time();
240 if (cur_time - devc->hold_last_sent > HOLD_REPEAT_INTERVAL) {
241 /* Force the last measurement out again. */
243 devc->token = TOKEN_MEAS_WAS_READOUT;
246 devc->hold_last_sent = cur_time;
252 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
253 devc->hold_last_sent = 0;
255 if (devc->state == ST_INIT) {
259 devc->state = ST_GET_TOKEN;
260 } else if (c == 0xbb) {
263 devc->state = ST_GET_LOG_HEADER;
264 sr_dbg("got command 0xbb");
266 } else if (devc->state == ST_GET_TOKEN) {
269 len = find_token_payload_len(devc->token);
270 if (len == -1 || len > 0) {
272 devc->state = ST_GET_DATA;
276 devc->state = ST_INIT;
278 } else if (devc->state == ST_GET_DATA) {
279 len = find_token_payload_len(devc->token);
281 /* We don't know this token. */
282 sr_dbg("Unknown 0xa5 token 0x%.2x", devc->token);
283 if (c == 0xa5 || c == 0xbb) {
284 /* Looks like a new command however. */
287 devc->state = ST_INIT;
289 devc->buf[devc->buf_len++] = c;
290 if (devc->buf_len > BUF_SIZE) {
291 /* Shouldn't happen, ignore. */
292 devc->state = ST_INIT;
296 devc->buf[devc->buf_len++] = c;
297 if (devc->buf_len == len) {
300 devc->state = ST_INIT;
301 } else if (devc->buf_len > BUF_SIZE) {
302 /* Shouldn't happen, ignore. */
303 devc->state = ST_INIT;
306 } else if (devc->state == ST_GET_LOG_HEADER) {
307 sr_dbg("log header: 0x%.2x", c);
308 if (devc->buf_len < 2)
309 devc->buf[devc->buf_len++] = c;
310 if (devc->buf_len == 2) {
311 sr_dbg("Device says it has %d bytes stored.",
312 ((devc->buf[0] << 8) + devc->buf[1]) - 100);
314 devc->state = ST_GET_LOG_RECORD_META;
316 } else if (devc->state == ST_GET_LOG_RECORD_META) {
317 sr_dbg("log meta: 0x%.2x", c);
318 if (c == RECORD_END) {
319 devc->state = ST_INIT;
320 /* Stop acquisition after transferring all stored
321 * records. Otherwise the frontend would have no
322 * way to tell where stored data ends and live
323 * measurements begin. */
324 sr_dev_acquisition_stop((struct sr_dev_inst *)sdi);
325 } else if (c == RECORD_DATA) {
327 devc->state = ST_GET_LOG_RECORD_DATA;
329 /* RECORD_DBA/RECORD_DBC + 7 bytes of metadata */
330 devc->buf[devc->buf_len++] = c;
331 if (devc->buf_len < 8)
332 /* Keep filling up the record header. */
334 if (devc->buf[0] == RECORD_DBA)
335 devc->cur_mqflags = SR_MQFLAG_SPL_FREQ_WEIGHT_A;
336 else if (devc->buf[0] == RECORD_DBC)
337 devc->cur_mqflags = SR_MQFLAG_SPL_FREQ_WEIGHT_C;
339 /* Shouldn't happen. */
340 sr_dbg("Unknown record token 0x%.2x", c);
343 packet.type = SR_DF_META;
344 packet.payload = &meta;
345 src = sr_config_new(SR_CONF_SAMPLE_INTERVAL,
346 g_variant_new_uint64(devc->buf[7] * 1000));
347 meta.config = g_slist_append(NULL, src);
348 sr_session_send(sdi, &packet);
352 } else if (devc->state == ST_GET_LOG_RECORD_DATA) {
353 sr_dbg("log data: 0x%.2x", c);
354 if (c == RECORD_DBA || c == RECORD_DBC || c == RECORD_DATA || c == RECORD_END) {
355 /* Work around off-by-one bug in device firmware. This
356 * happens only on the last record, i.e. before RECORD_END */
357 if (devc->buf_len & 1)
359 /* Done with this set of samples */
360 send_data(sdi, devc->buf, devc->buf_len / 2);
363 /* Process this meta marker in the right state. */
364 devc->state = ST_GET_LOG_RECORD_META;
365 process_byte(sdi, c, handle_packets);
367 devc->buf[devc->buf_len++] = c;
368 if (devc->buf_len == SAMPLES_PER_PACKET * 2) {
369 send_data(sdi, devc->buf, devc->buf_len / 2);
377 SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
379 const struct sr_dev_inst *sdi;
380 struct dev_context *devc;
381 struct sr_serial_dev_inst *serial;
382 unsigned char c, cmd;
386 if (!(sdi = cb_data))
391 if (revents == G_IO_IN) {
392 if (serial_read_nonblocking(serial, &c, 1) != 1)
394 process_byte(sdi, c, TRUE);
396 if (devc->enable_data_source_memory) {
397 if (devc->state == ST_GET_LOG_HEADER) {
398 /* Memory transfer started. */
399 devc->enable_data_source_memory = FALSE;
401 /* Tell device to start transferring from memory. */
402 cmd = CMD_TRANSFER_MEMORY;
403 serial_write_nonblocking(serial, &cmd, 1);
411 static int wait_for_token(const struct sr_dev_inst *sdi, int8_t *tokens, int timeout)
413 struct dev_context *devc;
414 struct sr_serial_dev_inst *serial;
421 devc->state = ST_INIT;
422 start_time = g_get_monotonic_time() / 1000;
424 if (serial_read_nonblocking(serial, &c, 1) != 1)
425 /* Device might have gone away. */
427 process_byte(sdi, c, FALSE);
428 if (devc->state != ST_INIT)
429 /* Wait for a whole packet to get processed. */
431 for (i = 0; tokens[i] != -1; i++) {
432 if (devc->token == tokens[i]) {
433 sr_spew("wait_for_token: got token 0x%.2x", devc->token);
437 if (timeout && g_get_monotonic_time() / 1000 - start_time > timeout)
438 return SR_ERR_TIMEOUT;
444 /* cmd is the command to send, tokens are the tokens that denote the state
445 * which the command affects. The first token is the desired state. */
446 static int cem_dt_885x_toggle(const struct sr_dev_inst *sdi, uint8_t cmd,
447 int8_t *tokens, int timeout)
449 struct dev_context *devc;
450 struct sr_serial_dev_inst *serial;
455 /* The device doesn't respond to commands very well. The
456 * only thing to do is wait for the token that will confirm
457 * whether the command worked or not, and resend if needed. */
459 if (serial_write_nonblocking(serial, (const void *)&cmd, 1) != 1)
461 if (wait_for_token(sdi, tokens, timeout) == SR_ERR)
463 if (devc->token == tokens[0])
471 SR_PRIV gboolean cem_dt_885x_recording_get(const struct sr_dev_inst *sdi,
474 struct dev_context *devc;
478 if (devc->recording == -1) {
479 /* Didn't pick up device state yet. */
480 tokens[0] = TOKEN_RECORDING_ON;
481 tokens[1] = TOKEN_RECORDING_OFF;
483 if (wait_for_token(sdi, tokens, 510) != SR_OK)
486 *state = devc->token == TOKEN_RECORDING_ON;
491 SR_PRIV int cem_dt_885x_recording_set(const struct sr_dev_inst *sdi,
494 struct dev_context *devc;
500 /* The toggle below needs the desired state in first position. */
502 tokens[0] = TOKEN_RECORDING_ON;
503 tokens[1] = TOKEN_RECORDING_OFF;
505 tokens[0] = TOKEN_RECORDING_OFF;
506 tokens[1] = TOKEN_RECORDING_ON;
510 if (devc->recording == -1) {
511 /* Didn't pick up device state yet. */
512 if (wait_for_token(sdi, tokens, 0) != SR_OK)
514 if (devc->token == tokens[0])
517 } else if (devc->recording == state)
521 /* Recording state notifications are sent at 2Hz, so allow just over
522 * that, 510ms, for the state to come in. */
523 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_RECORDING, tokens, 510);
528 SR_PRIV int cem_dt_885x_weight_freq_get(const struct sr_dev_inst *sdi)
530 struct dev_context *devc;
536 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
537 if (cur_setting == 0) {
538 /* Didn't pick up device state yet. */
539 tokens[0] = TOKEN_WEIGHT_FREQ_A;
540 tokens[1] = TOKEN_WEIGHT_FREQ_C;
542 if (wait_for_token(sdi, tokens, 0) != SR_OK)
544 if (devc->token == TOKEN_WEIGHT_FREQ_A)
545 return SR_MQFLAG_SPL_FREQ_WEIGHT_A;
547 return SR_MQFLAG_SPL_FREQ_WEIGHT_C;
552 SR_PRIV int cem_dt_885x_weight_freq_set(const struct sr_dev_inst *sdi, int freqw)
554 struct dev_context *devc;
555 int cur_setting, ret;
560 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
561 if (cur_setting == freqw)
562 /* Already set to this frequency weighting. */
565 /* The toggle below needs the desired state in first position. */
566 if (freqw == SR_MQFLAG_SPL_FREQ_WEIGHT_A) {
567 tokens[0] = TOKEN_WEIGHT_FREQ_A;
568 tokens[1] = TOKEN_WEIGHT_FREQ_C;
570 tokens[0] = TOKEN_WEIGHT_FREQ_C;
571 tokens[1] = TOKEN_WEIGHT_FREQ_A;
575 if (cur_setting == 0) {
576 /* Didn't pick up device state yet. */
577 if (wait_for_token(sdi, tokens, 0) != SR_OK)
579 if (devc->token == tokens[0])
584 /* 10ms timeout seems to work best for this. */
585 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_FREQ, tokens, 10);
590 SR_PRIV int cem_dt_885x_weight_time_get(const struct sr_dev_inst *sdi)
592 struct dev_context *devc;
598 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
599 if (cur_setting == 0) {
600 /* Didn't pick up device state yet. */
601 tokens[0] = TOKEN_WEIGHT_TIME_FAST;
602 tokens[1] = TOKEN_WEIGHT_TIME_SLOW;
604 if (wait_for_token(sdi, tokens, 0) != SR_OK)
606 if (devc->token == TOKEN_WEIGHT_TIME_FAST)
607 return SR_MQFLAG_SPL_TIME_WEIGHT_F;
609 return SR_MQFLAG_SPL_TIME_WEIGHT_S;
614 SR_PRIV int cem_dt_885x_weight_time_set(const struct sr_dev_inst *sdi, int timew)
616 struct dev_context *devc;
617 int cur_setting, ret;
622 cur_setting = devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
623 if (cur_setting == timew)
624 /* Already set to this time weighting. */
627 /* The toggle below needs the desired state in first position. */
628 if (timew == SR_MQFLAG_SPL_TIME_WEIGHT_F) {
629 tokens[0] = TOKEN_WEIGHT_TIME_FAST;
630 tokens[1] = TOKEN_WEIGHT_TIME_SLOW;
632 tokens[0] = TOKEN_WEIGHT_TIME_SLOW;
633 tokens[1] = TOKEN_WEIGHT_TIME_FAST;
637 if (cur_setting == 0) {
638 /* Didn't pick up device state yet. */
639 if (wait_for_token(sdi, tokens, 0) != SR_OK)
641 if (devc->token == tokens[0])
646 /* 51ms timeout seems to work best for this. */
647 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_TIME, tokens, 51);
652 SR_PRIV int cem_dt_885x_holdmode_get(const struct sr_dev_inst *sdi,
655 struct dev_context *devc;
660 if (devc->cur_mqflags == 0) {
661 tokens[0] = TOKEN_HOLD_MAX;
662 tokens[1] = TOKEN_HOLD_MIN;
663 tokens[2] = TOKEN_HOLD_NONE;
665 if (wait_for_token(sdi, tokens, 0) != SR_OK)
667 if (devc->token == TOKEN_HOLD_MAX)
668 devc->cur_mqflags = SR_MQFLAG_MAX;
669 else if (devc->token == TOKEN_HOLD_MIN)
670 devc->cur_mqflags = SR_MQFLAG_MIN;
672 *holdmode = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
677 SR_PRIV int cem_dt_885x_holdmode_set(const struct sr_dev_inst *sdi, int holdmode)
679 struct dev_context *devc;
680 int cur_setting, ret;
685 /* The toggle below needs the desired state in first position. */
686 if (holdmode == SR_MQFLAG_MAX) {
687 tokens[0] = TOKEN_HOLD_MAX;
688 tokens[1] = TOKEN_HOLD_MIN;
689 tokens[2] = TOKEN_HOLD_NONE;
690 } else if (holdmode == SR_MQFLAG_MIN) {
691 tokens[0] = TOKEN_HOLD_MIN;
692 tokens[1] = TOKEN_HOLD_MAX;
693 tokens[2] = TOKEN_HOLD_NONE;
695 tokens[0] = TOKEN_HOLD_NONE;
696 tokens[1] = TOKEN_HOLD_MAX;
697 tokens[2] = TOKEN_HOLD_MIN;
701 if (devc->cur_mqflags == 0) {
702 /* Didn't pick up device state yet. */
703 if (wait_for_token(sdi, tokens, 0) != SR_OK)
705 if (devc->token == tokens[0])
709 cur_setting = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
710 if (cur_setting == holdmode)
711 /* Already set correctly. */
715 /* 51ms timeout seems to work best for this. */
716 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_HOLD_MAX_MIN, tokens, 51);
721 SR_PRIV int cem_dt_885x_meas_range_get(const struct sr_dev_inst *sdi,
722 uint64_t *low, uint64_t *high)
724 struct dev_context *devc;
728 if (devc->cur_meas_range == 0) {
729 tokens[0] = TOKEN_MEAS_RANGE_30_130;
730 tokens[1] = TOKEN_MEAS_RANGE_30_80;
731 tokens[2] = TOKEN_MEAS_RANGE_50_100;
732 tokens[3] = TOKEN_MEAS_RANGE_80_130;
734 if (wait_for_token(sdi, tokens, 0) != SR_OK)
736 devc->cur_meas_range = devc->token;
739 switch (devc->cur_meas_range) {
740 case TOKEN_MEAS_RANGE_30_130:
744 case TOKEN_MEAS_RANGE_30_80:
748 case TOKEN_MEAS_RANGE_50_100:
752 case TOKEN_MEAS_RANGE_80_130:
763 SR_PRIV int cem_dt_885x_meas_range_set(const struct sr_dev_inst *sdi,
764 uint64_t low, uint64_t high)
766 struct dev_context *devc;
768 int8_t token, tokens[6];
771 if (low == 30 && high == 130)
772 token = TOKEN_MEAS_RANGE_30_130;
773 else if (low == 30 && high == 80)
774 token = TOKEN_MEAS_RANGE_30_80;
775 else if (low == 50 && high == 100)
776 token = TOKEN_MEAS_RANGE_50_100;
777 else if (low == 80 && high == 130)
778 token = TOKEN_MEAS_RANGE_80_130;
782 sr_dbg("want 0x%.2x", token);
783 /* The toggle below needs the desired state in first position. */
785 tokens[1] = TOKEN_MEAS_RANGE_30_130;
786 tokens[2] = TOKEN_MEAS_RANGE_30_80;
787 tokens[3] = TOKEN_MEAS_RANGE_50_100;
788 tokens[4] = TOKEN_MEAS_RANGE_80_130;
791 if (devc->cur_meas_range == 0) {
792 /* 110ms should be enough for two of these announcements */
793 if (wait_for_token(sdi, tokens, 110) != SR_OK)
795 devc->cur_meas_range = devc->token;
798 if (devc->cur_meas_range == token)
799 /* Already set to this range. */
802 /* For measurement range, it works best to ignore announcements of the
803 * current setting and keep resending the toggle quickly. */
805 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_MEAS_RANGE, tokens, 11);
810 SR_PRIV int cem_dt_885x_power_off(const struct sr_dev_inst *sdi)
812 struct sr_serial_dev_inst *serial;
817 cmd = CMD_TOGGLE_POWER_OFF;
819 serial_flush(serial);
820 if (serial_write_nonblocking(serial, (const void *)&cmd, 1) != 1)
822 /* It never takes more than 23ms for the next token to arrive. */
824 if (serial_read_nonblocking(serial, &c, 1) != 1)
825 /* Device is no longer responding. Good! */
829 /* In case the user manually turns on the device again, reset
830 * the port back to blocking. */
831 serial_close(serial);
832 serial_open(serial, SERIAL_RDWR);