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 int32_t dlm_hwcaps[] = {
29 SR_CONF_LOGIC_ANALYZER,
31 SR_CONF_TRIGGER_SLOPE,
32 SR_CONF_TRIGGER_SOURCE,
35 SR_CONF_HORIZ_TRIGGERPOS,
38 static const int32_t dlm_analog_caps[] = {
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 .hw_caps = &dlm_hwcaps,
181 .num_hwcaps = ARRAY_SIZE(dlm_hwcaps),
183 .analog_hwcaps = &dlm_analog_caps,
184 .num_analog_hwcaps = ARRAY_SIZE(dlm_analog_caps),
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 .hw_caps = &dlm_hwcaps,
210 .num_hwcaps = ARRAY_SIZE(dlm_hwcaps),
212 .analog_hwcaps = &dlm_analog_caps,
213 .num_analog_hwcaps = ARRAY_SIZE(dlm_analog_caps),
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 trigger: %s (source), %s (slope) %.2f (offset)",
271 (*config->trigger_sources)[state->trigger_source],
272 (*config->trigger_slopes)[state->trigger_slope],
273 state->horiz_triggerpos);
277 * Searches through an array of strings and returns the index to the
278 * array where a given string is located.
280 * @param value The string to search for.
281 * @param array The array of strings.
282 * @param result The index at which value is located in array. -1 on error.
284 * @return SR_ERR when value couldn't be found, SR_OK otherwise.
286 static int array_option_get(char *value, const char *(*array)[],
293 for (i = 0; (*array)[i]; ++i)
294 if (!g_strcmp0(value, (*array)[i])) {
306 * This function takes a value of the form "2.000E-03", converts it to a
307 * significand / factor pair and returns the index of an array where
308 * a matching pair was found.
310 * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
311 * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
312 * Therefore it's easier to break the number up into two strings and handle
315 * @param value The string to be parsed.
316 * @param array The array of s/f pairs.
317 * @param array_len The number of pairs in the array.
318 * @param result The index at which a matching pair was found.
320 * @return SR_ERR on any parsing error, SR_OK otherwise.
322 static int array_float_get(gchar *value, const uint64_t array[][2],
323 int array_len, int *result)
328 gchar ss[10], es[10];
330 memset(ss, 0, sizeof(ss));
331 memset(es, 0, sizeof(es));
333 strncpy(ss, value, 5);
334 strncpy(es, &(value[6]), 3);
336 if (sr_atof_ascii(ss, &s) != SR_OK)
338 if (sr_atoi(es, &i) != SR_OK)
341 /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
344 /* Adjust the significand/factor pair to make sure
345 * that f is a multiple of 1000.
347 while ((int)fmod(log10(f), 3) > 0) { s *= 10; f *= 10; }
349 /* Truncate s to circumvent rounding errors. */
352 for (i = 0; i < array_len; i++) {
353 if ( (s == array[i][0]) && (f == array[i][1]) ) {
363 * Obtains information about all analog channels from the oscilloscope.
364 * The internal state information is updated accordingly.
366 * @param scpi An open SCPI connection.
367 * @param config The device's device configuration.
368 * @param state The device's state information.
370 * @return SR_ERR on error, SR_OK otherwise.
372 static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
373 struct scope_config *config,
374 struct scope_state *state)
379 for (i = 0; i < config->analog_channels; ++i) {
381 if (dlm_analog_chan_state_get(scpi, i + 1,
382 &state->analog_states[i].state) != SR_OK)
385 if (dlm_analog_chan_vdiv_get(scpi, i + 1, &response) != SR_OK)
388 if (array_float_get(response, *config->vdivs, config->num_vdivs,
395 state->analog_states[i].vdiv = j;
397 if (dlm_analog_chan_voffs_get(scpi, i + 1,
398 &state->analog_states[i].vertical_offset) != SR_OK)
401 if (dlm_analog_chan_wrange_get(scpi, i + 1,
402 &state->analog_states[i].waveform_range) != SR_OK)
405 if (dlm_analog_chan_woffs_get(scpi, i + 1,
406 &state->analog_states[i].waveform_offset) != SR_OK)
409 if (dlm_analog_chan_coupl_get(scpi, i + 1, &response) != SR_OK) {
414 if (array_option_get(response, config->coupling_options,
415 &state->analog_states[i].coupling) != SR_OK) {
426 * Obtains information about all digital channels from the oscilloscope.
427 * The internal state information is updated accordingly.
429 * @param scpi An open SCPI connection.
430 * @param config The device's device configuration.
431 * @param state The device's state information.
433 * @return SR_ERR on error, SR_OK otherwise.
435 static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
436 struct scope_config *config,
437 struct scope_state *state)
441 if (!config->digital_channels)
443 sr_warn("Tried obtaining digital channel states on a " \
444 "model without digital inputs.");
448 for (i = 0; i < config->digital_channels; ++i) {
449 if (dlm_digital_chan_state_get(scpi, i + 1,
450 &state->digital_states[i]) != SR_OK) {
457 sr_warn("Tried obtaining pod states on a model without pods.");
461 for (i = 0; i < config->pods; ++i) {
462 if (dlm_digital_pod_state_get(scpi, i + 'A',
463 &state->pod_states[i]) != SR_OK)
471 * Obtains information about the sample rate from the oscilloscope.
472 * The internal state information is updated accordingly.
474 * @param sdi The device instance.
476 * @return SR_ERR on error, SR_OK otherwise.
478 SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi)
480 struct dev_context *devc;
481 struct scope_state *state;
485 state = devc->model_state;
487 /* No need to find an active channel to query the sample rate:
488 * querying any channel will do, so we use channel 1 all the time.
490 if (dlm_analog_chan_srate_get(sdi->conn, 1, &tmp_float) != SR_OK)
493 state->sample_rate = tmp_float;
499 * Obtains information about the current device state from the oscilloscope,
500 * including all analog and digital channel configurations.
501 * The internal state information is updated accordingly.
503 * @param sdi The device instance.
505 * @return SR_ERR on error, SR_OK otherwise.
507 SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
509 struct dev_context *devc;
510 struct scope_state *state;
511 struct scope_config *config;
517 config = devc->model_config;
518 state = devc->model_state;
520 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
523 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
526 if (dlm_timebase_get(sdi->conn, &response) != SR_OK)
529 if (array_float_get(response, *config->timebases,
530 config->num_timebases, &i) != SR_OK) {
538 if (dlm_horiz_trigger_pos_get(sdi->conn, &tmp_float) != SR_OK)
541 /* TODO: Check if the calculation makes sense for the DLM. */
542 state->horiz_triggerpos = tmp_float /
543 (((double)(*config->timebases)[state->timebase][0] /
544 (*config->timebases)[state->timebase][1]) * config->num_xdivs);
545 state->horiz_triggerpos -= 0.5;
546 state->horiz_triggerpos *= -1;
548 if (dlm_trigger_source_get(sdi->conn, &response) != SR_OK) {
553 if (array_option_get(response, config->trigger_sources,
554 &state->trigger_source) != SR_OK) {
561 if (dlm_trigger_slope_get(sdi->conn, &i) != SR_OK)
564 state->trigger_slope = i;
566 dlm_sample_rate_query(sdi);
568 scope_state_dump(config, state);
574 * Creates a new device state structure.
576 * @param config The device configuration to use.
578 * @return The newly allocated scope_state struct or NULL on error.
580 static struct scope_state *dlm_scope_state_new(struct scope_config *config)
582 struct scope_state *state;
584 if (!(state = g_try_malloc0(sizeof(struct scope_state))))
587 state->analog_states = g_malloc0(config->analog_channels *
588 sizeof(struct analog_channel_state));
590 state->digital_states = g_malloc0(config->digital_channels *
593 state->pod_states = g_malloc0(config->pods * sizeof(gboolean));
599 * Frees the memory that was allocated by a call to dlm_scope_state_new().
601 * @param state The device state structure whose memory is to be freed.
603 SR_PRIV void dlm_scope_state_destroy(struct scope_state *state)
605 g_free(state->analog_states);
606 g_free(state->digital_states);
607 g_free(state->pod_states);
611 SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index)
618 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
619 for (j = 0; scope_models[i].model_id[j]; j++) {
620 if (!strcmp(model_id, scope_models[i].model_id[j])) {
622 *model_name = (char *)scope_models[i].model_name[j];
626 if (*model_index != -1)
630 if (*model_index == -1) {
631 sr_err("Found unsupported DLM device with model identifier %s.",
640 * Attempts to initialize a DL/DLM device and prepares internal structures
641 * if a suitable device was found.
643 * @param sdi The device instance.
645 SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index)
649 struct sr_channel *ch;
650 struct dev_context *devc;
654 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
655 scope_models[model_index].analog_channels);
657 devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
658 scope_models[model_index].digital_channels);
660 /* Add analog channels. */
661 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
662 if (!(ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE,
663 (*scope_models[model_index].analog_names)[i])))
664 return SR_ERR_MALLOC;
665 sdi->channels = g_slist_append(sdi->channels, ch);
667 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
669 devc->analog_groups[i]->name = g_strdup(
670 (char *)(*scope_models[model_index].analog_names)[i]);
671 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
673 sdi->channel_groups = g_slist_append(sdi->channel_groups,
674 devc->analog_groups[i]);
677 /* Add digital channel groups. */
678 for (i = 0; i < scope_models[model_index].pods; ++i) {
679 g_snprintf(tmp, sizeof(tmp), "POD%d", i);
681 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
682 if (!devc->digital_groups[i])
683 return SR_ERR_MALLOC;
685 devc->digital_groups[i]->name = g_strdup(tmp);
686 sdi->channel_groups = g_slist_append(sdi->channel_groups,
687 devc->digital_groups[i]);
690 /* Add digital channels. */
691 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
692 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
693 (*scope_models[model_index].digital_names)[i])))
694 return SR_ERR_MALLOC;
695 sdi->channels = g_slist_append(sdi->channels, ch);
697 devc->digital_groups[i / 8]->channels = g_slist_append(
698 devc->digital_groups[i / 8]->channels, ch);
700 devc->model_config = &scope_models[model_index];
701 devc->frame_limit = 0;
703 if (!(devc->model_state = dlm_scope_state_new(devc->model_config)))
704 return SR_ERR_MALLOC;
706 /* Disable non-standard response behavior. */
707 if (dlm_response_headers_set(sdi->conn, FALSE) != SR_OK)
714 * Send an SCPI command, read the reply and store the result in scpi_response
715 * without performing any processing on it.
717 * @param scpi Previously initialised SCPI device structure.
718 * @param command The SCPI command to send to the device (can be NULL).
719 * @param scpi_response Pointer where to store the parsed result.
721 * @return SR_OK on success, SR_ERR on failure.
723 static int dlm_scpi_get_raw(struct sr_scpi_dev_inst *scpi,
724 const char *command, GArray **scpi_response)
730 if (sr_scpi_send(scpi, command) != SR_OK)
733 if (sr_scpi_read_begin(scpi) != SR_OK)
736 *scpi_response = g_array_new(FALSE, FALSE, sizeof(uint8_t));
738 while (!sr_scpi_read_complete(scpi)) {
739 len = sr_scpi_read_data(scpi, buf, sizeof(buf));
741 g_array_free(*scpi_response, TRUE);
742 *scpi_response = NULL;
745 g_array_append_vals(*scpi_response, buf, len);
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 * @samples Number of samples that were acquired.
787 * @ch_state Pointer to the state of the channel whose data we're processing.
788 * @sdi The device instance.
790 * @return SR_ERR when data is trucated, SR_OK otherwise.
792 static int dlm_analog_samples_send(GArray *data, int samples,
793 struct analog_channel_state *ch_state,
794 struct sr_dev_inst *sdi)
797 float voltage, range, offset;
799 struct dev_context *devc;
800 struct sr_channel *ch;
801 struct sr_datafeed_analog analog;
802 struct sr_datafeed_packet packet;
804 if (data->len < samples * sizeof(uint8_t)) {
805 sr_err("Truncated waveform data packet received.");
810 ch = devc->current_channel->data;
812 range = ch_state->waveform_range;
813 offset = ch_state->waveform_offset;
815 /* Convert byte sample to voltage according to
816 * page 269 of the Communication Interface User's Manual.
818 float_data = g_array_new(FALSE, FALSE, sizeof(float));
819 for (i = 0; i < samples; i++) {
820 voltage = (float)g_array_index(data, int8_t, i);
821 voltage = (range * voltage /
822 DLM_DIVISION_FOR_BYTE_FORMAT) + offset;
823 g_array_append_val(float_data, voltage);
826 analog.channels = g_slist_append(NULL, ch);
827 analog.num_samples = float_data->len;
828 analog.data = (float*)float_data->data;
829 analog.mq = SR_MQ_VOLTAGE;
830 analog.unit = SR_UNIT_VOLT;
832 packet.type = SR_DF_ANALOG;
833 packet.payload = &analog;
834 sr_session_send(sdi, &packet);
835 g_slist_free(analog.channels);
837 g_array_free(float_data, TRUE);
838 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
844 * Sends logic sample data off to the session bus.
846 * @param data The raw sample data.
847 * @samples Number of samples that were acquired.
848 * @ch_state Pointer to the state of the channel whose data we're processing.
849 * @sdi The device instance.
851 * @return SR_ERR when data is trucated, SR_OK otherwise.
853 static int dlm_digital_samples_send(GArray *data, int samples,
854 struct sr_dev_inst *sdi)
856 struct sr_datafeed_logic logic;
857 struct sr_datafeed_packet packet;
859 if (data->len < samples * sizeof(uint8_t)) {
860 sr_err("Truncated waveform data packet received.");
864 logic.length = samples;
866 logic.data = data->data;
867 packet.type = SR_DF_LOGIC;
868 packet.payload = &logic;
869 sr_session_send(sdi, &packet);
871 g_array_remove_range(data, 0, samples * sizeof(uint8_t));
877 * Attempts to query sample data from the oscilloscope in order to send it
878 * to the session bus for further processing.
880 * @param fd The file descriptor used as the event source.
881 * @param revents The received events.
882 * @param cb_data Callback data, in this case our device instance.
884 * @return TRUE in case of success or a recoverable error,
885 * FALSE when a fatal error was encountered.
887 SR_PRIV int dlm_data_receive(int fd, int revents, void *cb_data)
889 struct sr_channel *ch;
890 struct sr_dev_inst *sdi;
891 struct scope_state *model_state;
892 struct dev_context *devc;
893 struct sr_datafeed_packet packet;
895 int result, num_bytes, samples;
900 if (!(sdi = cb_data))
903 if (!(devc = sdi->priv))
906 if (!(model_state = (struct scope_state*)devc->model_state))
909 if (dlm_acq_length_get(sdi->conn, &samples) != SR_OK) {
910 sr_err("Failed to query acquisition length.");
914 packet.type = SR_DF_FRAME_BEGIN;
915 sr_session_send(sdi, &packet);
917 /* Request data for all active channels. */
918 for (devc->current_channel = devc->enabled_channels;
919 devc->current_channel;
920 devc->current_channel = devc->current_channel->next) {
921 ch = devc->current_channel->data;
924 case SR_CHANNEL_ANALOG:
925 result = dlm_analog_data_get(sdi->conn, ch->index + 1);
927 case SR_CHANNEL_LOGIC:
928 result = dlm_digital_data_get(sdi->conn);
931 sr_err("Invalid channel type encountered (%d).",
936 if (result != SR_OK) {
937 sr_err("Failed to query aquisition data.");
942 if (dlm_scpi_get_raw(sdi->conn, NULL, &data) != SR_OK) {
943 sr_err("Failed to receive waveform data from device.");
947 if (dlm_block_data_header_process(data, &num_bytes) != SR_OK) {
948 sr_err("Encountered malformed block data header.");
952 if (num_bytes == 0) {
953 sr_warn("Zero-length waveform data packet received. " \
954 "Live mode not supported yet, stopping " \
955 "acquisition and retrying.");
956 /* Don't care about return value here. */
957 dlm_acquisition_stop(sdi->conn);
962 case SR_CHANNEL_ANALOG:
963 if (dlm_analog_samples_send(data, samples,
964 &model_state->analog_states[ch->index],
969 case SR_CHANNEL_LOGIC:
970 if (dlm_digital_samples_send(data, samples,
976 sr_err("Invalid channel type encountered.");
980 g_array_free(data, TRUE);
983 packet.type = SR_DF_FRAME_END;
984 sr_session_send(sdi, &packet);
986 sdi->driver->dev_acquisition_stop(sdi, cb_data);
992 g_array_free(data, TRUE);