-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsb.bas
1853 lines (1478 loc) · 63.3 KB
/
tsb.bas
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
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
' CONSOLE TOOL FOR TINYSAFEBOOT
' ... the tiny and safe bootloader for AVR-ATtinys and ATmegas
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
' Written in 2011-2015 by Julien Thomas <joytec@gmx.de>
'
' 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/>.
'
' ----------------------------------------------------------------------
'
' To be compiled with FreeBasic 0.90 up
'
'-----------------------------------------------------------------------
' VARIABLE AND CONSTANTS DEFINITIONS
'-----------------------------------------------------------------------
'
#lang "fb"
' Working variables
dim astr as string
dim bstr as string
dim cstr as string
dim a as ubyte
dim b as ubyte
dim i as uinteger
dim j as uinteger
dim k as uinteger
dim z as uinteger
' Shared constants and variables
const Comset = ",N,8,1,CS,DS,RB0,TB0,ME,BIN"
dim shared Comport as string
dim shared ONEWIRE as ubyte = 0
dim shared JMPMODE as ubyte = 0
dim shared TINYMEGA as ubyte = 0
dim shared SLOWMODE as ubyte = 0
dim shared SILENT as ubyte = 0
dim shared BIGMEGA as ubyte = 0 ' address more than 16 bit
dim shared Filename as string
dim shared TSBIDENT as string
dim shared TSBBUILD as uinteger
dim shared TSBSTATUS as ubyte
dim shared SIG000 as ubyte
dim shared SIG001 as ubyte
dim shared SIG002 as ubyte
dim shared PAGESIZE as ushort
dim shared FLASHSIZE as uinteger
dim shared APPFLASH as uinteger
dim shared EEPROMSIZE as ushort
dim shared APPJUMP as uinteger
dim shared TIMEOUT as ubyte
dim shared PASSWORD as string
dim shared AAAA as uinteger = 0
dim shared ADDR as uinteger = 0
dim RXTXPB (3,1) as ubyte ' opcode conversion array
dim shared DEVPORTS(6) as ubyte ' device i/o ports data-base
const maxarray as uinteger = 1048576
dim shared BINARRAY(maxarray) as ubyte : clear BINARRAY(0), 255, maxarray
dim shared LASTPAGE(256) as ubyte : clear LASTPAGE(0), 255, 256
const REQUEST as string = "?"
const CONFIRM as string = "!"
'-----------------------------------------------------------------------
' SUBS AND FUNCTIONS
'-----------------------------------------------------------------------
declare sub SendCommand (as string)
declare sub DeactivateTSB()
declare sub ShowDeviceInfo()
declare sub TSBChecksum()
declare function StripSwitch(byref argument as string) as string
declare function CheckChecksum (as string) as ubyte
declare function RXBuffer () as string
declare function ReadLASTPAGE () as ubyte
declare function VerifyLASTPAGE () as ubyte
declare function Word2Date (byref InWord as ushort) as uinteger
declare function ActivateTSB (byref Comport as string) as ubyte
declare function LoadToArray () as ubyte
declare function LoadBinToArray () as ubyte
declare function LoadHexToArray () as ubyte
declare function SaveFromArray () as ubyte
declare function Check4SPM() as ubyte
declare function CurrentDateBlock () as string
declare function FWnumber () as string
declare function WriteLASTPAGE () as ubyte
declare function DatasToArray (byref avrname as string) as ubyte
declare function DatasToPortMatrix (byref avrname as string) as ubyte
declare function SignatureToDevicename (byref S0 as ubyte, S1 as ubyte, S2 as ubyte) as string
declare function ISODATE () as string
declare function passwordinput(byref pwprompt as string) as string
'-----------------------------------------------------------------------
function ISODATE () as string
return (left$(__DATE_ISO__,4) + mid$(__DATE_ISO__,6,2) + right$(__DATE_ISO__,2))
end function
function StripSwitch (byref argument as string) as string
return (UCase$(trim (argument, any "-/")))
end function
function CheckChecksum (byref ihline as string) as ubyte
dim I as ubyte
dim C as ubyte
if ihline = "" then return (255)
for I = 2 To (len (ihline)) Step 2
C = C + Val ("&h"+ (mid$(ihline,i,2)))
next I
C = 0 - C ' equal to +1 and 2's complement (type is ubyte)
'with checksum also added, this should be 0 if record line was o.k.
return (C)
end function
sub SendCommand (byref astr as string)
dim a as ushort
dim t as ushort
dim bstr as string
if len(astr) = 0 then exit sub
print #8, astr; ' send commando/data string
if ONEWIRE then ' strip off local echo
a = 0
do until a = len(astr)
t = timer + 1
if SLOWMODE = 1 then t = timer + 10
do until (loc(8)) or (timer > t) : loop
bstr = input$(1, #8)
a = a + 1
loop
endif
end sub
function RXBuffer () as string
dim a as ushort
dim t as uinteger
dim astr as string
dim bstr as string
bstr = ""
if SLOWMODE = 0 then
t = timer + 3 : do until loc(8) or (timer > t) : loop
if loc(8) = 0 then return ("")
t = timer + 3
do until eof(8) or (timer > t)
a = loc(8) : sleep 100 : a = loc(8) - a
if a = 0 then
astr = input$(loc(8), #8)
else
astr = input$(1, #8)
endif
bstr = bstr + astr
loop
else
t = timer + 10 : do until loc(8) or (timer > t) : loop
if loc(8) = 0 then return ("")
t = timer + 10
do until eof(8) or (timer > t)
a = loc(8) : sleep 3000 : a = loc(8) - a
if a = 0 then
astr = input$(loc(8), #8)
else
astr = input$(1, #8)
endif
bstr = bstr + astr
loop
endif
return (bstr)
end function
function ReadLASTPAGE () as ubyte
'load userdata from LASTPAGE into array LASTPAGE()
dim i as ushort
dim bstr as string
print #99, "READ USER DATA ... ";
SendCommand ("c")
bstr = RXBuffer
if len(bstr) < PAGESIZE+1 then return (255) ' page read error
if right$(bstr,1) <> CONFIRM then return (255) ' block not terminated
clear LASTPAGE(0), 255, 256
for i = 0 To PAGESIZE-1 : LASTPAGE (i) = bstr[i] : next i
APPJUMP = (LASTPAGE(0) + LASTPAGE(1)*256) ' low/high byte in memory
if TINYMEGA = 1 then APPJUMP = 0000 ' ATmegas APPJUMP not relevant
TIMEOUT = LASTPAGE(2) ' load timeout byte from LASTPAGE
i = 3 : PASSWORD = ""
do until (i = PAGESIZE) or (LASTPAGE(i) = 255)
PASSWORD = PASSWORD + chr$(LASTPAGE(i))
i = i + 1
loop
print #99, "OK" : print #99, ""
return (0)
end function
function VerifyLASTPAGE () as ubyte
dim i as ushort
dim bstr as string
print #99, "VERIFY USER DATA ... ";
SendCommand ("c") ' reload LASTPAGE
bstr = RXBuffer
if len(bstr) < 16 then return (255) ' page not correctly read
if right$(bstr,1) <> CONFIRM then return (255) ' block not terminated
for i = 0 to len(bstr)-1
if LASTPAGE (i) <> bstr[i] then exit for
next i
if i < (len(bstr)-1) then return (255) ' oh no, errors detected!
print #99, "OK" : print #99, ""
return (0) ' verification against LASTPAGE successfull
end function
function Word2Date (byref InWord as ushort) as uinteger
Word2Date = ( InWord and 31 ) + _
((InWord and 480) \ 32) * 100 + _
((Inword and 65024 ) \ 512) * 10000 _
+ 20000000
end function
function ActivateTSB (byref Comport as string) as ubyte
dim bstr as string
if open COM (Comport + Comset for binary as #8) > 0 then return (Err)
sleep 100
if SLOWMODE = 1 then
sleep 500
print #99, ""
print #99, "SLOW MODE. Please stand by ..."
endif
print #8, "@@@"; ' first try without password
' with no reaction, password may be needed
bstr = RXBuffer
if left$(bstr,3) = "@@@" then
ONEWIRE = 1 ' one-wire echo detected
bstr = right$(bstr, (len(bstr)-3)) ' detach echoed characters
print #99, "" : print #99, "ONE-WIRE INTERFACE DETECTED."
endif
if bstr = "" then
print
PASSWORD = passwordinput ("Password : ")
SendCommand (PASSWORD) ' submit password
bstr = RXBuffer
if bstr <> "" then ' now we got a reply
print : print "Password ... OK"
else return (255)
endif
endif
' analyze tsb's reply to gain device data (permanent data string)
if right$(bstr,1) <> CONFIRM then return (255) ' no good
if lcase$(left$(bstr,3)) <> "tsb" then return (255) ' no good
dim BUILDWORD as ushort
TSBIDENT = left$(bstr,3)
BUILDWORD = bstr[3] + bstr[4] * 256
TSBSTATUS = bstr[5]
SIG000 = bstr[6]
SIG001 = bstr[7]
SIG002 = bstr[8]
PAGESIZE = bstr[9] * 2 ' WORDS * 2 = BYTES
APPFLASH = (bstr[10] + bstr[11] * 256) * 2 ' WORDS * 2 = BYTES
FLASHSIZE = ((APPFLASH \ 1024) + 1) * 1024 ' round up to next higher multiple of 1024
EEPROMSIZE = (bstr[12] + bstr[13] * 256) + 1 ' EEPROM size always BYTES
' NOTE: While AVR/Assembler is accounting flash addresses space in WORDS
' (except from EEPROM locations), at PC side we are using BYTES
'
' Check for regular PAGESIZEs (transmission successfull)
if (PAGESIZE mod 16) > 0 then
print #99, "PAGESIZE NOT VALID - ABORT."
return (255)
endif
' consider TSB firmware with new date identifier and status byte
if BUILDWORD < 32768 then
TSBBUILD = Word2Date (BUILDWORD)
else 'old date encoding in tsb-fw (three bytes)
TSBBUILD = BUILDWORD + 65536 + 20000000
endif
' detect whether device is ATtiny or ATmega from identifier
' in last byte of device info block
' while decision for jmp/rjmp depends on memory size
select case asc(mid$(bstr,15,1))
case &h00 : JMPMODE = 0 : TINYMEGA = 0
case &h0C : JMPMODE = 1 : TINYMEGA = 0
case &hAA : JMPMODE = 0 : TINYMEGA = 1
end select
print #99, ""
return ReadLASTPAGE() ' 0 = OK / <> 0 = ERROR
end function
sub DeactivateTSB ()
print #8, "q";
close #8
end sub
function LoadBinToArray () as ubyte
clear BINARRAY(0), 255, maxarray
dim ff as ubyte = freefile
dim i as ubyte
dim as uinteger Max_AAAA = 0
' Load binary file
AAAA = 0
if open (Filename for binary as ff) <> 0 then
print "File not found!" : return (1)
endif
if lof(ff) = 0 then return (1)
do while AAAA < lof (ff)
BINARRAY(AAAA) = asc(input$(1,ff))
AAAA = AAAA + 1
loop
if AAAA <> lof(ff) then return (1) ' load error
close ff
' if function leaves here, the binary data from file has been loaded
' into BINARRAY() and AAAA is the number of bytes *sharply*.
return (0)
end function
function LoadHexToArray () as ubyte
clear BINARRAY(0), 255, maxarray
dim ff as ubyte = freefile
dim ihline as string
dim LL as ubyte
dim i as ubyte
dim as uinteger Max_AAAA = 0
' Try to load ihex encoded file by default.
' Return values:
' 0 = success
' 1 = file not found
' 2 = no valid ihex
' 3 = checksum error
' Load ihex file
if open (Filename for input as #ff) <> 0 then close ff: return (1)
While not eof(ff)
line input #ff, ihline ' fetch whole lines (CR/LF-terminated)
if left$(ihline,1) <> ":" then close ff : return (2) ' no valid ihex
ihline = UCase$(ihline)
if CheckChecksum (ihline) then close ff : return (3) ' checksum error
if mid$(ihline, 8,2) = "00" then
LL = Val("&h"+mid$(ihline,2,2))
AAAA = Val("&h"+mid$(ihline,4,4))
for i = 1 To LL
BINARRAY(AAAA) = val("&h" + mid$( ihline, 8 + i*2 , 2 ) )
if AAAA > Max_AAAA then Max_AAAA = AAAA: endif
AAAA = AAAA+1
next i
endif
Wend
AAAA = Max_AAAA ' set AAAA to max address of any hexfile content loaded
AAAA = AAAA+1 ' set return value of AAAA to AAAA+1 ( = number of bytes)
close ff
' if function leaves here, contents of the hex file have been successfully
' loaded into BINARRAY() with AAAA as the exact number of bytes thereof.
return (0)
end function
function LoadToArray () as ubyte
select case LoadHexToArray()
case 1 : print #99, "File not found." : return (1)
case 2 : if LoadBinToArray () <> 0 then return(1)
case 3 : print #99, "Checksum error in Hex record." : return (1)
end select
return (0)
end function
function SaveFromArray () as ubyte
if AAAA = 0 then return (1)
ADDR = 0
dim ff as ubyte = freefile
dim i as ubyte
dim C as ubyte
dim astr as string
const CRLF as string = chr$(13) + chr$(10) ' all lines CRLF terminated
if lcase$(right$(Filename,4)) = ".hex" _
or lcase$(right$(Filename,4)) = ".eep" then
' Save Intel HEX encoded file
if AAAA > 2047 then ' skip large areas of empty/undefined flash
do until (BINARRAY(ADDR) <> 255) or (ADDR > AAAA)
ADDR = ADDR + 1
loop
endif
if open (Filename for output as #ff) <> 0 then return (Err)
print #ff, ":020000020000FC"; CRLF; ' heading < 64 KB only
'print #ff, ":020000021000EC"; CRLF; ' heading for 128 KB devs
'print #ff, ":020000023000CC"; CRLF; ' heading for 256 KB devs
'print #ff, ":020000027000CC"; CRLF; ' heading for 512 KB devs
'print #ff, ":02000002F000CC"; CRLF; ' heading for 1024 KB devs
do until ADDR >= AAAA
astr = ":10" + whex (ADDR, 4) + "00"
for i = 1 to 16
astr = astr + whex(BINARRAY(ADDR),2)
ADDR = ADDR + 1
next i
C = 0
for i = 2 to (len (astr)) step 2
C = C + val ("&h"+ (mid$(astr,i,2)))
next I
C = 0 - C
astr = astr + whex(C,2)
print #ff, astr; CRLF;
loop
astr = ":00000001FF" : print #ff, astr; CRLF; ' end of file record
else
' Save binary file
ADDR = 0
if open (Filename for output as #ff) <> 0 then return (Err)
do until (ADDR = AAAA)
print #ff, chr$(BINARRAY(ADDR));
ADDR = ADDR + 1
loop
endif
close ff
return (Err)
end function
function Check4SPM() as ubyte
dim CheckADDR as ushort = 0
do until CheckADDR >= AAAA
if BINARRAY(CheckADDR)=&hE8 and BINARRAY(CheckADDR+1)=&h95 then
exit do
endif
CheckADDR = CheckADDR + 2
loop
if CheckADDR\2 < AAAA\2 then return (255) else return (0)
end function
function CurrentDateBlock () as string
' this function is currently not needed
dim bstr as string * 8
bstr = right$(date$, 4)
bstr = bstr + left$(date$,2)
bstr = bstr + mid$(date$,4, 2)
return bstr
end function
function FWnumber () as string
dim fstr as string * 8
dim taddr as ushort
dim BUILDWORD as ushort
taddr = AAAA - 256 ' start search well below last page
fstr = ""
do ' find TSB's device info block
taddr = taddr + 1
fstr = chr$(BINARRAY(taddr)) + chr$(BINARRAY(taddr+1)) + chr$(BINARRAY(taddr+2))
loop until (fstr = "tsb") or (fstr = "TSB") or (taddr = AAAA)
if taddr = AAAA then return ("")
BUILDWORD = (BINARRAY(taddr+3) + BINARRAY(taddr+4)*256)
if BUILDWORD < 32768 then
fstr = str$( Word2Date(BUILDWORD) )
else
fstr = str$( BINARRAY(taddr+3) + BINARRAY(taddr+4) * 256 + 65536 + 20000000)
endif
return fstr
end function
function WriteLASTPAGE () as ubyte
dim i as ushort
dim astr as string
dim bstr as string
print #99, "WRITE USER DATA ... ";
SendCommand ("C") ' command Change for TSB
if RXBuffer <> REQUEST then return (255) ' would be error
for i = 0 to PAGESIZE-1
Astr = Astr + chr$(LASTPAGE(i))
next i
SendCommand (CONFIRM + Astr)
if RXBuffer = CONFIRM then return (255) ' would be error
print #99, "OK" : print #99, ""
return (0) ' write of LASTPAGE successfully finished
end function
function DatasToArray (byref avrname as string) as ubyte
Clear BINARRAY(0), 255, maxarray
dim ihline as string
dim LL as ubyte
dim i as ubyte
dim max_AAAA as UInteger : max_AAAA = 0
avrname = lcase$(avrname)
restore
' search for beginning of tsb-templates / tt-datas
' (included and compiled into this program)
do
read ihline
loop until lcase$(ihline) = "begin templates" or ihline = ""
if ihline ="" then return (1) ' serious problem: no tt-datas found!
' o.k. datas found, now parse for the desired attiny
do
read ihline
loop until (ihline = avrname) or lcase$(ihline) = "end templates"
if ihline = "end templates" then return (1) ' did not find that stuff
do
read ihline 'fetch hex record from datas
if CheckChecksum (ihline) then return (1)
if mid$(ihline, 8,2) = "00" then
LL = Val("&h"+mid$(ihline,2,2))
AAAA = Val("&h"+mid$(ihline,4,4))
for i = 1 to LL
BINARRAY(AAAA) = Val("&h"+mid$(ihline, 8+i*2,2))
if AAAA > max_AAAA then max_AAAA=AAAA
AAAA = AAAA+1
next i
endif
loop until ucase$(ihline) = ":00000001FF" 'ihex terminator
AAAA = Max_AAAA+1 'set AAAA to max address of hexfile contents + 1
return (0)
end function
function DatasToPortMatrix (byref avrname as string) as ubyte
dim astr as string
dim a as Ubyte
dim i as Ubyte
avrname = lcase$(avrname)
' search datas of port definitions
restore
do
read astr
loop until lcase$(astr) = "begin devices" or astr = ""
if astr ="" then return (1)
' o.k. found them, now parse the desired ATtiny
do
read astr
astr = lcase$(astr)
loop until (astr = avrname) or lcase$(astr) = "end devices"
if astr ="end devices" then return (1) ' no match found
' device found, now skip the 3 signature bytes
read a, a, a
' load and convert port assignments
for i = 0 to 6
read DEVPORTS(i)
if DEVPORTS(i) < &hFF then DEVPORTS(i) = DEVPORTS(i) * 8
next i
return (0)
end function
function SignatureToDevicename (byref S0 as Ubyte, S1 as ubyte, S2 as ubyte) as string
dim astr as string
dim a as ubyte
dim avrname as string
dim i as ubyte
dim as ubyte Sig0, Sig1, Sig2
' find signature-to-devicename mapping for given signature
restore
do
read astr
loop until lcase$(astr) = "begin devices" or astr = ""
if astr ="" then return ("DATABASE ERROR")
' o.k. found them, now parse for given signature
do
read avrname ' read Device Name
read Sig0, Sig1, Sig2 ' read Device Signature
read a,a,a,a,a,a,a ' dummy read further port data
a = 0
if (S0 = Sig0) and (S1 = Sig1) and (S2 = Sig2) then a = 1
loop until (a=1) or lcase$(avrname) = "end devices"
if avrname ="end devices" then return ("UNKNOWN DEVICE") ' no match found
if left$(avrname,2) = "tn" then avrname = trim (avrname, "tn") : avrname = "ATtiny" + avrname
if left$(avrname,1) = "m" then avrname = trim (avrname, "m") : avrname = "ATmega" + avrname
if right$(avrname,1)= "A" then avrname = left$(avrname, len(avrname)-1)
return (avrname)
end function
sub ShowDeviceInfo()
print #99,
print #99, "TINY SAFE BOOTLOADER"
print #99, "VERSION : "; TSBBUILD
print #99, "STATUS : "; whex(TSBStatus)
print #99, "SIGNATURE : "; whex(SIG000,2);" "; whex(SIG001,2);" "; whex(SIG002,2)
print #99, "DEVICE : "; SignatureToDevicename(SIG000, SIG001, SIG002)
print #99, "FLASH : "; FLASHSIZE
print #99, "APPFLASH : "; APPFLASH
print #99, "PAGESIZE : "; PAGESIZE
print #99, "EEPROM : "; EEPROMSIZE
print #99, "APPJUMP : "; whex(APPJUMP,4)
print #99, "TIMEOUT : "; TIMEOUT
print #99,
end sub
sub TSBChecksum()
' Parse loaded and customized TSB-Firmware/Installer in BINARRAY():
'
' (1) Found TSB-Installer = calculate checksum beginning from 2nd page
' and store in the last 2 bytes prior to TSB-Firmware-segment
' (2) Found TSB- or other firmware =
' do not change anything
dim i as ushort
dim j as ushort
dim FWChecksum as ushort = &h0000
i = 3 ' skip possible RJMP/JMP in 1st page (TSB-Installer)
do : i = i + 1 : loop until (BINARRAY(i)) < 255 or (i > 256)
if i < 8 then return ' apparently no TSB-Installer
if i > 256 then return ' possibly TSB-Firmware, no checksum 2b calc'd
j = i + 2
i = i + 4
BINARRAY(j+0) = 0
BINARRAY(j+1) = 0
BINARRAY(AAAA) = 255
do
FWChecksum = FWChecksum + BINARRAY(i)
i = i + 1
loop until i = AAAA
BINARRAY(j+0) = FWChecksum \ 256
BINARRAY(j+1) = FWChecksum - (FWChecksum \ 256) * 256
end sub
function Passwordinput(byref pwprompt as String = "") as String
dim pstr As string
dim pchr As string
dim a as Ubyte
dim b as Ubyte
print pwprompt;
a = 0
b = 0
do
pchr = inkey$
if pchr <> "" then
a = asc (pchr)
if a = 13 then exit do ' enter
if a = 8 then
if b > 0 then
print chr$(8); ' backspace
print chr$(32);
print chr$(8);
b = b - 1
pstr = left$(pstr, b)
endif
else
pstr = pstr + chr$(a) : b = b + 1
print "*";
endif
endif
loop
print ""
return (pstr)
end function
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
' MAIN PROGRAM - PARSE COMMANDLINE
'-----------------------------------------------------------------------
Mainprogram:
' very last argument may be 'silent-switch'
if StripSwitch (command(__FB_ARGC__ - 1)) = "S" then
' no output to screen
open "NUL" for output as #99 : SILENT = 1
else
' messages output to screen
open "SCRN" for output as #99 : SILENT = 0
endif
if __FB_ARGC__ < 2 then goto HelpScreen
astr = StripSwitch(command$(1))
if astr = "" then goto HelpScreen
if astr = "L" then goto Licensenote
if astr = "H" or astr = "?" then goto HelpScreen
' 1st argument may be either some COM-Port or AVR-Devicename
astr = lcase$(command$(1))
If left$(astr,3) <> "com" and _
left$(astr,4) <> "/dev" and _
left$(astr,4) <> "/tty" then goto MakeMode ' make bootloader
Comport = command$(1) ' user wants to talk to the bootloader
' check comport string for baudrate
i = 0 : Comport = trim (Comport, ":")
do : i=i+1 : loop until (i=len(Comport)) or (mid$(Comport,i,1) = ":")
if i = (len(Comport)) then
' no baudrate specified, use default
Comport = Comport + ":9600"
else ' baudrate specified, decide for slow speed mode
if (val(right$(Comport, (len(Comport) - i ))) < 600) then SLOWMODE = 1
endif
astr = StripSwitch(command$(2)) ' check mode switch
' these options don't require filename
If astr = "" then goto ShowInfo
If astr = "I" then goto ShowInfo
If astr = "?" then goto HelpScreen
If astr = "P" then goto PasswordChange
If astr = "T" then goto TimeoutChange
If astr = "EE" then goto EEPROMerase
If astr = "FE" then goto FLASHerase
If astr = "XXX" then goto EmergencyErase
' these options require filename
Filename = (command$(3)) ' get filename
If Filename = "" then goto LError
If astr = "FR" then goto FLASHread
If astr = "FW" then goto FLASHwrite
If astr = "FV" then goto FLASHverify
If astr = "ER" then goto EEPROMread
If astr = "EW" then goto EEPROMwrite
if astr = "EV" then goto EEPROMverify
' other unspecified commands
goto LError
'-----------------------------------------------------------------------
' END COMMANDLINE PARSER
'-----------------------------------------------------------------------
'
'
'
'
'-----------------------------------------------------------------------
' MAKE CUSTOMIZED TSB FIRMWARE FLASH FILE FROM FIRMWARE TEMPLATE
'-----------------------------------------------------------------------
' Search internal database for template, make TSB with customized ports.
MakeMode:
AAAA = 0
ADDR = 0
astr = lcase$(astr)
Filename = Astr
print #99, ""
'
' STEP 1:
' First we search the Database for pre-assembled code template
' of the respective device that matches the given Filename.
'
if len (StripSwitch(Filename)) < 2 then goto LError
if DatasToArray(Filename) then
print #99, "Sorry, this Device is not in the database yet." : print #99, ""
goto LError
endif
'
' STEP 2:
' If Template has been successfully loaded, we get the port definitions
' (ports existing, ports i/o) for that device.
'
if DatasToPortMatrix(Filename) <> 0 then
print #99, "Matrix error." : print #99, ""
goto LError
endif
if AAAA < 250 then goto LError ' binary way too short
if Len (command$(2)) <> 4 then goto LError ' port definitions seem ok
' Filename for saving customized tsb
if command$(3) = "" then ' if no filename specified, save in hex anyway
Filename = "tsb_" + astr + "_" + _
lcase$(command$(2)) + "_" + FWnumber$() + ".hex"
else Filename = command$(3) ' otherwise try using filename from commandline
endif
'
' STEP 3:
' Prepare working array of RX/TX addresses
' for the ports of chosen device.
'
astr = UCase$(command$(2))
'valid port name ranges from "A" to "G"
a = asc(mid$(astr,1,1)) : if a < 65 or a > 71 then goto LError
a = a - 65 ' result can be 0,1,2,3
'valid port bits range from "0" to "7"
b = val (mid$(astr,2,1))
if b > 7 then print #99, "Portbit must range from 0 to 7." : goto LError
if DEVPORTS(a) = &hFF then print #99, "Invalid port assignment." : goto LError
RXTXPB(0,0) = DEVPORTS (a) + 0 + b ' PINx
RXTXPB(1,0) = DEVPORTS (a) + 8 + b ' DDRx
RXTXPB(2,0) = DEVPORTS (a) + 16 + b ' PORTx
'ensure that port name ranges from "A" to "G"
a = asc(mid$(astr,3,1)) : if a < 65 or a > 71 then goto LError
a = a - 65
'ensure that port bit ranges from "0" to "7"
b = val (mid$(astr,4,1))
if b > 7 then print #99, "Portbit must range from 0 to 7." : goto LError
if DEVPORTS(a) = &hFF then print #99, "Invalid port assignment." : goto LError
RXTXPB(0,1) = DEVPORTS (a) + 0 + b ' PINx
RXTXPB(1,1) = DEVPORTS (a) + 8 + b ' DDRx
RXTXPB(2,1) = DEVPORTS (a) + 16 + b ' PORTx
'for i = 0 to 2 : ? whex(RXTXPB(i,0),2), whex(RXTXPB(i,1),2): next i
print #99, "Make TSB from code template: "; Filename
print #99, ""
print #99, "RXD = P"; mid$(astr,1,2); " / TXD = P"; mid$(astr,3,2)
print #99, ""
'
' STEP 4:
' Search pre-assembled firmware template for dummy port assignments
' (being RX = PINB0/DDRB0/PORTB0 / TX = PINB1/DDRB1/PORTB1)
' and modify these with respect to the desired ports and portbits
'
do until ADDR >= AAAA
select case BINARRAY(ADDR+1)
case &h98, &h99, &h9A, &h9B ' find opcodes of cbi/sbic/sbi/sbis
select case (BINARRAY (ADDR+0) And &b00000111)
case 0
select case (BINARRAY (ADDR+0) And &b11111000)
case DEVPORTS(1)+0 : BINARRAY(ADDR+0) = RXTXPB(0,0) ' change PINB
case DEVPORTS(1)+8 : BINARRAY(ADDR+0) = RXTXPB(1,0) ' change DDRB
case DEVPORTS(1)+16 : BINARRAY(ADDR+0) = RXTXPB(2,0) ' change PORTB
end select