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 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);
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.probes = sdi->probes;
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 expressable in sigrok. */
175 static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c,
178 struct dev_context *devc;
182 if (!(devc = sdi->priv))
186 /* Device is in hold mode */
187 devc->cur_mqflags |= SR_MQFLAG_HOLD;
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;
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. */
199 devc->token = TOKEN_MEAS_WAS_READOUT;
202 devc->hold_last_sent = cur_time;
208 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
209 devc->hold_last_sent = 0;
211 if (devc->state == ST_INIT) {
215 devc->state = ST_GET_TOKEN;
216 } else if (c == 0xbb) {
219 devc->state = ST_GET_LOG;
221 } else if (devc->state == ST_GET_TOKEN) {
224 len = find_token_payload_len(devc->token);
225 if (len == -1 || len > 0) {
227 devc->state = ST_GET_DATA;
231 devc->state = ST_INIT;
233 } else if (devc->state == ST_GET_DATA) {
234 len = find_token_payload_len(devc->token);
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. */
242 devc->state = ST_INIT;
244 devc->buf[devc->buf_len++] = c;
245 if (devc->buf_len > BUF_SIZE) {
246 /* Shouldn't happen, ignore. */
247 devc->state = ST_INIT;
251 devc->buf[devc->buf_len++] = c;
252 if (devc->buf_len == len) {
255 devc->state = ST_INIT;
256 } else if (devc->buf_len > BUF_SIZE) {
257 /* Shouldn't happen, ignore. */
258 devc->state = ST_INIT;
261 } else if (devc->state == ST_GET_LOG) {
266 SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
268 const struct sr_dev_inst *sdi;
269 struct sr_serial_dev_inst *serial;
274 if (!(sdi = cb_data))
278 if (revents == G_IO_IN) {
279 if (serial_read(serial, &c, 1) != 1)
281 process_byte(sdi, c, TRUE);
288 static int wait_for_token(const struct sr_dev_inst *sdi, char *tokens, int timeout)
290 struct dev_context *devc;
291 struct sr_serial_dev_inst *serial;
298 devc->state = ST_INIT;
299 start_time = g_get_monotonic_time() / 1000;
301 if (serial_read(serial, &c, 1) != 1)
302 /* Device might have gone away. */
304 process_byte(sdi, c, FALSE);
305 if (devc->state != ST_INIT)
306 /* Wait for a whole packet to get processed. */
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);
314 if (timeout && g_get_monotonic_time() / 1000 - start_time > timeout)
315 return SR_ERR_TIMEOUT;
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. */
323 SR_PRIV int cem_dt_885x_toggle(const struct sr_dev_inst *sdi, uint8_t cmd,
324 char *tokens, int timeout)
326 struct dev_context *devc;
327 struct sr_serial_dev_inst *serial;
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. */
336 if (serial_write(serial, (const void *)&cmd, 1) != 1)
338 if (wait_for_token(sdi, tokens, timeout) == SR_ERR)
340 if (devc->token == tokens[0])
348 SR_PRIV gboolean cem_dt_885x_recording_get(const struct sr_dev_inst *sdi,
351 struct dev_context *devc;
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;
360 if (wait_for_token(sdi, tokens, 510) != SR_OK)
363 *state = devc->token == TOKEN_RECORDING_ON;
368 SR_PRIV int cem_dt_885x_recording_set(const struct sr_dev_inst *sdi,
371 struct dev_context *devc;
377 /* The toggle below needs the desired state in first position. */
379 tokens[0] = TOKEN_RECORDING_ON;
380 tokens[1] = TOKEN_RECORDING_OFF;
382 tokens[0] = TOKEN_RECORDING_OFF;
383 tokens[1] = TOKEN_RECORDING_ON;
387 if (devc->recording == -1) {
388 /* Didn't pick up device state yet. */
389 if (wait_for_token(sdi, tokens, 0) != SR_OK)
391 if (devc->token == tokens[0])
394 } else if (devc->recording == state)
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);
405 SR_PRIV int cem_dt_885x_weight_freq_get(const struct sr_dev_inst *sdi)
407 struct dev_context *devc;
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;
419 if (wait_for_token(sdi, tokens, 0) != SR_OK)
421 if (devc->token == TOKEN_WEIGHT_FREQ_A)
422 return SR_MQFLAG_SPL_FREQ_WEIGHT_A;
424 return SR_MQFLAG_SPL_FREQ_WEIGHT_C;
430 SR_PRIV int cem_dt_885x_weight_freq_set(const struct sr_dev_inst *sdi, int freqw)
432 struct dev_context *devc;
433 int cur_setting, ret;
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. */
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;
448 tokens[0] = TOKEN_WEIGHT_FREQ_C;
449 tokens[1] = TOKEN_WEIGHT_FREQ_A;
453 if (cur_setting == 0) {
454 /* Didn't pick up device state yet. */
455 if (wait_for_token(sdi, tokens, 0) != SR_OK)
457 if (devc->token == tokens[0])
462 /* 10ms timeout seems to work best for this. */
463 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_FREQ, tokens, 10);
468 SR_PRIV int cem_dt_885x_weight_time_get(const struct sr_dev_inst *sdi)
470 struct dev_context *devc;
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;
482 if (wait_for_token(sdi, tokens, 0) != SR_OK)
484 if (devc->token == TOKEN_WEIGHT_TIME_FAST)
485 return SR_MQFLAG_SPL_TIME_WEIGHT_F;
487 return SR_MQFLAG_SPL_TIME_WEIGHT_S;
493 SR_PRIV int cem_dt_885x_weight_time_set(const struct sr_dev_inst *sdi, int timew)
495 struct dev_context *devc;
496 int cur_setting, ret;
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. */
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;
511 tokens[0] = TOKEN_WEIGHT_TIME_SLOW;
512 tokens[1] = TOKEN_WEIGHT_TIME_FAST;
516 if (cur_setting == 0) {
517 /* Didn't pick up device state yet. */
518 if (wait_for_token(sdi, tokens, 0) != SR_OK)
520 if (devc->token == tokens[0])
525 /* 51ms timeout seems to work best for this. */
526 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_WEIGHT_TIME, tokens, 51);
531 SR_PRIV int cem_dt_885x_holdmode_get(const struct sr_dev_inst *sdi,
534 struct dev_context *devc;
539 if (devc->cur_mqflags == 0) {
540 tokens[0] = TOKEN_HOLD_MAX;
541 tokens[1] = TOKEN_HOLD_MIN;
542 tokens[2] = TOKEN_HOLD_NONE;
544 if (wait_for_token(sdi, tokens, 0) != SR_OK)
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;
551 *holdmode = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
556 SR_PRIV int cem_dt_885x_holdmode_set(const struct sr_dev_inst *sdi, int holdmode)
558 struct dev_context *devc;
559 int cur_setting, ret;
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;
574 tokens[0] = TOKEN_HOLD_NONE;
575 tokens[1] = TOKEN_HOLD_MAX;
576 tokens[2] = TOKEN_HOLD_MIN;
580 if (devc->cur_mqflags == 0) {
581 /* Didn't pick up device state yet. */
582 if (wait_for_token(sdi, tokens, 0) != SR_OK)
584 if (devc->token == tokens[0])
588 cur_setting = devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN);
589 if (cur_setting == holdmode)
590 /* Already set correctly. */
594 /* 51ms timeout seems to work best for this. */
595 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_HOLD_MAX_MIN, tokens, 51);
600 SR_PRIV int cem_dt_885x_meas_range_get(const struct sr_dev_inst *sdi,
601 uint64_t *low, uint64_t *high)
603 struct dev_context *devc;
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;
613 if (wait_for_token(sdi, tokens, 0) != SR_OK)
615 devc->cur_meas_range = devc->token;
618 switch (devc->cur_meas_range) {
619 case TOKEN_MEAS_RANGE_30_130:
623 case TOKEN_MEAS_RANGE_30_80:
627 case TOKEN_MEAS_RANGE_50_100:
631 case TOKEN_MEAS_RANGE_80_130:
642 SR_PRIV int cem_dt_885x_meas_range_set(const struct sr_dev_inst *sdi,
643 uint64_t low, uint64_t high)
645 struct dev_context *devc;
647 char token, tokens[6];
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;
661 sr_dbg("want 0x%.2x", token);
662 /* The toggle below needs the desired state in first position. */
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;
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)
674 devc->cur_meas_range = devc->token;
677 if (devc->cur_meas_range == token)
678 /* Already set to this range. */
681 /* For measurement range, it works best to ignore announcements of the
682 * current setting and keep resending the toggle quickly. */
684 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_MEAS_RANGE, tokens, 11);
689 SR_PRIV int cem_dt_885x_power_off(const struct sr_dev_inst *sdi)
691 struct sr_serial_dev_inst *serial;
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)
702 cmd = CMD_TOGGLE_POWER_OFF;
704 serial_flush(serial);
705 if (serial_write(serial, (const void *)&cmd, 1) != 1)
707 /* It never takes more than 23ms for the next token to arrive. */
709 if (serial_read(serial, &c, 1) != 1)
710 /* Device is no longer responding. Good! */
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);