2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <arpa/inet.h>
23 extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
24 static struct sr_dev_driver *di = &link_mso19_driver_info;
26 SR_PRIV int mso_send_control_message(struct sr_serial_dev_inst *serial,
27 uint16_t payload[], int n)
29 int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
37 if (!(buf = g_try_malloc(s))) {
38 sr_err("Failed to malloc message buffer.");
44 memcpy(p, mso_head, sizeof(mso_head));
45 p += sizeof(mso_head);
47 for (i = 0; i < n; i++) {
48 *(uint16_t *) p = htons(payload[i]);
51 memcpy(p, mso_foot, sizeof(mso_foot));
55 ret = serial_write(serial, buf + w, s - w);
70 SR_PRIV int mso_configure_trigger(struct sr_dev_inst *sdi)
72 struct dev_context *devc = sdi->priv;
73 uint16_t threshold_value = mso_calc_raw_from_mv(devc);
76 threshold_value = 0x153C;
77 uint8_t trigger_config = 0;
79 if (devc->trigger_slope)
80 trigger_config |= 0x04; //Trigger on falling edge
82 switch (devc->trigger_outsrc) {
84 trigger_config |= 0x00; //Trigger pulse output
87 trigger_config |= 0x08; //PWM DAC from the pattern generator buffer
90 trigger_config |= 0x18; //White noise
94 switch (devc->trigger_chan) {
96 trigger_config |= 0x00; //DSO level trigger //b00000000
99 trigger_config |= 0x20; //DSO level trigger & width < trigger_width
102 trigger_config |= 0x40; //DSO level trigger & width >= trigger_width
105 trigger_config |= 0x60; //LA combination trigger
109 //Last bit of trigger config reg 4 needs to be 1 for trigger enable,
110 //otherwise the trigger is not enabled
111 if (devc->use_trigger)
112 trigger_config |= 0x80;
115 ops[0] = mso_trans(3, threshold_value & 0xff);
116 //The trigger_config also holds the 2 MSB bits from the threshold value
117 ops[1] = mso_trans(4, trigger_config | (threshold_value >> 8) & 0x03);
118 ops[2] = mso_trans(5, devc->la_trigger);
119 ops[3] = mso_trans(6, devc->la_trigger_mask);
120 ops[4] = mso_trans(7, devc->trigger_holdoff[0]);
121 ops[5] = mso_trans(8, devc->trigger_holdoff[1]);
123 ops[6] = mso_trans(11,
124 devc->dso_trigger_width / SR_HZ_TO_NS(devc->cur_rate));
126 /* Select the SPI/I2C trigger config bank */
127 ops[7] = mso_trans(REG_CTL2, (devc->ctlbase2 | BITS_CTL2_BANK(2)));
128 /* Configure the SPI/I2C protocol trigger */
129 ops[8] = mso_trans(REG_PT_WORD(0), devc->protocol_trigger.word[0]);
130 ops[9] = mso_trans(REG_PT_WORD(1), devc->protocol_trigger.word[1]);
131 ops[10] = mso_trans(REG_PT_WORD(2), devc->protocol_trigger.word[2]);
132 ops[11] = mso_trans(REG_PT_WORD(3), devc->protocol_trigger.word[3]);
133 ops[12] = mso_trans(REG_PT_MASK(0), devc->protocol_trigger.mask[0]);
134 ops[13] = mso_trans(REG_PT_MASK(1), devc->protocol_trigger.mask[1]);
135 ops[14] = mso_trans(REG_PT_MASK(2), devc->protocol_trigger.mask[2]);
136 ops[15] = mso_trans(REG_PT_MASK(3), devc->protocol_trigger.mask[3]);
137 ops[16] = mso_trans(REG_PT_SPIMODE, devc->protocol_trigger.spimode);
138 /* Select the default config bank */
139 ops[17] = mso_trans(REG_CTL2, devc->ctlbase2);
141 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
144 SR_PRIV int mso_configure_threshold_level(struct sr_dev_inst *sdi)
146 struct dev_context *devc = sdi->priv;
148 return mso_dac_out(sdi, la_threshold_map[devc->la_threshold]);
151 SR_PRIV int mso_read_buffer(struct sr_dev_inst *sdi)
153 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
154 struct dev_context *devc = sdi->priv;
156 sr_dbg("Requesting buffer dump.");
157 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
160 SR_PRIV int mso_arm(struct sr_dev_inst *sdi)
162 struct dev_context *devc = sdi->priv;
164 mso_trans(REG_CTL1, devc->ctlbase1 | BIT_CTL1_RESETFSM),
165 mso_trans(REG_CTL1, devc->ctlbase1 | BIT_CTL1_ARM),
166 mso_trans(REG_CTL1, devc->ctlbase1),
169 sr_dbg("Requesting trigger arm.");
170 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
173 SR_PRIV int mso_force_capture(struct sr_dev_inst *sdi)
175 struct dev_context *devc = sdi->priv;
177 mso_trans(REG_CTL1, devc->ctlbase1 | 8),
178 mso_trans(REG_CTL1, devc->ctlbase1),
181 sr_dbg("Requesting forced capture.");
182 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
185 SR_PRIV int mso_dac_out(struct sr_dev_inst *sdi, uint16_t val)
187 struct dev_context *devc = sdi->priv;
189 mso_trans(REG_DAC1, (val >> 8) & 0xff),
190 mso_trans(REG_DAC2, val & 0xff),
191 mso_trans(REG_CTL1, devc->ctlbase1 | BIT_CTL1_RESETADC),
194 sr_dbg("Setting dac word to 0x%x.", val);
195 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
198 SR_PRIV inline uint16_t mso_calc_raw_from_mv(struct dev_context *devc)
200 return (uint16_t) (0x200 -
201 ((devc->dso_trigger_voltage / devc->dso_probe_attn) /
206 SR_PRIV int mso_parse_serial(const char *iSerial, const char *iProduct,
207 struct dev_context *devc)
209 unsigned int u1, u2, u3, u4, u5, u6;
212 /* FIXME: This code is in the original app, but I think its
213 * used only for the GUI */
214 /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
215 devc->num_sample_rates = 0x16;
217 devc->num_sample_rates = 0x10; */
221 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
222 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
226 devc->vbit = u1 / 10000;
228 devc->vbit = 4.19195;
229 devc->dac_offset = u2;
230 if (devc->dac_offset == 0)
231 devc->dac_offset = 0x1ff;
232 devc->offset_range = u3;
233 if (devc->offset_range == 0)
234 devc->offset_range = 0x17d;
237 * FIXME: There is more code on the original software to handle
238 * bigger iSerial strings, but as I can't test on my device
239 * I will not implement it yet
245 SR_PRIV int mso_reset_adc(struct sr_dev_inst *sdi)
247 struct dev_context *devc = sdi->priv;
250 ops[0] = mso_trans(REG_CTL1, (devc->ctlbase1 | BIT_CTL1_RESETADC));
251 ops[1] = mso_trans(REG_CTL1, devc->ctlbase1);
252 devc->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4;
254 sr_dbg("Requesting ADC reset.");
255 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
258 SR_PRIV int mso_reset_fsm(struct sr_dev_inst *sdi)
260 struct dev_context *devc = sdi->priv;
263 devc->ctlbase1 |= BIT_CTL1_RESETFSM;
264 ops[0] = mso_trans(REG_CTL1, devc->ctlbase1);
266 sr_dbg("Requesting ADC reset.");
267 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
270 SR_PRIV int mso_toggle_led(struct sr_dev_inst *sdi, int state)
272 struct dev_context *devc = sdi->priv;
275 devc->ctlbase1 &= ~BIT_CTL1_LED;
277 devc->ctlbase1 |= BIT_CTL1_LED;
278 ops[0] = mso_trans(REG_CTL1, devc->ctlbase1);
280 sr_dbg("Requesting LED toggle.");
281 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
284 SR_PRIV void stop_acquisition(const struct sr_dev_inst *sdi)
286 struct sr_datafeed_packet packet;
287 struct dev_context *devc;
290 sr_source_remove(devc->serial->fd);
292 /* Terminate session */
293 packet.type = SR_DF_END;
294 sr_session_send(sdi, &packet);
297 SR_PRIV int mso_clkrate_out(struct sr_serial_dev_inst *serial, uint16_t val)
300 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
301 mso_trans(REG_CLKRATE2, val & 0xff),
304 sr_dbg("Setting clkrate word to 0x%x.", val);
305 return mso_send_control_message(serial, ARRAY_AND_SIZE(ops));
308 SR_PRIV int mso_configure_rate(struct sr_dev_inst *sdi, uint32_t rate)
310 struct dev_context *devc = sdi->priv;
314 for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
315 if (rate_map[i].rate == rate) {
316 devc->ctlbase2 = rate_map[i].slowmode;
317 ret = mso_clkrate_out(devc->serial, rate_map[i].val);
319 devc->cur_rate = rate;
325 sr_err("Unsupported rate.");
334 SR_PRIV int mso_check_trigger(struct sr_serial_dev_inst *serial, uint8_t *info)
336 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
339 sr_dbg("Requesting trigger state.");
340 ret = mso_send_control_message(serial, ARRAY_AND_SIZE(ops));
341 if (info == NULL || ret != SR_OK)
346 if (serial_read(serial, &buf, 1) != 1) /* FIXME: Need timeout */
350 sr_dbg("Trigger state is: 0x%x.", *info);
354 SR_PRIV int mso_receive_data(int fd, int revents, void *cb_data)
357 struct sr_datafeed_packet packet;
358 struct sr_datafeed_logic logic;
359 struct sr_dev_inst *sdi;
363 struct drv_context *drvc = di->priv;
365 /* Find this device's devc struct by its fd. */
366 struct dev_context *devc = NULL;
367 for (l = drvc->instances; l; l = l->next) {
370 if (devc->serial->fd == fd)
375 /* Shouldn't happen. */
381 size_t s = serial_read(devc->serial, in, sizeof(in));
386 /* Check if we triggered, then send a command that we are ready
387 * to read the data */
388 if (devc->trigger_state != MSO_TRIGGER_DATAREADY) {
389 devc->trigger_state = in[0];
390 //printf("Got %c for trigger \n", in[0]);
391 if (devc->trigger_state == MSO_TRIGGER_DATAREADY) {
392 //printf("Trigger is ready %c\n", MSO_TRIGGER_DATAREADY);
393 mso_read_buffer(sdi);
396 mso_check_trigger(devc->serial, NULL);
401 /* the hardware always dumps 1024 samples, 24bits each */
402 if (devc->buffer_n < 3072) {
403 memcpy(devc->buffer + devc->buffer_n, in, s);
406 if (devc->buffer_n < 3072)
409 printf("Got samples, write out the data\n");
410 /* do the conversion */
411 uint8_t logic_out[1024];
412 double analog_out[1024];
413 for (i = 0; i < 1024; i++) {
414 /* FIXME: Need to do conversion to mV */
415 analog_out[i] = (devc->buffer[i * 3] & 0x3f) |
416 ((devc->buffer[i * 3 + 1] & 0xf) << 6);
417 logic_out[i] = ((devc->buffer[i * 3 + 1] & 0x30) >> 4) |
418 ((devc->buffer[i * 3 + 2] & 0x3f) << 2);
421 packet.type = SR_DF_LOGIC;
422 packet.payload = &logic;
425 logic.data = logic_out;
426 printf("Send Data\n");
427 sr_session_send(cb_data, &packet);
429 // Dont bother fixing this yet, keep it "old style"
431 packet.type = SR_DF_ANALOG;
432 packet.length = 1024;
433 packet.unitsize = sizeof(double);
434 packet.payload = analog_out;
435 sr_session_send(ctx->session_dev_id, &packet);
438 //printf("Send END\n");
439 //packet.type = SR_DF_END;
440 //sr_session_send(devc->session_dev_id, &packet);
442 // serial_flush(devc->serial);
443 // abort_acquisition(sdi);
444 // serial_close(devc->serial);
451 SR_PRIV int mso_configure_probes(const struct sr_dev_inst *sdi)
453 struct dev_context *devc;
454 struct sr_probe *probe;
456 int probe_bit, stage, i;
462 devc->la_trigger_mask = 0xFF; //the mask for the LA_TRIGGER (bits set to 0 matter, those set to 1 are ignored).
463 devc->la_trigger = 0x00; //The value of the LA byte that generates a trigger event (in that mode).
464 devc->dso_trigger_voltage = 3;
465 devc->dso_probe_attn = 1;
466 devc->trigger_outsrc = 0;
467 devc->trigger_chan = 3; //LA combination trigger
468 devc->use_trigger = FALSE;
470 for (l = sdi->probes; l; l = l->next) {
471 probe = (struct sr_probe *)l->data;
472 if (probe->enabled == FALSE)
475 int probe_bit = 1 << (probe->index);
476 if (!(probe->trigger))
479 devc->use_trigger = TRUE;
480 //Configure trigger mask and value.
481 for (tc = probe->trigger; *tc; tc++) {
482 devc->la_trigger_mask &= ~probe_bit;
484 devc->la_trigger |= probe_bit;