2 * This file is part of the libsigrok project.
4 * Copyright (C) 2023 Gerhard Sittig <gerhard.sittig@gmx.net>
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/>.
21 * Juntek JDS6600 is a DDS signal generator.
22 * Often rebranded, goes by different names, among them Joy-IT JDS6600.
24 * This driver was built using Kristoff Bonne's knowledge as seen in his
25 * MIT licensed Python code for JDS6600 control. For details see the
26 * https://github.com/on1arf/jds6600_python repository.
29 * - Model detection, which determines the upper output frequency limit
30 * (15..60MHz models exist).
31 * - Assumes exactly two channels. Other models were not seen out there.
32 * - Per channel configuration of: Waveform, output frequency, amplitude,
34 * - Phase between channels is a global property and affects multiple
35 * channels at the same time (their relation to each other).
38 * - Add support for the frequency measurement and/or the counter. This
39 * feature's availability may depend on or interact with the state of
40 * other generator channels. Needs consideration of constraints.
41 * - Add support for "modes" (sweep, pulse, burst; modulation if the
42 * device supports it).
43 * - Add support for download/upload of arbitrary waveforms. This needs
44 * infrastructure in common libsigrok code as well as in applications.
45 * At the moment "blob transfer" (waveform upload/download) appears to
47 * - Re-consider parameter value ranges. Frequency depends on the model.
48 * Amplitude depends on the model and frequencies. Can be -20..+20,
49 * or -10..+10, or -5..+5. Could be affected by offsets and further
50 * get clipped. This implementation caps application's input to the
51 * -20..+20 range, and sends the set request to the device. If any
52 * further transformation happens in the device then applications
53 * need to read back, this library driver doesn't.
55 * Implementation details:
56 * - Communicates via USB CDC at 115200/8n1 (virtual COM port).
57 * - Requests are in text format. Start with a ':' colon, followed by a
58 * single letter instruction opcode, followed by a number which either
59 * addresses a parameter (think hardware register) or storage slot for
60 * an arbitrary waveform. Can be followed by an '=' equals sign and a
61 * value. Multiple values are comma separated. The line may end in a
62 * '.' period. Several end-of-line conventions are supported by the
63 * devices' firmware versions, LF and CR/LF are reported to work.
64 * - Responses also are in text format. Start with a ':' colon, followed
65 * by an instruction letter, followed by a number (a parameter index,
66 * or a waveform index), followed by '=' equal sign and one or more
67 * values. Optionally ending in a '.' period. And ending in the
68 * firmware's end-of-line. Read responses will have this format.
69 * Responses to write requests might just have the ":ok." literal.
70 * - There are four instructions: 'r' to read and 'w' to write parameters
71 * (think "hardware registers", optionaly multi-valued), 'a' to write
72 * and 'b' to read arbitrary waveform data (sequence of sample values).
73 * - Am not aware of a vendor's documentation for the protocol. Joy-IT
74 * provides the JT-JDS6600-Communication-protocol.pdf document which
75 * leaves a lot of questions. This sigrok driver implementation used
76 * a lot of https://github.com/on1arf/jds6600_python knowledge for
77 * the initial version (MIT licenced Python code by Kristoff Bonne).
78 * - The requests take effect when sent from application code. While
79 * the requests remain uneffective when typed in interactive terminal
80 * sessions. Though there are ":ok" responses, the action would not
81 * happen in the device. It's assumed to be a firmware implementation
82 * constraint that is essential to keep in mind.
83 * - The right hand side of write requests or read responses can carry
84 * any number of values, both numbers and text, integers and floats.
85 * Still some of the parameters (voltages, times, frequencies) come in
86 * interesting formats. A floating point "mantissa" and an integer code
87 * for scaling the value. Not an exponent, but some kind of index. In
88 * addition to an open coded "fixed point" style multiplier that is
89 * implied and essential, but doesn't show on the wire. Interpretation
90 * of responses and phrasing of values in requests is arbitrary, this
91 * "black magic" was found by local experimentation (reading back the
92 * values which were configured by local UI interaction).
93 * - Communication is more reliable when the host unconditionally sends
94 * "function codes" (register and waveform indices) in two-digit form.
95 * Device firmware might implement rather specific assumptions.
96 * - Semantics of the right hand side in :rNN= and :bNN= read requests
97 * is uncertain. Just passing 0 in all situations worked in a local
98 * setup. As did omitting the value during interactive exploration.
100 * Example requests and responses.
101 * - Get model identification (max output frequency)
102 * TX text: --> :r00=0.
103 * TX bytes: --> 3a 72 30 30 3d 30 2e 0d 0a
104 * RX bytes: <-- 3a 72 30 30 3d 36 30 2e 0d 0a
105 * RX text: <-- :r00=60.
106 * - Get all channels' enabled state
107 * TX text: --> :r20=0.
108 * TX bytes: --> 3a 72 32 30 3d 30 2e 0d 0a
109 * RX bytes: <-- 3a 72 32 30 3d 31 2c 31 2e 0d 0a
110 * RX text: <-- :r20=1,1.
111 * - Get first channel's waveform selection
112 * TX text: --> :r21=0.
113 * TX bytes: --> 3a 72 32 31 3d 30 2e 0d 0a
114 * RX bytes: <-- 3a 72 32 31 3d 31 30 33 2e 0d 0a
115 * RX text: <-- :r21=103.
116 * - Set second channel's output frequency
117 * TX text: --> :w24=1234500,0.
118 * TX bytes: --> 3a 77 32 34 3d 31 32 33 34 35 30 30 2c 30 2e 0d 0a
119 * RX bytes: <-- 3a 6f 6b 0d 0a
121 * - Read arbitrary waveform number 13
122 * TX text: --> :b13=0.
123 * TX bytes: --> 3a 62 31 33 3d 30 2e 0d 0a
124 * RX bytes: <-- 3a 62 31 33 3d 34 30 39 35 2c 34 30 39 35 2c ... 2c 34 30 39 35 2c 34 30 39 35 2c 0d 0a
125 * RX text: <-- :b13=4095,4095,...,4095,4095,
134 #include "protocol.h"
136 #define WITH_SERIAL_RAW_DUMP 0 /* Includes EOL and non-printables. */
137 #define WITH_ARBWAVE_DOWNLOAD 0 /* Development HACK */
140 * The firmware's maximum response length. Seen when an arbitrary
141 * waveform gets retrieved. Carries 2048 samples in the 0..4095 range.
142 * Plus some decoration around that data.
143 * :b01=4095,4095,...,4095,<CRLF>
145 #define MAX_RSP_LENGTH (8 + 2048 * 5)
147 /* Times are in milliseconds. */
148 #define DELAY_AFTER_WRITE 10
149 #define DELAY_AFTER_FLASH 100
150 #define TIMEOUT_READ_CHUNK 20
151 #define TIMEOUT_IDENTIFY 200
153 /* Instruction codes. Read/write parameters/waveforms. */
154 #define INSN_WRITE_PARA 'w'
155 #define INSN_READ_PARA 'r'
156 #define INSN_WRITE_WAVE 'a'
157 #define INSN_READ_WAVE 'b'
159 /* Indices for "register access". */
162 IDX_SERIAL_NUMBER = 1,
163 IDX_CHANNELS_ENABLE = 20,
164 IDX_WAVEFORM_CH1 = 21,
165 IDX_WAVEFORM_CH2 = 22,
166 IDX_FREQUENCY_CH1 = 23,
167 IDX_FREQUENCY_CH2 = 24,
168 IDX_AMPLITUDE_CH1 = 25,
169 IDX_AMPLITUDE_CH2 = 26,
172 IDX_DUTYCYCLE_CH1 = 29,
173 IDX_DUTYCYCLE_CH2 = 30,
174 IDX_PHASE_CHANNELS = 31,
177 IDX_INPUT_COUPLING = 36,
178 IDX_MEASURE_GATE = 37,
179 IDX_MEASURE_MODE = 38,
180 IDX_COUNTER_RESET = 39,
181 IDX_SWEEP_STARTFREQ = 40,
182 IDX_SWEEP_ENDFREQ = 41,
184 IDX_SWEEP_DIRECTION = 43,
186 IDX_PULSE_WIDTH = 45,
187 IDX_PULSE_PERIOD = 46,
188 IDX_PULSE_OFFSET = 47,
189 IDX_PULSE_AMPLITUDE = 48,
190 IDX_BURST_COUNT = 49,
192 IDX_SYSTEM_SOUND = 51,
193 IDX_SYSTEM_BRIGHTNESS = 52,
194 IDX_SYSTEM_LANGUAGE = 53,
195 IDX_SYSTEM_SYNC = 54, /* "Tracking" channels? */
196 IDX_SYSTEM_ARBMAX = 55,
197 IDX_PROFILE_SAVE = 70,
198 IDX_PROFILE_LOAD = 71,
199 IDX_PROFILE_CLEAR = 72,
200 IDX_COUNTER_VALUE = 80,
201 IDX_MEAS_VALUE_FREQLOW = 81,
202 IDX_MEAS_VALUE_FREQHI = 82,
203 IDX_MEAS_VALUE_WIDTHHI = 83,
204 IDX_MEAS_VALUE_WIDTHLOW = 84,
205 IDX_MEAS_VALUE_PERIOD = 85,
206 IDX_MEAS_VALUE_DUTYCYCLE = 86,
207 IDX_MEAS_VALUE_U1 = 87,
208 IDX_MEAS_VALUE_U2 = 88,
209 IDX_MEAS_VALUE_U3 = 89,
212 /* Firmware's codes for waveform selection. */
213 enum waveform_index_t {
214 /* 17 pre-defined waveforms. */
219 WAVE_PARTIAL_SINE = 4,
225 WAVE_NEG_LADDER = 10,
229 WAVE_MULTI_TONE = 14,
233 /* Up to 60 arbitrary waveforms. */
234 WAVES_ARB_BASE = 100,
235 WAVE_ARB01 = WAVES_ARB_BASE + 1,
237 WAVE_ARB60 = WAVES_ARB_BASE + 60,
240 #define WAVES_COUNT_ARBITRARY (WAVES_PAST_LAST_ARB - WAVE_ARB01)
242 static const char *waveform_names[] = {
243 [WAVE_SINE] = "sine",
244 [WAVE_SQUARE] = "square",
245 [WAVE_PULSE] = "pulse",
246 [WAVE_TRIANGLE] = "triangle",
247 [WAVE_PARTIAL_SINE] = "partial-sine",
248 [WAVE_CMOS] = "cmos",
250 [WAVE_HALF_WAVE] = "half-wave",
251 [WAVE_FULL_WAVE] = "full-wave",
252 [WAVE_POS_LADDER] = "pos-ladder",
253 [WAVE_NEG_LADDER] = "neg-ladder",
254 [WAVE_NOISE] = "noise",
255 [WAVE_EXP_RISE] = "exp-rise",
256 [WAVE_EXP_DECAY] = "exp-decay",
257 [WAVE_MULTI_TONE] = "multi-tone",
258 [WAVE_SINC] = "sinc",
259 [WAVE_LORENZ] = "lorenz",
261 #define WAVEFORM_ARB_NAME_FMT "arb-%02zu"
263 static void log_raw_bytes(const char *caption, GString *buff)
267 if (!WITH_SERIAL_RAW_DUMP)
269 if (sr_log_loglevel_get() < SR_LOG_SPEW)
274 text = sr_hexdump_new((const uint8_t *)buff->str, buff->len);
275 sr_spew("%s%s", caption, text->str);
276 sr_hexdump_free(text);
280 * Writes a text line to the serial port. Normalizes end-of-line
281 * including trailing period.
291 static int serial_send_textline(const struct sr_dev_inst *sdi,
292 GString *s, unsigned int delay_ms)
294 struct sr_serial_dev_inst *conn;
296 size_t padlen, rdlen, wrlen;
308 * Trim surrounding whitespace. Normalize to canonical format.
309 * Make sure there is enough room for the period and CR/LF
310 * (and NUL termination). Use a glib API that's easy to adjust
311 * the padded length of. Performance is not a priority here.
315 g_string_append_c(s, '\0');
316 rdptr = sr_text_trim_spaces(s->str);
317 rdlen = strlen(rdptr);
318 if (rdlen && rdptr[rdlen - 1] == '.')
320 g_string_set_size(s, rdlen);
321 g_string_append_c(s, '.');
322 sr_spew("serial TX text: --> %s", rdptr);
323 g_string_append_c(s, '\r');
324 g_string_append_c(s, '\n');
325 rdlen = strlen(rdptr);
326 log_raw_bytes("serial TX bytes: --> ", s);
328 /* Handle chunked writes, check for transmission errors. */
330 ret = serial_write_blocking(conn, rdptr, rdlen, 0);
341 g_usleep(delay_ms * 1000);
347 * Reads a text line from the serial port. Assumes that only a single
348 * response text line is in flight (does not handle the case of more
349 * receive data following after the first EOL). Transparently deals
350 * with trailing period and end-of-line, so callers need not bother.
352 * Checks plausibility when the caller specifies conditions to check.
353 * Optionally returns references (and lengths) to the response's RHS.
354 * That's fine because data resides in a caller provided buffer.
356 static int serial_recv_textline(const struct sr_dev_inst *sdi,
357 GString *s, unsigned int delay_ms, unsigned int timeout_ms,
358 gboolean *is_ok, char wants_insn, size_t wants_index,
359 char **rhs_start, size_t *rhs_length)
361 struct sr_serial_dev_inst *ser;
365 guint64 now_us, deadline_us;
366 gboolean has_timedout;
367 char *eol_pos, *endptr;
369 unsigned long got_index;
386 g_string_set_size(s, MAX_RSP_LENGTH);
387 g_string_truncate(s, 0);
389 /* Arrange for overall receive timeout when caller specified. */
390 now_us = deadline_us = 0;
392 now_us = g_get_monotonic_time();
393 deadline_us = now_us;
394 deadline_us += timeout_ms * 1000;
398 rdlen = s->allocated_len - 1 - s->len;
400 /* Get another chunk of receive data. Check for EOL. */
401 ret = serial_read_blocking(ser, rdptr, rdlen, delay_ms);
408 eol_pos = strchr(rdptr, '\n');
411 g_string_set_size(s, s->len + got);
412 /* Check timeout expiration upon empty reception. */
413 has_timedout = FALSE;
414 if (timeout_ms && !got) {
415 now_us = g_get_monotonic_time();
416 if (now_us >= deadline_us)
424 log_raw_bytes("serial RX bytes: <-- ", s);
426 /* Normalize the received text line. */
429 (void)sr_text_trim_spaces(rdptr);
430 rdlen = strlen(rdptr);
431 sr_spew("serial RX text: <-- %s", rdptr);
432 if (rdlen && rdptr[rdlen - 1] == '.')
433 rdptr[--rdlen] = '\0';
435 /* Check conditions as requested by the caller. */
436 if (is_ok || wants_insn || rhs_start) {
438 sr_dbg("serial read, colon missing");
445 * The check for 'ok' is terminal. Does not combine with
446 * responses which carry payload data on their RHS.
449 *is_ok = strcmp(rdptr, "ok") == 0;
450 sr_dbg("serial read, 'ok' check %d", *is_ok);
451 return *is_ok ? SR_OK : SR_ERR_DATA;
454 * Conditional strict checks for caller's expected fields.
455 * Unconditional weaker checks for general structure.
457 if (wants_insn && *rdptr != wants_insn) {
458 sr_dbg("serial read, unexpected insn");
463 case INSN_WRITE_PARA:
465 case INSN_WRITE_WAVE:
470 sr_dbg("serial read, unknown insn %c", got_insn);
474 ret = sr_atoul_base(rdptr, &got_index, &endptr, 10);
475 if (ret != SR_OK || !endptr)
477 if (wants_index && got_index != wants_index) {
478 sr_dbg("serial read, unexpected index %lu", got_index);
482 if (rhs_start || rhs_length) {
484 sr_dbg("serial read, equals sign missing");
491 /* Response is considered plausible here. */
495 *rhs_length = strlen(rdptr);
498 log_raw_bytes("serial RX bytes: <-- ", s);
499 sr_dbg("serial read, unterminated response, discarded");
501 sr_dbg("serial read, no EOL seen");
505 /* Formatting helpers for request construction. */
507 static void append_insn_read_para(GString *s, char insn, size_t idx)
509 g_string_append_printf(s, ":%c%02zu=0", insn, idx & 0xff);
512 static void append_insn_write_para_va(GString *s, char insn, size_t idx,
513 const char *fmt, va_list args) ATTR_FMT_PRINTF(4, 0);
514 static void append_insn_write_para_va(GString *s, char insn, size_t idx,
515 const char *fmt, va_list args)
517 g_string_append_printf(s, ":%c%02zu=", insn, idx & 0xff);
518 g_string_append_vprintf(s, fmt, args);
521 static void append_insn_write_para_dots(GString *s, char insn, size_t idx,
522 const char *fmt, ...) ATTR_FMT_PRINTF(4, 5);
523 static void append_insn_write_para_dots(GString *s, char insn, size_t idx,
524 const char *fmt, ...)
529 append_insn_write_para_va(s, insn, idx, fmt, args);
534 * Turn comma separators into whitespace. Simplifies the interpretation
535 * of multi-value response payloads. Also replaces any trailing period
536 * in case callers kept one in the receive buffer.
538 static void replace_separators(char *s)
546 if (s[0] == '.' && s[1] == '\0') {
555 * Convenience to interpret responses' values. Also concentrates the
556 * involved magic and simplifies diagnostics. It's essential to apply
557 * implicit multipliers, and to properly combine multiple fields into
558 * the resulting parameter's value (think scaling and offsetting).
561 static const double scales_freq[] = {
565 static int parse_freq_text(char *s, double *value)
572 replace_separators(s);
574 /* First word is a mantissa, in centi-Hertz. :-O */
575 word = sr_text_next_word(s, &s);
576 ret = sr_atod(word, &dvalue);
580 /* Next word is an encoded scaling factor. */
581 word = sr_text_next_word(s, &s);
582 ret = sr_atoul_base(word, &scale, NULL, 10);
585 sr_spew("parse freq, mant %f, scale %lu", dvalue, scale);
586 if (scale >= ARRAY_SIZE(scales_freq))
589 /* Do scale the mantissa's value. */
591 dvalue /= scales_freq[scale];
592 sr_spew("parse freq, value %f", dvalue);
599 static int parse_volt_text(char *s, double *value)
604 /* Single value, in units of mV. */
605 ret = sr_atod(s, &dvalue);
608 sr_spew("parse volt, mant %f", dvalue);
610 sr_spew("parse volt, value %f", dvalue);
617 static int parse_bias_text(char *s, double *value)
623 * Single value, in units of 10mV with a 10V offset. Capped to
624 * the +9.99V..-9.99V range. The Joy-IT PDF is a little weird
625 * suggesting that ":w27=9999." translates to 9.99 volts.
627 ret = sr_atod(s, &dvalue);
630 sr_spew("parse bias, mant %f", dvalue);
637 sr_spew("parse bias, value %f", dvalue);
644 static int parse_duty_text(char *s, double *value)
650 * Single value, in units of 0.1% (permille).
651 * Scale to the 0.0..1.0 range.
653 ret = sr_atod(s, &dvalue);
656 sr_spew("parse duty, mant %f", dvalue);
658 sr_spew("parse duty, value %f", dvalue);
665 static int parse_phase_text(char *s, double *value)
670 /* Single value, in units of deci-degrees. */
671 ret = sr_atod(s, &dvalue);
674 sr_spew("parse phase, mant %f", dvalue);
676 sr_spew("parse phase, value %f", dvalue);
684 * Convenience to generate request presentations. Also concentrates the
685 * involved magic and simplifies diagnostics. It's essential to apply
686 * implicit multipliers, and to properly create all request fields that
687 * communicate a value to the device's firmware (think scale and offset).
690 static void write_freq_text(GString *s, double freq)
692 unsigned long scale_idx;
693 const char *text_pos;
695 sr_spew("write freq, value %f", freq);
696 text_pos = &s->str[s->len];
699 * First word is mantissa in centi-Hertz. Second word is a
700 * scaling factor code. Keep scaling simple, always scale
701 * by a factor of 1.0.
704 freq *= scales_freq[scale_idx];
707 g_string_append_printf(s, "%.0f,%lu", freq, scale_idx);
708 sr_spew("write freq, text %s", text_pos);
711 static void write_volt_text(GString *s, double volt)
713 const char *text_pos;
715 sr_spew("write volt, value %f", volt);
716 text_pos = &s->str[s->len];
719 * Single value in units of 1mV.
720 * Limit input values to the 0..+20 range. This writer is only
721 * used by the amplitude setter.
728 g_string_append_printf(s, "%.0f", volt);
729 sr_spew("write volt, text %s", text_pos);
732 static void write_bias_text(GString *s, double volt)
734 const char *text_pos;
736 sr_spew("write bias, value %f", volt);
737 text_pos = &s->str[s->len];
740 * Single value in units of 10mV with a 10V offset. Capped to
741 * the +9.99..-9.99 range.
750 g_string_append_printf(s, "%.0f", volt);
751 sr_spew("write bias, text %s", text_pos);
754 static void write_duty_text(GString *s, double duty)
756 const char *text_pos;
758 sr_spew("write duty, value %f", duty);
759 text_pos = &s->str[s->len];
762 * Single value in units of 0.1% (permille). Capped to the
771 g_string_append_printf(s, "%.0f", duty);
772 sr_spew("write duty, text %s", text_pos);
775 static void write_phase_text(GString *s, double phase)
777 const char *text_pos;
779 sr_spew("write phase, value %f", phase);
780 text_pos = &s->str[s->len];
783 * Single value in units of deci-degrees.
784 * Kept to the 0..360 range by means of a modulo operation.
786 phase = fmod(phase, 360.0);
789 g_string_append_printf(s, "%.0f", phase);
790 sr_spew("write phase, text %s", text_pos);
794 * Convenience communication wrapper. Re-uses a buffer in devc, which
795 * simplifies resource handling in error paths. Sends a parameter-less
796 * read-request. Then receives a response which can carry values.
798 static int quick_send_read_then_recv(const struct sr_dev_inst *sdi,
799 char insn, size_t idx,
800 unsigned int read_timeout_ms,
801 char **rhs_start, size_t *rhs_length)
803 struct dev_context *devc;
812 if (!devc->quick_req)
813 devc->quick_req = g_string_sized_new(MAX_RSP_LENGTH);
816 g_string_truncate(s, 0);
817 append_insn_read_para(s, insn, idx);
818 ret = serial_send_textline(sdi, s, DELAY_AFTER_WRITE);
822 ret = serial_recv_textline(sdi, s,
823 TIMEOUT_READ_CHUNK, read_timeout_ms,
824 NULL, insn, idx, rhs_start, rhs_length);
832 * Convenience communication wrapper, re-uses a buffer in devc. Sends a
833 * write-request with parameters. Then receives an "ok" style response.
834 * Had to put the request details after the response related parameters
835 * because of the va_list API.
837 static int quick_send_write_then_recv_ok(const struct sr_dev_inst *sdi,
838 unsigned int read_timeout_ms, gboolean *is_ok,
839 char insn, size_t idx, const char *fmt, ...) ATTR_FMT_PRINTF(6, 7);
840 static int quick_send_write_then_recv_ok(const struct sr_dev_inst *sdi,
841 unsigned int read_timeout_ms, gboolean *is_ok,
842 char insn, size_t idx, const char *fmt, ...)
844 struct dev_context *devc;
855 if (!devc->quick_req)
856 devc->quick_req = g_string_sized_new(MAX_RSP_LENGTH);
859 g_string_truncate(s, 0);
861 append_insn_write_para_va(s, insn, idx, fmt, args);
863 ret = serial_send_textline(sdi, s, DELAY_AFTER_WRITE);
867 ret = serial_recv_textline(sdi, s,
868 TIMEOUT_READ_CHUNK, read_timeout_ms,
869 &ok, '\0', 0, NULL, NULL);
879 * High level getters/setters for device properties.
880 * To be used by the api.c config get/set infrastructure.
883 SR_PRIV int jds6600_get_chans_enable(const struct sr_dev_inst *sdi)
885 struct dev_context *devc;
887 char *rdptr, *word, *endptr;
888 struct devc_dev *device;
889 struct devc_chan *chans;
897 /* Transmit the request, receive the response. */
898 ret = quick_send_read_then_recv(sdi,
899 INSN_READ_PARA, IDX_CHANNELS_ENABLE,
903 sr_dbg("get enabled, response text: %s", rdptr);
905 /* Interpret the response (multiple values, boolean). */
906 replace_separators(rdptr);
907 device = &devc->device;
908 chans = devc->channel_config;
909 for (idx = 0; idx < device->channel_count_gen; idx++) {
910 word = sr_text_next_word(rdptr, &rdptr);
914 ret = sr_atoul_base(word, &on, &endptr, 10);
915 if (ret != SR_OK || !endptr || *endptr)
917 chans[idx].enabled = on;
923 SR_PRIV int jds6600_get_waveform(const struct sr_dev_inst *sdi, size_t ch_idx)
925 struct dev_context *devc;
927 char *rdptr, *endptr;
928 struct devc_wave *waves;
929 struct devc_chan *chan;
938 waves = &devc->waveforms;
939 if (ch_idx >= ARRAY_SIZE(devc->channel_config))
941 chan = &devc->channel_config[ch_idx];
943 /* Transmit the request, receive the response. */
944 ret = quick_send_read_then_recv(sdi,
945 INSN_READ_PARA, IDX_WAVEFORM_CH1 + ch_idx,
949 sr_dbg("get waveform, response text: %s", rdptr);
952 * Interpret the response (integer value, waveform code).
953 * Lookup the firmware's code for that waveform in the
954 * list of user perceivable names for waveforms.
957 ret = sr_atoul_base(rdptr, &code, &endptr, 10);
960 for (idx = 0; idx < waves->names_count; idx++) {
961 if (code != waves->fw_codes[idx])
963 chan->waveform_code = code;
964 chan->waveform_index = idx;
965 sr_dbg("get waveform, code %lu, idx %zu, name %s",
966 code, idx, waves->names[idx]);
973 #if WITH_ARBWAVE_DOWNLOAD
975 * Development HACK. Get a waveform from the device. Uncertain where to
976 * dump it though. Have yet to identify a sigrok API for waveforms.
978 static int jds6600_get_arb_waveform(const struct sr_dev_inst *sdi, size_t idx)
980 struct dev_context *devc;
981 struct devc_wave *waves;
983 char *rdptr, *word, *endptr;
992 waves = &devc->waveforms;
994 if (idx >= waves->arbitrary_count)
997 /* Transmit the request, receive the response. */
998 ret = quick_send_read_then_recv(sdi,
1003 sr_dbg("get arb wave, response text: %s", rdptr);
1005 /* Extract the sequence of samples for the waveform. */
1006 replace_separators(rdptr);
1008 while (rdptr && *rdptr) {
1009 word = sr_text_next_word(rdptr, &rdptr);
1013 ret = sr_atoul_base(word, &value, &endptr, 10);
1014 if (ret != SR_OK || !endptr || *endptr) {
1015 sr_dbg("get arb wave, conv error: %s", word);
1020 sr_dbg("get arb wave, samples count: %zu", sample_count);
1026 SR_PRIV int jds6600_get_frequency(const struct sr_dev_inst *sdi, size_t ch_idx)
1028 struct dev_context *devc;
1029 struct devc_chan *chan;
1037 if (ch_idx >= ARRAY_SIZE(devc->channel_config))
1039 chan = &devc->channel_config[ch_idx];
1041 /* Transmit the request, receive the response. */
1042 ret = quick_send_read_then_recv(sdi,
1043 INSN_READ_PARA, IDX_FREQUENCY_CH1 + ch_idx,
1047 sr_dbg("get frequency, response text: %s", rdptr);
1049 /* Interpret the response (value and scale, frequency). */
1050 ret = parse_freq_text(rdptr, &freq);
1053 sr_dbg("get frequency, value %f", freq);
1054 chan->output_frequency = freq;
1058 SR_PRIV int jds6600_get_amplitude(const struct sr_dev_inst *sdi, size_t ch_idx)
1060 struct dev_context *devc;
1061 struct devc_chan *chan;
1069 if (ch_idx >= ARRAY_SIZE(devc->channel_config))
1071 chan = &devc->channel_config[ch_idx];
1073 /* Transmit the request, receive the response. */
1074 ret = quick_send_read_then_recv(sdi,
1075 INSN_READ_PARA, IDX_AMPLITUDE_CH1 + ch_idx,
1079 sr_dbg("get amplitude, response text: %s", rdptr);
1081 /* Interpret the response (single value, a voltage). */
1082 ret = parse_volt_text(rdptr, &);
1085 sr_dbg("get amplitude, value %f", amp);
1086 chan->amplitude = amp;
1090 SR_PRIV int jds6600_get_offset(const struct sr_dev_inst *sdi, size_t ch_idx)
1092 struct dev_context *devc;
1093 struct devc_chan *chan;
1101 if (ch_idx >= ARRAY_SIZE(devc->channel_config))
1103 chan = &devc->channel_config[ch_idx];
1105 /* Transmit the request, receive the response. */
1106 ret = quick_send_read_then_recv(sdi,
1107 INSN_READ_PARA, IDX_OFFSET_CH1 + ch_idx,
1111 sr_dbg("get offset, response text: %s", rdptr);
1113 /* Interpret the response (single value, an offset). */
1114 ret = parse_bias_text(rdptr, &off);
1117 sr_dbg("get offset, value %f", off);
1122 SR_PRIV int jds6600_get_dutycycle(const struct sr_dev_inst *sdi, size_t ch_idx)
1124 struct dev_context *devc;
1125 struct devc_chan *chan;
1133 if (ch_idx >= ARRAY_SIZE(devc->channel_config))
1135 chan = &devc->channel_config[ch_idx];
1137 /* Transmit the request, receive the response. */
1138 ret = quick_send_read_then_recv(sdi,
1139 INSN_READ_PARA, IDX_DUTYCYCLE_CH1 + ch_idx,
1143 sr_dbg("get duty cycle, response text: %s", rdptr);
1145 /* Interpret the response (single value, a percentage). */
1146 ret = parse_duty_text(rdptr, &duty);
1149 sr_dbg("get duty cycle, value %f", duty);
1150 chan->dutycycle = duty;
1154 SR_PRIV int jds6600_get_phase_chans(const struct sr_dev_inst *sdi)
1156 struct dev_context *devc;
1165 /* Transmit the request, receive the response. */
1166 ret = quick_send_read_then_recv(sdi,
1167 INSN_READ_PARA, IDX_PHASE_CHANNELS,
1171 sr_dbg("get phase, response text: %s", rdptr);
1173 /* Interpret the response (single value, an angle). */
1174 ret = parse_phase_text(rdptr, &phase);
1177 sr_dbg("get phase, value %f", phase);
1178 devc->channels_phase = phase;
1182 SR_PRIV int jds6600_set_chans_enable(const struct sr_dev_inst *sdi)
1184 struct dev_context *devc;
1185 struct devc_chan *chans;
1196 /* Transmit the request, receive an "ok" style response. */
1197 chans = devc->channel_config;
1198 en_text = g_string_sized_new(20);
1199 for (idx = 0; idx < devc->device.channel_count_gen; idx++) {
1201 g_string_append_c(en_text, ',');
1202 g_string_append_c(en_text, chans[idx].enabled ? '1' : '0');
1204 sr_dbg("set enabled, request text: %s", en_text->str);
1205 ret = quick_send_write_then_recv_ok(sdi, 0, NULL,
1206 INSN_WRITE_PARA, IDX_CHANNELS_ENABLE, "%s", en_text->str);
1207 g_string_free(en_text, 20);
1214 SR_PRIV int jds6600_set_waveform(const struct sr_dev_inst *sdi, size_t ch_idx)
1216 struct dev_context *devc;
1217 struct devc_chan *chan;
1225 if (ch_idx >= devc->device.channel_count_gen)
1227 chan = &devc->channel_config[ch_idx];
1229 /* Transmit the request, receive an "ok" style response. */
1230 ret = quick_send_write_then_recv_ok(sdi, 0, NULL,
1231 INSN_WRITE_PARA, IDX_WAVEFORM_CH1 + ch_idx,
1232 "%" PRIu32, chan->waveform_code);
1239 #if WITH_ARBWAVE_DOWNLOAD
1241 * Development HACK. Send a waveform to the device. Uncertain where
1242 * to get it from though. Just generate some stupid pattern that's
1243 * seen on the LCD later.
1245 * Local experiments suggest that writing another waveform after having
1246 * written one earlier results in the next waveform to become mangled.
1247 * It appears to start with an all-bits-set pattern for a remarkable
1248 * number of samples, before the actually written pattern is seen. Some
1249 * delay after reception of the ":ok" response may be required to avoid
1253 /* Stupid creation of one sample value. Gets waveform index and sample count. */
1254 static uint16_t make_sample(size_t wave, size_t curr, size_t total)
1256 uint16_t max_value, high_value, low_value;
1257 size_t ival, high_width;
1260 /* Get the waveform's amplitudes. */
1262 high_value = max_value / (wave + 3);
1263 high_value = max_value - high_value;
1264 low_value = max_value - high_value;
1266 /* Get pulses' total interval, high and low half-periods. */
1267 ival = (total - 10) / wave;
1268 high_width = ival / 2;
1270 /* Check location in the current period. */
1272 is_high = curr <= high_width;
1273 return is_high ? high_value : low_value;
1276 /* Creation and download of the sequence of samples. */
1277 static int jds6600_set_arb_waveform(const struct sr_dev_inst *sdi, size_t idx)
1279 struct dev_context *devc;
1280 struct devc_wave *waves;
1282 size_t samples_total, samples_curr;
1292 waves = &devc->waveforms;
1294 if (idx >= waves->arbitrary_count)
1297 /* Construct a pattern that depends on the waveform index. */
1298 wave_text = g_string_sized_new(MAX_RSP_LENGTH);
1299 samples_total = 2048;
1301 for (samples_curr = 0; samples_curr < samples_total; samples_curr++) {
1302 value = make_sample(idx, samples_curr, samples_total);
1304 g_string_append_c(wave_text, ',');
1305 g_string_append_printf(wave_text, "%" PRIu16, value);
1307 sr_dbg("set arb wave, request text: %s", wave_text->str);
1309 /* Transmit the request, receive an "ok" style response. */
1310 ret = quick_send_write_then_recv_ok(sdi, 0, &ok,
1311 INSN_WRITE_WAVE, idx, "%s", wave_text->str);
1314 sr_dbg("set arb wave, response ok: %d", ok);
1316 if (DELAY_AFTER_FLASH)
1317 g_usleep(DELAY_AFTER_FLASH * 1000);
1323 SR_PRIV int jds6600_set_frequency(const struct sr_dev_inst *sdi, size_t ch_idx)
1325 struct dev_context *devc;
1326 struct devc_chan *chan;
1336 if (ch_idx >= devc->device.channel_count_gen)
1338 chan = &devc->channel_config[ch_idx];
1340 /* Limit input values to the range supported by the model. */
1341 freq = chan->output_frequency;
1344 if (freq > devc->device.max_output_frequency)
1345 freq = devc->device.max_output_frequency;
1347 /* Transmit the request, receive an "ok" style response. */
1348 freq_text = g_string_sized_new(32);
1349 write_freq_text(freq_text, freq);
1350 ret = quick_send_write_then_recv_ok(sdi, 0, NULL,
1351 INSN_WRITE_PARA, IDX_FREQUENCY_CH1 + ch_idx,
1352 "%s", freq_text->str);
1353 g_string_free(freq_text, TRUE);
1360 SR_PRIV int jds6600_set_amplitude(const struct sr_dev_inst *sdi, size_t ch_idx)
1362 struct dev_context *devc;
1363 struct devc_chan *chan;
1372 if (ch_idx >= devc->device.channel_count_gen)
1374 chan = &devc->channel_config[ch_idx];
1376 /* Transmit the request, receive an "ok" style response. */
1377 volt_text = g_string_sized_new(32);
1378 write_volt_text(volt_text, chan->amplitude);
1379 ret = quick_send_write_then_recv_ok(sdi, 0, NULL,
1380 INSN_WRITE_PARA, IDX_AMPLITUDE_CH1 + ch_idx,
1381 "%s", volt_text->str);
1382 g_string_free(volt_text, TRUE);
1389 SR_PRIV int jds6600_set_offset(const struct sr_dev_inst *sdi, size_t ch_idx)
1391 struct dev_context *devc;
1392 struct devc_chan *chan;
1401 if (ch_idx >= devc->device.channel_count_gen)
1403 chan = &devc->channel_config[ch_idx];
1405 /* Transmit the request, receive an "ok" style response. */
1406 volt_text = g_string_sized_new(32);
1407 write_bias_text(volt_text, chan->offset);
1408 ret = quick_send_write_then_recv_ok(sdi, 0, NULL,
1409 INSN_WRITE_PARA, IDX_OFFSET_CH1 + ch_idx,
1410 "%s", volt_text->str);
1411 g_string_free(volt_text, TRUE);
1418 SR_PRIV int jds6600_set_dutycycle(const struct sr_dev_inst *sdi, size_t ch_idx)
1420 struct dev_context *devc;
1421 struct devc_chan *chan;
1430 if (ch_idx >= devc->device.channel_count_gen)
1432 chan = &devc->channel_config[ch_idx];
1434 /* Transmit the request, receive an "ok" style response. */
1435 duty_text = g_string_sized_new(32);
1436 write_duty_text(duty_text, chan->dutycycle);
1437 ret = quick_send_write_then_recv_ok(sdi, 0, NULL,
1438 INSN_WRITE_PARA, IDX_DUTYCYCLE_CH1 + ch_idx,
1439 "%s", duty_text->str);
1440 g_string_free(duty_text, TRUE);
1447 SR_PRIV int jds6600_set_phase_chans(const struct sr_dev_inst *sdi)
1449 struct dev_context *devc;
1450 GString *phase_text;
1459 /* Transmit the request, receive an "ok" style response. */
1460 phase_text = g_string_sized_new(32);
1461 write_phase_text(phase_text, devc->channels_phase);
1462 ret = quick_send_write_then_recv_ok(sdi, 0, NULL,
1463 INSN_WRITE_PARA, IDX_PHASE_CHANNELS,
1464 "%s", phase_text->str);
1465 g_string_free(phase_text, TRUE);
1473 * High level helpers for the scan/probe phase. Identify the attached
1474 * device and synchronize to its current state and its capabilities.
1477 SR_PRIV int jds6600_identify(struct sr_dev_inst *sdi)
1479 struct dev_context *devc;
1481 char *rdptr, *endptr;
1482 unsigned long devtype;
1484 (void)append_insn_write_para_dots;
1492 /* Transmit "read device type" request, receive the response. */
1493 ret = quick_send_read_then_recv(sdi,
1494 INSN_READ_PARA, IDX_DEVICE_TYPE,
1495 TIMEOUT_IDENTIFY, &rdptr, NULL);
1498 sr_dbg("identify, device type '%s'", rdptr);
1500 /* Interpret the response (integer value, max freq). */
1502 ret = sr_atoul_base(rdptr, &devtype, &endptr, 10);
1503 if (ret != SR_OK || !endptr)
1505 devc->device.device_type = devtype;
1507 /* Transmit "read serial number" request. receive response. */
1508 ret = quick_send_read_then_recv(sdi,
1509 INSN_READ_PARA, IDX_SERIAL_NUMBER,
1513 sr_dbg("identify, serial number '%s'", rdptr);
1515 /* Keep the response (in string format, some serial number). */
1516 devc->device.serial_number = g_strdup(rdptr);
1521 SR_PRIV int jds6600_setup_devc(struct sr_dev_inst *sdi)
1523 struct dev_context *devc;
1524 size_t alloc_count, assign_idx, idx;
1525 struct devc_dev *device;
1526 struct devc_wave *waves;
1527 enum waveform_index_t code;
1538 * Derive maximum output frequency from detected device type.
1539 * Open coded generator channel count.
1541 device = &devc->device;
1542 if (!device->device_type)
1544 device->max_output_frequency = device->device_type;
1545 device->max_output_frequency *= SR_MHZ(1);
1546 device->channel_count_gen = MAX_GEN_CHANNELS;
1548 /* Construct the list of waveform names and their codes. */
1549 waves = &devc->waveforms;
1550 waves->builtin_count = WAVES_COUNT_BUILTIN;
1551 waves->arbitrary_count = WAVES_COUNT_ARBITRARY;
1552 alloc_count = waves->builtin_count;
1553 alloc_count += waves->arbitrary_count;
1554 waves->names_count = alloc_count;
1555 waves->fw_codes = g_malloc0(alloc_count * sizeof(waves->fw_codes[0]));
1557 waves->names = g_malloc0(alloc_count * sizeof(waves->names[0]));
1558 if (!waves->names || !waves->fw_codes) {
1559 g_free(waves->names);
1560 g_free(waves->fw_codes);
1561 return SR_ERR_MALLOC;
1564 for (idx = 0; idx < waves->builtin_count; idx++) {
1566 name = g_strdup(waveform_names[idx]);
1567 waves->fw_codes[assign_idx] = code;
1568 waves->names[assign_idx] = name;
1571 for (idx = 0; idx < waves->arbitrary_count; idx++) {
1572 code = WAVE_ARB01 + idx;
1573 name = g_strdup_printf(WAVEFORM_ARB_NAME_FMT, idx + 1);
1574 waves->fw_codes[assign_idx] = code;
1575 waves->names[assign_idx] = name;
1578 waves->names[assign_idx] = NULL;
1581 * Populate internal channel configuration details from the
1582 * device's current state. Emit a series of queries which
1583 * update internal knowledge.
1585 * Implementation detail: Channel count is low, all parameters
1586 * are simple scalars. Communication cycles are few, while we
1587 * still are in the scan/probe phase and successfully verified
1588 * the device to respond. Disconnects and other exceptional
1589 * conditions are extremely unlikely. Not checking every getter
1590 * call's return value is acceptable here.
1593 ret |= jds6600_get_chans_enable(sdi);
1594 for (idx = 0; idx < device->channel_count_gen; idx++) {
1595 ret |= jds6600_get_waveform(sdi, idx);
1596 ret |= jds6600_get_frequency(sdi, idx);
1597 ret |= jds6600_get_amplitude(sdi, idx);
1598 ret |= jds6600_get_offset(sdi, idx);
1599 ret |= jds6600_get_dutycycle(sdi, idx);
1603 ret |= jds6600_get_phase_chans(sdi);
1607 #if WITH_ARBWAVE_DOWNLOAD
1609 * Development HACK, to see how waveform upload works.
1610 * How to forward the data to the application? Or the
1611 * sigrok session actually? Provide these as acquisition
1614 ret |= jds6600_get_arb_waveform(sdi, 13);
1617 ret |= jds6600_set_arb_waveform(sdi, 12);
1618 ret |= jds6600_set_arb_waveform(sdi, 13);