-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd.c
2062 lines (1964 loc) · 58 KB
/
sd.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
/*
SD.C : Serial doodad.
Copyright (C) 2014 Danny Chouinard
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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See the full text of the license in file COPYING.
To reach me, execute this command in a bourne shell:
$ echo [email protected] | tr "[a-z]" "[n-za-m]"
That's a rot13 of my e-mail address.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <termio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
FILE *fp;
time_t t;
char twirl[4]={"/-\\|"};
fd_set FDS;
int noctrl=0;
int baud=9600;
int pbaud=115200;
int oldstatus=0;
int status;
int klock=0;
int tictacw=0;
int tictacx=0;
int samples=-1;
int longterm=0;
int period=1;
int printdrift=0;
int f_cpu=10000000;
int i,k,s;
int timestamp=0;
int help=0;
int timing=0;
int programming=0;
int sdsdp=0;
int holdreset=0;
int bbterm=0;
uint8_t twirlc=0;
int invertdtr=0;
int pulsedtr=0;
unsigned char c;
char filename[1024];
char part[20]={"Unknown!"};
int raw=0;
int signature;
int pagesize=0;
int pages=0;
int flashsize=0;
int verbose=1;
int dochiperase=0;
int dowritelfuse=0;
int dowritehfuse=0;
int dowriteefuse=0;
int doreadflash=0;
int dowriteflash=0;
int idelay=-1;
int sdpidelay=-1;
int lfuse=0xFF;
int hfuse=0xFF;
int efuse=0xFF;
int newlfuse=0xFF;
int newhfuse=0xFF;
int newefuse=0xFF;
int cd=1;
int ri=1;
int dsr=1;
int cts=1;
int dtr=1;
int rts=1;
int signaltype=1;
int transition=0;
int h,m,s,n,level,oldcdlevel;
int i;
int comfd=0;
long double f,d;
long double sf;
struct tm *ts;
struct timeval tvs,tvn,tvp,tvd;
int clocal=CLOCAL;
int speed=B9600;
int crtscts=0;
int parity=0;
int bits=CS8;
int stopbits=0;
int cdmode=0;
int option;
int interval=0;
int audio=0;
struct termios stbuf;
struct termio cons, oricons;
char device[128];
char buffer[128];
int flags;
char tictacstring[200];
char capturefile[256];
char macro[1024];
int ocrnl=0;
int localecho=0;
int hex=0;
struct timeval timeout;
struct timeval start,now,diff;
int sendmacro=-1;
int capture=0;
int programend;
FILE *capturefp;
uint8_t *flashbuffer;
void bbexit(int code) {
ioctl(0,TCSETA,&oricons);
printf("\n");
exit(code);
}
void bbatexit(void) {
ioctl(0,TCSETA,&oricons);
bbexit(0);
}
void intexit(int code) {
printf("\r\nINT signal received.\r\n");
bbexit(code);
}
void termexit(int code) {
printf("\r\nTERM signal received.\r\n");
bbexit(code);
}
void killexit(int code) {
printf("\r\nKILL signal received.\r\n");
bbexit(code);
}
void bigassclock(void) {
static unsigned char initted=0;
struct timeval tv;
unsigned int i,j,l,op;
unsigned char c,g,n,p;
static unsigned char *o;
char *ct;
static unsigned char d[]={
"A0002620262026202620262026202620A000042404240424042404240424042404240424\
A000082008200820A000280028002800A000A000082008200820A000082008200820A0002620\
262026202620A0000820082008200820A000280028002800A000082008200820A000A0002800\
28002800A000262026202620A000A00008200820082008200820082008200820A00026202620\
2620A000262026202620A000A000262026202620A00008200820082008200200020002002000\
02002000020002000200"
};
if(!initted) {
printf("\n\n\n\n\n\n\n\n\n");
o=malloc(4096);
initted=1;
}
gettimeofday(&tv,0);
ct=ctime(&tv.tv_sec);
op=0; o[op++]=13; o[op++]=033; o[op++]='['; o[op++]='9'; o[op++]='A';
for(l=0;l<9;l++) {
o[op++]=' ';
for(j=0;j<8;j++) {
c=ct[j+11]; if(c==':') c='9'+1;
c-='0'; p=0;
for(i=0;i<4;i++) {
p=1-p; n=d[c*36+l*4+i]; if(n=='A') n='9'+1;
n-='0';
if(p==1 && n!=0 ) {
o[op++]=033; o[op++]='['; o[op++]='7'; o[op++]='m';
for(g=0;g<n;g++) o[op++]=' ';
o[op++]=033; o[op++]='['; o[op++]='0'; o[op++]='m';
} else {
for(g=0;g<n;g++) o[op++]=' ';
}
}
if(j<7) { o[op++]=' '; o[op++]=' '; }
}
o[op++]='\n';
}
o[op]=0; write(2,o,op);
}
void rtson(void) {
ioctl(comfd,TIOCMGET,&flags);
flags|=TIOCM_RTS;
ioctl(comfd,TIOCMSET,&flags);
}
void rtsoff(void) {
ioctl(comfd,TIOCMGET,&flags);
flags&=~TIOCM_RTS;
ioctl(comfd,TIOCMSET,&flags);
}
void dtron(void) {
ioctl(comfd,TIOCMGET,&flags);
flags|=TIOCM_DTR;
ioctl(comfd,TIOCMSET,&flags);
}
void dtroff(void) {
ioctl(comfd,TIOCMGET,&flags);
flags&=~TIOCM_DTR;
ioctl(comfd,TIOCMSET,&flags);
}
void dtrtoggle(void) {
if(dtr) dtroff();
else dtron();
dtr^=1;
}
void rtstoggle(void) {
if(rts) rtsoff();
else rtson();
rts^=1;
}
uint8_t cdstatus(void) {
uint8_t r=0;
int status;
r=ioctl(comfd,TIOCMGET,&status);
if(r) {
fprintf(stderr,"ioctl failed with code %d.\n",r);
exit(1);
}
if(status&TIOCM_CAR) r=1;
return(r);
}
uint8_t fakecd(void) {
static int status;
if((rand()%100)==59) status^=1;
return(status);
}
void tvdiff(struct timeval *op1, struct timeval *op2, struct timeval *result) {
int units, micros;
micros=op1->tv_usec-op2->tv_usec;
units=op1->tv_sec-op2->tv_sec;
if(micros<0) {
micros+=1000000;
units--;
}
result->tv_sec=units;
result->tv_usec=micros;
}
void ptv(int shortform,struct timeval *v) {
if(shortform) {
printf("%2d.%06d",(int)v->tv_sec,(int)v->tv_usec);
} else {
printf("%6d.%06d",(int)v->tv_sec,(int)v->tv_usec);
}
}
void usage(void) {
fprintf(stderr,"\n");
fprintf(stderr,"Serial Doodad. (C)2014 Danny Chouinard.\n");
fprintf(stderr,"\n");
fprintf(stderr,"Usage: sd mode_option [mode sub-options].\n");
fprintf(stderr,"\n");
fprintf(stderr," -t ... Timing mode (-th for usage).\n");
fprintf(stderr," -p ... AVR programming mode (-ph for usage).\n");
fprintf(stderr," -b ... BBTERM mode (-bh for usage).\n");
fprintf(stderr," -k Show a large clock mode.\n");
fprintf(stderr,"\n");
fprintf(stderr," -d dev Specify serial device. (All modes, \"/dev/tty\" prefix optional).\n");
fprintf(stderr,"\n");
fprintf(stderr,"Environment variables: SDDEV, SDBAUD.\n");
fprintf(stderr,"\n");
exit(1);
}
void bbtermusage(void) {
fprintf(stderr,"\n");
fprintf(stderr,"Serial Doodad, terminal mode. (C)2014 Danny Chouinard.\n");
fprintf(stderr,"\n");
fprintf(stderr,"Usage: sd -b [terminal mode options]\n");
fprintf(stderr,"\n");
fprintf(stderr," -d dev Serial device (/dev/tty optional).\n");
fprintf(stderr," -c file Set capture filename.\n");
fprintf(stderr," -b N Set baud rate.\n");
fprintf(stderr," -f Invert DTR state.\n");
fprintf(stderr," -n Don't obey special control codes from serial line.\n");
fprintf(stderr," -o Output CR upon LF.\n");
fprintf(stderr," -e Local echo.\n");
fprintf(stderr," -x Enable hardware modem signals.\n");
fprintf(stderr," -w Hex mode on.\n");
fprintf(stderr," -y Enable CTS/RTS handshake.\n");
fprintf(stderr," -a Two stop bits instead of one.\n");
fprintf(stderr," -m text Set macro string.\n");
fprintf(stderr,"\n");
fprintf(stderr,"While in terminal mode, Type Control-A H to get help.\n");
fprintf(stderr,"\n");
}
void timingusage(void) {
fprintf(stderr,"\n");
fprintf(stderr,"Serial Doodad, timing mode. (C)2014 Danny Chouinard.\n");
fprintf(stderr,"\n");
fprintf(stderr,"Usage: sd -t [timing mode options]\n");
fprintf(stderr,"\n");
fprintf(stderr," -a Ring the bell every signal.\n");
fprintf(stderr," -b baud Set baud rate.\n");
fprintf(stderr," -s N Signal input (0=RD, 1=CD, 2=DSR, 3=CTS, 4=RI).\n");
fprintf(stderr," -t N Signal transition (0:High-Low 1:Low-High, 2=Any).\n");
fprintf(stderr," -d dev Serial device (/dev/tty optional).\n");
fprintf(stderr," -f Invert DTR state.\n");
fprintf(stderr," -i ms Sampling minimum interval in milliseconds.\n");
fprintf(stderr," -l Start a long-term sample.\n");
fprintf(stderr," -p N Perform a long-term checkpoint, interval N.\n");
fprintf(stderr," -n N Do N samples (default=infinite).\n");
fprintf(stderr," -x Print timestamps.\n");
fprintf(stderr," -y N Pulse DTR at modulo N seconds.\n");
fprintf(stderr,"\n");
fprintf(stderr," -o HZ Show adjustment parameters for Tic Tac clock frequency HZ.\n");
fprintf(stderr," -w Set time on Tic Tac clock.\n");
fprintf(stderr," -u str Transmit string to Tic Tac clock.\n");
}
void programusage(void) {
fprintf(stderr,"\n");
if(!sdsdp) {
fprintf(stderr," TD 3 ---|<---/\\/\\/--- RESET\n");
fprintf(stderr," DTR 4 --------/\\/\\/--- MOSI\n");
fprintf(stderr," GND 5 ---------------- GND\n");
fprintf(stderr," DSR 6 ---------------- MISO\n");
fprintf(stderr," RTS 7 --------/\\/\\/--- SCK\n");
fprintf(stderr,"\n");
}
fprintf(stderr,"Serial Doodad, AVR programming mode");
if(sdsdp) fprintf(stderr," with SDP module");
fprintf(stderr,". (C)2014 Danny Chouinard.\n");
fprintf(stderr,"\n");
fprintf(stderr,"Usage: sd -p [programming mode options]\n");
fprintf(stderr,"\n");
fprintf(stderr," -b Use binary instead of Intel Hex file format.\n");
fprintf(stderr," -d dev Serial device (/dev/tty optional, append \"+sdp\" with SDP module).\n");
fprintf(stderr," -s N Switch baud rate on SDP module.\n");
fprintf(stderr," -i N delay in microseconds at clock transitions.\n");
fprintf(stderr," -v N Set verbose level (0-3). Use -v2 to show part and fuses.\n");
fprintf(stderr," -r FN Read FLASH contents and write into FN file.\n");
fprintf(stderr," -w FN Write FLASH contents from FN file.\n");
fprintf(stderr," -E N Write efuse.\n");
fprintf(stderr," -L N Write lfuse.\n");
fprintf(stderr," -H N Write hfuse.\n");
fprintf(stderr," -e End with RESET held disabled.\n");
fprintf(stderr," -z Perform chip erase.\n");
fprintf(stderr,"\n");
fprintf(stderr,"Environment variables: SDDEV, SDBAUD, SDPBAUD, SDPIDELAY.\n");
fprintf(stderr,"\n");
}
unsigned char getonebyte(int from, int sec, int usec) {
unsigned char c=0;
int a;
fd_set FDS;
struct timeval timeout;
timeout.tv_sec=sec;
timeout.tv_usec=usec;
FD_ZERO(&FDS);
if(from&1) FD_SET(comfd,&FDS);
if(from&2) FD_SET(0,&FDS);
a=select(comfd+1,&FDS,NULL,NULL,&timeout);
if(FD_ISSET(comfd,&FDS)) {
a=read(comfd,&c,1);
if(a) {
return(c);
}
}
return(0);
}
void readstatus(void) {
int r,status;
r=ioctl(comfd,TIOCMGET,&status);
if(r) {
fprintf(stderr,"ioctl failed with code %d.\n",r);
exit(1);
}
ri=cd=dsr=cts=0;
if(status&TIOCM_CD) cd=1;
if(status&TIOCM_DSR) dsr=1;
if(status&TIOCM_CTS) cts=1;
if(status&TIOCM_RI) ri=1;
}
void getonesample(void) {
static int oldsig=0;
int first=1;
int sig=0;
if(signaltype==0) getonebyte(1,10000,0);
else {
while(1) {
readstatus();
switch(signaltype) {
case 1: sig=cd; break;
case 2: sig=dsr; break;
case 3: sig=cts; break;
case 4: sig=ri; break;
}
if(first) {
first=0;
oldsig=sig;
} else {
if(sig!=oldsig) {
oldsig=sig;
if(transition==2) return;
if(sig==0 && transition==0) return;
if(sig!=0 && transition==1) return;
}
}
usleep(100);
}
}
}
void doprintdrift(long double ratio, int f_cpu) {
long double clock, drift,t;
unsigned int d0,d1,d2,d3,slow;
d1=d2=d3=0;
slow=0;
clock=(long double)f_cpu/f;
drift=(1.0L-ratio)*4294967296.0L;
if(drift<0.0L) {
slow=1;
drift=0.0L-drift;
}
t=drift;
d0=t/16777216.0L;
t-=d0*16777216.0L;
d1=t/65536.0L;
t-=d1*65536.0L;
d2=t/256.0L;
t-=d2*256.0L;
d3=t;
if(d0) {
printf(" [XXXXXX] XX");
} else {
printf(" [%02X%02X%02X] ",d1,d2,d3);
if(slow) printf("S");
else printf("F");
}
}
void dtrpulse(int ontime, int offtime) {
dtrtoggle();
usleep(ontime);
dtrtoggle();
usleep(offtime);
}
void xmittictacstring() {
unsigned char c,b;
int i,j;
printf("X"); fflush(stdout);
usleep(200000);
printf("M"); fflush(stdout);
dtrpulse(793457,100000); // Approx 13000/16384 second.
printf("I"); fflush(stdout);
dtrpulse(125000,100000); // Short press.
printf("T"); fflush(stdout);
dtrpulse(400000,300000); // Long press, longish delay.
printf(": "); fflush(stdout);
i=strlen(tictacstring);
tictacstring[i++]=0xff;
tictacstring[i]=0;
i=0;
while(tictacstring[i]) {
b=(unsigned char)tictacstring[i];
c=b;
for(j=0;j<8;j++) {
if(b&128) dtrpulse(4000,2000);
else dtrpulse(1000,2000);
b<<=1;
}
usleep(10000);
if(c>=32 && c<=126) printf("%c",c);
fflush(stdout);
i++;
}
usleep(100000);
}
void showpart(void) {
if(!strcmp(part,"?")) {
fprintf(stderr,"Unknown part/Read error.\n");
exit(1);
}
printf("0x%06X %s Flash: %gKB(%d[%d]). Fuses: L:0x%02X H:0x%02X E:0x%02X\n",signature,part,(double)flashsize/(double)1024,pages,pagesize,lfuse,hfuse,efuse);
// printf(" Signature = 0x%06X\n",signature);
// printf(" Flash size = %d\n",flashsize);
// printf(" Page size = %d\n",pagesize);
// printf(" Pages = %d\n",pages);
// printf(" LFUSE = 0x%02X\n",lfuse);
// printf(" HFUSE = 0x%02X\n",hfuse);
// printf(" EFUSE = 0x%02X\n",efuse);
}
void showchar(char c) {
if(c>=' ' &&c<127) putchar(c);
if(c<32) printf("^%c",'A'+c-1);
}
void showstring(char *s) {
int l;
l=strlen(s);
for(i=0;i<l;i++) showchar(s[i]);
}
uint8_t chat(char *sendstring, char ack) {
char var[20];
char val[20];
int sendtimer;
int sendtime;
int mode=0;
int index;
char recb;
char sb;
int stringlen;
int stringp;
recb=0;
index=0;
stringp=0;
mode=0;
sendtime=2+115200/baud/2;
sendtimer=0;
if(verbose>3) {
fprintf(stderr,"DCCD baud=%d, sendtime=%d, sendtimer=%d.\n",baud,sendtime,sendtimer);
}
if(verbose>2) {
printf("Chat: Sending string \"");
showstring(sendstring);
printf("\"");
if(ack) {
printf(" expecting character '");
showchar(ack);
printf("'.\n");
}
}
stringlen=strlen(sendstring);
recb=0;
while(recb!=ack || stringp<stringlen) {
recb=getonebyte(1,0,1000);
if(recb) {
sendtimer=0;
if(recb=='>') mode=1;
if(recb=='=') mode=2;
if(recb=='\r' || recb=='\n') mode=0;
if(verbose>1) {
putchar(recb);
fflush(stdout);
}
switch(recb) {
case '>': mode=1; index=0; break;
case '=': if(mode==1) mode=2; index=0; break;
case '\r': break;
case '\n':
mode=0;
if(index!=0 && val[0]!=0 && var[0]!=0) {
if(!strcmp(var,"PAGESIZE")) {
pagesize=strtol(val,NULL,0);
}
if(!strcmp(var,"PAGESIZE")) {
pagesize=strtol(val,NULL,0);
}
if(!strcmp(var,"PAGES")) {
pages=strtol(val,NULL,0);
}
if(!strcmp(var,"PART")) {
strcpy(part,val);
}
if(!strcmp(var,"SIGNATURE")) {
signature=strtol(val,NULL,0);
}
if(!strcmp(var,"LFUSE")) {
lfuse=strtol(val,NULL,0);
}
if(!strcmp(var,"EFUSE")) {
efuse=strtol(val,NULL,0);
}
if(!strcmp(var,"HFUSE")) {
hfuse=strtol(val,NULL,0);
}
}
index=0; val[0]=var[0]=0;
break;
default:
if(mode==1) {
var[index++]=recb;
var[index]=0;
}
if(mode==2) {
val[index++]=recb;
val[index]=0;
}
}
} else {
sendtimer++;
}
if(sendtimer>sendtime) {
sendtimer=0;
if(stringp<stringlen) {
sb=sendstring[stringp++];
if(sb>=32 && sb<127 && verbose>2) {
printf("\033[7m%c\033[0m",sb); fflush(stdout);
}
write(comfd,&sb,1);
}
}
}
flashsize=pagesize*pages;
return(0);
}
uint8_t chatreadflash(char *sendstring, char ack) {
int address;
char recb;
char sb;
int n,i,stringlen;
int stringp;
address=0;
recb=0;
n=0;
stringp=0;
stringlen=strlen(sendstring);
while(recb!=ack || stringp<stringlen) {
if(stringp<stringlen) {
sb=sendstring[stringp++];
write(comfd,&sb,1);
}
recb=getonebyte(1,0,10000);
if(recb=='>') {
if(verbose) printf("\r");
for(i=0;i<pagesize;i++) {
if(address>=flashsize) {
fprintf(stderr,"Woah! Read more data than flash is supposed to hold!\n.");
return(1);
}
flashbuffer[address]=getonebyte(1,2,0);
address++;
}
if(verbose==1) {
printf("\r%06X ",address);
fflush(stdout);
}
i=getonebyte(1,1,0);
if(i!='\r') {
fprintf(stderr,"Did not receive expected carriage return at end of page.\n");
return(1);
}
i=getonebyte(1,1,0);
if(i!='\n') {
fprintf(stderr,"Did not receive expected newline at end of page.\n");
return(1);
}
} else {
if(verbose>1) {
if(recb) {
putchar(recb);
fflush(stdout);
}
}
}
}
if(verbose>1) printf("\rDone. \n");
if(address!=flashsize) {
fprintf(stderr,"Oops, only read %d bytes from flash sized %d bytes.\n",address,flashsize);
return(1);
}
return(0);
}
void setbaud(int b) {
switch(b) {
case 0: speed = B0;break;
case 50: speed = B50;break;
case 75: speed = B75;break;
case 110: speed = B110;break;
case 150: speed = B150;break;
case 300: speed = B300;break;
case 600: speed = B600;break;
case 1200: speed = B1200;break;
case 2400: speed = B2400;break;
case 4800: speed = B4800;break;
case 9600: speed = B9600;break;
case 19200: speed = B19200;break;
case 38400: speed = B38400;break;
case 57600: speed = B57600;break;
case 115200: speed = B115200;break;
default: fprintf(stderr,"Invalid baudrate.\n"); exit(1);
}
tcgetattr(comfd,&stbuf);
stbuf.c_cflag&=~CBAUD;
stbuf.c_cflag|=speed;
if (tcsetattr(comfd, TCSANOW, &stbuf) < 0) {
fprintf(stderr,"Can't tcsetattr set device.\n");
exit(1);
}
}
void sck(uint8_t v) {
if(v) rtson();
else rtsoff();
}
void mosi(uint8_t v) {
if(v) dtron();
else dtroff();
}
uint8_t miso(void) {
readstatus();
return(dsr);
}
void reseton(void) {
ioctl(comfd,TIOCCBRK);
}
void resetoff(void) {
ioctl(comfd,TIOCSBRK);
}
void resetpulse(void) {
c=0;
write(comfd,&c,1);
}
uint8_t xfr(uint8_t b) {
uint8_t v;
uint8_t i;
uint8_t r;
char buf[1];
char bitshow[20];
buf[0]=0x0;
r=0;
strcpy(bitshow,"76543210 76543210");
if(verbose>2) printf("\r%02X->MISO %s MOSI->XX",b,bitshow); fflush(stdout);
for(i=0;i<8;i++) {
if(b&(128>>i)) {
mosi(1);
if(verbose>2) bitshow[i]='-';
} else {
mosi(0);
if(verbose>2) bitshow[i]='_';
}
usleep(idelay);
sck(1);
usleep(idelay);
v=miso();
sck(0);
if(v) {
if(verbose>2) bitshow[i+10]='-';
r|=(128>>i);
} else {
if(verbose>2) bitshow[i+10]='_';
}
if(verbose>2) {
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
printf("%s MOSI->%02X",bitshow,r);
}
fflush(stdout);
}
if(verbose>2) printf("\n");
return(r);
}
int xfrxpct(uint8_t b, uint8_t r) {
uint8_t rb;
rb=xfr(b);
if(rb!=r) {
fprintf(stderr,"Readback error, expected %02x, got %02x.\n",r,rb);
return(1);
}
return(0);
}
void readbackerror(void) {
fprintf(stderr,"Read back error.\n");
exit(1);
}
int8_t readlfuse(void) {
if(xfrxpct(0x50,0x00)) return(1);
if(xfrxpct(0x00,0x50)) return(1);
if(xfrxpct(0x00,0x00)) return(1);
lfuse=xfr(0x00);
if(verbose>2) printf("LFUSE = 0x%02X\n",lfuse);
return(0);
}
int8_t readefuse(void) {
if(xfrxpct(0x50,0x00)) return(1);
if(xfrxpct(0x08,0x50)) return(1);
if(xfrxpct(0x00,0x08)) return(1);
efuse=xfr(0x00);
if(verbose>2) printf("EFUSE = 0x%02X\n",efuse);
return(0);
}
int8_t readhfuse(void) {
if(xfrxpct(0x58,0x00)) return(1);
if(xfrxpct(0x08,0x58)) return(1);
if(xfrxpct(0x00,0x08)) return(1);
hfuse=xfr(0x00);
if(verbose>2) printf("HFUSE = 0x%02X\n",hfuse);
return(0);
}
int readsignature(void) {
uint8_t b;
if(sdsdp) return(0); // This is all done by inq on SDP module.
signature=0;
for(i=0;i<3;i++) {
if(xfrxpct(0x30,0x00)) return(1);
if(xfrxpct(0x00,0x30)) return(1);
if(xfrxpct(i,0x00)) return(1);
signature<<=8;
b=xfr(0x00);
signature|=b;
if(verbose>1) printf("Signature byte %d = 0x%02X\n",i,b);
}
switch(signature) {
case 0x1E9483: strcpy(part,"AT90PWM[23]16"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9383: strcpy(part,"AT90PWM[23]B"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E9388: strcpy(part,"AT90PWM81"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E96C1: strcpy(part,"AT90SCR100"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9382: strcpy(part,"ATA6289"); pagesize=64; flashsize=8192; break;
case 0x1E9781: strcpy(part,"CAN128"); pagesize=256; flashsize=0x1FFFF; break;
case 0x1E9581: strcpy(part,"CAN32"); pagesize=256; flashsize=0x7FFF; break;
case 0x1E9681: strcpy(part,"CAN64"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9703: strcpy(part,"ATmega1280"); pagesize=256; flashsize=0x1FFFF; break;
case 0x1E9704: strcpy(part,"ATmega1281"); pagesize=256; flashsize=0x1FFFF; break;
case 0x1E9705: strcpy(part,"ATmega1284P"); pagesize=256; flashsize=0x1FFFF; break;
case 0x1EA701: strcpy(part,"ATmega128RFA1"); pagesize=256; flashsize=0x1ffff; break;
case 0x1E9401: strcpy(part,"ATmega161"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9404: strcpy(part,"ATmega162"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9402: strcpy(part,"ATmega163"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E940F: strcpy(part,"ATmega164"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9407: strcpy(part,"ATmega165[P]"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E940B: strcpy(part,"ATmega168P"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9405: strcpy(part,"ATmega169[P][A]"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9403: strcpy(part,"ATmega16[A]"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E940C: strcpy(part,"ATmega16HVA"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E940E: strcpy(part,"ATmega16HVA2"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E940D: strcpy(part,"ATmega16HVB"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9484: strcpy(part,"ATmega16M1"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9489: strcpy(part,"ATmega16U2"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9488: strcpy(part,"ATmega16U4"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9801: strcpy(part,"ATmega2560"); pagesize=256; flashsize=0x3FFFF; break;
case 0x1E9802: strcpy(part,"ATmega2561"); pagesize=256; flashsize=0x3FFFF; break;
case 0x1E9502: strcpy(part,"ATmega32"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9501: strcpy(part,"ATmega323"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9511: strcpy(part,"ATmega324PA"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9505: strcpy(part,"ATmega325"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9506: strcpy(part,"ATmega3250"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E950F: strcpy(part,"ATmega328P"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9503: strcpy(part,"ATmega329"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9504: strcpy(part,"ATmega3290"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9586: strcpy(part,"ATmega32C1"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9510: strcpy(part,"ATmega32HVB"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9584: strcpy(part,"ATmega32M1"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E958A: strcpy(part,"ATmega32U2"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9587: strcpy(part,"ATmega32U4"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9588: strcpy(part,"ATmega32U6"); pagesize=128; flashsize=0x7FFF; break;
case 0x1E9507: strcpy(part,"ATmega406"); pagesize=128; flashsize=0x9FFF; break;
case 0x1E920A: strcpy(part,"ATmega48P"); pagesize=64; flashsize=0xFFF; break;
case 0x1E9608: strcpy(part,"ATmega640"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9609: strcpy(part,"ATmega644"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E960A: strcpy(part,"ATmega644P[A]"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9605: strcpy(part,"ATmega645"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9606: strcpy(part,"ATmega6450"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9603: strcpy(part,"ATmega649"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9604: strcpy(part,"ATmega6490"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E960B: strcpy(part,"ATmega649"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9686: strcpy(part,"ATmega64C1"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9610: strcpy(part,"ATmega64HVE"); pagesize=128; flashsize=0xFFFF; break;
case 0x1E9684: strcpy(part,"ATmega64M1"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E9307: strcpy(part,"ATmega8"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E9306: strcpy(part,"ATmega8515"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E9308: strcpy(part,"ATmega8535"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E930F: strcpy(part,"ATmega88P[A]"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E9389: strcpy(part,"ATmega8U2"); pagesize=128; flashsize=0x1FFF; break;
case 0x1E9003: strcpy(part,"ATtiny10"); pagesize=32; flashsize=0x3FF; break;
case 0x1E9007: strcpy(part,"ATtiny13"); pagesize=32; flashsize=1024; break;
case 0x1E9487: strcpy(part,"ATtiny167"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E910F: strcpy(part,"ATtiny20"); pagesize=64; flashsize=0x7FF; break;
case 0x1E910A: strcpy(part,"ATtiny2313[A]"); pagesize=32; flashsize=0x7FF; break;
case 0x1E910B: strcpy(part,"ATtiny24[A]"); pagesize=32; flashsize=0x7FF; break;
case 0x1E9108: strcpy(part,"ATtiny25"); pagesize=32; flashsize=0x7FF; break;
case 0x1E910C: strcpy(part,"ATtiny261[A]"); pagesize=32; flashsize=0x7FF; break;
case 0x1E900A: strcpy(part,"ATtiny4"); pagesize=32; flashsize=0x1FF; break;
case 0x1E920E: strcpy(part,"ATtiny40"); pagesize=64; flashsize=0xFFF; break;
case 0x1E920D: strcpy(part,"ATtiny4313"); pagesize=64; flashsize=0xFFF; break;
case 0x1E920C: strcpy(part,"ATtiny43U"); pagesize=64; flashsize=0xFFF; break;
case 0x1E9207: strcpy(part,"ATtiny44[A]"); pagesize=64; flashsize=0xFFF; break;
case 0x1E9206: strcpy(part,"ATtiny45"); pagesize=64; flashsize=0xFFF; break;
case 0x1E9208: strcpy(part,"ATtiny461[A]"); pagesize=64; flashsize=0xFFF; break;
case 0x1E9209: strcpy(part,"ATtiny48"); pagesize=64; flashsize=0xFFF; break;
case 0x1E9009: strcpy(part,"ATtiny5"); pagesize=32; flashsize=0x1FF; break;
case 0x1E930C: strcpy(part,"ATtiny84[A]"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E930B: strcpy(part,"ATtiny85"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E930D: strcpy(part,"ATtiny861[A]"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E9387: strcpy(part,"ATtiny87"); pagesize=128; flashsize=0x1FFF; break;
case 0x1E9311: strcpy(part,"ATtiny88"); pagesize=64; flashsize=0x1FFF; break;
case 0x1E9008: strcpy(part,"ATtiny9"); pagesize=32; flashsize=0x3FF; break;
case 0x1E9782: strcpy(part,"AT90USB128[67]"); pagesize=256; flashsize=0x1FFFF; break;
case 0x1E9482: strcpy(part,"AT90USB162"); pagesize=128; flashsize=0x3FFF; break;
case 0x1E9682: strcpy(part,"AT90USB64[67]"); pagesize=256; flashsize=0xFFFF; break;
case 0x1E974C: strcpy(part,"ATxmega128A1"); pagesize=512; flashsize=139264; break;
case 0x1E9742: strcpy(part,"ATxmega128A3"); pagesize=512; flashsize=139264; break;
case 0x1E9748: strcpy(part,"ATxmega128D3"); pagesize=512; flashsize=139264; break;
case 0x1E9441: strcpy(part,"ATxmega16A4"); pagesize=256; flashsize=20480; break;
case 0x1E9442: strcpy(part,"ATxmega16D4"); pagesize=256; flashsize=20480; break;
case 0x1E9744: strcpy(part,"ATxmega192A3"); pagesize=512; flashsize=204800; break;
case 0x1E9749: strcpy(part,"ATxmega192D3"); pagesize=512; flashsize=204800; break;
case 0x1E9842: strcpy(part,"ATxmega256A3"); pagesize=512; flashsize=270336; break;
case 0x1E9843: strcpy(part,"ATxmega256A3B"); pagesize=512; flashsize=270336; break;
case 0x1E9844: strcpy(part,"ATxmega256D3"); pagesize=512; flashsize=270336; break;
case 0x1E9541: strcpy(part,"ATxmega32A4"); pagesize=256; flashsize=36864; break;
case 0x1E9542: strcpy(part,"ATxmega32D4"); pagesize=256; flashsize=36864; break;
case 0x1E964E: strcpy(part,"ATxmega64A1"); pagesize=256; flashsize=69632; break;
case 0x1E9642: strcpy(part,"ATxmega64A3"); pagesize=256; flashsize=69632; break;
case 0x1E964A: strcpy(part,"ATxmega64D3"); pagesize=256; flashsize=69632; break;
default: fprintf(stderr,"Unknown part!\n"); return(1);
}
if(flashsize) {
flashsize++;
pages=flashsize/pagesize;
}
if(readlfuse()) return(1);
if(readhfuse()) return(1);
if(readefuse()) return(1);
return(0);
}
uint programinit(void) {
int n;
char tmpstr[64];
if(sdsdp) {
dtron();
for(i=0;i<20;i++) {
getonebyte(1,0,1000); // Flush input and delay a little.
}
dtroff();
usleep(10000);
chat("\r",'#');
usleep(10000);
if(pbaud!=baud && pbaud!=0) {
if(verbose>2) printf("Switching temporary baud rate to %d.\n",pbaud); fflush(stdout);
sprintf(tmpstr,"sbr%d\r",16000000/5/pbaud);
chat(tmpstr,'#');
usleep(20000);
baud=pbaud;
setbaud(baud);
if(verbose>2) printf("Switched.\n"); fflush(stdout);
usleep(20000);
}
sprintf(tmpstr,"ver%d\r",verbose);
chat(tmpstr,'#');
sprintf(tmpstr,"del%d\r",idelay);
chat(tmpstr,'#');
chat("inq\r",'#');
if(verbose>1) printf("\n");
} else {
sck(0);
reseton();
if(verbose>1) printf("Entering AVR programming mode.\n"); fflush(stdout);
n=0;
while(n<5) {