-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.pas
More file actions
1048 lines (914 loc) · 62.7 KB
/
Main.pas
File metadata and controls
1048 lines (914 loc) · 62.7 KB
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
unit Main;
interface
uses
System.SysUtils, System.StrUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Math,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls, FMX.Edit, FMX.ListBox, FMX.ListView,
XBeeWiFi,
IdGlobal, IdBaseComponent, IdComponent, IdRawBase, IdRawClient, IdIcmpClient, IdStack, FMX.Layouts, FMX.Memo,
Time,
Advanced {,
Debug};
type
TLoaderType = (ltCore, ltVerifyRAM, ltProgramEEPROM, ltLaunchStart, ltLaunchFinal);
{Define XBee Info record}
PXBee = ^ TXBee;
TXBee = record
PCPort : String; {Pseudo-Communication Port (derived from MacAddr}
HostIPAddr : String; {Host's IP Address on the network adapter connecting to this XBee Wi-Fi's network}
IPAddr : String; {IP Address}
IPPort : Cardinal; {IP Port}
MacAddrHigh : Cardinal; {Upper 16 bits of MAC address}
MacAddrLow : Cardinal; {Lower 32 bits of MAC address}
NodeID : String; {Friendly Node ID}
CfgChecksum : Cardinal; {Configuration checksum}
end;
TForm1 = class(TForm)
PCPortLabel: TLabel;
IdentifyButton: TButton;
PCPortCombo: TComboBox;
OpenDialog: TOpenDialog;
Progress: TProgressBar;
ProgressLabel: TLabel;
StatusLabel: TLabel;
ButtonLayout: TLayout;
Button1: TButton;
Button2: TButton;
ResetPulseButton: TButton;
LoadButton: TButton;
TransmitButton: TButton;
XBeeInfoLabel: TLabel;
XBeeInfo: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure IdentifyButtonClick(Sender: TObject);
procedure ResetPulseButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure PCPortComboChange(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure TransmitButtonClick(Sender: TObject);
procedure LoadButtonClick(Sender: TObject);
private
{ Private declarations }
procedure GenerateResetSignal;
function EnforceXBeeConfiguration: Boolean;
procedure GenerateLoaderPacket(LoaderType: TLoaderType; PacketID: Integer);
public
{ Public declarations }
end;
EFileCorrupt = class(Exception); {File Corrupt exception}
EDownload = class(Exception); {Download protocol base exception class}
ESoftDownload = class(EDownload); {Soft download protocol error}
EHardDownload = class(EDownload); {Hard download protocol error; fatal}
var
Form1 : TForm1;
Time : TTime;
HostIPAddr : Cardinal; {Our IP Address (can be different if multiple network adapters)}
XBee : TXBeeWiFi;
TxBuf : TIdBytes; {Transmit packet (resized per packet)}
RxBuf : TIdBytes; {Receive packet (resized on receive)}
FBinImage : PByteArray; {A copy of the Propeller Application's binary image (used to generate the download stream)}
FBinSize : Integer; {The size of FBinImage (in longs)}
IgnorePCPortChange : Boolean; {PCPortChange processing flag}
XBeeInfoList : array of TXBee; {Holds identification information for XBee Wi-Fi modules on network}
const
MinSerTimeout = 100;
SerTimeout = 1000;
AppTimeout = 200;
CSumUnknown = $FFFFFFFF; {Unknown checksum value}
ImageLimit = 32768; {Max size of Propeller Application image file}
InitialBaud = 115200; {Initial XBee-to-Propeller baud rate}
FinalBaud = 921600; //460800; {Final XBee-to-Propeller baud rate}
DynamicWaitFactor = 2; {Multiply factor for dynamic waits; x times maximum round-trip time}
{The RxHandshake array consists of 125 bytes encoded to represent the expected 250-bit (125-byte @ 2 bits/byte) response
of continuing-LFSR stream bits from the Propeller, prompted by the timing templates following the TxHandshake stream.}
RxHandshake : array[0..124] of byte = ($EE,$CE,$CE,$CF,$EF,$CF,$EE,$EF,$CF,$CF,$EF,$EF,$CF,$CE,$EF,$CF,
$EE,$EE,$CE,$EE,$EF,$CF,$CE,$EE,$CE,$CF,$EE,$EE,$EF,$CF,$EE,$CE,
$EE,$CE,$EE,$CF,$EF,$EE,$EF,$CE,$EE,$EE,$CF,$EE,$CF,$EE,$EE,$CF,
$EF,$CE,$CF,$EE,$EF,$EE,$EE,$EE,$EE,$EF,$EE,$CF,$CF,$EF,$EE,$CE,
$EF,$EF,$EF,$EF,$CE,$EF,$EE,$EF,$CF,$EF,$CF,$CF,$CE,$CE,$CE,$CF,
$CF,$EF,$CE,$EE,$CF,$EE,$EF,$CE,$CE,$CE,$EF,$EF,$CF,$CF,$EE,$EE,
$EE,$CE,$CF,$CE,$CE,$CF,$CE,$EE,$EF,$EE,$EF,$EF,$CF,$EF,$CE,$CE,
$EF,$CE,$EE,$CE,$EF,$CE,$CE,$EE,$CF,$CF,$CE,$CF,$CF);
{Call frame}
InitCallFrame : array [0..7] of byte = ($FF, $FF, $F9, $FF, $FF, $FF, $F9, $FF); {See ValidateImageDataIntegrity for info on InitCallFrame}
implementation
{$R *.fmx}
{----------------------------------------------------------------------------------------------------}
{----------------------------------------------------------------------------------------------------}
{------------------------------------------- Event Methods ------------------------------------------}
{----------------------------------------------------------------------------------------------------}
{----------------------------------------------------------------------------------------------------}
procedure TForm1.FormCreate(Sender: TObject);
begin
Time := TTime.Create;
XBee := TXBeeWiFi.Create;
XBee.SerialTimeout := SerTimeout;
XBee.ApplicationTimeout := AppTimeout;
end;
{----------------------------------------------------------------------------------------------------}
procedure TForm1.FormDestroy(Sender: TObject);
begin
XBee.Destroy;
Time.Destroy;
end;
{----------------------------------------------------------------------------------------------------}
procedure TForm1.Button1Click(Sender: TObject);
begin
XBee.SetItem(xbIO2Mode, pinOutHigh);
end;
{----------------------------------------------------------------------------------------------------}
procedure TForm1.Button2Click(Sender: TObject);
begin
XBee.SetItem(xbIO2Mode, pinOutLow);
end;
{----------------------------------------------------------------------------------------------------}
procedure TForm1.IdentifyButtonClick(Sender: TObject);
var
IPIdx : Integer;
Nums : TSimpleNumberList;
PXB : PXBee;
{----------------}
{ TODO : Find resolution for Host IP in cases where we don't know the network. }
procedure SendIdentificationPacket(DestinationIP: String; HostIP: String = '255.255.255.255');
var
Idx : Cardinal;
ComboIdx : Integer;
begin
XBee.RemoteIPAddr := DestinationIP;
AdvancedSearchForm.LastSearchedListView.Items.Add.Text := DestinationIP;
if XBee.GetItem(xbIPAddr, Nums) then
begin
for Idx := 0 to High(Nums) do
begin {Found one or more XBee Wi-Fi modules on the network}
{Create XBee Info block and fill with identifying information}
SetLength(XBeeInfoList, Length(XBeeInfoList)+1);
PXB := @XBeeInfoList[High(XBeeInfoList)];
PXB.CfgChecksum := CSumUnknown;
PXB.HostIPAddr := HostIP;
PXB.IPAddr := FormatIPAddr(Nums[Idx]);
XBee.RemoteIPAddr := PXB.IPAddr;
if XBee.GetItem(xbIPPort, PXB.IPPort) then
if XBee.GetItem(xbMacHigh, PXB.MacAddrHigh) then
if XBee.GetItem(xbMacLow, PXB.MacAddrLow) then
if XBee.GetItem(xbNodeID, PXB.NodeID) then
begin
{Create pseudo-port name}
PXB.PCPort := 'XB-' + ifthen(PXB.NodeID <> '', PXB.NodeID, inttohex(PXB.MacAddrHigh, 4) + inttohex(PXB.MacAddrLow, 8));
{Check for duplicates}
ComboIdx := PCPortCombo.Items.IndexOf(PXB.PCPort);
if (ComboIdx = -1) or (XBeeInfoList[PCPortCombo.ListItems[ComboIdx].Tag].IPAddr <> PXB.IPAddr) then {Add only unique XBee modules found; ignore duplicates}
PCPortCombo.ListItems[PCPortCombo.Items.Add(PXB.PCPort)].Tag := High(XBeeInfoList)
else
begin
SetLength(XBeeInfoList, Length(XBeeInfoList)-1); {Else remove info record}
end;
end;
end;
end;
end;
{----------------}
begin
Form1.Cursor := crHourGlass;
IgnorePCPortChange := True;
try {Busy}
AdvancedSearchForm.LastSearchedListView.ClearItems;
{Clear port list}
PCPortCombo.ItemIndex := -1;
PCPortCombo.Clear;
SetLength(XBeeInfoList, 0);
{Add "searching" message (and set object to nil)}
PCPortCombo.ListItems[PCPortCombo.Items.Add('Searching...')].Tag := -1;
PCPortCombo.ItemIndex := 0;
Application.ProcessMessages;
XBeeInfo.Text := '';
{ TODO : Harden IdentifyButtonClick for interim errors. Handle gracefully. }
if (GStack.LocalAddresses.Count = 0) then raise Exception.Create('Error: No network connection!');
{Search networks}
for IPIdx := 0 to GStack.LocalAddresses.Count-1 do {For all host IP addresses (multiple network adapters)}
SendIdentificationPacket(MakeDWordIntoIPv4Address(IPv4ToDWord(GStack.LocalAddresses[IPIdx]) or $000000FF), GStack.LocalAddresses[IPIdx]);
for IPIdx := 0 to AdvancedSearchForm.CustomListView.Items.Count-1 do {For all custom networks}
SendIdentificationPacket(AdvancedSearchForm.CustomListView.Items[IPIdx].Text {, Need to find a host IP here!});
{Do final updating of PCPortCombo list}
if PCPortCombo.Count > 1 then
begin {Must have found at least one XBee Wi-Fi, delete "searching" item}
PCPortCombo.ItemIndex := -1;
PCPortCombo.Items.Delete(0);
end
else {Else, replace "searching" items with "none found"}
PCPortCombo.Items[0] := '...None Found';
{Append Advanced Options to end of list}
PCPortCombo.ListItems[PCPortCombo.Items.Add('<<Advanced Options>>')].Tag := -1;
PCPortCombo.Enabled := True;
PCPortCombo.DropDown;
finally
IgnorePCPortChange := False;
Form1.Cursor := crDefault;
end;
end;
{----------------------------------------------------------------------------------------------------}
procedure TForm1.TransmitButtonClick(Sender: TObject);
var
i : Integer;
r : Byte;
TxBuffLength : Integer;
RxCount : Integer;
FVersion : Byte;
FVersionMode : Boolean;
FDownloadMode : Byte;
Checksum : Integer; {Target Propeller Application's checksum (low byte = 0)}
TotalPackets : Integer; {Total number of image packets}
PacketID : Integer; {ID of packet transmitted}
Retry : Integer; {Retry counter}
RemainingTxTime : Cardinal; {Holds remaining time to transmit packet}
Acknowledged : Boolean; {True = positive/negative acknowledgement received from loader, False = no response from loader}
STime : Int64;
const
pReset = Integer.MinValue;
{----------------}
function Long(Addr: TIdBytes): Cardinal;
{Returns four bytes starting at Addr as a single cardinal value (Long)}
begin
Result := (Addr[3] shl 24) + (Addr[2] shl 16) or (Addr[1] shl 8) or Addr[0];
end;
{----------------}
function DynamicSerTimeout: Integer;
{Returns serial timeout adjusted for recent communication delays; minimum MinSerTimeout ms, maximum SerTimeout ms}
begin
Result := Max(MinSerTimeout, Min(XBee.UDPMaxRoundTrip*DynamicWaitFactor, SerTimeout));
end;
{----------------}
procedure UpdateProgress(Offset: Integer; Status: String = ''; Show: Boolean = True);
{Update progress bar.}
begin
if Offset > 0 then
begin
if Progress.Tag = 0 then
begin
Progress.Opacity := 1;
Progress.Value := Progress.Value + Offset;
end
else
Progress.Tag := Min(0, Progress.Tag + Offset);
end
else
begin
if Offset = Integer.MinValue then Offset := -Trunc(Progress.Value);
if Offset < 0 then
begin
Progress.Tag := Offset;
Progress.Opacity := 0.5;
end;
end;
if Status <> '' then StatusLabel.Text := Status;
Progress.Visible := Show;
Application.ProcessMessages;
// SendDebugMessage('Progress Updated: ' + Trunc(Progress.Value).ToString + ' of ' + Trunc(Progress.Max).ToString, True);
end;
{----------------}
procedure InitializeProgress(MaxIndex: Cardinal);
{Initialize Progress Bar}
begin
Progress.Value := 0;
Progress.Max := MaxIndex;
Progress.Tag := 0;
StatusLabel.Text := '';
UpdateProgress(0);
end;
{----------------}
function TransmitPacket: Integer;
{Transmit (and retransmit if necessary) the packet in TxBuf, waiting for non-retransmit response or timeout.
Returns response value (if any), raises exception otherwise.}
var
Retry : Integer;
begin
Retry := 3;
repeat {(Re)Transmit packet} { Send application image packet, get acknowledgement, retransmit as necessary}
if Retry < 3 then UpdateProgress(-1);
UpdateProgress(+1);
Time.Left(Trunc((Length(TxBuf)*10/FinalBaud)*1000)); { Mark required Tx time}
if not XBee.SendUDP(TxBuf, True, False) then
raise EHardDownload.Create('Error: Can not transmit packet!');
Acknowledged := XBee.ReceiveUDP(RxBuf, DynamicSerTimeout) and (Length(RxBuf) = 4); { Wait for positive/negative acknowledgement, or timeout}
RemainingTxTime := Time.Left; { Check remaining time to transmit (should be 0 ms)}
dec(Retry); { Loop and retransmit until timely non-retransmit acknowledgement received, or retry count exhausted}
{Repeat - (Re)Transmit packet...}
{ TODO : Revisit phase variance timing trap }
until ( Acknowledged and (RemainingTxTime = 0) and (Long(@RxBuf[0]) <> Long(@TxBuf[0])) ) or (Retry = 0);
if not ( Acknowledged and (RemainingTxTime = 0) and (Long(@RxBuf[0]) <> Long(@TxBuf[0])) ) then
raise EHardDownload.Create('Error: connection lost!'); { No acknowledgement received? Error}
Result := Long(@RxBuf[0]);
end;
{----------------}
begin
try {Handle download errors}
if FBinSize = 0 then exit;
TransmitButton.Enabled := False;
FVersionMode := False;
FDownloadMode := 1; {The download command; 1 = write to RAM and run, 2 = write to EEPROM and stop, 3 = write to EEPROM and run}
try {Reserved Memory}
STime := Ticks;
{Determine number of required packets for target application image; value becomes first Packet ID}
SetRoundMode(rmUp);
TotalPackets := Round(FBinSize*4 / (XBee.MaxDataSize-4*1)); {Calculate required number of packets for target image; binary image size (in bytes) / (max packet size - packet header)}
PacketID := TotalPackets;
{Calculate target application checksum (used for RAM Checksum confirmation)}
Checksum := 0;
for i := 0 to FBinSize*4-1 do inc(Checksum, FBinImage[i]);
for i := 0 to high(InitCallFrame) do inc(Checksum, InitCallFrame[i]);
{Initialize Progress Bar to proper size}
InitializeProgress(8 + TotalPackets);
{Begin download process}
if not XBee.ConnectSerialUDP then
begin
showmessage('Cannot connect');
exit;
end;
try {UDP Connected}
Retry := 3;
repeat {Connecting Propeller} {Try connecting up to 3 times}
UpdateProgress(pReset);
{Generate initial packet (handshake, timing templates, and Propeller Loader's Download Stream) all stored in TxBuf}
GenerateLoaderPacket(ltCore, TotalPackets);
try {Connecting...}
{(Enforce XBee Configuration and...) Generate reset signal, then wait for serial transfer window}
UpdateProgress(0, 'Connecting');
GenerateResetSignal;
{Send initial packet and wait for 200 ms (reset period) + serial transfer time + 20 ms (to position timing templates)}
if not XBee.SendUDP(TxBuf, True, False) then {Send Connect and Loader packet}
raise EHardDownload.Create('Error: Can not send connection request!');
IndySleep(200 + Trunc(Length(TxBuf)*10 / InitialBaud * 1000) + 20);
{Prep and send timing templates, then wait for serial transfer time}
UpdateProgress(+1);
SetLength(TxBuf, XBee.MaxDataSize);
FillChar(TxBuf[0], XBee.MaxDataSize, $F9);
if not XBee.SendUDP(TxBuf, True, False) then {Send timing template packet}
raise EHardDownload.Create('Error: Can not request connection response!');
IndySleep(Trunc(Length(TxBuf)*10 / InitialBaud * 1000));
{ TODO : Revisit handshake receive loop to check for all possibilities and how they are handled. }
repeat {Flush receive buffer and get handshake response}
if not XBee.ReceiveUDP(RxBuf, SerTimeout) then {Receive response}
raise ESoftDownload.Create('Error: No connection response from Propeller!');
if Length(RxBuf) = 129 then {Validate response}
begin
for i := 0 to 124 do if RxBuf[i] <> RxHandshake[i] then
raise EHardDownload.Create('Error: Unrecognized response - not a Propeller?'); {Validate handshake response}
for i := 125 to 128 do FVersion := (FVersion shr 2 and $3F) or ((RxBuf[i] and $1) shl 6) or ((RxBuf[i] and $20) shl 2); {Parse hardware version}
if FVersion <> 1 then
raise EHardDownload.Create('Error: Expected Propeller v1, but found Propeller v' + FVersion.ToString); {Validate hardware version}
end;
{Repeat - Flush receive buffer and get handshake response...}
until Length(RxBuf) = 129; {Loop if not correct (to flush receive buffer of previous data)}
{Receive RAM checksum response}
UpdateProgress(+1);
if not XBee.ReceiveUDP(RxBuf, DynamicSerTimeout) or (Length(RxBuf) <> 1) then {Receive Loader RAM Checksum Response}
raise ESoftDownload.Create('Error: No loader checksum response!');
if RxBuf[0] <> $FE then
raise EHardDownload.Create('Error: Loader failed checksum test');
{Now loader starts up in the Propeller; wait for loader's "ready" signal}
UpdateProgress(+1);
Acknowledged := XBee.ReceiveUDP(RxBuf, DynamicSerTimeout); {Receive loader's response}
if not Acknowledged or (Length(RxBuf) <> 4) then {Verify ready signal format}
raise ESoftDownload.Create('Error: No "Ready" signal from loader!');
if Cardinal(RxBuf[0]) <> PacketID then {Verify ready signal}
raise EHardDownload.Create('Error: Loader''s "Ready" signal unrecognized!');
except {on - Connecting...}
{Error? Repeat if possible on Soft error, else re-raise the exeption to exit}
on E:ESoftDownload do
begin
Acknowledged := False;
dec(Retry);
if Retry = 0 then raise EHardDownload.Create(E.Message);
end
else
raise;
end;
{repeat - Connecting Propeller...}
until Acknowledged;
{Switch to final baud rate}
UpdateProgress(+1, 'Increasing connection speed');
if not XBee.SetItem(xbSerialBaud, FinalBaud) then
raise EHardDownload.Create('Error: Unable to increase connection speed!');
{Transmit packetized target application}
i := 0;
repeat {Transmit target application packets} {Transmit application image}
TxBuffLength := 1 + Min((XBee.MaxDataSize div 4)-1, FBinSize - i); { Determine packet length (in longs); header + packet limit or remaining data length}
SetLength(TxBuf, TxBuffLength*4); { Set buffer length (Packet Length) (in longs)}
Move(PacketID, TxBuf[0], 4); { Store Packet ID}
Move(FBinImage[i*4], TxBuf[4], (TxBuffLength-1)*4); { Store section of data}
UpdateProgress(0, 'Sending packet: ' + (TotalPackets-PacketID+1).ToString + ' of ' + TotalPackets.ToString);
if TransmitPacket <> PacketID-1 then { Transmit packet (retransmit as necessary)}
raise EHardDownload.Create('Error: communication failed!'); { Error if unexpected response}
inc(i, TxBuffLength-1); { Increment image index}
dec(PacketID); { Decrement Packet ID (to next packet)}
{repeat - Transmit target application packets...}
until PacketID = 0; {Loop until done}
UpdateProgress(+1, 'Verifying RAM');
{Send verify RAM command} {Verify RAM Checksum}
GenerateLoaderPacket(ltVerifyRAM, PacketID); {Generate VerifyRAM executable packet}
if TransmitPacket <> -Checksum then {Transmit packet (retransmit as necessary)}
raise EHardDownload.Create('Error: RAM Checksum Failure!'); { Error if RAM Checksum differs}
PacketID := -Checksum; {Ready next packet; ID's by checksum now }
UpdateProgress(+1, 'Requesting Application Launch');
{Send verified/launch command} {Verified/Launch}
GenerateLoaderPacket(ltLaunchStart, PacketID); {Generate LaunchStart executable packet}
if TransmitPacket <> PacketID-1 then {Transmit packet (retransmit as necessary)}
raise EHardDownload.Create('Error: communication failed!'); { Error if unexpected response}
dec(PacketID); {Ready next packet}
UpdateProgress(+1, 'Application Launching');
{Send launch command} {Verified}
GenerateLoaderPacket(ltLaunchFinal, PacketID); {Generate LaunchFinal executable packet}
XBee.SendUDP(TxBuf, True, False); {Transmit last packet (Launch step 2); no retransmission}
UpdateProgress(+1, 'Success');
finally {UDP Connected}
XBee.DisconnectSerialUDP;
end;
finally {Reserved Memory}
IndySleep(500);
UpdateProgress(0, '', False);
TransmitButton.Enabled := True;
end;
except {on - Handle download errors}
on E:EDownload do ShowMessage(E.Message);
end;
end;
{----------------------------------------------------------------------------------------------------}
procedure TForm1.LoadButtonClick(Sender: TObject);
var
FStream : TFileStream;
ImageSize : Integer;
FName : String;
{----------------}
procedure ValidateImageDataIntegrity(Buffer: PByteArray; ImageSize: Integer; Filename: String);
{Validate Propeller application image data integrity in Buffer. This is done through a series of tests that verify that the file is not too small.
Also installs initial call frame.
PROPELLER APPLICATION FORMAT:
The Propeller Application image consists of data blocks for initialization, program, variables, and data/stack space. The first block, initialization, describes the application's
startup paramemters, including the position of the other blocks within the image, as shown below.
long 0 (bytes 0:3) - Clock Frequency
byte 4 - Clock Mode
byte 5 - Checksum (this value causes additive checksum of bytes 0 to ImageLimit-1 to equal 0)
word 3 - Start of Code pointer (must always be $0010)
word 4 - Start of Variables pointer
word 5 - Start of Stack Space pointer
word 6 - Current Program pointer (points to first public method of object)
word 7 - Current Stack Space pointer (points to first run-time usable space of stack)
WHAT GETS DOWNLOADED:
To save time, the Propeller Tool does not download the entire Propeller Application Image. Instead, it downloads only the parts of the image from long 0 through the end of code (up to the
start of variables) and then the Propeller chip itself writes zeros (0) to the rest of the RAM/EEPROM, after the end of code (up to 32 Kbytes), and inserts the initial call frame in the
proper location. This effectively clears (initializes) all global variables to zero (0) and sets all available stack and free space to zero (0) as well.
INITIAL CALL FRAME:
The Initial Call Frame is stuffed into the Propeller Application's image at location DBase-8 (eight bytes (2 longs) before the start of stack space). The Propeller Chip itself stores the
Initial Call Frame into those locations at the end of the download process. The initial call frame is exactly like standard run-time call frames in their format, but different in value.
Call Frame Format: PBase VBase DBase PCurr Return Extra... (each are words arranged in this order from Word0 to Word3; 4 words = two longs)
Initial Call Frame Data: $FFFF $FFF9 $FFFF $FFF9 n/a n/a
Note: PBase is Start of Object Program, VBase is Start of Variables, DBase is Start of Data/Stack, PCurr is current program location (PC).
The Initial Call Frame is stuffed prior to DBase so that if one-too-many returns are executed by the Spin-based Propeller Application, the Initial Call Frame is popped off the stack next
which instructs the Spin Interpreter to jump to location $FFF9 (PCurr) and execute the byte code there (which is two instructions to perform a "Who am I?" followed by a cog stop, "COGID ID"
and "COGSTOP ID", to halt the cog). NOTE: The two duplicate longs $FFFFFFF9 are used for simplicity and the first long just happens to set the right bits to indicate to an ABORT command
that it has reached the point where it should stop popping the stack and actually execute code.
Note that a Call Frame is followed by one or more longs of data. Every call, whether it be to an inter-object method or an intra-object method, results in a call frame that consists
of the following:
Return Information : 2 longs (first two longs shown in Call Frame Format, above, but with different data)
Return Result : 1 long
Method Parameters : x longs (in left-to-right order)
Local Variables : y longs (in left-to-right order)
Intermediate Workspace : z longs
The first four items in the call stack are easy to determine from the code. The last, Intermediate Workspace, is much more difficult because it relates directly to how and when the
interpreter pushes and pops items on the stack during expression evaluations.}
var
Idx : Integer;
CheckSum : Byte;
begin
{Raise exception if file truncated}
if (ImageSize < 16) or (ImageSize < PWordArray(Buffer)[4]) then
raise EFileCorrupt.Create(ifthen(Filename <> '', 'File '''+Filename+'''', 'Image') + ' is truncated or is not a Propeller Application' + ifthen(Filename <> '',' file', '') + '!'+#$D#$A#$D#$A+ifthen(Filename <> '', 'File', 'Image') + ' size is less than 16 bytes or is less than word 4 (VarBase) indicates.');
if (PWordArray(Buffer)[3] <> $0010) then
raise EFileCorrupt.Create('Initialization code invalid! ' + ifthen(Filename <> '', 'File '''+Filename+'''', 'Image') + ' is corrupt or is not a Propeller Application' + ifthen(Filename <> '',' file', '') +'!'+#$D#$A#$D#$A+'Word 3 (CodeBase) must be $0010.');
{Write initial call frame}
// copymemory(@Buffer[min($7FF8, max($0010, PWordArray(Buffer)[5] - 8))], @InitCallFrame[0], 8);
move(InitCallFrame[0], Buffer[min($7FF8, max($0010, PWordArray(Buffer)[5] - 8))], 8);
{Raise exception if file's checksum incorrect}
CheckSum := 0;
for Idx := 0 to ImageLimit-1 do CheckSum := CheckSum + Buffer[Idx];
if CheckSum <> 0 then
raise EFileCorrupt.Create('Checksum Error! ' + ifthen(Filename <> '', 'File '''+Filename+'''', 'Image') + ' is corrupt or is not a Propeller Application' + ifthen(Filename <> '',' file', '') +'!'+#$D#$A#$D#$A+'Byte 5 (Checksum) is incorrect.');
{Check for extra data beyond code}
Idx := PWordArray(Buffer)[4];
while (Idx < ImageSize) and ((Buffer[Idx] = 0) or ((Idx >= PWordArray(Buffer)[5] - 8) and (Idx < PWordArray(Buffer)[5]) and (Buffer[Idx] = InitCallFrame[Idx-(PWordArray(Buffer)[5]-8)]))) do inc(Idx);
if Idx < ImageSize then
begin
// {$IFDEF ISLIB}
// if (CPrefs[GUIDisplay].IValue in [0, 2]) then {Dialog display needed? Show error.}
// {$ENDIF}
// begin
// MessageBeep(MB_ICONWARNING);
raise EFileCorrupt.Create(ifthen(Filename <> '', 'File '''+Filename+'''', 'Image') + ' contains data after code space' + {$IFNDEF ISLIB}' that was not generated by the Propeller Tool' +{$ENDIF} '. This data will not be displayed or downloaded.');
// messagedlg(ifthen(Filename <> '', 'File '''+Filename+'''', 'Image') + ' contains data after code space' + {$IFNDEF ISLIB}' that was not generated by the Propeller Tool'{$ENDIF}+ '. This data will not be displayed or downloaded.', mtWarning, [mbOK], 0);
// end;
end;
end;
{----------------}
begin
{Process file/image}
{Set open dialog parameters}
OpenDialog.Title := 'Download Propeller Application Image';
OpenDialog.Filter := 'Propeller Applications (*.binary, *.eeprom)|*.binary;*.eeprom|All Files (*.*)|*.*';
OpenDialog.FilterIndex := 0;
OpenDialog.FileName := '';
{Show Open Dialog}
if not OpenDialog.Execute then exit;
FName := OpenDialog.Filename;
if fileexists(FName) then
begin {File found, load it up}
{Initialize}
ImageSize := 0;
fillchar(FBinImage[0], ImageLimit, 0);
{Load the file}
FStream := TFileStream.Create(FName, fmOpenRead+fmShareDenyWrite);
try
ImageSize := FStream.Read(FBinImage^, ImageLimit);
finally
FStream.Free;
end;
end;
try
{Validate application image (and install initial call frame)}
ValidateImageDataIntegrity(FBinImage, min(ImageLimit, ImageSize), FName);
FBinSize := ImageSize div 4;
{Download image to Propeller chip (use VBase (word 4) value as the 'image long-count size')}
// Propeller.Download(Buffer, PWordArray(Buffer)[4] div 4, DownloadCmd);
TransmitButton.Enabled := True;
except
on E: EFileCorrupt do
begin {Image corrupt, show error and exit}
// if (CPrefs[GUIDisplay].IValue in [0, 2]) then
// begin
TransmitButton.Enabled := False;
ShowMessage(E.Message);
// ErrorMsg('052-'+E.Message); {Dialog display needed? Show error.}
// end
// else
// begin
// {$IFDEF ISLIBWRAP}
// StdOutMsg(pmtError, '052-'+E.Message); {Else only write message to standard out}
// {$ENDIF}
// end;
exit;
end;
end;
end;
{----------------------------------------------------------------------------------------------------}
procedure TForm1.ResetPulseButtonClick(Sender: TObject);
begin
GenerateResetSignal;
end;
{----------------------------------------------------------------------------------------------------}
procedure TForm1.PCPortComboChange(Sender: TObject);
{XBee Wi-Fi selected; configure to communicate with it}
var
PXB : PXBee;
begin
if IgnorePCPortChange then exit;
if (PCPortCombo.ItemIndex > -1) and (PCPortCombo.ListItems[PCPortCombo.ItemIndex].Tag > -1) then
begin {If XBee Wi-Fi module item selected}
PXB := @XBeeInfoList[PCPortCombo.ListItems[PCPortCombo.ItemIndex].Tag];
{Note our IP address used to access it; used later to set it's destination IP for serial to Wi-Fi communcation back to us}
HostIPAddr := IPv4ToDWord(PXB.HostIPAddr);
{Update information field}
XBeeInfo.Text := '[ ' + FormatMACAddr(PXB.MacAddrHigh, PXB.MacAddrLow) + ' ] - ' + PXB.IPAddr + ' : ' + inttostr(PXB.IPPort);
{Set remote serial IP address and port and enable buttons}
XBee.RemoteIPAddr := PXB.IPAddr;
XBee.RemoteSerialIPPort := PXB.IPPort;
ButtonLayout.Enabled := True;
end
else
begin {Else, possibly "Advanced Options" selected}
IgnorePCPortChange := True;
try
{Clear display and disable buttons}
HostIPAddr := 0;
XBeeInfo.Text := '';
XBee.RemoteIPAddr := '';
XBee.RemoteSerialIPPort := 0;
ButtonLayout.Enabled := False;
{Remove "None Found..." message, if any}
if (PCPortCombo.Count = 2) and (PCPortCombo.ListItems[0].Tag = -1) then PCPortCombo.Items.Delete(0);
{Reset combo box selection}
PCPortCombo.ItemIndex := -1;
{Display advanced search options}
AdvancedSearchForm.ShowModal;
finally
IgnorePCPortChange := False;
end;
end;
end;
{----------------------------------------------------------------------------------------------------}
{----------------------------------------------------------------------------------------------------}
{------------------------------------------ Private Methods -----------------------------------------}
{----------------------------------------------------------------------------------------------------}
{----------------------------------------------------------------------------------------------------}
procedure TForm1.GenerateResetSignal;
{Generate Reset Pulse}
begin
{ TODO : Enhance GenerateResetSignal for errors. }
if EnforceXBeeConfiguration then
if not XBee.SetItem(xbOutputState, $0010) then {Start reset pulse (low) and serial hold (high)}
raise Exception.Create('Error Generating Reset Signal');
end;
{----------------------------------------------------------------------------------------------------}
function TForm1.EnforceXBeeConfiguration: Boolean;
{Validate necessary XBee configuration; set attributes if needed.
Returns True if XBee properly configured; false otherwise.}
var
PXB : PXbee;
{----------------}
function Validate(Attribute: xbCommand; Value: Cardinal; ReadOnly: Boolean = False): Boolean;
{Check if XBee Attribute is equal to Value; if not, set it as such.
Set ReadOnly if attribute should be read and compared, but not written.
Returns True upon exit if Attribute = Value.}
var
Setting : Cardinal;
begin
if not XBee.GetItem(Attribute, Setting) then raise Exception.Create('Can not read XBee attribute.');
Result := Setting = Value;
if not Result and not ReadOnly then
begin
if not XBee.SetItem(Attribute, Value) then raise Exception.Create('Can not set XBee attribute.');
Result := True;
end;
end;
{----------------}
begin
{ TODO : Enhance Enforce... to log any error }
PXB := @XBeeInfoList[PCPortCombo.ListItems[PCPortCombo.ItemIndex].Tag];
Result := (PXB.CfgChecksum <> CSumUnknown) and (Validate(xbChecksum, PXB.CfgChecksum, True)); {Is the configuration known and valid?}
if not Result then {If not...}
begin
Validate(xbSerialIP, SerialUDP, False); { Ensure XBee's Serial Service uses UDP packets [WRITE DISABLED DUE TO FIRMWARE BUG]}
Validate(xbIPDestination, HostIPAddr); { Ensure Serial-to-IP destination is us (our IP)}
Validate(xbOutputMask, $7FFF); { Ensure output mask is proper (default, in this case)}
Validate(xbRTSFLow, pinEnabled); { Ensure RTS flow pin is enabled (input)}
Validate(xbIO4Mode, pinOutLow); { Ensure serial hold pin is set to output low}
Validate(xbIO2Mode, pinOutHigh); { Ensure reset pin is set to output high}
Validate(xbIO4Timer, 2); { Ensure serial hold pin's timer is set to 200 ms}
Validate(xbIO2Timer, 1); { Ensure reset pin's timer is set to 100 ms}
Validate(xbSerialMode, TransparentMode {APIwoEscapeMode} {APIwEscapeMode}, False); { Ensure Serial Mode is transparent [WRITE DISABLED DUE TO FIRMWARE BUG]}
Validate(xbSerialBaud, InitialBaud); { Ensure baud rate is set to initial speed}
Validate(xbSerialParity, ParityNone); { Ensure parity is none}
Validate(xbSerialStopBits, StopBits1); { Ensure stop bits is 1}
Validate(xbPacketingTimeout, 3); { Ensure packetization timout is 3 character times}
XBee.GetItem(xbChecksum, PXB.CfgChecksum); { Record new configuration checksum}
Result := True;
end;
end;
{--------------------------------------------------------------------------------}
procedure TForm1.GenerateLoaderPacket(LoaderType: TLoaderType; PacketID: Integer);
{Generate a single packet (in TxBuf) that contains the mini-loader (IP_Loader.spin) according to LoaderType.
Initially, LoaderType should be ltCore, followed by other types as needed.
If LoaderType is ltCore...
* target application's total packet count must be included in PacketID.
* generated packet contains the Propeller handshake, timing templates, and core code from the Propeller Loader Image (IP_Loader.spin),
encoded in an optimized format (3, 4, or 5 bits per byte; 7 to 11 bytes per long).
Note: optimal encoding means, for every 5 contiguous bits in Propeller Application Image (LSB first) 3, 4, or 5 bits can be translated to a byte.
The process requires 5 bits input (ie: indexed into the PDSTx array) and gets a byte out that contains the first 3, 4, or 5 bits encoded
in the Propeller Download stream format. The 2nd dimention of the PDSTx array contains the number of bits acutally encoded. If less than
5 bits were translated, the remaining bits lead the next 5-bit translation unit input to the translation process.
If LoaderType is not ltCore...
* PacketIDs should be less than 0 for this type of packet in order to work with the mini-loader core.
* generated packet is a snippet of loader code aligned to be executable from the Core's packet buffer. This snippet is in raw form (it is not
encoded) and should be transmitted as such.}
var
Idx : Integer; {General index value}
BValue : Byte; {Binary Value to translate}
BitsIn : Byte; {Number of bits ready for translation}
BCount : Integer; {Total number of bits translated}
LoaderImage : PByteArray; {Adjusted Loader Memory image}
LoaderStream : PByteArray; {Loader Download Stream (Generated from Loader Image inside GenerateStream())}
LoaderStreamSize : Integer; {Size of Loader Download Stream}
Checksum : Integer; {Loader application's checksum (low byte = 0)}
TxBuffLength : Integer;
RawSize : Integer;
const
dtTx = 0; {Data type: Translation pattern}
dtBits = 1; {Date type: Bits translated}
{Power of 2 - 1 array. Index into this array with the desired power of 2 (1 through 5) and element value is mask equal to power of 2 minus 1}
Pwr2m1 : array[1..5] of byte = ($01, $03, $07, $0F, $1F);
{Propeller Download Stream Translator array. Index into this array using the "Binary Value" (usually 5 bits) to translate,
the incoming bit size (again, usually 5), and the desired data element to retrieve (dtTx = translation, dtBits = bit count
actually translated.}
{Binary Incoming Translation }
{Value, Bit Size, or Bit Count}
PDSTx : array[0..31, 1..5, dtTx..dtBits] of byte =
{*** 1-BIT ***} {*** 2-BIT ***} {*** 3-BIT ***} {*** 4-BIT ***} {*** 5-BIT ***}
( ( {%00000} ($FE, 1), {%00000} ($F2, 2), {%00000} ($92, 3), {%00000} ($92, 3), {%00000} ($92, 3) ),
( {%00001} ($FF, 1), {%00001} ($F9, 2), {%00001} ($C9, 3), {%00001} ($C9, 3), {%00001} ($C9, 3) ),
( (0, 0), {%00010} ($FA, 2), {%00010} ($CA, 3), {%00010} ($CA, 3), {%00010} ($CA, 3) ),
( (0, 0), {%00011} ($FD, 2), {%00011} ($E5, 3), {%00011} ($25, 4), {%00011} ($25, 4) ),
( (0, 0), (0, 0), {%00100} ($D2, 3), {%00100} ($D2, 3), {%00100} ($D2, 3) ),
( (0, 0), (0, 0), {%00101} ($E9, 3), {%00101} ($29, 4), {%00101} ($29, 4) ),
( (0, 0), (0, 0), {%00110} ($EA, 3), {%00110} ($2A, 4), {%00110} ($2A, 4) ),
( (0, 0), (0, 0), {%00111} ($F5, 3), {%00111} ($95, 4), {%00111} ($95, 4) ),
( (0, 0), (0, 0), (0, 0), {%01000} ($92, 3), {%01000} ($92, 3) ),
( (0, 0), (0, 0), (0, 0), {%01001} ($49, 4), {%01001} ($49, 4) ),
( (0, 0), (0, 0), (0, 0), {%01010} ($4A, 4), {%01010} ($4A, 4) ),
( (0, 0), (0, 0), (0, 0), {%01011} ($A5, 4), {%01011} ($A5, 4) ),
( (0, 0), (0, 0), (0, 0), {%01100} ($52, 4), {%01100} ($52, 4) ),
( (0, 0), (0, 0), (0, 0), {%01101} ($A9, 4), {%01101} ($A9, 4) ),
( (0, 0), (0, 0), (0, 0), {%01110} ($AA, 4), {%01110} ($AA, 4) ),
( (0, 0), (0, 0), (0, 0), {%01111} ($D5, 4), {%01111} ($D5, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%10000} ($92, 3) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%10001} ($C9, 3) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%10010} ($CA, 3) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%10011} ($25, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%10100} ($D2, 3) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%10101} ($29, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%10110} ($2A, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%10111} ($95, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%11000} ($92, 3) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%11001} ($49, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%11010} ($4A, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%11011} ($A5, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%11100} ($52, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%11101} ($A9, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%11110} ($AA, 4) ),
( (0, 0), (0, 0), (0, 0), (0, 0), {%11111} ($55, 5) )
);
{After reset, the Propeller's exact clock rate is not known by either the host or the Propeller itself, so communication with the Propeller takes place based on
a host-transmitted timing template that the Propeller uses to read the stream and generate the responses. The host first transmits the 2-bit timing template,
then transmits a 250-bit Tx handshake, followed by 250 timing templates (one for each Rx handshake bit expected) which the Propeller uses to properly transmit
the Rx handshake sequence. Finally, the host transmits another eight timing templates (one for each bit of the Propeller's version number expected) which the
Propeller uses to properly transmit it's 8-bit hardware/firmware version number.
After the Tx Handshake and Rx Handshake are properly exchanged, the host and Propeller are considered "connected," at which point the host can send a download
command followed by image size and image data, or simply end the communication.
PROPELLER HANDSHAKE SEQUENCE: The handshake (both Tx and Rx) are based on a Linear Feedback Shift Register (LFSR) tap sequence that repeats only after 255
iterations. The generating LFSR can be created in Pascal code as the following function (assuming FLFSR is pre-defined Byte variable that is set to ord('P')
prior to the first call of IterateLFSR). This is the exact function that was used in previous versions of the Propeller Tool and Propellent software.
function IterateLFSR: Byte;
begin //Iterate LFSR, return previous bit 0
Result := FLFSR and $01;
FLFSR := FLFSR shl 1 and $FE or (FLFSR shr 7 xor FLFSR shr 5 xor FLFSR shr 4 xor FLFSR shr 1) and 1;
end;
The handshake bit stream consists of the lowest bit value of each 8-bit result of the LFSR described above. This LFSR has a domain of 255 combinations, but
the host only transmits the first 250 bits of the pattern, afterwards, the Propeller generates and transmits the next 250-bits based on continuing with the same
LFSR sequence. In this way, the host-transmitted (host-generated) stream ends 5 bits before the LFSR starts repeating the initial sequence, and the host-received
(Propeller generated) stream that follows begins with those remaining 5 bits and ends with the leading 245 bits of the host-transmitted stream.
For speed and compression reasons, this handshake stream has been encoded as tightly as possible into the pattern described below.
The TxHandshake array consists of 209 bytes that are encoded to represent the required '1' and '0' timing template bits, 250 bits representing the
lowest bit values of 250 iterations of the Propeller LFSR (seeded with ASCII 'P'), 250 more timing template bits to receive the Propeller's handshake
response, and more to receive the version.}
TxHandshake : array[0..208] of byte = ($49, {First timing template ('1' and '0') plus first two bits of handshake ('0' and '1')}
$AA,$52,$A5,$AA,$25,$AA,$D2,$CA,$52,$25,$D2,$D2,$D2,$AA,$49,$92, {Remaining 248 bits of handshake...}
$C9,$2A,$A5,$25,$4A,$49,$49,$2A,$25,$49,$A5,$4A,$AA,$2A,$A9,$CA,
$AA,$55,$52,$AA,$A9,$29,$92,$92,$29,$25,$2A,$AA,$92,$92,$55,$CA,
$4A,$CA,$CA,$92,$CA,$92,$95,$55,$A9,$92,$2A,$D2,$52,$92,$52,$CA,
$D2,$CA,$2A,$FF,
$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29, {250 timing templates ('1' and '0') to receive 250-bit handshake from Propeller.}
$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29, {This is encoded as two pairs per byte; 125 bytes}
$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,
$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,
$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,
$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,
$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,
$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,$29,
$29,$29,$29,$29, {8 timing templates ('1' and '0') to receive 8-bit Propeller version; two pairs per byte; 4 bytes}
$93,$92,$92,$92,$92,$92,$92,$92,$92,$92,$F2); {Download command (1; program RAM and run); 11 bytes}
{Raw loader image. This is a memory image of a Propeller Application written in PASM that fits into our initial download packet. Once started,
it assists with the remainder of the download (at a faster speed and with more relaxed interstitial timing conducive of Internet Protocol delivery.
This memory image isn't used as-is; before download, it is first adjusted to contain special values assigned by this host (communication timing and
synchronization values) and then is translated into an optimized Propeller Download Stream understandable by the Propeller ROM-based boot loader.}
RawLoaderImage : array[0..347] of byte = ($00,$B4,$C4,$04,$6F,$C3,$10,$00,$5C,$01,$64,$01,$54,$01,$68,$01,
$4C,$01,$02,$00,$44,$01,$00,$00,$48,$E8,$BF,$A0,$48,$EC,$BF,$A0,
$49,$A0,$BC,$A1,$01,$A0,$FC,$28,$F1,$A1,$BC,$80,$A0,$9E,$CC,$A0,
$49,$A0,$BC,$F8,$F2,$8F,$3C,$61,$05,$9E,$FC,$E4,$04,$A4,$FC,$A0,
$4E,$A2,$BC,$A0,$08,$9C,$FC,$20,$FF,$A2,$FC,$60,$00,$A3,$FC,$68,
$01,$A2,$FC,$2C,$49,$A0,$BC,$A0,$F1,$A1,$BC,$80,$01,$A2,$FC,$29,
$49,$A0,$BC,$F8,$48,$E8,$BF,$70,$11,$A2,$7C,$E8,$0A,$A4,$FC,$E4,
$4A,$92,$BC,$A0,$4C,$3C,$FC,$50,$54,$A6,$FC,$A0,$53,$3A,$BC,$54,
$53,$56,$BC,$54,$53,$58,$BC,$54,$04,$A4,$FC,$A0,$00,$A8,$FC,$A0,
$4C,$9E,$BC,$A0,$4B,$A0,$BC,$A1,$00,$A2,$FC,$A0,$80,$A2,$FC,$72,
$F2,$8F,$3C,$61,$21,$9E,$F8,$E4,$31,$00,$78,$5C,$F1,$A1,$BC,$80,
$49,$A0,$BC,$F8,$F2,$8F,$3C,$61,$00,$A3,$FC,$70,$01,$A2,$FC,$29,
$26,$00,$4C,$5C,$51,$A8,$BC,$68,$08,$A8,$FC,$20,$4D,$3C,$FC,$50,
$1E,$A4,$FC,$E4,$01,$A6,$FC,$80,$19,$00,$7C,$5C,$1E,$9E,$BC,$A0,
$FF,$9F,$FC,$60,$4C,$9E,$7C,$86,$00,$84,$68,$0C,$4E,$A8,$3C,$C2,
$09,$00,$54,$5C,$01,$9C,$FC,$C1,$55,$00,$70,$5C,$55,$A6,$FC,$84,
$40,$AA,$3C,$08,$04,$80,$FC,$80,$43,$74,$BC,$80,$3A,$A6,$FC,$E4,
$55,$74,$FC,$54,$09,$00,$7C,$5C,$00,$00,$00,$00,$00,$00,$00,$00,
$80,$00,$00,$00,$00,$02,$00,$00,$00,$80,$00,$00,$FF,$FF,$F9,$FF,
$10,$C0,$07,$00,$00,$00,$00,$80,$00,$00,$00,$40,$B6,$02,$00,$00,
$5B,$01,$00,$00,$08,$02,$00,$00,$55,$73,$CB,$00,$50,$45,$01,$00,
$00,$00,$00,$00,$35,$C7,$08,$35,$2C,$32,$00,$00);
RawLoaderInitOffset = -8*4; {Offset (in bytes) from end of Loader Image pointing to where most host-initialized values exist.
Host-Initialized values are: Initial Bit Time, Final Bit Time, 1.5x Bit Time, Failsafe timeout,
End of Packet timeout, and ExpectedID. In addition, the image checksum at word 5 needs to be
updated. All these values need to be updated before the download stream is generated.}
MaxRxSenseError = 23; {Maximum number of cycles by which the detection of a start bit could be off (as affected by the Loader code)}
{Loader VerifyRAM snippet}
VerifyRAM : array[0..67] of byte = ($44,$A4,$BC,$A0,$40,$A4,$BC,$84,$02,$A4,$FC,$2A,$40,$82,$14,$08,
$04,$80,$D4,$80,$58,$A4,$D4,$E4,$0A,$A4,$FC,$04,$04,$A4,$FC,$84,
$52,$8A,$3C,$08,$04,$A4,$FC,$84,$52,$8A,$3C,$08,$01,$80,$FC,$84,
$40,$A4,$BC,$00,$52,$82,$BC,$80,$60,$80,$7C,$E8,$41,$9C,$BC,$A4,
$09,$00,$7C,$5C);
{Loader LaunchStart snippet}
LaunchStart : array[0..27] of byte = ($B8,$68,$FC,$58,$58,$68,$FC,$50,$09,$00,$7C,$5C,$06,$80,$FC,$04,
$10,$80,$7C,$86,$00,$84,$54,$0C,$02,$8C,$7C,$0C);
{Loader LaunchFinal snippet}
LaunchFinal : array[0..15] of byte = ($06,$80,$FC,$04,$10,$80,$7C,$86,$00,$84,$54,$0C,$02,$8C,$7C,$0C);
{Loader executable snippets}
ExeSnippet : array[ltVerifyRAM..ltLaunchFinal] of PByteArray = (@VerifyRAM, @VerifyRAM, @LaunchStart, @LaunchFinal);
ExeSnippetSize : array[ltVerifyRAM..ltLaunchFinal] of Integer = (Length(VerifyRAM), Length(VerifyRAM), Length(LaunchStart), Length(LaunchFinal));
InitCallFrame : array [0..7] of byte = ($FF, $FF, $F9, $FF, $FF, $FF, $F9, $FF);
// {The TxHandshake array consists of 250 bytes representing the bit 0 values (0 or 1) of each of 250 iterations of the LFSR (seeded with ASCII 'P') described above.}
{ TxHandshake : array[1..250] of byte = (0,1,0,1,1,1,0,0,1,1,1,1,0,1,0,1,1,1,1,1,0,0,0,1,1,1,0,0,1,0,1,0,0,0,1,1,1,1,0,0,0,0,1,0,0,1,0,0,1,0,1,1,1,1,0,0,1,0,0,0,1,0,0,0,
1,1,0,1,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,1,0,1,1,0,1,1,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,1,1,1,0,1,1,0,1,0,1,1,0,1,0,0,1,1,1,1,1,
1,1,1,0,0,1,1,0,1,1,1,1,0,1,1,1,0,1,0,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,1,1,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,1,0,0,1,0,1,0,1,0,0,1,
0,0,0,0,0,1,0,0,0,0,1,1,1,0,1,1,1,1,1,1,0,1,1,0,0,0,0,1,1,0,0,0,1,0,0,1,1,0,0,0,0,0,1,1,0,1,0,0,0,1,0,1,0,0,1,1,0,1);
}
// {The RxHandshake array consists of 250 bytes representing the bit 0 values (0 or 1) of each of 250 successive iterations of the LFSR of TxHandshake, above.}
{ RxHandshake : array[1..250] of byte = (0,1,0,0,0,0,1,0,1,1,1,0,0,1,1,1,1,0,1,0,1,1,1,1,1,0,0,0,1,1,1,0,0,1,0,1,0,0,0,1,1,1,1,0,0,0,0,1,0,0,1,0,0,1,0,1,1,1,1,0,0,1,0,0,
0,1,0,0,0,1,1,0,1,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,1,0,1,1,0,1,1,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,1,1,1,0,1,1,0,1,0,1,1,0,1,0,0,
1,1,1,1,1,1,1,1,0,0,1,1,0,1,1,1,1,0,1,1,1,0,1,0,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,1,1,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,1,0,0,1,0,1,
0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1,0,1,1,1,1,1,1,0,1,1,0,0,0,0,1,1,0,0,0,1,0,0,1,1,0,0,0,0,0,1,1,0,1,0,0,0,1,0,1,0);
}
{----------------}
procedure SetHostInitializedValue(Addr: Integer; Value: Integer);
{Adjust LoaderImage to contain Value (long) at Addr}
var
Idx : Integer;
begin
for Idx := 0 to 3 do LoaderImage[Addr+Idx] := Value shr (Idx*8) and $FF;
end;
{----------------}
begin
if LoaderType = ltCore then
begin {Generate specially-prepared stream of mini-loader's core (with handshake, timing templates, and host-initialized timing}
{Reserve memory for Raw Loader Image}
RawSize := (high(RawLoaderImage)+1) div 4;
getmem(LoaderImage, RawSize*4+1); {Reserve LoaderImage space for RawLoaderImage data plus 1 extra byte to accommodate generation routine}
getmem(LoaderStream, RawSize*4 * 11); {Reserve LoaderStream space for maximum-sized download stream}
try {Reserved memory}
{Prepare Loader Image}
Move(RawLoaderImage, LoaderImage[0], RawSize*4); {Copy raw loader image to LoaderImage (for adjustments and processing)}
{Clear checksum and set host-initialized values}
LoaderImage[5] := 0;
SetRoundMode(rmNearest);
SetHostInitializedValue(RawSize*4+RawLoaderInitOffset, Round(80000000 / InitialBaud)); {Initial Bit Time}
SetHostInitializedValue(RawSize*4+RawLoaderInitOffset + 4, Round(80000000 / FinalBaud)); {Final Bit Time}
SetHostInitializedValue(RawSize*4+RawLoaderInitOffset + 8, Round(((1.5 * 80000000) / FinalBaud) - MaxRxSenseError)); {1.5x Final Bit Time minus maximum start bit sense error}
SetHostInitializedValue(RawSize*4+RawLoaderInitOffset + 12, 2 * 80000000 div (3 * 4)); {Failsafe Timeout (seconds-worth of Loader's Receive loop iterations)}
SetHostInitializedValue(RawSize*4+RawLoaderInitOffset + 16, Round(2 * 80000000 / FinalBaud * 10 / 12)); {EndOfPacket Timeout (2 bytes worth of Loader's Receive loop iterations)}
SetHostInitializedValue(RawSize*4+RawLoaderInitOffset + 20, PacketID); {First Expected Packet ID; total packet count}
{Recalculate and update checksum}
Checksum := 0;
for Idx := 0 to RawSize*4-1 do inc(Checksum, LoaderImage[Idx]);
for Idx := 0 to high(InitCallFrame) do inc(Checksum, InitCallFrame[Idx]);
LoaderImage[5] := 256-(CheckSum and $FF); {Update loader image so low byte of checksum calculates to 0}
{Generate Propeller Loader Download Stream from adjusted LoaderImage (above); Output delivered to LoaderStream and LoaderStreamSize}
BCount := 0;
LoaderStreamSize := 0;
while BCount < (RawSize*4) * 8 do {For all bits in data stream...}
// while BCount < ((RawSize*4) * 8) div 3 do {For all bits in data stream...}
begin
BitsIn := Min(5, (RawSize*4) * 8 - BCount); { Determine number of bits in current unit to translate; usually 5 bits}
BValue := ( (LoaderImage[BCount div 8] shr (BCount mod 8)) + { Extract next translation unit (contiguous bits, LSB first; usually 5 bits)}