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_TRIGGER_SLOPE,
32 SR_CONF_TRIGGER_SOURCE,
35 SR_CONF_HORIZ_TRIGGERPOS,
38 static const uint32_t dlm_analog_devopts[] = {
44 static const char *dlm_coupling_options[] = {
52 /* Note: Values must correlate to the trigger_slopes values */
53 static const char *dlm_trigger_slopes[] = {
59 static const char *dlm_2ch_trigger_sources[] = {
67 /* TODO: Is BITx handled correctly or is Dx required? */
68 static const char *dlm_4ch_trigger_sources[] = {
86 static const uint64_t dlm_timebases[][2] = {
129 static const uint64_t dlm_vdivs[][2] = {
151 static const char *scope_analog_channel_names[] = {
158 static const char *scope_digital_channel_names[] = {
169 static struct scope_config scope_models[] = {
171 .model_id = {"710105", "710115", "710125", NULL},
172 .model_name = {"DLM2022", "DLM2032", "DLM2052", NULL},
173 .analog_channels = 2,
174 .digital_channels = 0,
177 .analog_names = &scope_analog_channel_names,
178 .digital_names = &scope_digital_channel_names,
180 .devopts = &dlm_devopts,
181 .num_devopts = ARRAY_SIZE(dlm_devopts),
183 .analog_devopts = &dlm_analog_devopts,
184 .num_analog_devopts = ARRAY_SIZE(dlm_analog_devopts),
186 .coupling_options = &dlm_coupling_options,
187 .trigger_sources = &dlm_2ch_trigger_sources,
188 .trigger_slopes = &dlm_trigger_slopes,
190 .timebases = &dlm_timebases,
191 .num_timebases = ARRAY_SIZE(dlm_timebases),
194 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
200 .model_id = {"710110", "710120", "710130", NULL},
201 .model_name = {"DLM2024", "DLM2034", "DLM2054", NULL},
202 .analog_channels = 4,
203 .digital_channels = 8,
206 .analog_names = &scope_analog_channel_names,
207 .digital_names = &scope_digital_channel_names,
209 .devopts = &dlm_devopts,
210 .num_devopts = ARRAY_SIZE(dlm_devopts),
212 .analog_devopts = &dlm_analog_devopts,
213 .num_analog_devopts = ARRAY_SIZE(dlm_analog_devopts),
215 .coupling_options = &dlm_coupling_options,
216 .trigger_sources = &dlm_4ch_trigger_sources,
217 .trigger_slopes = &dlm_trigger_slopes,
219 .timebases = &dlm_timebases,
220 .num_timebases = ARRAY_SIZE(dlm_timebases),
223 .num_vdivs = ARRAY_SIZE(dlm_vdivs),
231 * Prints out the state of the device as we currently know it.
233 * @param config This is the scope configuration.
234 * @param state The current scope state to print.
236 static void scope_state_dump(struct scope_config *config,
237 struct scope_state *state)
242 for (i = 0; i < config->analog_channels; ++i) {
243 tmp = sr_voltage_string((*config->vdivs)[state->analog_states[i].vdiv][0],
244 (*config->vdivs)[state->analog_states[i].vdiv][1]);
245 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
246 i + 1, state->analog_states[i].state ? "On" : "Off",
247 (*config->coupling_options)[state->analog_states[i].coupling],
248 tmp, state->analog_states[i].vertical_offset);
251 for (i = 0; i < config->digital_channels; ++i) {
252 sr_info("State of digital channel %d -> %s", i,
253 state->digital_states[i] ? "On" : "Off");
256 for (i = 0; i < config->pods; ++i) {
257 sr_info("State of digital POD %d -> %s", i,
258 state->pod_states[i] ? "On" : "Off");
261 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
262 (*config->timebases)[state->timebase][1]);
263 sr_info("Current timebase: %s", tmp);
266 tmp = sr_samplerate_string(state->sample_rate);
267 sr_info("Current samplerate: %s", tmp);
270 sr_info("Current samples per acquisition (i.e. frame): %d",
271 state->samples_per_frame);
273 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
274 (*config->trigger_sources)[state->trigger_source],
275 (*config->trigger_slopes)[state->trigger_slope],
276 state->horiz_triggerpos);
280 * Searches through an array of strings and returns the index to the
281 * array where a given string is located.
283 * @param value The string to search for.
284 * @param array The array of strings.
285 * @param result The index at which value is located in array. -1 on error.
287 * @return SR_ERR when value couldn't be found, SR_OK otherwise.
289 static int array_option_get(char *value, const char *(*array)[],
296 for (i = 0; (*array)[i]; ++i)
297 if (!g_strcmp0(value, (*array)[i])) {
309 * This function takes a value of the form "2.000E-03", converts it to a
310 * significand / factor pair and returns the index of an array where
311 * a matching pair was found.
313 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
314 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
315 * Therefore it's easier to break the number up into two strings and handle
318 * @param value The string to be parsed.
319 * @param array The array of s/f pairs.
320 * @param array_len The number of pairs in the array.
321 * @param result The index at which a matching pair was found.
323 * @return SR_ERR on any parsing error, SR_OK otherwise.
325 static int array_float_get(gchar *value, const uint64_t array[][2],
326 int array_len, int *result)
332 gchar ss[10], es[10];
334 memset(ss, 0, sizeof(ss));
335 memset(es, 0, sizeof(es));
337 strncpy(ss, value, 5);
338 strncpy(es, &(value[6]), 3);
340 if (sr_atof_ascii(ss, &s) != SR_OK)
342 if (sr_atoi(es, &i) != SR_OK)
345 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
348 /* Adjust the significand/factor pair to make sure
349 * that f is a multiple of 1000.
351 while ((int)fmod(log10(f), 3) > 0) { s *= 10; f *= 10; }
353 /* Truncate s to circumvent rounding errors. */
354 s_int = (unsigned int)s;
356 for (i = 0; i < array_len; i++) {
357 if ( (s_int == array[i][0]) && (f == array[i][1]) ) {
367 * Obtains information about all analog channels from the oscilloscope.
368 * The internal state information is updated accordingly.
370 * @param scpi An open SCPI connection.
371 * @param config The device's device configuration.
372 * @param state The device's state information.
374 * @return SR_ERR on error, SR_OK otherwise.
376 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
377 struct scope_config *config,
378 struct scope_state *state)
383 for (i = 0; i < config->analog_channels; ++i) {
385 if (dlm_analog_chan_state_get(scpi, i + 1,
386 &state->analog_states[i].state) != SR_OK)
389 if (dlm_analog_chan_vdiv_get(scpi, i + 1, &response) != SR_OK)
392 if (array_float_get(response, *config->vdivs, config->num_vdivs,
399 state->analog_states[i].vdiv = j;
401 if (dlm_analog_chan_voffs_get(scpi, i + 1,
402 &state->analog_states[i].vertical_offset) != SR_OK)
405 if (dlm_analog_chan_wrange_get(scpi, i + 1,
406 &state->analog_states[i].waveform_range) != SR_OK)
409 if (dlm_analog_chan_woffs_get(scpi, i + 1,
410 &state->analog_states[i].waveform_offset) != SR_OK)
413 if (dlm_analog_chan_coupl_get(scpi, i + 1, &response) != SR_OK) {
418 if (array_option_get(response, config->coupling_options,
419 &state->analog_states[i].coupling) != SR_OK) {
430 * Obtains information about all digital channels from the oscilloscope.
431 * The internal state information is updated accordingly.
433 * @param scpi An open SCPI connection.
434 * @param config The device's device configuration.
435 * @param state The device's state information.
437 * @return SR_ERR on error, SR_OK otherwise.
439 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
440 struct scope_config *config,
441 struct scope_state *state)
445 if (!config->digital_channels)
447 sr_warn("Tried obtaining digital channel states on a " \
448 "model without digital inputs.");
452 for (i = 0; i < config->digital_channels; ++i) {
453 if (dlm_digital_chan_state_get(scpi, i + 1,
454 &state->digital_states[i]) != SR_OK) {
461 sr_warn("Tried obtaining pod states on a model without pods.");
465 for (i = 0; i < config->pods; ++i) {
466 if (dlm_digital_pod_state_get(scpi, i + 'A',
467 &state->pod_states[i]) != SR_OK)
475 * Obtains information about the sample rate from the oscilloscope.
476 * The internal state information is updated accordingly.
478 * @param sdi The device instance.
480 * @return SR_ERR on error, SR_OK otherwise.
482 SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi)
484 struct dev_context *devc;
485 struct scope_state *state;
489 state = devc->model_state;
491 /* No need to find an active channel to query the sample rate:
492 * querying any channel will do, so we use channel 1 all the time.
494 if (dlm_analog_chan_srate_get(sdi->conn, 1, &tmp_float) != SR_OK)
497 state->sample_rate = tmp_float;
503 * Obtains information about the current device state from the oscilloscope,
504 * including all analog and digital channel configurations.
505 * The internal state information is updated accordingly.
507 * @param sdi The device instance.
509 * @return SR_ERR on error, SR_OK otherwise.
511 SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
513 struct dev_context *devc;
514 struct scope_state *state;
515 struct scope_config *config;
521 config = devc->model_config;
522 state = devc->model_state;
524 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
527 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
530 if (dlm_timebase_get(sdi->conn, &response) != SR_OK)
533 if (array_float_get(response, *config->timebases,
534 config->num_timebases, &i) != SR_OK) {
542 if (dlm_horiz_trigger_pos_get(sdi->conn, &tmp_float) != SR_OK)
545 /* TODO: Check if the calculation makes sense for the DLM. */
546 state->horiz_triggerpos = tmp_float /
547 (((double)(*config->timebases)[state->timebase][0] /
548 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
549 state->horiz_triggerpos -= 0.5;
550 state->horiz_triggerpos *= -1;
552 if (dlm_trigger_source_get(sdi->conn, &response) != SR_OK) {
557 if (array_option_get(response, config->trigger_sources,
558 &state->trigger_source) != SR_OK) {
565 if (dlm_trigger_slope_get(sdi->conn, &i) != SR_OK)
568 state->trigger_slope = i;
570 if (dlm_acq_length_get(sdi->conn, &state->samples_per_frame) != SR_OK) {
571 sr_err("Failed to query acquisition length.");
575 dlm_sample_rate_query(sdi);
577 scope_state_dump(config, state);
583 * Creates a new device state structure.
585 * @param config The device configuration to use.
587 * @return The newly allocated scope_state struct.
589 static struct scope_state *dlm_scope_state_new(struct scope_config *config)
591 struct scope_state *state;
593 state = g_malloc0(sizeof(struct scope_state));
595 state->analog_states = g_malloc0(config->analog_channels *
596 sizeof(struct analog_channel_state));
598 state->digital_states = g_malloc0(config->digital_channels *
601 state->pod_states = g_malloc0(config->pods * sizeof(gboolean));
607 * Frees the memory that was allocated by a call to dlm_scope_state_new().
609 * @param state The device state structure whose memory is to be freed.
611 SR_PRIV void dlm_scope_state_destroy(struct scope_state *state)
613 g_free(state->analog_states);
614 g_free(state->digital_states);
615 g_free(state->pod_states);
619 SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index)
626 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
627 for (j = 0; scope_models[i].model_id[j]; j++) {
628 if (!strcmp(model_id, scope_models[i].model_id[j])) {
630 *model_name = (char *)scope_models[i].model_name[j];
634 if (*model_index != -1)
638 if (*model_index == -1) {
639 sr_err("Found unsupported DLM device with model identifier %s.",
648 * Attempts to initialize a DL/DLM device and prepares internal structures
649 * if a suitable device was found.
651 * @param sdi The device instance.
653 SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index)
657 struct sr_channel *ch;
658 struct dev_context *devc;
662 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
663 scope_models[model_index].analog_channels);
665 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
666 scope_models[model_index].digital_channels);
668 /* Add analog channels. */
669 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
670 if (!(ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE,
671 (*scope_models[model_index].analog_names)[i])))
672 return SR_ERR_MALLOC;
673 sdi->channels = g_slist_append(sdi->channels, ch);
675 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
677 devc->analog_groups[i]->name = g_strdup(
678 (char *)(*scope_models[model_index].analog_names)[i]);
679 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
681 sdi->channel_groups = g_slist_append(sdi->channel_groups,
682 devc->analog_groups[i]);
685 /* Add digital channel groups. */
686 for (i = 0; i < scope_models[model_index].pods; ++i) {
687 g_snprintf(tmp, sizeof(tmp), "POD%d", i);
689 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
690 if (!devc->digital_groups[i])
691 return SR_ERR_MALLOC;
693 devc->digital_groups[i]->name = g_strdup(tmp);
694 sdi->channel_groups = g_slist_append(sdi->channel_groups,
695 devc->digital_groups[i]);
698 /* Add digital channels. */
699 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
700 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
701 (*scope_models[model_index].digital_names)[i])))
702 return SR_ERR_MALLOC;
703 sdi->channels = g_slist_append(sdi->channels, ch);
705 devc->digital_groups[i / 8]->channels = g_slist_append(
706 devc->digital_groups[i / 8]->channels, ch);
708 devc->model_config = &scope_models[model_index];
709 devc->frame_limit = 0;
711 if (!(devc->model_state = dlm_scope_state_new(devc->model_config)))
712 return SR_ERR_MALLOC;
714 /* Disable non-standard response behavior. */
715 if (dlm_response_headers_set(sdi->conn, FALSE) != SR_OK)
721 SR_PRIV int dlm_channel_data_request(const struct sr_dev_inst *sdi)
723 struct dev_context *devc;
724 struct sr_channel *ch;
728 ch = devc->current_channel->data;
731 case SR_CHANNEL_ANALOG:
732 result = dlm_analog_data_get(sdi->conn, ch->index + 1);
734 case SR_CHANNEL_LOGIC:
735 result = dlm_digital_data_get(sdi->conn);
738 sr_err("Invalid channel type encountered (%d).",
744 devc->data_pending = TRUE;
746 devc->data_pending = FALSE;
752 * Reads and removes the block data header from a given data input.
753 * Format is #ndddd... with n being the number of decimal digits d.
754 * The string dddd... contains the decimal-encoded length of the data.
755 * Example: #9000000013 would yield a length of 13 bytes.
757 * @param data The input data.
758 * @param len The determined input data length.
760 static int dlm_block_data_header_process(GArray *data, int *len)
765 if (g_array_index(data, gchar, 0) != '#')
768 n = (uint8_t)(g_array_index(data, gchar, 1) - '0');
770 for (i = 0; i < n; i++)
771 s[i] = g_array_index(data, gchar, 2 + i);
774 if (sr_atoi(s, len) != SR_OK)
777 g_array_remove_range(data, 0, 2 + n);
783 * Turns raw sample data into voltages and sends them off to the session bus.
785 * @param data The raw sample data.
786 * @ch_state Pointer to the state of the channel whose data we're processing.
787 * @sdi The device instance.
789 * @return SR_ERR when data is trucated, SR_OK otherwise.
791 static int dlm_analog_samples_send(GArray *data,
792 struct analog_channel_state *ch_state,
793 struct sr_dev_inst *sdi)
796 float voltage, range, offset;
798 struct dev_context *devc;
799 struct scope_state *model_state;
800 struct sr_channel *ch;
801 struct sr_datafeed_analog analog;
802 struct sr_datafeed_packet packet;
805 model_state = devc->model_state;
806 samples = model_state->samples_per_frame;
807 ch = devc->current_channel->data;
809 if (data->len < samples * sizeof(uint8_t)) {
810 sr_err("Truncated waveform data packet received.");
814 range = ch_state->waveform_range;
815 offset = ch_state->waveform_offset;
817 /* Convert byte sample to voltage according to
818 * page 269 of the Communication Interface User's Manual.
820 float_data = g_array_new(FALSE, FALSE, sizeof(float));
821 for (i = 0; i < samples; i++) {
822 voltage = (float)g_array_index(data, int8_t, i);
823 voltage = (range * voltage /
824 DLM_DIVISION_FOR_BYTE_FORMAT) + offset;
825 g_array_append_val(float_data, voltage);
828 analog.channels = g_slist_append(NULL, ch);
829 analog.num_samples = float_data->len;
830 analog.data = (float*)float_data->data;
831 analog.mq = SR_MQ_VOLTAGE;
832 analog.unit = SR_UNIT_VOLT;
834 packet.type = SR_DF_ANALOG;
835 packet.payload = &analog;
836 sr_session_send(sdi, &packet);
837 g_slist_free(analog.channels);
839 g_array_free(float_data, TRUE);
840 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
846 * Sends logic sample data off to the session bus.
848 * @param data The raw sample data.
849 * @ch_state Pointer to the state of the channel whose data we're processing.
850 * @sdi The device instance.
852 * @return SR_ERR when data is trucated, SR_OK otherwise.
854 static int dlm_digital_samples_send(GArray *data,
855 struct sr_dev_inst *sdi)
857 struct dev_context *devc;
858 struct scope_state *model_state;
860 struct sr_datafeed_logic logic;
861 struct sr_datafeed_packet packet;
864 model_state = devc->model_state;
865 samples = model_state->samples_per_frame;
867 if (data->len < samples * sizeof(uint8_t)) {
868 sr_err("Truncated waveform data packet received.");
872 logic.length = samples;
874 logic.data = data->data;
875 packet.type = SR_DF_LOGIC;
876 packet.payload = &logic;
877 sr_session_send(sdi, &packet);
879 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
885 * Attempts to query sample data from the oscilloscope in order to send it
886 * to the session bus for further processing.
888 * @param fd The file descriptor used as the event source.
889 * @param revents The received events.
890 * @param cb_data Callback data, in this case our device instance.
892 * @return TRUE in case of success or a recoverable error,
893 * FALSE when a fatal error was encountered.
895 SR_PRIV int dlm_data_receive(int fd, int revents, void *cb_data)
897 struct sr_dev_inst *sdi;
898 struct scope_state *model_state;
899 struct dev_context *devc;
900 struct sr_channel *ch;
901 struct sr_datafeed_packet packet;
902 int chunk_len, num_bytes;
903 static GArray *data = NULL;
908 if (!(sdi = cb_data))
911 if (!(devc = sdi->priv))
914 if (!(model_state = (struct scope_state*)devc->model_state))
917 /* Are we waiting for a response from the device? */
918 if (!devc->data_pending)
921 /* Check if a new query response is coming our way. */
923 if (sr_scpi_read_begin(sdi->conn) == SR_OK)
924 /* The 16 here accounts for the header and EOL. */
925 data = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t),
926 16 + model_state->samples_per_frame);
931 /* Store incoming data. */
932 chunk_len = sr_scpi_read_data(sdi->conn, devc->receive_buffer,
933 RECEIVE_BUFFER_SIZE);
935 sr_err("Error while reading data: %d", chunk_len);
938 g_array_append_vals(data, devc->receive_buffer, chunk_len);
940 /* Read the entire query response before processing. */
941 if (!sr_scpi_read_complete(sdi->conn))
944 /* We finished reading and are no longer waiting for data. */
945 devc->data_pending = FALSE;
947 /* Signal the beginning of a new frame if this is the first channel. */
948 if (devc->current_channel == devc->enabled_channels) {
949 packet.type = SR_DF_FRAME_BEGIN;
950 sr_session_send(sdi, &packet);
953 if (dlm_block_data_header_process(data, &num_bytes) != SR_OK) {
954 sr_err("Encountered malformed block data header.");
958 if (num_bytes == 0) {
959 sr_warn("Zero-length waveform data packet received. " \
960 "Live mode not supported yet, stopping " \
961 "acquisition and retrying.");
962 /* Don't care about return value here. */
963 dlm_acquisition_stop(sdi->conn);
964 g_array_free(data, TRUE);
965 dlm_channel_data_request(sdi);
969 ch = devc->current_channel->data;
971 case SR_CHANNEL_ANALOG:
972 if (dlm_analog_samples_send(data,
973 &model_state->analog_states[ch->index],
977 case SR_CHANNEL_LOGIC:
978 if (dlm_digital_samples_send(data, sdi) != SR_OK)
982 sr_err("Invalid channel type encountered.");
986 g_array_free(data, TRUE);
989 /* Signal the end of this frame if this was the last enabled channel
990 * and set the next enabled channel. Then, request its data.
992 if (!devc->current_channel->next) {
993 packet.type = SR_DF_FRAME_END;
994 sr_session_send(sdi, &packet);
995 devc->current_channel = devc->enabled_channels;
997 /* As of now we only support importing the current acquisition
998 * data so we're going to stop at this point.
1000 sdi->driver->dev_acquisition_stop(sdi, cb_data);
1003 devc->current_channel = devc->current_channel->next;
1005 if (dlm_channel_data_request(sdi) != SR_OK) {
1006 sr_err("Failed to request aquisition data.");
1014 g_array_free(data, TRUE);