]> sigrok.org Git - libsigrok.git/blob - src/hardware/ipdbg-logic-analyser/protocol.c
ipdbg-la: Add changes for IPDBG project
[libsigrok.git] / src / hardware / ipdbg-logic-analyser / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2016 danselmi <da@da>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21 #ifdef _WIN32
22 #define _WIN32_WINNT 0x0501
23 #include <winsock2.h>
24 #include <ws2tcpip.h>
25 #endif
26 //#include <glib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #ifndef _WIN32
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <netdb.h>
34 #endif
35 #include <errno.h>
36 #include "protocol.h"
37
38 #define BUFFER_SIZE 4
39
40
41 #define Start                      0xFE
42 #define reset                      0xAB
43 #define IPDBG_LA_ID                0xBB
44 #define Escape                     0x55
45
46
47 /* Command opcodes */
48 #define set_trigger                0x00
49 #define Trigger                    0xF0
50 #define LA                         0x0F
51 #define Masks                      0xF1
52 #define Mask                       0xF3
53 #define Value                      0xF7
54 #define Last_Masks                 0xF9
55 #define Mask_last                  0xFB
56 #define Value_last                 0xFF
57 #define delay                      0x1F
58 #define K_Mauslesen                0xAA
59
60
61 SR_PRIV int sendEscaping(struct ipdbg_org_la_tcp *tcp, char *dataToSend, int length);
62
63 SR_PRIV struct ipdbg_org_la_tcp *ipdbg_org_la_new_tcp(void)
64 {
65     struct ipdbg_org_la_tcp *tcp;
66
67     tcp = g_malloc0(sizeof(struct ipdbg_org_la_tcp));
68
69     tcp->address = NULL;
70     tcp->port = NULL;
71     tcp->socket = -1;
72
73     return tcp;
74 }
75
76 SR_PRIV int ipdbg_org_la_tcp_open(struct ipdbg_org_la_tcp *tcp)
77 {
78         struct addrinfo hints;
79         struct addrinfo *results, *res;
80         int err;
81
82         memset(&hints, 0, sizeof(hints));
83         hints.ai_family = AF_UNSPEC;
84         hints.ai_socktype = SOCK_STREAM;
85         hints.ai_protocol = IPPROTO_TCP;
86
87         err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
88
89         if (err) {
90                 sr_err("Address lookup failed: %s:%s: %s", tcp->address, tcp->port,
91                         gai_strerror(err));
92                 return SR_ERR;
93         }
94
95         for (res = results; res; res = res->ai_next) {
96                 if ((tcp->socket = socket(res->ai_family, res->ai_socktype,
97                                                 res->ai_protocol)) < 0)
98                         continue;
99                 if (connect(tcp->socket, res->ai_addr, res->ai_addrlen) != 0) {
100                         close(tcp->socket);
101                         tcp->socket = -1;
102                         continue;
103                 }
104                 break;
105         }
106
107         freeaddrinfo(results);
108
109         if (tcp->socket < 0) {
110                 sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
111                                 g_strerror(errno));
112                 return SR_ERR;
113         }
114
115         return SR_OK;
116 }
117
118 SR_PRIV int ipdbg_org_la_tcp_send(struct ipdbg_org_la_tcp *tcp, const uint8_t *buf, size_t len)
119 {
120         int out;
121
122         out = send(tcp->socket, buf, len, 0);
123
124         if (out < 0) {
125                 sr_err("Send error: %s", g_strerror(errno));
126                 return SR_ERR;
127         }
128
129         if ((unsigned int)out < len) {
130                 sr_dbg("Only sent %d/%d bytes of data.", out, (int)len);
131         }
132
133         return SR_OK;
134 }
135
136 SR_PRIV int ipdbg_org_la_tcp_receive(struct ipdbg_org_la_tcp *tcp, uint8_t *buf, int bufsize)
137 {
138     int received = 0;
139
140     while(received < bufsize)
141     {
142
143         int len;
144
145         len = recv(tcp->socket, buf+received, bufsize-received, 0);
146
147         if (len < 0) {
148             sr_err("Receive error: %s", g_strerror(errno));
149             return SR_ERR;
150         }
151         else
152         {
153             received += len;
154         }
155     }
156
157         return received;
158 }
159
160 SR_PRIV int ipdbg_org_la_tcp_close(struct ipdbg_org_la_tcp *tcp)
161 {
162     int ret = SR_ERR;
163         if (close(tcp->socket) >= 0)
164         ret = SR_OK;
165
166     tcp->socket = -1;
167
168     return ret;
169 }
170
171 SR_PRIV void ipdbg_org_la_tcp_free(struct ipdbg_org_la_tcp *tcp)
172 {
173         g_free(tcp->address);
174         g_free(tcp->port);
175 }
176
177 SR_PRIV int ipdbg_org_la_convert_trigger(const struct sr_dev_inst *sdi)
178 {
179     struct ipdbg_org_la_dev_context *devc;
180     struct sr_trigger *trigger;
181     struct sr_trigger_stage *stage;
182     struct sr_trigger_match *match;
183     const GSList *l, *m;
184
185     devc = sdi->priv;
186
187     devc->num_stages = 0;
188     devc->num_transfers = 0;
189     devc->raw_sample_buf = NULL; /// name convert_trigger to init acquisition...
190     for (int i = 0; i < devc->DATA_WIDTH_BYTES; i++) // Hier werden die Trigger-Variabeln 0 gesetzt!
191     {
192         devc->trigger_mask[i] = 0;
193         devc->trigger_value[i] = 0;
194         devc->trigger_mask_last[i] = 0;
195         devc->trigger_value_last[i] = 0;
196     }
197     sr_err("\nDATA_WITH_BYTES:%i\n",devc->DATA_WIDTH_BYTES);
198
199 //    devc->trigger_value[0] = 0x00;
200 //    devc->trigger_value_last[0] = 0xff;
201 //    devc->trigger_mask[0] = 0xff;
202 //    devc->trigger_mask_last[0] = 0xff;
203
204
205     if (!(trigger = sr_session_trigger_get(sdi->session))) //
206     {
207         return SR_OK;
208     }
209
210
211     devc->num_stages = g_slist_length(trigger->stages);
212     if (devc->num_stages != devc->DATA_WIDTH_BYTES)
213     {
214
215         sr_err("\nThis device only supports %d trigger stages.",
216                 devc->DATA_WIDTH_BYTES);
217
218         return SR_ERR;
219     }
220
221     for (l = trigger->stages; l; l = l->next)
222     {
223             stage = l->data;
224         for (m = stage->matches; m; m = m->next)
225         {
226             match = m->data;
227             unsigned int byteIndex = (match->channel->index) /8;
228             unsigned char matchPattern = 1 << (match->channel->index - 8* byteIndex);
229             //zeroTrigger |= matchPattern;
230             //sr_err("\n\nzerotrigger:%x\n\n",zeroTrigger);
231             //sr_err("\nbyteIndex:%i",byteIndex);
232             //sr_err("\nmatch Pattern:%i\n",matchPattern);
233
234             if (!match->channel->enabled)
235                 /* Ignore disabled channels with a trigger. */
236                 continue;
237             if (match->match == SR_TRIGGER_ONE )
238             {
239                 devc->trigger_value[byteIndex] |= matchPattern;
240                 devc->trigger_mask[byteIndex] |= matchPattern;
241                 devc->trigger_mask_last[byteIndex] &= ~matchPattern;
242                 //sr_err("\n========ONE MASK===========");
243
244             }
245             else if (match->match == SR_TRIGGER_ZERO)
246             {
247                 devc->trigger_value[byteIndex] &= ~matchPattern;
248                 devc->trigger_mask[byteIndex] |= matchPattern;
249                 devc->trigger_mask_last[byteIndex] &= ~matchPattern;
250                 //sr_err("\n========ZERO MASK===========");
251             }
252             else if ( match->match == SR_TRIGGER_RISING)
253             {
254                 devc->trigger_value[byteIndex] |= matchPattern;
255                 devc->trigger_value_last[byteIndex] &= ~matchPattern;
256                 devc->trigger_mask[byteIndex] |= matchPattern;
257                 devc->trigger_mask_last[byteIndex] |= matchPattern;
258                 //sr_err("\n==========RISING===========");
259
260             }
261             else if (match->match == SR_TRIGGER_FALLING )
262             {
263                 devc->trigger_value[byteIndex] &= ~matchPattern;
264                 devc->trigger_value_last[byteIndex] |= matchPattern;
265                 devc->trigger_mask[byteIndex] |= matchPattern;
266                 devc->trigger_mask_last[byteIndex] |= matchPattern;
267                 //sr_err("\n========FALlING===========");
268             }
269
270         }
271
272     }
273
274 //            sr_err("\n VAL LAST:%x\n",devc->trigger_value_last[0]);
275 //            sr_err("\n VAL:%x\n",devc->trigger_value[0]);
276 //            sr_err("\n MASK:%x\n",devc->trigger_mask[0]);
277 //            sr_err("\n MASK LAST:%x\n",devc->trigger_mask_last[0]);
278
279     return SR_OK;
280 }
281
282 SR_PRIV int ipdbg_org_la_receive_data(int fd, int revents, void *cb_data)
283 {
284     //sr_err("receive Data0\n");
285
286
287     const struct sr_dev_inst *sdi;
288     struct ipdbg_org_la_dev_context *devc;
289
290
291
292     (void)fd;
293         (void)revents;
294
295     sdi = (const struct sr_dev_inst *)cb_data;
296     if (!sdi)
297     {
298         return FALSE;
299     }
300     //sr_err("receive Data1\n");
301
302     if (!(devc = sdi->priv))
303     {
304         return FALSE;
305
306     }
307     //sr_err("receive Data2\n");
308
309
310     struct ipdbg_org_la_tcp *tcp = sdi->conn;
311     struct sr_datafeed_packet packet;
312     struct sr_datafeed_logic logic;
313
314
315
316     /*sr_warn("---");
317     if (devc->num_transfers == 0 && revents == 0)
318     { //
319         sr_warn("warten auf Eingangsdaten");
320         // Ignore timeouts as long as we haven't received anything
321         return TRUE;
322     }*/
323
324     if (!devc->raw_sample_buf)
325     {
326         //sr_warn("allocating buffer");
327         devc->raw_sample_buf = g_try_malloc(devc->limit_samples*devc->DATA_WIDTH_BYTES);
328
329         if (!devc->raw_sample_buf) {
330             sr_warn("Sample buffer malloc failed.");
331             return FALSE;
332         }
333
334     }
335
336
337     if (devc->num_transfers < devc->limit_samples_max*devc->DATA_WIDTH_BYTES)
338     {
339         sr_err("1");
340         unsigned char byte;
341
342
343         if (ipdbg_org_la_tcp_receive(tcp, &byte, 1) == 1)
344         {
345             if(devc->num_transfers < devc->limit_samples*devc->DATA_WIDTH_BYTES)
346                 devc->raw_sample_buf[devc->num_transfers] = byte;
347             devc->num_transfers++;
348         }
349
350     }
351     else
352     {
353         sr_err("Received %d bytes", devc->num_transfers);
354
355         sr_dbg("Received %d bytes.", devc->num_transfers);
356
357         if (devc->delay_value > 0) {
358             /* There are pre-trigger samples, send those first. */
359             packet.type = SR_DF_LOGIC;
360             packet.payload = &logic;
361             //logic.length = devc->delay_value-1;
362             logic.length = devc->delay_value*devc->DATA_WIDTH_BYTES;
363             logic.unitsize = devc->DATA_WIDTH_BYTES;
364             logic.data = devc->raw_sample_buf;
365             sr_session_send(cb_data, &packet);
366         }
367
368         /* Send the trigger. */
369         packet.type = SR_DF_TRIGGER;
370         sr_session_send(cb_data, &packet);
371
372         /* Send post-trigger samples. */
373         packet.type = SR_DF_LOGIC;
374         packet.payload = &logic;
375         //logic.length = devc->limit_samples - devc->delay_value+1;
376         logic.length = (devc->limit_samples - devc->delay_value)*devc->DATA_WIDTH_BYTES;
377         logic.unitsize = devc->DATA_WIDTH_BYTES;
378         logic.data = devc->raw_sample_buf + devc->delay_value*devc->DATA_WIDTH_BYTES;
379         //logic.data = devc->raw_sample_buf + devc->delay_value-1;
380         sr_session_send(cb_data, &packet);
381
382         g_free(devc->raw_sample_buf);
383         devc->raw_sample_buf = NULL;
384
385         //serial_flush(serial);
386         ipdbg_org_la_abort_acquisition(sdi);//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
387     }
388
389     return TRUE;
390 }
391
392 SR_PRIV int ipdbg_org_la_sendDelay(struct ipdbg_org_la_dev_context *devc, struct ipdbg_org_la_tcp *tcp)
393 {
394     //sr_warn("delay");
395
396     int maxSample;
397
398     maxSample = //0x1 << (devc->ADDR_WIDTH);
399                                 devc->limit_samples;
400
401     devc->delay_value = (maxSample/100.0) * devc->capture_ratio;
402     uint8_t Befehl[1];
403     Befehl[0] = LA;
404     ipdbg_org_la_tcp_send(tcp, Befehl, 1);
405     Befehl[0] = delay;
406     ipdbg_org_la_tcp_send(tcp, Befehl, 1);
407
408     //sr_warn("delay 2");
409
410
411     char buf[4] = { devc->delay_value        & 0x000000ff,
412                    (devc->delay_value >>  8) & 0x000000ff,
413                    (devc->delay_value >> 16) & 0x000000ff,
414                    (devc->delay_value >> 24) & 0x000000ff};
415
416     sendEscaping(tcp, buf, devc->ADDR_WIDTH_BYTES);
417
418     //sr_warn("send delay_value: 0x%.2x", devc->delay_value);
419
420     return SR_OK;
421 }
422
423 SR_PRIV int ipdbg_org_la_sendTrigger(struct ipdbg_org_la_dev_context *devc, struct ipdbg_org_la_tcp *tcp)
424 {
425     /////////////////////////////////////////////Mask////////////////////////////////////////////////////////////
426     uint8_t buf[1];
427     buf[0] = Trigger;
428     ipdbg_org_la_tcp_send(tcp, buf, 1);
429     buf[0] = Masks;
430     ipdbg_org_la_tcp_send(tcp, buf, 1);
431     buf[0] = Mask;
432     ipdbg_org_la_tcp_send(tcp, buf, 1);
433
434     sendEscaping(tcp, devc->trigger_mask, devc->DATA_WIDTH_BYTES);
435
436     //sr_warn("send trigger_mask: %x", devc->trigger_mask[0]);
437
438
439      /////////////////////////////////////////////Value////////////////////////////////////////////////////////////
440     buf[0]= Trigger;
441     ipdbg_org_la_tcp_send(tcp, buf, 1);
442     buf[0] = Masks;
443     ipdbg_org_la_tcp_send(tcp, buf, 1);
444     buf[0] = Value;
445     ipdbg_org_la_tcp_send(tcp, buf, 1);
446
447
448     sendEscaping(tcp, devc->trigger_value, devc->DATA_WIDTH_BYTES);
449
450     //sr_warn("send trigger_value: 0x%.2x", devc->trigger_value[0]);
451
452
453     /////////////////////////////////////////////Mask_last////////////////////////////////////////////////////////////
454     buf[0] = Trigger;
455     ipdbg_org_la_tcp_send(tcp, buf, 1);
456     buf[0] = Last_Masks;
457     ipdbg_org_la_tcp_send(tcp, buf, 1);
458     buf[0] = Mask_last;
459     ipdbg_org_la_tcp_send(tcp, buf, 1);
460
461
462     sendEscaping(tcp, devc->trigger_mask_last, devc->DATA_WIDTH_BYTES);
463
464
465     //sr_warn("send trigger_mask_last: 0x%.2x", devc->trigger_mask_last[0]);
466
467
468     /////////////////////////////////////////////Value_last////////////////////////////////////////////////////////////
469     buf[0] = Trigger;
470     ipdbg_org_la_tcp_send(tcp, buf, 1);
471     buf[0]= Last_Masks;
472     ipdbg_org_la_tcp_send(tcp, buf, 1);
473     buf[0]= Value_last;
474     ipdbg_org_la_tcp_send(tcp, buf, 1);
475
476
477     sendEscaping(tcp, devc->trigger_value_last, devc->DATA_WIDTH_BYTES);
478
479
480     //sr_warn("send trigger_value_last: 0x%.2x", devc->trigger_value_last[0]);
481
482     return SR_OK;
483 }
484
485 SR_PRIV int sendEscaping(struct ipdbg_org_la_tcp *tcp, char *dataToSend, int length)
486 {
487
488     while(length--)
489     {
490         uint8_t payload = *dataToSend++;
491         //sr_warn("payload %d", payload);
492
493         //sr_warn("send really");
494
495         if ( payload == (uint8_t)reset )
496         {
497             uint8_t escapeSymbol = Escape;
498             sr_warn("Escape");
499
500             if(ipdbg_org_la_tcp_send(tcp, &escapeSymbol, 1) != SR_OK)
501                 sr_warn("can't send escape");
502
503
504         }
505
506         if ( payload == (char)Escape )
507         {
508             uint8_t escapeSymbol = Escape;
509             sr_warn("Escape");
510
511             if(ipdbg_org_la_tcp_send(tcp, &escapeSymbol, 1) != SR_OK)
512                 sr_warn("can't send escape");
513         }
514
515         if (ipdbg_org_la_tcp_send(tcp, &payload, 1) != SR_OK)
516         {
517             sr_warn("Can't send data");
518         }
519          //sr_warn("length %d", length);
520
521     }
522     return SR_OK;
523 }
524
525 SR_PRIV void ipdbg_org_la_get_addrwidth_and_datawidth(struct ipdbg_org_la_tcp *tcp, struct ipdbg_org_la_dev_context *devc)
526 {
527     //sr_err("getAddrAndDataWidth\n");
528     uint8_t buf[8];
529     uint8_t auslesen[1];
530     auslesen[0]= K_Mauslesen;
531
532     if(ipdbg_org_la_tcp_send(tcp, auslesen, 1) != SR_OK)
533         sr_warn("Can't send K_Mauslesen");
534     //g_usleep(RESPONSE_DELAY_US);
535
536
537
538     /// delay
539     if(ipdbg_org_la_tcp_receive(tcp, buf, 8) != 8)
540         sr_warn("getAddrAndDataWidth failed");
541
542     //sr_warn("getAddrAndDataWidth 0x%x:0x%x:0x%x:0x%x 0x%x:0x%x:0x%x:0x%x", buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);
543
544     devc->DATA_WIDTH  =  buf[0]        & 0x000000FF;
545     devc->DATA_WIDTH |= (buf[1] <<  8) & 0x0000FF00;
546     devc->DATA_WIDTH |= (buf[2] << 16) & 0x00FF0000;
547     devc->DATA_WIDTH |= (buf[3] << 24) & 0xFF000000;
548
549     devc->ADDR_WIDTH  =  buf[4]        & 0x000000FF;
550     devc->ADDR_WIDTH |= (buf[5] <<  8) & 0x0000FF00;
551     devc->ADDR_WIDTH |= (buf[6] << 16) & 0x00FF0000;
552     devc->ADDR_WIDTH |= (buf[7] << 24) & 0xFF000000;
553
554
555
556     //sr_warn("Datawidth: %d  Addrwdth : %d", devc->DATA_WIDTH, devc->ADDR_WIDTH);
557
558     int HOST_WORD_SIZE = 8; // bits/ word
559
560     devc->DATA_WIDTH_BYTES = (devc->DATA_WIDTH+HOST_WORD_SIZE -1)/HOST_WORD_SIZE;
561     devc->ADDR_WIDTH_BYTES = (devc->ADDR_WIDTH+HOST_WORD_SIZE -1)/HOST_WORD_SIZE;
562     devc->limit_samples_max = (0x01 << devc->ADDR_WIDTH);
563         devc->limit_samples = (0x01 << HOST_WORD_SIZE);
564     //sr_warn("DATA_WIDTH_BYTES: %d  ADDR_WIDTH_BYTES : %d", devc->DATA_WIDTH_BYTES, devc->ADDR_WIDTH_BYTES);
565
566
567
568     devc->trigger_mask       = g_malloc0(devc->DATA_WIDTH_BYTES);
569     devc->trigger_value      = g_malloc0(devc->DATA_WIDTH_BYTES);
570     devc->trigger_mask_last  = g_malloc0(devc->DATA_WIDTH_BYTES);
571     devc->trigger_value_last = g_malloc0(devc->DATA_WIDTH_BYTES);
572
573
574 }
575
576 SR_PRIV struct ipdbg_org_la_dev_context *ipdbg_org_la_dev_new(void)
577 {
578     struct ipdbg_org_la_dev_context *devc;
579
580     devc = g_malloc0(sizeof(struct ipdbg_org_la_dev_context));
581
582
583     devc->capture_ratio = 50;
584     ///devc->num_bytes = 0;
585
586     return devc;
587 }
588
589 SR_PRIV int ipdbg_org_la_sendReset(struct ipdbg_org_la_tcp *tcp)
590 {
591     uint8_t buf[1];
592     buf[0]= reset;
593     if(ipdbg_org_la_tcp_send(tcp, buf, 1) != SR_OK)
594         sr_warn("Reset can't send");
595     return SR_OK;
596 }
597
598 SR_PRIV int ipdbg_org_la_requestID(struct ipdbg_org_la_tcp *tcp)
599 {
600     uint8_t buf[1];
601     buf[0]= IPDBG_LA_ID;
602     if(ipdbg_org_la_tcp_send(tcp, buf, 1) != SR_OK)
603         sr_warn("IDBG can't send");
604
605     char ID[4];
606     if(ipdbg_org_la_tcp_receive(tcp, (uint8_t*)ID, 4) != 4)
607     {
608         sr_warn("IDBG can't read");
609     }
610
611
612     if (strncmp(ID, "IDBG", 4)) {
613         sr_err("Invalid reply (expected 'IDBG' '%c%c%c%c').", ID[0], ID[1], ID[2], ID[3]);
614         return SR_ERR;
615     }
616
617     return SR_OK;
618 }
619
620 SR_PRIV void ipdbg_org_la_abort_acquisition(const struct sr_dev_inst *sdi)
621 {
622     struct sr_datafeed_packet packet;
623
624     struct ipdbg_org_la_tcp *tcp = sdi->conn;
625
626         sr_session_source_remove(sdi->session, tcp->socket);
627
628     /* Terminate session */
629     packet.type = SR_DF_END;
630     sr_session_send(sdi, &packet);
631 }
632
633 SR_PRIV int ipdbg_org_la_sendStart(struct ipdbg_org_la_tcp *tcp)
634 {
635     uint8_t buf[1];
636     buf[0] = Start;
637
638    if(ipdbg_org_la_tcp_send(tcp, buf, 1) != SR_OK)
639         sr_warn("Reset can't send");
640     return SR_OK;
641 }
642