2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net>
5 * Based on the Hameg HMO driver by poljar (Damir Jelić) <poljarinho@gmail.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * <em>Yokogawa DL/DLM series</em> oscilloscope driver
28 static const uint32_t dlm_devopts[] = {
29 SR_CONF_LOGIC_ANALYZER,
31 SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
32 SR_CONF_SAMPLERATE | SR_CONF_GET,
33 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
34 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
35 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
36 SR_CONF_NUM_TIMEBASE | SR_CONF_GET,
37 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
40 static const uint32_t dlm_analog_devopts[] = {
41 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_NUM_VDIV | SR_CONF_GET,
46 static const char *dlm_coupling_options[] = {
54 /* Note: Values must correlate to the trigger_slopes values */
55 static const char *dlm_trigger_slopes[] = {
61 static const char *dlm_2ch_trigger_sources[] = {
69 /* TODO: Is BITx handled correctly or is Dx required? */
70 static const char *dlm_4ch_trigger_sources[] = {
88 static const uint64_t dlm_timebases[][2] = {
131 static const uint64_t dlm_vdivs[][2] = {
153 static const char *scope_analog_channel_names[] = {
160 static const char *scope_digital_channel_names[] = {
171 static struct scope_config scope_models[] = {
173 .model_id = {"710105", "710115", "710125", NULL},
174 .model_name = {"DLM2022", "DLM2032", "DLM2052", NULL},
175 .analog_channels = 2,
176 .digital_channels = 0,
179 .analog_names = &scope_analog_channel_names,
180 .digital_names = &scope_digital_channel_names,
182 .devopts = &dlm_devopts,
183 .num_devopts = ARRAY_SIZE(dlm_devopts),
185 .analog_devopts = &dlm_analog_devopts,
186 .num_analog_devopts = ARRAY_SIZE(dlm_analog_devopts),
188 .coupling_options = &dlm_coupling_options,
189 .trigger_sources = &dlm_2ch_trigger_sources,
190 .trigger_slopes = &dlm_trigger_slopes,
192 .timebases = &dlm_timebases,
193 .num_timebases = ARRAY_SIZE(dlm_timebases),
196 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
202 .model_id = {"710110", "710120", "710130", NULL},
203 .model_name = {"DLM2024", "DLM2034", "DLM2054", NULL},
204 .analog_channels = 4,
205 .digital_channels = 8,
208 .analog_names = &scope_analog_channel_names,
209 .digital_names = &scope_digital_channel_names,
211 .devopts = &dlm_devopts,
212 .num_devopts = ARRAY_SIZE(dlm_devopts),
214 .analog_devopts = &dlm_analog_devopts,
215 .num_analog_devopts = ARRAY_SIZE(dlm_analog_devopts),
217 .coupling_options = &dlm_coupling_options,
218 .trigger_sources = &dlm_4ch_trigger_sources,
219 .trigger_slopes = &dlm_trigger_slopes,
221 .timebases = &dlm_timebases,
222 .num_timebases = ARRAY_SIZE(dlm_timebases),
225 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
233 * Prints out the state of the device as we currently know it.
235 * @param config This is the scope configuration.
236 * @param state The current scope state to print.
238 static void scope_state_dump(struct scope_config *config,
239 struct scope_state *state)
244 for (i = 0; i < config->analog_channels; ++i) {
245 tmp = sr_voltage_string((*config->vdivs)[state->analog_states[i].vdiv][0],
246 (*config->vdivs)[state->analog_states[i].vdiv][1]);
247 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
248 i + 1, state->analog_states[i].state ? "On" : "Off",
249 (*config->coupling_options)[state->analog_states[i].coupling],
250 tmp, state->analog_states[i].vertical_offset);
253 for (i = 0; i < config->digital_channels; ++i) {
254 sr_info("State of digital channel %d -> %s", i,
255 state->digital_states[i] ? "On" : "Off");
258 for (i = 0; i < config->pods; ++i) {
259 sr_info("State of digital POD %d -> %s", i,
260 state->pod_states[i] ? "On" : "Off");
263 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
264 (*config->timebases)[state->timebase][1]);
265 sr_info("Current timebase: %s", tmp);
268 tmp = sr_samplerate_string(state->sample_rate);
269 sr_info("Current samplerate: %s", tmp);
272 sr_info("Current samples per acquisition (i.e. frame): %d",
273 state->samples_per_frame);
275 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
276 (*config->trigger_sources)[state->trigger_source],
277 (*config->trigger_slopes)[state->trigger_slope],
278 state->horiz_triggerpos);
282 * Searches through an array of strings and returns the index to the
283 * array where a given string is located.
285 * @param value The string to search for.
286 * @param array The array of strings.
287 * @param result The index at which value is located in array. -1 on error.
289 * @return SR_ERR when value couldn't be found, SR_OK otherwise.
291 static int array_option_get(char *value, const char *(*array)[],
298 for (i = 0; (*array)[i]; ++i)
299 if (!g_strcmp0(value, (*array)[i])) {
311 * This function takes a value of the form "2.000E-03", converts it to a
312 * significand / factor pair and returns the index of an array where
313 * a matching pair was found.
315 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
316 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
317 * Therefore it's easier to break the number up into two strings and handle
320 * @param value The string to be parsed.
321 * @param array The array of s/f pairs.
322 * @param array_len The number of pairs in the array.
323 * @param result The index at which a matching pair was found.
325 * @return SR_ERR on any parsing error, SR_OK otherwise.
327 static int array_float_get(gchar *value, const uint64_t array[][2],
328 int array_len, int *result)
334 gchar ss[10], es[10];
336 memset(ss, 0, sizeof(ss));
337 memset(es, 0, sizeof(es));
339 strncpy(ss, value, 5);
340 strncpy(es, &(value[6]), 3);
342 if (sr_atof_ascii(ss, &s) != SR_OK)
344 if (sr_atoi(es, &i) != SR_OK)
347 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
350 /* Adjust the significand/factor pair to make sure
351 * that f is a multiple of 1000.
353 while ((int)fmod(log10(f), 3) > 0) { s *= 10; f *= 10; }
355 /* Truncate s to circumvent rounding errors. */
356 s_int = (unsigned int)s;
358 for (i = 0; i < array_len; i++) {
359 if ( (s_int == array[i][0]) && (f == array[i][1]) ) {
369 * Obtains information about all analog channels from the oscilloscope.
370 * The internal state information is updated accordingly.
372 * @param scpi An open SCPI connection.
373 * @param config The device's device configuration.
374 * @param state The device's state information.
376 * @return SR_ERR on error, SR_OK otherwise.
378 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
379 struct scope_config *config,
380 struct scope_state *state)
385 for (i = 0; i < config->analog_channels; ++i) {
387 if (dlm_analog_chan_state_get(scpi, i + 1,
388 &state->analog_states[i].state) != SR_OK)
391 if (dlm_analog_chan_vdiv_get(scpi, i + 1, &response) != SR_OK)
394 if (array_float_get(response, *config->vdivs, config->num_vdivs,
401 state->analog_states[i].vdiv = j;
403 if (dlm_analog_chan_voffs_get(scpi, i + 1,
404 &state->analog_states[i].vertical_offset) != SR_OK)
407 if (dlm_analog_chan_wrange_get(scpi, i + 1,
408 &state->analog_states[i].waveform_range) != SR_OK)
411 if (dlm_analog_chan_woffs_get(scpi, i + 1,
412 &state->analog_states[i].waveform_offset) != SR_OK)
415 if (dlm_analog_chan_coupl_get(scpi, i + 1, &response) != SR_OK) {
420 if (array_option_get(response, config->coupling_options,
421 &state->analog_states[i].coupling) != SR_OK) {
432 * Obtains information about all digital channels from the oscilloscope.
433 * The internal state information is updated accordingly.
435 * @param scpi An open SCPI connection.
436 * @param config The device's device configuration.
437 * @param state The device's state information.
439 * @return SR_ERR on error, SR_OK otherwise.
441 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
442 struct scope_config *config,
443 struct scope_state *state)
447 if (!config->digital_channels)
449 sr_warn("Tried obtaining digital channel states on a " \
450 "model without digital inputs.");
454 for (i = 0; i < config->digital_channels; ++i) {
455 if (dlm_digital_chan_state_get(scpi, i + 1,
456 &state->digital_states[i]) != SR_OK) {
463 sr_warn("Tried obtaining pod states on a model without pods.");
467 for (i = 0; i < config->pods; ++i) {
468 if (dlm_digital_pod_state_get(scpi, i + 'A',
469 &state->pod_states[i]) != SR_OK)
477 * Obtains information about the sample rate from the oscilloscope.
478 * The internal state information is updated accordingly.
480 * @param sdi The device instance.
482 * @return SR_ERR on error, SR_OK otherwise.
484 SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi)
486 struct dev_context *devc;
487 struct scope_state *state;
491 state = devc->model_state;
493 /* No need to find an active channel to query the sample rate:
494 * querying any channel will do, so we use channel 1 all the time.
496 if (dlm_analog_chan_srate_get(sdi->conn, 1, &tmp_float) != SR_OK)
499 state->sample_rate = tmp_float;
505 * Obtains information about the current device state from the oscilloscope,
506 * including all analog and digital channel configurations.
507 * The internal state information is updated accordingly.
509 * @param sdi The device instance.
511 * @return SR_ERR on error, SR_OK otherwise.
513 SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
515 struct dev_context *devc;
516 struct scope_state *state;
517 struct scope_config *config;
523 config = devc->model_config;
524 state = devc->model_state;
526 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
529 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
532 if (dlm_timebase_get(sdi->conn, &response) != SR_OK)
535 if (array_float_get(response, *config->timebases,
536 config->num_timebases, &i) != SR_OK) {
544 if (dlm_horiz_trigger_pos_get(sdi->conn, &tmp_float) != SR_OK)
547 /* TODO: Check if the calculation makes sense for the DLM. */
548 state->horiz_triggerpos = tmp_float /
549 (((double)(*config->timebases)[state->timebase][0] /
550 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
551 state->horiz_triggerpos -= 0.5;
552 state->horiz_triggerpos *= -1;
554 if (dlm_trigger_source_get(sdi->conn, &response) != SR_OK) {
559 if (array_option_get(response, config->trigger_sources,
560 &state->trigger_source) != SR_OK) {
567 if (dlm_trigger_slope_get(sdi->conn, &i) != SR_OK)
570 state->trigger_slope = i;
572 if (dlm_acq_length_get(sdi->conn, &state->samples_per_frame) != SR_OK) {
573 sr_err("Failed to query acquisition length.");
577 dlm_sample_rate_query(sdi);
579 scope_state_dump(config, state);
585 * Creates a new device state structure.
587 * @param config The device configuration to use.
589 * @return The newly allocated scope_state struct.
591 static struct scope_state *dlm_scope_state_new(struct scope_config *config)
593 struct scope_state *state;
595 state = g_malloc0(sizeof(struct scope_state));
597 state->analog_states = g_malloc0(config->analog_channels *
598 sizeof(struct analog_channel_state));
600 state->digital_states = g_malloc0(config->digital_channels *
603 state->pod_states = g_malloc0(config->pods * sizeof(gboolean));
609 * Frees the memory that was allocated by a call to dlm_scope_state_new().
611 * @param state The device state structure whose memory is to be freed.
613 SR_PRIV void dlm_scope_state_destroy(struct scope_state *state)
615 g_free(state->analog_states);
616 g_free(state->digital_states);
617 g_free(state->pod_states);
621 SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index)
628 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
629 for (j = 0; scope_models[i].model_id[j]; j++) {
630 if (!strcmp(model_id, scope_models[i].model_id[j])) {
632 *model_name = (char *)scope_models[i].model_name[j];
636 if (*model_index != -1)
640 if (*model_index == -1) {
641 sr_err("Found unsupported DLM device with model identifier %s.",
650 * Attempts to initialize a DL/DLM device and prepares internal structures
651 * if a suitable device was found.
653 * @param sdi The device instance.
655 SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index)
659 struct sr_channel *ch;
660 struct dev_context *devc;
664 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
665 scope_models[model_index].analog_channels);
667 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
668 scope_models[model_index].digital_channels);
670 /* Add analog channels. */
671 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
672 if (!(ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE,
673 (*scope_models[model_index].analog_names)[i])))
674 return SR_ERR_MALLOC;
675 sdi->channels = g_slist_append(sdi->channels, ch);
677 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
679 devc->analog_groups[i]->name = g_strdup(
680 (char *)(*scope_models[model_index].analog_names)[i]);
681 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
683 sdi->channel_groups = g_slist_append(sdi->channel_groups,
684 devc->analog_groups[i]);
687 /* Add digital channel groups. */
688 for (i = 0; i < scope_models[model_index].pods; ++i) {
689 g_snprintf(tmp, sizeof(tmp), "POD%d", i);
691 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
692 if (!devc->digital_groups[i])
693 return SR_ERR_MALLOC;
695 devc->digital_groups[i]->name = g_strdup(tmp);
696 sdi->channel_groups = g_slist_append(sdi->channel_groups,
697 devc->digital_groups[i]);
700 /* Add digital channels. */
701 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
702 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
703 (*scope_models[model_index].digital_names)[i])))
704 return SR_ERR_MALLOC;
705 sdi->channels = g_slist_append(sdi->channels, ch);
707 devc->digital_groups[i / 8]->channels = g_slist_append(
708 devc->digital_groups[i / 8]->channels, ch);
710 devc->model_config = &scope_models[model_index];
711 devc->frame_limit = 0;
713 if (!(devc->model_state = dlm_scope_state_new(devc->model_config)))
714 return SR_ERR_MALLOC;
716 /* Disable non-standard response behavior. */
717 if (dlm_response_headers_set(sdi->conn, FALSE) != SR_OK)
723 SR_PRIV int dlm_channel_data_request(const struct sr_dev_inst *sdi)
725 struct dev_context *devc;
726 struct sr_channel *ch;
730 ch = devc->current_channel->data;
733 case SR_CHANNEL_ANALOG:
734 result = dlm_analog_data_get(sdi->conn, ch->index + 1);
736 case SR_CHANNEL_LOGIC:
737 result = dlm_digital_data_get(sdi->conn);
740 sr_err("Invalid channel type encountered (%d).",
746 devc->data_pending = TRUE;
748 devc->data_pending = FALSE;
754 * Reads and removes the block data header from a given data input.
755 * Format is #ndddd... with n being the number of decimal digits d.
756 * The string dddd... contains the decimal-encoded length of the data.
757 * Example: #9000000013 would yield a length of 13 bytes.
759 * @param data The input data.
760 * @param len The determined input data length.
762 static int dlm_block_data_header_process(GArray *data, int *len)
767 if (g_array_index(data, gchar, 0) != '#')
770 n = (uint8_t)(g_array_index(data, gchar, 1) - '0');
772 for (i = 0; i < n; i++)
773 s[i] = g_array_index(data, gchar, 2 + i);
776 if (sr_atoi(s, len) != SR_OK)
779 g_array_remove_range(data, 0, 2 + n);
785 * Turns raw sample data into voltages and sends them off to the session bus.
787 * @param data The raw sample data.
788 * @ch_state Pointer to the state of the channel whose data we're processing.
789 * @sdi The device instance.
791 * @return SR_ERR when data is trucated, SR_OK otherwise.
793 static int dlm_analog_samples_send(GArray *data,
794 struct analog_channel_state *ch_state,
795 struct sr_dev_inst *sdi)
798 float voltage, range, offset;
800 struct dev_context *devc;
801 struct scope_state *model_state;
802 struct sr_channel *ch;
803 struct sr_datafeed_analog analog;
804 struct sr_datafeed_packet packet;
807 model_state = devc->model_state;
808 samples = model_state->samples_per_frame;
809 ch = devc->current_channel->data;
811 if (data->len < samples * sizeof(uint8_t)) {
812 sr_err("Truncated waveform data packet received.");
816 range = ch_state->waveform_range;
817 offset = ch_state->waveform_offset;
819 /* Convert byte sample to voltage according to
820 * page 269 of the Communication Interface User's Manual.
822 float_data = g_array_new(FALSE, FALSE, sizeof(float));
823 for (i = 0; i < samples; i++) {
824 voltage = (float)g_array_index(data, int8_t, i);
825 voltage = (range * voltage /
826 DLM_DIVISION_FOR_BYTE_FORMAT) + offset;
827 g_array_append_val(float_data, voltage);
830 analog.channels = g_slist_append(NULL, ch);
831 analog.num_samples = float_data->len;
832 analog.data = (float*)float_data->data;
833 analog.mq = SR_MQ_VOLTAGE;
834 analog.unit = SR_UNIT_VOLT;
836 packet.type = SR_DF_ANALOG;
837 packet.payload = &analog;
838 sr_session_send(sdi, &packet);
839 g_slist_free(analog.channels);
841 g_array_free(float_data, TRUE);
842 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
848 * Sends logic sample data off to the session bus.
850 * @param data The raw sample data.
851 * @ch_state Pointer to the state of the channel whose data we're processing.
852 * @sdi The device instance.
854 * @return SR_ERR when data is trucated, SR_OK otherwise.
856 static int dlm_digital_samples_send(GArray *data,
857 struct sr_dev_inst *sdi)
859 struct dev_context *devc;
860 struct scope_state *model_state;
862 struct sr_datafeed_logic logic;
863 struct sr_datafeed_packet packet;
866 model_state = devc->model_state;
867 samples = model_state->samples_per_frame;
869 if (data->len < samples * sizeof(uint8_t)) {
870 sr_err("Truncated waveform data packet received.");
874 logic.length = samples;
876 logic.data = data->data;
877 packet.type = SR_DF_LOGIC;
878 packet.payload = &logic;
879 sr_session_send(sdi, &packet);
881 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
887 * Attempts to query sample data from the oscilloscope in order to send it
888 * to the session bus for further processing.
890 * @param fd The file descriptor used as the event source.
891 * @param revents The received events.
892 * @param cb_data Callback data, in this case our device instance.
894 * @return TRUE in case of success or a recoverable error,
895 * FALSE when a fatal error was encountered.
897 SR_PRIV int dlm_data_receive(int fd, int revents, void *cb_data)
899 struct sr_dev_inst *sdi;
900 struct scope_state *model_state;
901 struct dev_context *devc;
902 struct sr_channel *ch;
903 struct sr_datafeed_packet packet;
904 int chunk_len, num_bytes;
905 static GArray *data = NULL;
910 if (!(sdi = cb_data))
913 if (!(devc = sdi->priv))
916 if (!(model_state = (struct scope_state*)devc->model_state))
919 /* Are we waiting for a response from the device? */
920 if (!devc->data_pending)
923 /* Check if a new query response is coming our way. */
925 if (sr_scpi_read_begin(sdi->conn) == SR_OK)
926 /* The 16 here accounts for the header and EOL. */
927 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t),
928 16 + model_state->samples_per_frame);
933 /* Store incoming data. */
934 chunk_len = sr_scpi_read_data(sdi->conn, devc->receive_buffer,
935 RECEIVE_BUFFER_SIZE);
937 sr_err("Error while reading data: %d", chunk_len);
940 g_array_append_vals(data, devc->receive_buffer, chunk_len);
942 /* Read the entire query response before processing. */
943 if (!sr_scpi_read_complete(sdi->conn))
946 /* We finished reading and are no longer waiting for data. */
947 devc->data_pending = FALSE;
949 /* Signal the beginning of a new frame if this is the first channel. */
950 if (devc->current_channel == devc->enabled_channels) {
951 packet.type = SR_DF_FRAME_BEGIN;
952 sr_session_send(sdi, &packet);
955 if (dlm_block_data_header_process(data, &num_bytes) != SR_OK) {
956 sr_err("Encountered malformed block data header.");
960 if (num_bytes == 0) {
961 sr_warn("Zero-length waveform data packet received. " \
962 "Live mode not supported yet, stopping " \
963 "acquisition and retrying.");
964 /* Don't care about return value here. */
965 dlm_acquisition_stop(sdi->conn);
966 g_array_free(data, TRUE);
967 dlm_channel_data_request(sdi);
971 ch = devc->current_channel->data;
973 case SR_CHANNEL_ANALOG:
974 if (dlm_analog_samples_send(data,
975 &model_state->analog_states[ch->index],
979 case SR_CHANNEL_LOGIC:
980 if (dlm_digital_samples_send(data, sdi) != SR_OK)
984 sr_err("Invalid channel type encountered.");
988 g_array_free(data, TRUE);
991 /* Signal the end of this frame if this was the last enabled channel
992 * and set the next enabled channel. Then, request its data.
994 if (!devc->current_channel->next) {
995 packet.type = SR_DF_FRAME_END;
996 sr_session_send(sdi, &packet);
997 devc->current_channel = devc->enabled_channels;
999 /* As of now we only support importing the current acquisition
1000 * data so we're going to stop at this point.
1002 sdi->driver->dev_acquisition_stop(sdi, cb_data);
1005 devc->current_channel = devc->current_channel->next;
1007 if (dlm_channel_data_request(sdi) != SR_OK) {
1008 sr_err("Failed to request aquisition data.");
1016 g_array_free(data, TRUE);