]> sigrok.org Git - libsigrok.git/blame - src/hardware/link-mso19/protocol.c
std_serial_dev_acquisition_stop(): Drop unneeded parameter.
[libsigrok.git] / src / hardware / link-mso19 / protocol.c
CommitLineData
df92e5cf 1/*
50985c20 2 * This file is part of the libsigrok project.
df92e5cf 3 *
f48cef78
UH
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5 * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt>
6 * Copyright (C) 2013 Lior Elazary <lelazary@yahoo.com>
df92e5cf 7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
6ec6c43b 22#include <config.h>
df92e5cf 23#include "protocol.h"
24
753d722f
UH
25/* serial protocol */
26#define mso_trans(a, v) \
27 (((v) & 0x3f) | (((v) & 0xc0) << 6) | (((a) & 0xf) << 8) | \
28 ((~(v) & 0x20) << 1) | ((~(v) & 0x80) << 7))
29
30static const char mso_head[] = { 0x40, 0x4c, 0x44, 0x53, 0x7e };
31static const char mso_foot[] = { 0x7e };
32
df92e5cf 33extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
df92e5cf 34
4db2aaff 35SR_PRIV int mso_send_control_message(struct sr_serial_dev_inst *serial,
00b44ccb 36 uint16_t payload[], int n)
4db2aaff 37{
38 int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
39 char *p, *buf;
40
41 ret = SR_ERR;
42
43 if (serial->fd < 0)
44 goto ret;
45
a95f142e 46 buf = g_malloc(s);
4db2aaff 47
48 p = buf;
49 memcpy(p, mso_head, sizeof(mso_head));
50 p += sizeof(mso_head);
51
52 for (i = 0; i < n; i++) {
a24e9d04 53 *(uint16_t *) p = g_htons(payload[i]);
4db2aaff 54 p += 2;
55 }
56 memcpy(p, mso_foot, sizeof(mso_foot));
57
58 w = 0;
59 while (w < s) {
60 ret = serial_write(serial, buf + w, s - w);
61 if (ret < 0) {
62 ret = SR_ERR;
63 goto free;
64 }
65 w += ret;
66 }
67 ret = SR_OK;
68free:
69 g_free(buf);
70ret:
71 return ret;
72}
73
753d722f 74SR_PRIV int mso_configure_trigger(const struct sr_dev_inst *sdi)
4b719338 75{
76 struct dev_context *devc = sdi->priv;
087a9161 77 uint16_t threshold_value = mso_calc_raw_from_mv(devc);
4b719338 78
00b44ccb
UH
79 threshold_value = 0x153C;
80 uint8_t trigger_config = 0;
4b719338 81
00b44ccb
UH
82 if (devc->trigger_slope)
83 trigger_config |= 0x04; //Trigger on falling edge
4b719338 84
85 switch (devc->trigger_outsrc) {
86 case 1:
00b44ccb 87 trigger_config |= 0x00; //Trigger pulse output
4b719338 88 break;
89 case 2:
00b44ccb 90 trigger_config |= 0x08; //PWM DAC from the pattern generator buffer
4b719338 91 break;
92 case 3:
00b44ccb 93 trigger_config |= 0x18; //White noise
4b719338 94 break;
4b719338 95 }
96
087a9161 97 switch (devc->trigger_chan) {
00b44ccb
UH
98 case 0:
99 trigger_config |= 0x00; //DSO level trigger //b00000000
100 break;
101 case 1:
102 trigger_config |= 0x20; //DSO level trigger & width < trigger_width
103 break;
104 case 2:
105 trigger_config |= 0x40; //DSO level trigger & width >= trigger_width
106 break;
107 case 3:
108 trigger_config |= 0x60; //LA combination trigger
109 break;
110 }
111
112 //Last bit of trigger config reg 4 needs to be 1 for trigger enable,
113 //otherwise the trigger is not enabled
114 if (devc->use_trigger)
115 trigger_config |= 0x80;
087a9161 116
117 uint16_t ops[18];
118 ops[0] = mso_trans(3, threshold_value & 0xff);
00b44ccb 119 //The trigger_config also holds the 2 MSB bits from the threshold value
5952553f 120 ops[1] = mso_trans(4, trigger_config | ((threshold_value >> 8) & 0x03));
087a9161 121 ops[2] = mso_trans(5, devc->la_trigger);
122 ops[3] = mso_trans(6, devc->la_trigger_mask);
123 ops[4] = mso_trans(7, devc->trigger_holdoff[0]);
124 ops[5] = mso_trans(8, devc->trigger_holdoff[1]);
125
126 ops[6] = mso_trans(11,
00b44ccb
UH
127 devc->dso_trigger_width /
128 SR_HZ_TO_NS(devc->cur_rate));
4b719338 129
130 /* Select the SPI/I2C trigger config bank */
087a9161 131 ops[7] = mso_trans(REG_CTL2, (devc->ctlbase2 | BITS_CTL2_BANK(2)));
4b719338 132 /* Configure the SPI/I2C protocol trigger */
087a9161 133 ops[8] = mso_trans(REG_PT_WORD(0), devc->protocol_trigger.word[0]);
134 ops[9] = mso_trans(REG_PT_WORD(1), devc->protocol_trigger.word[1]);
135 ops[10] = mso_trans(REG_PT_WORD(2), devc->protocol_trigger.word[2]);
136 ops[11] = mso_trans(REG_PT_WORD(3), devc->protocol_trigger.word[3]);
137 ops[12] = mso_trans(REG_PT_MASK(0), devc->protocol_trigger.mask[0]);
138 ops[13] = mso_trans(REG_PT_MASK(1), devc->protocol_trigger.mask[1]);
139 ops[14] = mso_trans(REG_PT_MASK(2), devc->protocol_trigger.mask[2]);
140 ops[15] = mso_trans(REG_PT_MASK(3), devc->protocol_trigger.mask[3]);
141 ops[16] = mso_trans(REG_PT_SPIMODE, devc->protocol_trigger.spimode);
4b719338 142 /* Select the default config bank */
087a9161 143 ops[17] = mso_trans(REG_CTL2, devc->ctlbase2);
4b719338 144
4db2aaff 145 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
4b719338 146}
147
753d722f 148SR_PRIV int mso_configure_threshold_level(const struct sr_dev_inst *sdi)
4b719338 149{
150 struct dev_context *devc = sdi->priv;
151
152 return mso_dac_out(sdi, la_threshold_map[devc->la_threshold]);
153}
154
155SR_PRIV int mso_read_buffer(struct sr_dev_inst *sdi)
156{
157 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
158 struct dev_context *devc = sdi->priv;
159
160 sr_dbg("Requesting buffer dump.");
161 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
162}
163
753d722f 164SR_PRIV int mso_arm(const struct sr_dev_inst *sdi)
4b719338 165{
166 struct dev_context *devc = sdi->priv;
167 uint16_t ops[] = {
168 mso_trans(REG_CTL1, devc->ctlbase1 | BIT_CTL1_RESETFSM),
169 mso_trans(REG_CTL1, devc->ctlbase1 | BIT_CTL1_ARM),
170 mso_trans(REG_CTL1, devc->ctlbase1),
171 };
172
173 sr_dbg("Requesting trigger arm.");
174 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
175}
176
177SR_PRIV int mso_force_capture(struct sr_dev_inst *sdi)
178{
179 struct dev_context *devc = sdi->priv;
180 uint16_t ops[] = {
181 mso_trans(REG_CTL1, devc->ctlbase1 | 8),
182 mso_trans(REG_CTL1, devc->ctlbase1),
183 };
184
185 sr_dbg("Requesting forced capture.");
186 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
187}
188
753d722f 189SR_PRIV int mso_dac_out(const struct sr_dev_inst *sdi, uint16_t val)
4b719338 190{
191 struct dev_context *devc = sdi->priv;
192 uint16_t ops[] = {
193 mso_trans(REG_DAC1, (val >> 8) & 0xff),
194 mso_trans(REG_DAC2, val & 0xff),
195 mso_trans(REG_CTL1, devc->ctlbase1 | BIT_CTL1_RESETADC),
196 };
197
198 sr_dbg("Setting dac word to 0x%x.", val);
199 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
200}
201
c442ffda 202SR_PRIV uint16_t mso_calc_raw_from_mv(struct dev_context *devc)
4b719338 203{
204 return (uint16_t) (0x200 -
00b44ccb
UH
205 ((devc->dso_trigger_voltage / devc->dso_probe_attn) /
206 devc->vbit));
4b719338 207}
208
df92e5cf 209SR_PRIV int mso_parse_serial(const char *iSerial, const char *iProduct,
00b44ccb 210 struct dev_context *devc)
df92e5cf 211{
212 unsigned int u1, u2, u3, u4, u5, u6;
213
31e53772
UH
214 (void)iProduct;
215
00b44ccb
UH
216 /* FIXME: This code is in the original app, but I think its
217 * used only for the GUI */
218 /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
219 devc->num_sample_rates = 0x16;
220 else
221 devc->num_sample_rates = 0x10; */
df92e5cf 222
223 /* parse iSerial */
224 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
00b44ccb 225 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
df92e5cf 226 return SR_ERR;
227 devc->hwmodel = u4;
228 devc->hwrev = u5;
df92e5cf 229 devc->vbit = u1 / 10000;
230 if (devc->vbit == 0)
231 devc->vbit = 4.19195;
232 devc->dac_offset = u2;
233 if (devc->dac_offset == 0)
234 devc->dac_offset = 0x1ff;
235 devc->offset_range = u3;
236 if (devc->offset_range == 0)
237 devc->offset_range = 0x17d;
238
239 /*
240 * FIXME: There is more code on the original software to handle
241 * bigger iSerial strings, but as I can't test on my device
242 * I will not implement it yet
243 */
244
245 return SR_OK;
246}
247
4db2aaff 248SR_PRIV int mso_reset_adc(struct sr_dev_inst *sdi)
df92e5cf 249{
4db2aaff 250 struct dev_context *devc = sdi->priv;
251 uint16_t ops[2];
df92e5cf 252
4db2aaff 253 ops[0] = mso_trans(REG_CTL1, (devc->ctlbase1 | BIT_CTL1_RESETADC));
254 ops[1] = mso_trans(REG_CTL1, devc->ctlbase1);
255 devc->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4;
df92e5cf 256
4db2aaff 257 sr_dbg("Requesting ADC reset.");
258 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
259}
df92e5cf 260
4db2aaff 261SR_PRIV int mso_reset_fsm(struct sr_dev_inst *sdi)
262{
263 struct dev_context *devc = sdi->priv;
264 uint16_t ops[1];
df92e5cf 265
4db2aaff 266 devc->ctlbase1 |= BIT_CTL1_RESETFSM;
267 ops[0] = mso_trans(REG_CTL1, devc->ctlbase1);
df92e5cf 268
4db2aaff 269 sr_dbg("Requesting ADC reset.");
270 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
df92e5cf 271}
272
4db2aaff 273SR_PRIV int mso_toggle_led(struct sr_dev_inst *sdi, int state)
df92e5cf 274{
275 struct dev_context *devc = sdi->priv;
4db2aaff 276 uint16_t ops[1];
df92e5cf 277
4db2aaff 278 devc->ctlbase1 &= ~BIT_CTL1_LED;
279 if (state)
280 devc->ctlbase1 |= BIT_CTL1_LED;
281 ops[0] = mso_trans(REG_CTL1, devc->ctlbase1);
df92e5cf 282
4db2aaff 283 sr_dbg("Requesting LED toggle.");
df92e5cf 284 return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
285}
286
287SR_PRIV void stop_acquisition(const struct sr_dev_inst *sdi)
288{
df92e5cf 289 struct dev_context *devc;
290
291 devc = sdi->priv;
102f1239 292 serial_source_remove(sdi->session, devc->serial);
df92e5cf 293
3be42bc2 294 std_session_send_df_end(sdi, LOG_PREFIX);
df92e5cf 295}
296
297SR_PRIV int mso_clkrate_out(struct sr_serial_dev_inst *serial, uint16_t val)
298{
299 uint16_t ops[] = {
300 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
301 mso_trans(REG_CLKRATE2, val & 0xff),
302 };
303
304 sr_dbg("Setting clkrate word to 0x%x.", val);
305 return mso_send_control_message(serial, ARRAY_AND_SIZE(ops));
306}
307
753d722f 308SR_PRIV int mso_configure_rate(const struct sr_dev_inst *sdi, uint32_t rate)
df92e5cf 309{
310 struct dev_context *devc = sdi->priv;
311 unsigned int i;
312 int ret = SR_ERR;
313
314 for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
315 if (rate_map[i].rate == rate) {
316 devc->ctlbase2 = rate_map[i].slowmode;
4b719338 317 ret = mso_clkrate_out(devc->serial, rate_map[i].val);
df92e5cf 318 if (ret == SR_OK)
319 devc->cur_rate = rate;
320 return ret;
321 }
322 }
4db2aaff 323
00b44ccb 324 if (ret != SR_OK)
4db2aaff 325 sr_err("Unsupported rate.");
326
df92e5cf 327 return ret;
328}
329
365f04d6 330SR_PRIV int mso_check_trigger(struct sr_serial_dev_inst *serial, uint8_t *info)
df92e5cf 331{
332 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
333 int ret;
334
335 sr_dbg("Requesting trigger state.");
336 ret = mso_send_control_message(serial, ARRAY_AND_SIZE(ops));
98fec29e 337 if (!info || ret != SR_OK)
df92e5cf 338 return ret;
339
00b44ccb
UH
340 uint8_t buf = 0;
341 if (serial_read(serial, &buf, 1) != 1) /* FIXME: Need timeout */
df92e5cf 342 ret = SR_ERR;
365f04d6 343 if (!info)
344 *info = buf;
df92e5cf 345
346 sr_dbg("Trigger state is: 0x%x.", *info);
347 return ret;
348}
349
350SR_PRIV int mso_receive_data(int fd, int revents, void *cb_data)
351{
df92e5cf 352 struct sr_datafeed_packet packet;
353 struct sr_datafeed_logic logic;
354 struct sr_dev_inst *sdi;
df92e5cf 355 GSList *l;
4db2aaff 356 int i;
df92e5cf 357
41812aca 358 struct drv_context *drvc = di->context;
df92e5cf 359
360 /* Find this device's devc struct by its fd. */
4db2aaff 361 struct dev_context *devc = NULL;
df92e5cf 362 for (l = drvc->instances; l; l = l->next) {
363 sdi = l->data;
364 devc = sdi->priv;
365 if (devc->serial->fd == fd)
366 break;
367 devc = NULL;
368 }
369 if (!devc)
370 /* Shouldn't happen. */
371 return TRUE;
372
373 (void)revents;
374
375 uint8_t in[1024];
376 size_t s = serial_read(devc->serial, in, sizeof(in));
5a24e89c 377
df92e5cf 378 if (s <= 0)
379 return FALSE;
00b44ccb
UH
380
381 /* Check if we triggered, then send a command that we are ready
382 * to read the data */
383 if (devc->trigger_state != MSO_TRIGGER_DATAREADY) {
384 devc->trigger_state = in[0];
385 if (devc->trigger_state == MSO_TRIGGER_DATAREADY) {
386 mso_read_buffer(sdi);
387 devc->buffer_n = 0;
388 } else {
389 mso_check_trigger(devc->serial, NULL);
390 }
391 return TRUE;
392 }
df92e5cf 393
394 /* the hardware always dumps 1024 samples, 24bits each */
395 if (devc->buffer_n < 3072) {
396 memcpy(devc->buffer + devc->buffer_n, in, s);
397 devc->buffer_n += s;
398 }
399 if (devc->buffer_n < 3072)
5a24e89c 400 return TRUE;
df92e5cf 401
402 /* do the conversion */
403 uint8_t logic_out[1024];
404 double analog_out[1024];
405 for (i = 0; i < 1024; i++) {
406 /* FIXME: Need to do conversion to mV */
407 analog_out[i] = (devc->buffer[i * 3] & 0x3f) |
00b44ccb 408 ((devc->buffer[i * 3 + 1] & 0xf) << 6);
ff08a52a 409 (void)analog_out;
df92e5cf 410 logic_out[i] = ((devc->buffer[i * 3 + 1] & 0x30) >> 4) |
00b44ccb 411 ((devc->buffer[i * 3 + 2] & 0x3f) << 2);
df92e5cf 412 }
413
414 packet.type = SR_DF_LOGIC;
415 packet.payload = &logic;
416 logic.length = 1024;
417 logic.unitsize = 1;
418 logic.data = logic_out;
695dc859 419 sr_session_send(sdi, &packet);
df92e5cf 420
00b44ccb 421 devc->num_samples += 1024;
eb913174 422
00b44ccb
UH
423 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
424 sr_info("Requested number of samples reached.");
6525d819 425 sdi->driver->dev_acquisition_stop(sdi);
00b44ccb
UH
426 }
427
428 return TRUE;
df92e5cf 429}
5a24e89c 430
ba7dd8bb 431SR_PRIV int mso_configure_channels(const struct sr_dev_inst *sdi)
5a24e89c 432{
5a24e89c 433 struct dev_context *devc;
ba7dd8bb 434 struct sr_channel *ch;
5a24e89c 435 GSList *l;
5a24e89c 436 char *tc;
437
5a24e89c 438 devc = sdi->priv;
5a24e89c 439
00b44ccb
UH
440 devc->la_trigger_mask = 0xFF; //the mask for the LA_TRIGGER (bits set to 0 matter, those set to 1 are ignored).
441 devc->la_trigger = 0x00; //The value of the LA byte that generates a trigger event (in that mode).
442 devc->dso_trigger_voltage = 3;
443 devc->dso_probe_attn = 1;
444 devc->trigger_outsrc = 0;
445 devc->trigger_chan = 3; //LA combination trigger
446 devc->use_trigger = FALSE;
087a9161 447
ba7dd8bb
UH
448 for (l = sdi->channels; l; l = l->next) {
449 ch = (struct sr_channel *)l->data;
450 if (ch->enabled == FALSE)
5a24e89c 451 continue;
452
ba7dd8bb
UH
453 int channel_bit = 1 << (ch->index);
454 if (!(ch->trigger))
5a24e89c 455 continue;
456
00b44ccb 457 devc->use_trigger = TRUE;
5a24e89c 458 //Configure trigger mask and value.
ba7dd8bb
UH
459 for (tc = ch->trigger; *tc; tc++) {
460 devc->la_trigger_mask &= ~channel_bit;
00b44ccb 461 if (*tc == '1')
ba7dd8bb 462 devc->la_trigger |= channel_bit;
00b44ccb
UH
463 }
464 }
5a24e89c 465
466 return SR_OK;
5a24e89c 467}