]> sigrok.org Git - libsigrok.git/blob - src/hardware/link-mso19/protocol.c
std_serial_dev_acquisition_stop(): Drop unneeded parameter.
[libsigrok.git] / src / hardware / link-mso19 / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
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>
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
22 #include <config.h>
23 #include "protocol.h"
24
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
33 extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
34
35 SR_PRIV int mso_send_control_message(struct sr_serial_dev_inst *serial,
36                                      uint16_t payload[], int n)
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
46         buf = g_malloc(s);
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++) {
53                 *(uint16_t *) p = g_htons(payload[i]);
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
74 SR_PRIV int mso_configure_trigger(const struct sr_dev_inst *sdi)
75 {
76         struct dev_context *devc = sdi->priv;
77         uint16_t threshold_value = mso_calc_raw_from_mv(devc);
78
79         threshold_value = 0x153C;
80         uint8_t trigger_config = 0;
81
82         if (devc->trigger_slope)
83                 trigger_config |= 0x04; //Trigger on falling edge
84
85         switch (devc->trigger_outsrc) {
86         case 1:
87                 trigger_config |= 0x00; //Trigger pulse output
88                 break;
89         case 2:
90                 trigger_config |= 0x08; //PWM DAC from the pattern generator buffer
91                 break;
92         case 3:
93                 trigger_config |= 0x18; //White noise
94                 break;
95         }
96
97         switch (devc->trigger_chan) {
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;
116
117         uint16_t ops[18];
118         ops[0] = mso_trans(3, threshold_value & 0xff);
119         //The trigger_config also holds the 2 MSB bits from the threshold value
120         ops[1] = mso_trans(4, trigger_config | ((threshold_value >> 8) & 0x03));
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,
127                            devc->dso_trigger_width /
128                            SR_HZ_TO_NS(devc->cur_rate));
129
130         /* Select the SPI/I2C trigger config bank */
131         ops[7] = mso_trans(REG_CTL2, (devc->ctlbase2 | BITS_CTL2_BANK(2)));
132         /* Configure the SPI/I2C protocol trigger */
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);
142         /* Select the default config bank */
143         ops[17] = mso_trans(REG_CTL2, devc->ctlbase2);
144
145         return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
146 }
147
148 SR_PRIV int mso_configure_threshold_level(const struct sr_dev_inst *sdi)
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
164 SR_PRIV int mso_arm(const struct sr_dev_inst *sdi)
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
189 SR_PRIV int mso_dac_out(const struct sr_dev_inst *sdi, uint16_t val)
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
202 SR_PRIV uint16_t mso_calc_raw_from_mv(struct dev_context *devc)
203 {
204         return (uint16_t) (0x200 -
205                            ((devc->dso_trigger_voltage / devc->dso_probe_attn) /
206                             devc->vbit));
207 }
208
209 SR_PRIV int mso_parse_serial(const char *iSerial, const char *iProduct,
210                              struct dev_context *devc)
211 {
212         unsigned int u1, u2, u3, u4, u5, u6;
213
214         (void)iProduct;
215
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; */
222
223         /* parse iSerial */
224         if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
225                                         &u1, &u2, &u3, &u4, &u5, &u6) != 6)
226                 return SR_ERR;
227         devc->hwmodel = u4;
228         devc->hwrev = u5;
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
248 SR_PRIV int mso_reset_adc(struct sr_dev_inst *sdi)
249 {
250         struct dev_context *devc = sdi->priv;
251         uint16_t ops[2];
252
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;
256
257         sr_dbg("Requesting ADC reset.");
258         return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
259 }
260
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];
265
266         devc->ctlbase1 |= BIT_CTL1_RESETFSM;
267         ops[0] = mso_trans(REG_CTL1, devc->ctlbase1);
268
269         sr_dbg("Requesting ADC reset.");
270         return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops));
271 }
272
273 SR_PRIV int mso_toggle_led(struct sr_dev_inst *sdi, int state)
274 {
275         struct dev_context *devc = sdi->priv;
276         uint16_t ops[1];
277
278         devc->ctlbase1 &= ~BIT_CTL1_LED;
279         if (state)
280                 devc->ctlbase1 |= BIT_CTL1_LED;
281         ops[0] = mso_trans(REG_CTL1, devc->ctlbase1);
282
283         sr_dbg("Requesting LED toggle.");
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 dev_context *devc;
290
291         devc = sdi->priv;
292         serial_source_remove(sdi->session, devc->serial);
293
294         std_session_send_df_end(sdi, LOG_PREFIX);
295 }
296
297 SR_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
308 SR_PRIV int mso_configure_rate(const struct sr_dev_inst *sdi, uint32_t rate)
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;
317                         ret = mso_clkrate_out(devc->serial, rate_map[i].val);
318                         if (ret == SR_OK)
319                                 devc->cur_rate = rate;
320                         return ret;
321                 }
322         }
323
324         if (ret != SR_OK)
325                 sr_err("Unsupported rate.");
326
327         return ret;
328 }
329
330 SR_PRIV int mso_check_trigger(struct sr_serial_dev_inst *serial, uint8_t *info)
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));
337         if (!info || ret != SR_OK)
338                 return ret;
339
340         uint8_t buf = 0;
341         if (serial_read(serial, &buf, 1) != 1)  /* FIXME: Need timeout */
342                 ret = SR_ERR;
343         if (!info)
344                 *info = buf;
345
346         sr_dbg("Trigger state is: 0x%x.", *info);
347         return ret;
348 }
349
350 SR_PRIV int mso_receive_data(int fd, int revents, void *cb_data)
351 {
352         struct sr_datafeed_packet packet;
353         struct sr_datafeed_logic logic;
354         struct sr_dev_inst *sdi;
355         GSList *l;
356         int i;
357
358         struct drv_context *drvc = di->context;
359
360         /* Find this device's devc struct by its fd. */
361         struct dev_context *devc = NULL;
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));
377
378         if (s <= 0)
379                 return FALSE;
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         }
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)
400                 return TRUE;
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) |
408                     ((devc->buffer[i * 3 + 1] & 0xf) << 6);
409                 (void)analog_out;
410                 logic_out[i] = ((devc->buffer[i * 3 + 1] & 0x30) >> 4) |
411                     ((devc->buffer[i * 3 + 2] & 0x3f) << 2);
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;
419         sr_session_send(sdi, &packet);
420
421         devc->num_samples += 1024;
422
423         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
424                 sr_info("Requested number of samples reached.");
425                 sdi->driver->dev_acquisition_stop(sdi);
426         }
427
428         return TRUE;
429 }
430
431 SR_PRIV int mso_configure_channels(const struct sr_dev_inst *sdi)
432 {
433         struct dev_context *devc;
434         struct sr_channel *ch;
435         GSList *l;
436         char *tc;
437
438         devc = sdi->priv;
439
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;
447
448         for (l = sdi->channels; l; l = l->next) {
449                 ch = (struct sr_channel *)l->data;
450                 if (ch->enabled == FALSE)
451                         continue;
452
453                 int channel_bit = 1 << (ch->index);
454                 if (!(ch->trigger))
455                         continue;
456
457                 devc->use_trigger = TRUE;
458                 //Configure trigger mask and value.
459                 for (tc = ch->trigger; *tc; tc++) {
460                         devc->la_trigger_mask &= ~channel_bit;
461                         if (*tc == '1')
462                                 devc->la_trigger |= channel_bit;
463                 }
464         }
465
466         return SR_OK;
467 }