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