]>
Commit | Line | Data |
---|---|---|
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 | ||
30 | static const char mso_head[] = { 0x40, 0x4c, 0x44, 0x53, 0x7e }; | |
31 | static const char mso_foot[] = { 0x7e }; | |
32 | ||
df92e5cf | 33 | extern SR_PRIV struct sr_dev_driver link_mso19_driver_info; |
df92e5cf | 34 | |
4db2aaff | 35 | SR_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; | |
68 | free: | |
69 | g_free(buf); | |
70 | ret: | |
71 | return ret; | |
72 | } | |
73 | ||
753d722f | 74 | SR_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 | 148 | SR_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 | ||
155 | SR_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 | 164 | SR_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 | ||
177 | SR_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 | 189 | SR_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 | 202 | SR_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 | 209 | SR_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 | 248 | SR_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 | 261 | SR_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 | 273 | SR_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 | ||
287 | SR_PRIV void stop_acquisition(const struct sr_dev_inst *sdi) | |
288 | { | |
289 | struct sr_datafeed_packet packet; | |
290 | struct dev_context *devc; | |
291 | ||
292 | devc = sdi->priv; | |
102f1239 | 293 | serial_source_remove(sdi->session, devc->serial); |
df92e5cf | 294 | |
295 | /* Terminate session */ | |
296 | packet.type = SR_DF_END; | |
297 | sr_session_send(sdi, &packet); | |
298 | } | |
299 | ||
300 | SR_PRIV int mso_clkrate_out(struct sr_serial_dev_inst *serial, uint16_t val) | |
301 | { | |
302 | uint16_t ops[] = { | |
303 | mso_trans(REG_CLKRATE1, (val >> 8) & 0xff), | |
304 | mso_trans(REG_CLKRATE2, val & 0xff), | |
305 | }; | |
306 | ||
307 | sr_dbg("Setting clkrate word to 0x%x.", val); | |
308 | return mso_send_control_message(serial, ARRAY_AND_SIZE(ops)); | |
309 | } | |
310 | ||
753d722f | 311 | SR_PRIV int mso_configure_rate(const struct sr_dev_inst *sdi, uint32_t rate) |
df92e5cf | 312 | { |
313 | struct dev_context *devc = sdi->priv; | |
314 | unsigned int i; | |
315 | int ret = SR_ERR; | |
316 | ||
317 | for (i = 0; i < ARRAY_SIZE(rate_map); i++) { | |
318 | if (rate_map[i].rate == rate) { | |
319 | devc->ctlbase2 = rate_map[i].slowmode; | |
4b719338 | 320 | ret = mso_clkrate_out(devc->serial, rate_map[i].val); |
df92e5cf | 321 | if (ret == SR_OK) |
322 | devc->cur_rate = rate; | |
323 | return ret; | |
324 | } | |
325 | } | |
4db2aaff | 326 | |
00b44ccb | 327 | if (ret != SR_OK) |
4db2aaff | 328 | sr_err("Unsupported rate."); |
329 | ||
df92e5cf | 330 | return ret; |
331 | } | |
332 | ||
365f04d6 | 333 | SR_PRIV int mso_check_trigger(struct sr_serial_dev_inst *serial, uint8_t *info) |
df92e5cf | 334 | { |
335 | uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) }; | |
336 | int ret; | |
337 | ||
338 | sr_dbg("Requesting trigger state."); | |
339 | ret = mso_send_control_message(serial, ARRAY_AND_SIZE(ops)); | |
98fec29e | 340 | if (!info || ret != SR_OK) |
df92e5cf | 341 | return ret; |
342 | ||
00b44ccb UH |
343 | uint8_t buf = 0; |
344 | if (serial_read(serial, &buf, 1) != 1) /* FIXME: Need timeout */ | |
df92e5cf | 345 | ret = SR_ERR; |
365f04d6 | 346 | if (!info) |
347 | *info = buf; | |
df92e5cf | 348 | |
349 | sr_dbg("Trigger state is: 0x%x.", *info); | |
350 | return ret; | |
351 | } | |
352 | ||
353 | SR_PRIV int mso_receive_data(int fd, int revents, void *cb_data) | |
354 | { | |
df92e5cf | 355 | struct sr_datafeed_packet packet; |
356 | struct sr_datafeed_logic logic; | |
357 | struct sr_dev_inst *sdi; | |
df92e5cf | 358 | GSList *l; |
4db2aaff | 359 | int i; |
df92e5cf | 360 | |
41812aca | 361 | struct drv_context *drvc = di->context; |
df92e5cf | 362 | |
363 | /* Find this device's devc struct by its fd. */ | |
4db2aaff | 364 | struct dev_context *devc = NULL; |
df92e5cf | 365 | for (l = drvc->instances; l; l = l->next) { |
366 | sdi = l->data; | |
367 | devc = sdi->priv; | |
368 | if (devc->serial->fd == fd) | |
369 | break; | |
370 | devc = NULL; | |
371 | } | |
372 | if (!devc) | |
373 | /* Shouldn't happen. */ | |
374 | return TRUE; | |
375 | ||
376 | (void)revents; | |
377 | ||
378 | uint8_t in[1024]; | |
379 | size_t s = serial_read(devc->serial, in, sizeof(in)); | |
5a24e89c | 380 | |
df92e5cf | 381 | if (s <= 0) |
382 | return FALSE; | |
00b44ccb UH |
383 | |
384 | /* Check if we triggered, then send a command that we are ready | |
385 | * to read the data */ | |
386 | if (devc->trigger_state != MSO_TRIGGER_DATAREADY) { | |
387 | devc->trigger_state = in[0]; | |
388 | if (devc->trigger_state == MSO_TRIGGER_DATAREADY) { | |
389 | mso_read_buffer(sdi); | |
390 | devc->buffer_n = 0; | |
391 | } else { | |
392 | mso_check_trigger(devc->serial, NULL); | |
393 | } | |
394 | return TRUE; | |
395 | } | |
df92e5cf | 396 | |
397 | /* the hardware always dumps 1024 samples, 24bits each */ | |
398 | if (devc->buffer_n < 3072) { | |
399 | memcpy(devc->buffer + devc->buffer_n, in, s); | |
400 | devc->buffer_n += s; | |
401 | } | |
402 | if (devc->buffer_n < 3072) | |
5a24e89c | 403 | return TRUE; |
df92e5cf | 404 | |
405 | /* do the conversion */ | |
406 | uint8_t logic_out[1024]; | |
407 | double analog_out[1024]; | |
408 | for (i = 0; i < 1024; i++) { | |
409 | /* FIXME: Need to do conversion to mV */ | |
410 | analog_out[i] = (devc->buffer[i * 3] & 0x3f) | | |
00b44ccb | 411 | ((devc->buffer[i * 3 + 1] & 0xf) << 6); |
ff08a52a | 412 | (void)analog_out; |
df92e5cf | 413 | logic_out[i] = ((devc->buffer[i * 3 + 1] & 0x30) >> 4) | |
00b44ccb | 414 | ((devc->buffer[i * 3 + 2] & 0x3f) << 2); |
df92e5cf | 415 | } |
416 | ||
417 | packet.type = SR_DF_LOGIC; | |
418 | packet.payload = &logic; | |
419 | logic.length = 1024; | |
420 | logic.unitsize = 1; | |
421 | logic.data = logic_out; | |
422 | sr_session_send(cb_data, &packet); | |
423 | ||
00b44ccb | 424 | devc->num_samples += 1024; |
eb913174 | 425 | |
00b44ccb UH |
426 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { |
427 | sr_info("Requested number of samples reached."); | |
428 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
429 | } | |
430 | ||
431 | return TRUE; | |
df92e5cf | 432 | } |
5a24e89c | 433 | |
ba7dd8bb | 434 | SR_PRIV int mso_configure_channels(const struct sr_dev_inst *sdi) |
5a24e89c | 435 | { |
5a24e89c | 436 | struct dev_context *devc; |
ba7dd8bb | 437 | struct sr_channel *ch; |
5a24e89c | 438 | GSList *l; |
5a24e89c | 439 | char *tc; |
440 | ||
5a24e89c | 441 | devc = sdi->priv; |
5a24e89c | 442 | |
00b44ccb UH |
443 | devc->la_trigger_mask = 0xFF; //the mask for the LA_TRIGGER (bits set to 0 matter, those set to 1 are ignored). |
444 | devc->la_trigger = 0x00; //The value of the LA byte that generates a trigger event (in that mode). | |
445 | devc->dso_trigger_voltage = 3; | |
446 | devc->dso_probe_attn = 1; | |
447 | devc->trigger_outsrc = 0; | |
448 | devc->trigger_chan = 3; //LA combination trigger | |
449 | devc->use_trigger = FALSE; | |
087a9161 | 450 | |
ba7dd8bb UH |
451 | for (l = sdi->channels; l; l = l->next) { |
452 | ch = (struct sr_channel *)l->data; | |
453 | if (ch->enabled == FALSE) | |
5a24e89c | 454 | continue; |
455 | ||
ba7dd8bb UH |
456 | int channel_bit = 1 << (ch->index); |
457 | if (!(ch->trigger)) | |
5a24e89c | 458 | continue; |
459 | ||
00b44ccb | 460 | devc->use_trigger = TRUE; |
5a24e89c | 461 | //Configure trigger mask and value. |
ba7dd8bb UH |
462 | for (tc = ch->trigger; *tc; tc++) { |
463 | devc->la_trigger_mask &= ~channel_bit; | |
00b44ccb | 464 | if (*tc == '1') |
ba7dd8bb | 465 | devc->la_trigger |= channel_bit; |
00b44ccb UH |
466 | } |
467 | } | |
5a24e89c | 468 | |
469 | return SR_OK; | |
5a24e89c | 470 | } |