forked from jromang/dgtpi
-
Notifications
You must be signed in to change notification settings - Fork 6
/
dgtpicom.c
1721 lines (1498 loc) · 38.1 KB
/
dgtpicom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* functions to communicate to a DGT3000 using I2C
* version 0.8
*
* Copyright (C) 2015 DGT
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <pthread.h>
#include <sched.h>
#include "dgtpicom.h"
#include "dgtpicom_dgt3000.h"
int ww;
char piModel;
// while loop
void *wl(void *x) {
int e;
int i=0;
while (1)
if (ww) {
e=dgtpicom_configure();
if (e<0)
printf("%d: Configure failed!\n",i);
e=dgtpicom_set_text("Hello world",0,0,0);
if (e<0)
printf("%d: Display failed!\n",i);
// e=dgtpicom_set_and_run(1,0,10,0,2,0,10,0);
// if (e<0)
// printf("%d: SetNRun failed!\n",i);
i++;
#ifdef debug
bug.sendTotal++;
#endif
} else {
usleep(10000);
}
return 0;
}
int main (int argc, char *argv[]) {
int e;
//char message[255];
//char t[6]
char but,tim;
// get direct acces to the perhicels
if (dgtpicom_init()) return ERROR_MEM;
// configure dgt3000 for mode 25
e = dgtpicom_configure();
if (e<0)
return e;
// 7 arguments -> setnrun
if (argc==8) {
unsigned char leftrun, rightrun=0;
if (argv[1][0]=='l')
leftrun=1;
else if (argv[1][0]=='L')
leftrun=2;
else if (argv[1][0]=='r')
rightrun=1;
else if (argv[1][0]=='R')
rightrun=2;
dgtpicom_set_and_run(leftrun,
atoi(argv[2]),
atoi(argv[3]),
atoi(argv[4]),
rightrun,
atoi(argv[5]),
atoi(argv[6]),
atoi(argv[7]) );
} else if (argc>1) {
unsigned char beep=0, ldots=0, rdots=0;
dgtpicom_set_and_run(0,0,0,0,0,0,0,0);
if (argc>2)
beep=atoi(argv[2]);
if (argc>4) {
ldots=atoi(argv[3]);
rdots=atoi(argv[4]);
}
if ( argv[1][0]=='~' ) {
dgtpicom_set_text("DGT PI",beep,ldots,rdots);
while(1) {
usleep(1000000);
dgtpicom_set_text(" DGT PI",0,ldots,rdots);
usleep(1000000);
dgtpicom_set_text(" DGT PI",0,ldots,rdots);
usleep(1000000);
dgtpicom_set_text(" DGT PI",0,ldots,rdots);
usleep(1000000);
dgtpicom_set_text(" DGT PI",0,ldots,rdots);
usleep(1000000);
dgtpicom_set_text(" DGT PI",0,ldots,rdots);
usleep(1000000);
dgtpicom_set_text(" DGT PI",0,ldots,rdots);
usleep(1000000);
dgtpicom_set_text(" DGT PI",0,ldots,rdots);
usleep(1000000);
dgtpicom_set_text(" DGT PI",0,ldots,rdots);
usleep(1000000);
dgtpicom_set_text(" DGT PI",0,ldots,rdots);
usleep(1000000);
dgtpicom_set_text("DGT PI",0,ldots,rdots);
}
} else if ( argv[1][0]=='*' ){
while(1) {
if ( dgtpicom_set_text(" DGT PI -",beep,ldots,rdots) != ERROR_OK )
i2cReset();
usleep(200000);
if ( dgtpicom_set_text(" DGT PI ||",beep,ldots,rdots) != ERROR_OK )
i2cReset();
usleep(200000);
if ( dgtpicom_set_text(" DGT PI |",beep,ldots,rdots) != ERROR_OK )
i2cReset();
usleep(200000);
if ( dgtpicom_set_text(" DGT PI /",beep,ldots,rdots) != ERROR_OK )
i2cReset();
usleep(200000);
}
} else {
// try three times to end and set de display
dgtpicom_set_text(argv[1],beep,ldots,rdots);
}
} else {
#ifdef debug
printf(" %.3f ",(float)*timer()/1000000);
printf("started\n");
usleep(10000);
ww=1;
pthread_t w;
pthread_create(&w, NULL, wl, NULL);
#else
if ( dgtpicom_off(1) < 0 )
dgtpicom_off(1);
#endif
but=tim=0;
while(1) {
// printf("rxMaxBuf=%d ",bug.rxMaxBuf);
// dgt3000GetTime(t);
// printf("time=%d:%02d.%02d %d:%02d.%02d\n",t[0],t[1],t[2],t[3],t[4],t[5]);
if (dgtpicom_get_button_message(&but,&tim)) {
if (but&0x40) {
if (ww)
ww=0;
else
ww=1;
}
if (but==0x20) {
break;
}
printf("%.3f ",(float)*timer()/1000000);
printf("button=%02x, time=%d\n",but,tim);
// dgt3000EndDisplay();
}
usleep(10000);
}
// dgtpicom_configure();
// dgtpicom_set_text("Booting",0,0,0);
}
dgtpicom_stop();
#ifdef debug
printf("%.3f ",(float)*timer()/1000000);
printf("After %d messages:\n",bug.sendTotal);
printf("Send failed: display=%d, endDisplay=%d, changeState=%d, setCC=%d, setNRun=%d\n",
bug.displaySF, bug.endDisplaySF, bug.changeStateSF, bug.setCCSF, bug.setNRunSF);
printf("Ack failed : display=%d, endDisplay=%d, changeState=%d, setCC=%d, setNRun=%d\n",
bug.displayAF, bug.endDisplayAF, bug.changeStateAF, bug.setCCAF, bug.setNRunAF);
printf("Recieve Errors: timeout=%d, wrongAdr=%d, bufferFull=%d, sizeMismatch=%d, CRCFault=%d\n",
bug.rxTimeout, bug.rxWrongAdr, bug.rxBufferFull, bug.rxSizeMismatch, bug.rxCRCFault);
printf("Max recieve buffer size=%d\n",bug.rxMaxBuf);
#endif
// succes?
return ERROR_OK;
}
// Get direct access to BCM2708/9 chip.
int dgtpicom_init() {
int memfd, base;
void *gpio_map, *timer_map, *i2c_slave_map, *i2c_master_map;
struct sched_param params;
memset(&dgtRx,0,sizeof(dgtReceive_t));
#ifdef debug
memset(&bug,0,sizeof(debug_t));
#endif
piModel = checkPiModel();
if (piModel==4)
base=0xfe000000;
else if (piModel==1)
base=0x20000000;
else
base=0x3f000000;
memfd = open("/dev/mem",O_RDWR|O_SYNC);
if(memfd < 0) {
#ifdef debug
printf("/dev/mem open error, run as root\n");
#endif
return ERROR_MEM;
}
gpio_map = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, GPIO_BASE+base);
timer_map = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, TIMER_BASE+base);
i2c_slave_map = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, I2C_SLAVE_BASE+base);
i2c_master_map = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, I2C_MASTER_BASE+base);
close(memfd);
if( gpio_map == MAP_FAILED || timer_map == MAP_FAILED || i2c_slave_map == MAP_FAILED || i2c_master_map == MAP_FAILED) {
#ifdef debug
printf("Map failed\n");
#endif
return ERROR_MEM;
}
// GPIO pointers
gpio = (volatile unsigned *)gpio_map;
gpioset = gpio + 7; // set bit register offset 28
gpioclr = gpio + 10; // clr bit register
gpioin = gpio + 13; // read all bits register
// timer pointer
timerh = (u_int32_t *)((char *)timer_map + 4);
timerl = (u_int32_t *)((char *)timer_map + 8);
// i2c slave pointers
i2cSlave = (volatile unsigned *)i2c_slave_map;
i2cSlaveRSR = i2cSlave + 1;
i2cSlaveSLV = i2cSlave + 2;
i2cSlaveCR = i2cSlave + 3;
i2cSlaveFR = i2cSlave + 4;
// i2c master pointers
i2cMaster = (volatile unsigned *)i2c_master_map;
i2cMasterS = i2cMaster + 1;
i2cMasterDLEN = i2cMaster + 2;
i2cMasterA = i2cMaster + 3;
i2cMasterFIFO = i2cMaster + 4;
i2cMasterDiv = i2cMaster + 5;
i2cMasterDel = i2cMaster + 6;
// check wiring
// configured as an output? probably in use for something else
if ((*gpio & 0x1c0) == 0x40) {
#ifdef debug
printf("Error, GPIO02 configured as output, in use? We asume not a DGTPI\n");
#endif
return ERROR_LINES;
}
if ((*gpio & 0xe00) == 0x200) {
#ifdef debug
printf("Error, GPIO03 configured as output, in use? We asume not a DGTPI\n");
#endif
return ERROR_LINES;
}
if (piModel==4)
{
if ((*(gpio+1) & 0x07) == 0x01) {
#ifdef debug
printf("Error, GPIO10 configured as output, in use? We asume not a DGTPI\n");
#endif
return ERROR_LINES;
}
if ((*(gpio+1) & 0x38) == 0x08) {
#ifdef debug
printf("Error, GPIO11 configured as output, in use? We asume not a DGTPI\n");
#endif
return ERROR_LINES;
}
}
else
{
if ((*(gpio+1) & 0x07000000) == 0x01000000) {
#ifdef debug
printf("Error, GPIO18 configured as output, in use? We asume not a DGTPI\n");
#endif
return ERROR_LINES;
}
if ((*(gpio+1) & 0x38000000) == 0x08000000) {
#ifdef debug
printf("Error, GPIO19 configured as output, in use? We asume not a DGTPI\n");
#endif
return ERROR_LINES;
}
}
// pinmode GPIO2,GPIO3=input
*gpio &= 0xfffff03f;
if (piModel==4)
{
// pinmode GPIO10,GPIO11=input
*(gpio+1) &= 0xffffffc0;
}
else
{
// pinmode GPIO18,GPIO19=input
*(gpio+1) &= 0xc0ffffff;
}
usleep(1);
// all pins hi through pullup?
if (piModel==4)
{
if ((*gpioin & 0x0c0c)!=0x0c0c) {
#ifdef debug
printf("Error, pin(s) low, shortcircuit, or no connection?\n");
#endif
return ERROR_LINES;
}
}
else
{
if ((*gpioin & 0xc000c)!=0xc000c) {
#ifdef debug
printf("Error, pin(s) low, shortcircuit, or no connection?\n");
#endif
return ERROR_LINES;
}
}
/* // check SDA connection
// gpio18 low
*gpioclr = 1<<18;
// gpio18 output
*(gpio+1) |= 0x01000000;
usleep(1);
// check gpio 18 and 2
if ((*gpioin & 0x40004)!=0) {
#ifdef debug
printf("Error, SDA not connected\n");
#endif
// gpio18 back to input
*(gpio+1) &= 0xf8ffffff;
return ERROR_LINES;
}
if ((*gpioin & 0x80008)!=0x80008) {
#ifdef debug
printf("Error, SDA connected to SCL\n");
#endif
// gpio18 back to input
*(gpio+1) &= 0xf8ffffff;
return ERROR_LINES;
}
// gpio18 back to input
*(gpio+1) &= 0xf8ffffff;
// check SCL connection
// gpio19 low
*gpioclr = 1<<19;
// gpio19 output
*(gpio+1) |= 0x08000000;
usleep(1);
// check gpio 19 and 3
if ((*gpioin & 0x80008)!=0) {
#ifdef debug
printf("Error, SCL not connected\n");
#endif
// gpio19 back to input
*(gpio+1) &= 0xc7ffffff;
return ERROR_LINES;
}
// gpio19 back to input
*(gpio+1) &= 0xc7ffffff;
*/
i2cReset();
// set to I2CMaster destination adress
*i2cMasterA=8;
dgtRx.on=1;
pthread_create(&receiveThread, NULL, dgt3000Receive, NULL);
// give thread max priority
params.sched_priority = sched_get_priority_max(SCHED_FIFO);
pthread_setschedparam(receiveThread, SCHED_FIFO, ¶ms);
return ERROR_OK;
}
// Configure the dgt3000.
int dgtpicom_configure() {
int e;
int wakeCount = 0;
int setCCCount = 0;
int resetCount = 0;
// get the clock into the right state
while (1) {
// set to mode 25 and run
e=dgt3000Mode25();
if (e==ERROR_NACK || e==ERROR_NOACK) {
// no postive ack, not in cc
// set central controll
// try 3 times
setCCCount++;
// setCC>3?
if (setCCCount>3) {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending setCentralControll failed three times\n\n");
ERROR_PIN_LO;
#endif
return e;
}
usleep(10000);
dgt3000SetCC();
} else if (e==ERROR_TIMEOUT) {
// timeout, line stay low -> reset i2c
resetCount++;
if (resetCount>1) {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("I2C error, remove jack plug\n\n");
ERROR_PIN_LO;
#endif
return e;
}
i2cReset();
continue;
} else if (e==ERROR_CST || e==ERROR_LINES) {
// message not acked, probably collision
continue;
} else if (e==ERROR_SILENT) {
// message not acked, probably clock off -> wake
// wake#++
wakeCount++;
// wake#>3? -> error
if (wakeCount>3) {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending wake command failed three times\n");
ERROR_PIN_LO;
#endif
return e;
}
dgt3000Wake();
continue;
} else {
// succes!
//usleep(1000);
break;
}
}
return ERROR_OK;
}
// send set and run command to dgt3000
int dgtpicom_set_and_run(char lr, char lh, char lm, char ls,
char rr, char rh, char rm, char rs) {
int e;
int sendCount = 0;
setnrun[4]=lh;
setnrun[5]=((lm/10)<<4) | (lm%10);
setnrun[6]=((ls/10)<<4) | (ls%10);
setnrun[7]=rh;
setnrun[8]=((rm/10)<<4) | (rm%10);
setnrun[9]=((rs/10)<<4) | (rs%10);
setnrun[10]=lr | (rr<<2);
crc_calc(setnrun);
while (1) {
sendCount++;
if (sendCount>3) {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending SetNRun failed three times on error%d\n\n",e);
ERROR_PIN_LO;
#endif
return e;
}
e=dgt3000SetNRun(setnrun);
// succes?
if (e==ERROR_OK)
return ERROR_OK;
}
}
// Send set and run command to the dgt3000 with current clock values.
int dgtpicom_run(char lr, char rr) {
return dgtpicom_set_and_run(
lr,
dgtRx.time[0],
((dgtRx.time[1]&0xf0)>>4)*10 + (dgtRx.time[1]&0x0f),
((dgtRx.time[2]&0xf0)>>4)*10 + (dgtRx.time[2]&0x0f),
rr,
dgtRx.time[3],
((dgtRx.time[4]&0xf0)>>4)*10 + (dgtRx.time[4]&0x0f),
((dgtRx.time[5]&0xf0)>>4)*10 + (dgtRx.time[5]&0x0f));
}
// Set a text message on the DGT3000.
int dgtpicom_set_text(char text[], char beep, char ld, char rd) {
int i,e;
int sendCount = 0;
for (i=0;i<11;i++) {
if(text[i]==0) break;
display[i+4]=text[i];
}
for (;i<11;i++) {
display[i+4]=32;
}
display[16]=beep;
display[18]=ld;
display[19]=rd;
crc_calc(display);
while (1) {
sendCount++;
if (sendCount>3) {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending clear display failed three times on error%d\n\n",e);
ERROR_PIN_LO;
#endif
return e;
}
e=dgt3000EndDisplay();
// succes?
if (e==ERROR_OK)
break;
}
sendCount=0;
while (1) {
sendCount++;
if (sendCount>3) {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending display command failed three times on error%d\n\n",e);
ERROR_PIN_LO;
#endif
return e;
}
// succes?
e=dgt3000Display(display);
if (e==ERROR_OK)
break;
}
return ERROR_OK;
}
// End a text message on the DGT3000 an return to clock mode.
int dgtpicom_end_text() {
int e;
int sendCount = 0;
while (1) {
sendCount++;
if (sendCount>3) {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending end display failed three times on error%d\n\n",e);
ERROR_PIN_LO;
#endif
return e;
}
e=dgt3000EndDisplay();
// succes?
if (e==ERROR_OK)
return ERROR_OK;
}
}
// Put the last received time message in time[].
void dgtpicom_get_time(char time[]) {
time[0]=dgtRx.time[0];
time[1]=((dgtRx.time[1]&0xf0)>>4)*10 + (dgtRx.time[1]&0x0f);
time[2]=((dgtRx.time[2]&0xf0)>>4)*10 + (dgtRx.time[2]&0x0f);
time[3]=dgtRx.time[3];
time[4]=((dgtRx.time[4]&0xf0)>>4)*10 + (dgtRx.time[4]&0x0f);
time[5]=((dgtRx.time[5]&0xf0)>>4)*10 + (dgtRx.time[5]&0x0f);
}
// Get a button message from the buffer returns number of messages in
// the buffer or recieve error if one occured.
int dgtpicom_get_button_message(char *buttons, char *time) {
int e=dgtRx.error;
dgtRx.error=0;
if (e<0)
return e;
//button availible?
if(dgtRx.buttonStart != dgtRx.buttonEnd) {
*buttons=dgtRx.buttonPres[dgtRx.buttonStart];
*time=dgtRx.buttonTime[dgtRx.buttonStart];
dgtRx.buttonStart=(dgtRx.buttonStart+1)%DGTRX_BUTTON_BUFFER_SIZE;
return (dgtRx.buttonEnd-dgtRx.buttonStart)%DGTRX_BUTTON_BUFFER_SIZE + 1;
} else {
return ERROR_OK;
}
}
// Return the current button state.
int dgtpicom_get_button_state() {
return dgtRx.lastButtonState;
}
// Turn off the dgt3000.
int dgtpicom_off(char returnMode) {
int e;
mode25[4]=32+returnMode;
crc_calc(mode25);
/* // send mode 25 message
e=i2cSend(mode25,0x10);
// send succesful?
if (e<0) {
#ifdef debug
bug.changeStateSF++;
#endif
return e;
}
// listen to our own adress an get Reply
e=dgt3000GetAck(0x10,0x0b,10000);
// ack received?
if (e<0) {
#ifdef debug
bug.changeStateAF++;
#endif
return e;
}
// negetive ack not in CC
if (dgtRx.ack[1]!=8)
return ERROR_NACK;
*/
mode25[4]=0;
crc_calc(mode25);
// send mode 25 message
e=i2cSend(mode25,0x00);
// send succesful?
if (e<0) {
#ifdef debug
bug.changeStateSF++;
#endif
return e;
}
return ERROR_OK;
}
// Disable the I2C hardware.
void dgtpicom_stop() {
// stop listening to broadcasts
*i2cSlaveSLV=16;
// stop thread
dgtRx.on=0;
// wait for thread to finish
pthread_join(receiveThread, NULL);
// disable i2cSlave device
*i2cSlaveCR=0;
// pinmode GPIO2,GPIO3=input
*gpio &= 0xfffff03f;
if (piModel==4)
{
// pinmode GPIO10,GPIO11=input
*(gpio+1) &= 0xffffffc0;
}
else
{
// pinmode GPIO18,GPIO19=input
*(gpio+1) &= 0xc0ffffff;
}
}
// send a wake command to the dgt3000
int dgt3000Wake() {
int e;
u_int64_t t;
// send wake
*i2cMasterA=40;
e=i2cSend(ping,0x00);
*i2cMasterA=8;
// succes? -> error. Wake messages should never get an Ack
if (e==ERROR_OK) {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending wake command failed, received Ack, this should never hapen\n");
ERROR_PIN_LO;
#endif
return ERROR_NACK;
}
// Get Hello message (in max 10ms, usualy 5ms)
t=*timer()+10000;
while (*timer()<t) {
if (dgtRx.hello==1)
return ERROR_OK;
usleep(100);
}
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending wake command failed, no hello\n");
ERROR_PIN_LO;
#endif
return ERROR_NOACK;
}
// send set central controll command to dgt3000
int dgt3000SetCC() {
int e;
// send setCC, error? retry
e=i2cSend(centralControll,0x10);
// send succedfull?
if (e<0) {
#ifdef debug
ERROR_PIN_HI;
bug.setCCSF++;
printf("%.3f ",(float)*timer()/1000000);
printf("sending SetCentralControll command failed, sending failed\n");
ERROR_PIN_LO;
#endif
return e;
}
// listen to our own adress and get Reply
e=dgt3000GetAck(0x10,0x0f,10000);
// ack received?
if (e<0) {
#ifdef debug
ERROR_PIN_HI;
bug.setCCAF++;
printf("%.3f ",(float)*timer()/1000000);
printf("sending SetCentralControll command failed, no ack\n");
ERROR_PIN_LO;
#endif
return e;
}
// is positive ack?
if ((dgtRx.ack[1]&8) == 8)
return ERROR_OK;
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending SetCentralControll command failed, negative ack, clock running\n");
ERROR_PIN_LO;
#endif
// nack clock running
return ERROR_NACK;
}
// send set mode 25 to dgt3000
int dgt3000Mode25() {
int e;
mode25[4]=57;
crc_calc(mode25);
// send mode 25 message
e=i2cSend(mode25, 0x10);
// send succesful?
if (e<0) {
#ifdef debug
ERROR_PIN_HI;
bug.changeStateSF++;
printf("%.3f ",(float)*timer()/1000000);
printf("sending mode25 command failed, sending failed\n");
ERROR_PIN_LO;
#endif
return e;
}
// listen to our own adress an get Reply
e=dgt3000GetAck(0x10,0x0b,10000);
// ack received?
if (e<0) {
#ifdef debug
ERROR_PIN_HI;
bug.changeStateAF++;
printf("%.3f ",(float)*timer()/1000000);
printf("sending mode25 command failed, no ack\n");
ERROR_PIN_LO;
#endif
return e;
}
if (dgtRx.ack[1]==8) return ERROR_OK;
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending mode25 command failed, negative ack, not in Central Controll\n");
ERROR_PIN_LO;
#endif
// negetive ack not in CC
return ERROR_NACK;
}
// send end display to dgt3000 to clear te display
int dgt3000EndDisplay() {
int e;
// send end Display
e=i2cSend(endDisplay,0x10);
// send succesful?
if (e<0) {
#ifdef debug
ERROR_PIN_HI;
bug.endDisplaySF++;
printf("%.3f ",(float)*timer()/1000000);
printf("sending end display command failed, sending failed\n");
ERROR_PIN_LO;
#endif
return e;
}
// get fast Reply = already empty
e=dgt3000GetAck(0x10,0x07,1200);
// display already empty
if (e==ERROR_OK) {
if ((dgtRx.ack[1]&0x07) == 0x05) {
return ERROR_OK;
} else {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending end display command failed, negative specific ack:%02x\n",dgtRx.ack[1]);
ERROR_PIN_LO;
#endif
return ERROR_NACK;
}
}
//get slow broadcast Reply = display changed
e=dgt3000GetAck(0x00,0x07,10000);
// ack received?
if (e<0) {
#ifdef debug
ERROR_PIN_HI;
bug.endDisplayAF++;
printf("%.3f ",(float)*timer()/1000000);
printf("sending end display command failed, no ack\n");
ERROR_PIN_LO;
#endif
return e;
}
// display emptied
if ((dgtRx.ack[1]&0x07) == 0x00)
return ERROR_OK;
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending end display command failed, negative broadcast ack:%02x\n",dgtRx.ack[1]);
ERROR_PIN_LO;
#endif
return ERROR_NACK;
}
// send display command to dgt3000
int dgt3000Display(char dm[]) {
int e;
// send the message
e=i2cSend(dm,0x00);
// send succesful?
if (e<0) {
#ifdef debug
ERROR_PIN_HI;
bug.displaySF++;
printf("%.3f ",(float)*timer()/1000000);
printf("sending display command failed, sending failed\n");
ERROR_PIN_LO;
#endif
return e;
}
// get (broadcast) reply
e=dgt3000GetAck(0x00,0x06,10000);
// no reply
if (e<0) {
#ifdef debug
ERROR_PIN_HI;
bug.displayAF++;
printf("%.3f ",(float)*timer()/1000000);
printf("sending display command failed, no ack\n");
ERROR_PIN_LO;
#endif
return e;
}
// nack, already displaying message
if ((dgtRx.ack[1]&0xf3)==0x23) {
#ifdef debug
ERROR_PIN_HI;
printf("%.3f ",(float)*timer()/1000000);
printf("sending display command failed, display already busy\n");
ERROR_PIN_LO;
#endif
return ERROR_NACK;
}
return ERROR_OK;
}
// send set and run command to dgt3000
int dgt3000SetNRun(char srm[]) {
int e;
e=i2cSend(srm,0x10);
// send succesful?
if (e<0) {
#ifdef debug
ERROR_PIN_HI;
bug.setNRunSF++;