-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprogrammer.cpp
More file actions
1941 lines (1773 loc) · 64.4 KB
/
programmer.cpp
File metadata and controls
1941 lines (1773 loc) · 64.4 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
/*
* Copyright (C) 2011-2012 Doug Brown
*
* 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.
*
*/
#include "programmer.h"
#include <QDebug>
#include <QWaitCondition>
#include <QMutex>
#include <QTimer>
typedef enum ProgrammerCommandState
{
WaitingForNextCommand = 0,
WriteSIMMWaitingSetSectorLayoutReply,
WriteSIMMWaitingSectorLayoutDataReply,
WriteSIMMWaitingSetSizeReply,
WriteSIMMWaitingSetVerifyModeReply,
WriteSIMMWaitingSetChipMaskReply,
WriteSIMMWaitingSetChipMaskValueReply,
WriteSIMMWaitingEraseReply,
WriteSIMMWaitingWriteReply,
WriteSIMMWaitingFinishReply,
WriteSIMMWaitingWriteMoreReply,
ElectricalTestWaitingStartReply,
ElectricalTestWaitingNextStatus,
ElectricalTestWaitingFirstFail,
ElectricalTestWaitingSecondFail,
ReadSIMMWaitingStartReply,
ReadSIMMWaitingStartOffsetReply,
ReadSIMMWaitingLengthReply,
ReadSIMMWaitingData,
ReadSIMMWaitingStatusReply,
BootloaderStateAwaitingOKReply,
BootloaderStateAwaitingReply,
BootloaderStateAwaitingOKReplyToBootloader,
BootloaderStateAwaitingReplyToBootloader,
BootloaderStateAwaitingUnplug,
BootloaderStateAwaitingPlug,
BootloaderStateAwaitingUnplugToBootloader,
BootloaderStateAwaitingPlugToBootloader,
IdentificationWaitingSetSizeReply,
IdentificationAwaitingOKReply,
IdentificationWaitingData,
IdentificationAwaitingDoneReply,
BootloaderEraseProgramAwaitingStartOKReply,
BootloaderEraseProgramWaitingFinishReply,
BootloaderEraseProgramWaitingWriteMoreReply,
BootloaderEraseProgramWaitingWriteReply,
WritePortionWaitingSetSectorLayoutReply,
WritePortionWaitingSectorLayoutDataReply,
WritePortionWaitingSetSizeReply,
WritePortionWaitingSetVerifyModeReply,
WritePortionWaitingSetChipMaskReply,
WritePortionWaitingSetChipMaskValueReply,
WritePortionWaitingEraseReply,
WritePortionWaitingEraseConfirmation,
WritePortionWaitingEraseResult,
WritePortionWaitingWriteAtReply,
ReadFWVersionAwaitingOKReply,
ReadFWVersionWaitingData,
ReadFWVersionAwaitingDoneReply
} ProgrammerCommandState;
typedef enum ProgrammerBoardFoundState
{
ProgrammerBoardNotFound,
ProgrammerBoardFound
} ProgrammerBoardFoundState;
typedef enum ProgrammerCommand
{
EnterWaitingMode = 0,
DoElectricalTest,
IdentifyChips,
ReadByte,
ReadChips,
EraseChips,
WriteChips,
GetBootloaderState,
EnterBootloader,
EnterProgrammer,
BootloaderEraseAndWriteProgram,
SetSIMMLayout_AddressStraight,
SetSIMMLayout_AddressShifted,
SetVerifyWhileWriting,
SetNoVerifyWhileWriting,
ErasePortion,
WriteChipsAt,
ReadChipsAt,
SetChipsMask,
SetSectorLayout,
GetFirmwareVersion
} ProgrammerCommand;
typedef enum ProgrammerReply
{
CommandReplyOK,
CommandReplyError,
CommandReplyInvalid
} ProgrammerReply;
typedef enum ComputerReadReply
{
ComputerReadOK,
ComputerReadCancel
} ComputerReadReply;
typedef enum ProgrammerReadReply
{
ProgrammerReadOK,
ProgrammerReadError,
ProgrammerReadMoreData,
ProgrammerReadFinished,
ProgrammerReadConfirmCancel
} ProgrammerReadReply;
typedef enum ComputerWriteReply
{
ComputerWriteMore,
ComputerWriteFinish,
ComputerWriteCancel
} ComputerWriteReply;
typedef enum ProgrammerWriteReply
{
ProgrammerWriteOK,
ProgrammerWriteError,
ProgrammerWriteConfirmCancel,
ProgrammerWriteVerificationError = 0x80 /* high bit */
} ProgrammerWriteReply;
typedef enum ProgrammerIdentifyReply
{
ProgrammerIdentifyDone
} ProgrammerIdentifyReply;
typedef enum ProgrammerElectricalTestReply
{
ProgrammerElectricalTestFail,
ProgrammerElectricalTestDone
} ProgrammerElectricalTestReply;
typedef enum BootloaderStateReply
{
BootloaderStateInBootloader,
BootloaderStateInProgrammer
} BootloaderStateReply;
typedef enum ProgrammerBootloaderEraseWriteReply
{
BootloaderWriteOK,
BootloaderWriteError,
BootloaderWriteConfirmCancel
} ProgrammerBootloaderEraseWriteReply;
typedef enum ComputerBootloaderEraseWriteRequest
{
ComputerBootloaderWriteMore = 0,
ComputerBootloaderFinish,
ComputerBootloaderCancel
} ComputerBootloaderEraseWriteRequest;
typedef enum ProgrammerErasePortionOfChipReply
{
ProgrammerErasePortionOK = 0,
ProgrammerErasePortionError,
ProgrammerErasePortionFinished
} ProgrammerErasePortionOfChipReply;
typedef enum ProgrammerGetFWVersionReply
{
ProgrammerGetFWVersionDone
} ProgrammerGetFWVersionReply;
#define PROGRAMMER_USB_VENDOR_ID 0x16D0
#define PROGRAMMER_USB_DEVICE_ID 0x06AA
#define WRITE_CHUNK_SIZE 1024
#define READ_CHUNK_SIZE 1024
#define FIRMWARE_CHUNK_SIZE 1024
#define BLOCK_ERASE_SIZE (256*1024UL)
static ProgrammerCommandState curState = WaitingForNextCommand;
// After identifying that we're in the main program, what will be the command
// we will send and the state we will be waiting in?
static ProgrammerCommandState nextState = WaitingForNextCommand;
static uint8_t nextSendByte = 0;
static ProgrammerBoardFoundState foundState = ProgrammerBoardNotFound;
static QString programmerBoardPortName;
Programmer::Programmer(QObject *parent) :
QObject(parent),
_chipID(":/chipid/chipid.txt")
{
detectedDeviceRevision = 0;
identifyIsForWriteAttempt = false;
identifyWriteIsEntireSIMM = false;
_verifyMode = VerifyAfterWrite;
_verifyBadChipMask = 0;
verifyArray = new QByteArray();
verifyBuffer = new QBuffer(verifyArray);
verifyBuffer->open(QBuffer::ReadWrite);
serialPort = new QextSerialPort(QextSerialPort::EventDriven);
connect(serialPort, SIGNAL(readyRead()), SLOT(dataReady()));
}
Programmer::~Programmer()
{
closePort();
delete serialPort;
verifyBuffer->close();
delete verifyBuffer;
delete verifyArray;
}
void Programmer::readSIMM(QIODevice *device, uint32_t len)
{
// We're not verifying in this case
isReadVerifying = false;
internalReadSIMM(device, len);
}
void Programmer::internalReadSIMM(QIODevice *device, uint32_t len, uint32_t offset)
{
readDevice = device;
lenRead = 0;
readOffset = offset;
// Len == 0 means read the entire SIMM
if (len == 0)
{
lenRemaining = _simmCapacity;
trueLenToRead = _simmCapacity;
}
else if (len % READ_CHUNK_SIZE)
{
// We have to read a full chunk of data, so we read a little bit
// past the actual length requested but only return the amount
// requested.
uint32_t lastExtraChunk = (len % READ_CHUNK_SIZE);
lenRemaining = len - lastExtraChunk + READ_CHUNK_SIZE;
trueLenToRead = len;
}
else // already a multiple of READ_CHUNK_SIZE, no correction needed
{
lenRemaining = len;
trueLenToRead = len;
}
if (offset > 0)
{
startProgrammerCommand(ReadChipsAt, ReadSIMMWaitingStartOffsetReply);
}
else
{
startProgrammerCommand(ReadChips, ReadSIMMWaitingStartReply);
}
}
void Programmer::writeToSIMM(QIODevice *device, uint8_t chipsMask)
{
writeDevice = device;
writeChipMask = chipsMask;
if (writeDevice->size() > SIMMCapacity())
{
curState = WaitingForNextCommand;
emit writeStatusChanged(WriteFileTooBig);
return;
}
else
{
lenWritten = 0;
writeLenRemaining = writeDevice->size();
writeOffset = 0;
// Start out by identifying the chips so that we can send the correct
// erase sector layout. We have to save some flags to indicate that the
// identification is the start of a write. This isn't strictly necessary
// for full chip erases, but I do it for consistency.
identifyIsForWriteAttempt = true;
identifyWriteIsEntireSIMM = true;
identificationShiftCounter = 0;
startProgrammerCommand(SetSIMMLayout_AddressStraight, IdentificationWaitingSetSizeReply);
}
}
void Programmer::writeToSIMM(QIODevice *device, uint32_t startOffset, uint32_t length, uint8_t chipsMask)
{
writeDevice = device;
writeChipMask = chipsMask;
if ((writeDevice->size() > SIMMCapacity()) ||
(startOffset + length > SIMMCapacity()))
{
curState = WaitingForNextCommand;
emit writeStatusChanged(WriteFileTooBig);
return;
}
else if ((startOffset % BLOCK_ERASE_SIZE) || (length % BLOCK_ERASE_SIZE))
{
curState = WaitingForNextCommand;
emit writeStatusChanged(WriteEraseBlockWrongSize);
return;
}
else
{
lenWritten = 0;
writeLenRemaining = writeDevice->size() - startOffset;
if (writeLenRemaining > length)
{
writeLenRemaining = length;
}
device->seek(startOffset);
writeOffset = startOffset;
writeLength = length;
// Start out by identifying the chips so that we can send the correct
// erase sector layout. We have to save some flags to indicate that the
// identification is the start of a write.
identifyIsForWriteAttempt = true;
identifyWriteIsEntireSIMM = false;
identificationShiftCounter = 0;
startProgrammerCommand(SetSIMMLayout_AddressStraight, IdentificationWaitingSetSizeReply);
}
}
void Programmer::sendByte(uint8_t b)
{
serialPort->write((const char *)&b, 1);
}
void Programmer::sendWord(uint32_t w)
{
sendByte((w >> 0) & 0xFF);
sendByte((w >> 8) & 0xFF);
sendByte((w >> 16) & 0xFF);
sendByte((w >> 24) & 0xFF);
}
uint8_t Programmer::readByte()
{
uint8_t returnVal;
serialPort->read((char *)&returnVal, 1);
// TODO: Error checking if read fails?
return returnVal;
}
void Programmer::dataReady()
{
while (!serialPort->atEnd())
{
handleChar(readByte());
}
}
void Programmer::handleChar(uint8_t c)
{
switch (curState)
{
case WaitingForNextCommand:
// Not expecting anything. Ignore it.
break;
// Expecting reply after we told the programmer the sector layout to use.
// Go ahead and send the sector layout even if we're doing a full erase.
// It makes the code more maintainable and opens up possibilities for the future.
case WriteSIMMWaitingSetSectorLayoutReply:
case WritePortionWaitingSetSectorLayoutReply:
switch (c)
{
case CommandReplyOK:
// We are talking with firmware that supports receiving sector layout data! Yay!
for (int i = 0; i < sectorGroups.count(); i++)
{
// Send the count of sectors in this group
sendWord(sectorGroups[i].first);
// Send the size of each sector in this group
sendWord(sectorGroups[i].second);
}
// Send a 0 to terminate the list of sector groups.
sendWord(0);
// This should cause the programmer to respond back to us with a yea or nay.
curState = (curState == WriteSIMMWaitingSetSectorLayoutReply) ?
WriteSIMMWaitingSectorLayoutDataReply : WritePortionWaitingSectorLayoutDataReply;
break;
case CommandReplyInvalid:
case CommandReplyError:
default:
// If this command fails, just silently ignore the error and move
// onto setting the SIMM address unlock pattern instead.
uint8_t setLayoutCommand = (SIMMChip() == SIMM_TSOP_x8) ?
SetSIMMLayout_AddressShifted : SetSIMMLayout_AddressStraight;
ProgrammerCommandState newState = (curState == WriteSIMMWaitingSetSectorLayoutReply) ?
WriteSIMMWaitingSetSizeReply : WritePortionWaitingSetSizeReply;
startProgrammerCommand(setLayoutCommand, newState);
}
break;
// Expecting reply after the programmer allowed us to send the sector layout
case WriteSIMMWaitingSectorLayoutDataReply:
case WritePortionWaitingSectorLayoutDataReply:
switch (c)
{
case CommandReplyOK: {
// All good! Now move onto setting the SIMM address unlock pattern
uint8_t setLayoutCommand = (SIMMChip() == SIMM_TSOP_x8) ?
SetSIMMLayout_AddressShifted : SetSIMMLayout_AddressStraight;
ProgrammerCommandState newState = (curState == WriteSIMMWaitingSectorLayoutDataReply) ?
WriteSIMMWaitingSetSizeReply : WritePortionWaitingSetSizeReply;
startProgrammerCommand(setLayoutCommand, newState);
break;
}
case CommandReplyInvalid:
case CommandReplyError:
// Error after trying to send the sector layout. The firmware clearly supports the command,
// so we need to return an error.
qDebug() << "Error reply sending erase sector layout.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteError);
break;
}
break;
// Expecting reply after we told the programmer the size of SIMM to expect
case WriteSIMMWaitingSetSizeReply:
case WritePortionWaitingSetSizeReply:
switch (c)
{
case CommandReplyOK:
// If we got an OK reply, we're good to go. Next, check for the
// "verify while writing" capability if needed...
uint8_t verifyCommand;
if (verifyMode() == VerifyWhileWriting)
{
verifyCommand = SetVerifyWhileWriting;
}
else
{
verifyCommand = SetNoVerifyWhileWriting;
}
if (curState == WriteSIMMWaitingSetSizeReply)
{
curState = WriteSIMMWaitingSetVerifyModeReply;
}
else if (curState == WritePortionWaitingSetSizeReply)
{
curState = WritePortionWaitingSetVerifyModeReply;
}
sendByte(verifyCommand);
break;
case CommandReplyInvalid:
case CommandReplyError:
// If we got an error reply, we MAY still be OK unless we were
// requesting the large SIMM type, in which case the firmware
// doesn't support the large SIMM type so the user needs to know.
if (SIMMChip() != SIMM_PLCC_x8)
{
// Uh oh -- this is an old firmware that doesn't support a big
// SIMM. Let the caller know that the programmer board needs a
// firmware update.
qDebug() << "Programmer board needs firmware update.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteNeedsFirmwareUpdateBiggerSIMM);
}
else
{
// Error reply, but we're writing a small SIMM, so the firmware
// doesn't need updating -- it just didn't know how to handle
// the "set size" command. But that's OK -- it only supports
// the size we requested, so nothing's wrong.
// So...check for the "verify while writing" capability if needed.
uint8_t verifyCommand;
if (verifyMode() == VerifyWhileWriting)
{
verifyCommand = SetVerifyWhileWriting;
}
else
{
verifyCommand = SetNoVerifyWhileWriting;
}
if (curState == WriteSIMMWaitingSetSizeReply)
{
curState = WriteSIMMWaitingSetVerifyModeReply;
}
else if (curState == WritePortionWaitingSetSizeReply)
{
curState = WritePortionWaitingSetVerifyModeReply;
}
sendByte(verifyCommand);
}
break;
}
break;
// Expecting reply from programmer after we told it to verify during write
// (or not to verify during write)
case WriteSIMMWaitingSetVerifyModeReply:
case WritePortionWaitingSetVerifyModeReply:
switch (c)
{
case CommandReplyOK:
// If we got an OK reply, we're good. Now try to set the chip mask.
if (curState == WriteSIMMWaitingSetVerifyModeReply)
{
sendByte(SetChipsMask);
curState = WriteSIMMWaitingSetChipMaskReply;
}
else if (curState == WritePortionWaitingSetVerifyModeReply)
{
sendByte(SetChipsMask);
curState = WritePortionWaitingSetChipMaskReply;
}
break;
case CommandReplyInvalid:
case CommandReplyError:
// If we got an error reply, we MAY still be OK unless we were
// asking to verify while writing, in which case the firmware
// doesn't support verify during write so the user needs to know.
if (verifyMode() == VerifyWhileWriting)
{
// Uh oh -- this is an old firmware that doesn't support verify
// while write. Let the caller know that the programmer board
// needs a firmware update.
qDebug() << "Programmer board needs firmware update.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteNeedsFirmwareUpdateVerifyWhileWrite);
}
else
{
// Error reply, but we're not trying to verify while writing, so
// the firmware doesn't need updating -- it just didn't know how to handle
// the "set verify mode" command. But that's OK -- we don't need
// that command if we're not verifying while writing.
// So move onto the next thing to try.
if (curState == WriteSIMMWaitingSetVerifyModeReply)
{
sendByte(SetChipsMask);
curState = WriteSIMMWaitingSetChipMaskReply;
}
else if (curState == WritePortionWaitingSetVerifyModeReply)
{
sendByte(SetChipsMask);
curState = WritePortionWaitingSetChipMaskReply;
}
}
break;
}
break;
case WriteSIMMWaitingSetChipMaskReply:
case WritePortionWaitingSetChipMaskReply:
switch (c)
{
case CommandReplyOK:
// OK, now we can send the chip mask and move onto the next state
sendByte(writeChipMask);
if (curState == WriteSIMMWaitingSetChipMaskReply)
{
curState = WriteSIMMWaitingSetChipMaskValueReply;
}
else if (curState == WritePortionWaitingSetChipMaskReply)
{
curState = WritePortionWaitingSetChipMaskValueReply;
}
break;
case CommandReplyInvalid:
case CommandReplyError:
// Error reply. If we're trying to set a mask of 0x0F, no error, it
// just means the firmware's out of date and doesn't support setting
// custom chip masks. Ignore and move on.
if (writeChipMask == 0x0F)
{
// OK, erase the SIMM and get the ball rolling.
// Special case: Send out notification we are starting an erase command.
// I don't have any hooks into the process between now and the erase reply.
emit writeStatusChanged(WriteErasing);
if (curState == WriteSIMMWaitingSetChipMaskReply)
{
sendByte(EraseChips);
curState = WriteSIMMWaitingEraseReply;
}
else if (curState == WritePortionWaitingSetChipMaskReply)
{
sendByte(ErasePortion);
curState = WritePortionWaitingEraseReply;
}
}
else
{
// Uh oh -- this is an old firmware that doesn't support custom
// chip masks. Let the caller know that the programmer board
// needs a firmware update.
qDebug() << "Programmer board needs firmware update.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteNeedsFirmwareUpdateIndividualChips);
}
break;
}
break;
case WriteSIMMWaitingSetChipMaskValueReply:
case WritePortionWaitingSetChipMaskValueReply:
switch (c)
{
case CommandReplyOK:
// OK, erase the SIMM and get the ball rolling.
// Special case: Send out notification we are starting an erase command.
// I don't have any hooks into the process between now and the erase reply.
emit writeStatusChanged(WriteErasing);
if (curState == WriteSIMMWaitingSetChipMaskValueReply)
{
sendByte(EraseChips);
curState = WriteSIMMWaitingEraseReply;
}
else if (curState == WritePortionWaitingSetChipMaskValueReply)
{
sendByte(ErasePortion);
curState = WritePortionWaitingEraseReply;
}
break;
case CommandReplyInvalid:
case CommandReplyError:
// Error after trying to set the value.
qDebug() << "Error reply setting chip mask.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteError);
break;
}
break;
// Expecting reply from programmer after we told it to erase the chip
case WriteSIMMWaitingEraseReply:
{
switch (c)
{
case CommandReplyOK:
sendByte(WriteChips);
curState = WriteSIMMWaitingWriteReply;
qDebug() << "Chips erased. Now asking to start writing...";
emit writeStatusChanged(WriteEraseComplete);
emit writeTotalLengthChanged(writeLenRemaining);
emit writeCompletionLengthChanged(lenWritten);
break;
case CommandReplyError:
qDebug() << "Error erasing chips.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteEraseFailed);
break;
}
break;
}
case WritePortionWaitingEraseReply:
{
switch (c)
{
case CommandReplyOK:
sendWord(writeOffset);
sendWord(writeLength);
qDebug("Sending %u, %u", writeOffset, writeLength);
curState = WritePortionWaitingEraseConfirmation;
qDebug() << "Sent erase positions, waiting for reply...";
break;
case CommandReplyError:
// Uh oh -- this is an old firmware that doesn't support verify
// while write. Let the caller know that the programmer board
// needs a firmware update.
qDebug() << "Programmer board needs firmware update.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteNeedsFirmwareUpdateErasePortion);
break;
}
break;
}
case WritePortionWaitingEraseConfirmation:
{
switch (c)
{
case ProgrammerErasePortionOK:
curState = WritePortionWaitingEraseResult;
break;
case ProgrammerErasePortionError:
// Programmer didn't like the position/length we gave it
qDebug() << "Programmer didn't like erase pos/length.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteEraseFailed);
break;
}
break;
}
case WritePortionWaitingEraseResult:
{
switch (c)
{
case ProgrammerErasePortionFinished:
// we're done erasing, now it's time to write the data
// starting at where we wanted to flash to
sendByte(WriteChipsAt);
curState = WritePortionWaitingWriteAtReply;
qDebug() << "Chips partially erased. Now asking to start writing...";
emit writeStatusChanged(WriteEraseComplete);
break;
case ProgrammerErasePortionError:
// Programmer failed to erase
qDebug() << "Programmer had error erasing.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteEraseFailed);
break;
}
break;
}
case WritePortionWaitingWriteAtReply:
{
switch (c)
{
case CommandReplyOK:
sendWord(writeOffset);
qDebug() << "Sending" << writeOffset;
curState = WriteSIMMWaitingWriteReply;
emit writeTotalLengthChanged(writeLenRemaining);
emit writeCompletionLengthChanged(lenWritten);
qDebug() << "Partial write command accepted, sending offset...";
break;
case CommandReplyError:
case CommandReplyInvalid:
default:
// Programmer failed to erase
qDebug() << "Programmer didn't accept 'write at' command.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteError);
break;
}
break;
}
// Expecting reply from programmer after we sent a chunk of data to write
// (or after we first told it we're going to start writing)
case WriteSIMMWaitingWriteReply:
// This is a special case in the protocol for efficiency.
if (c & ProgrammerWriteVerificationError)
{
_verifyBadChipMask = c & ~ProgrammerWriteVerificationError;
qDebug() << "Verification error during write.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteVerificationFailure);
break;
}
else
{
switch (c)
{
case CommandReplyOK:
// We're in write SIMM mode. Now ask to start writing
if (writeLenRemaining > 0)
{
sendByte(ComputerWriteMore);
curState = WriteSIMMWaitingWriteMoreReply;
qDebug() << "Write more..." << writeLenRemaining << "remaining.";
}
else
{
sendByte(ComputerWriteFinish);
curState = WriteSIMMWaitingFinishReply;
qDebug() << "Finished writing. Sending write finish command...";
}
break;
case CommandReplyError:
qDebug() << "Error entering write mode.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteError);
break;
}
}
break;
// Expecting reply from programmer after we requested to write another data chunk
case WriteSIMMWaitingWriteMoreReply:
{
qDebug() << "Write more reply:" << c;
switch (c)
{
case ProgrammerWriteOK:
{
qDebug() << "Programmer replied OK to send 1024 bytes of data! Sending...";
// Write the next chunk of data to the SIMM...
int chunkSize = WRITE_CHUNK_SIZE;
if (writeLenRemaining < WRITE_CHUNK_SIZE)
{
chunkSize = writeLenRemaining;
}
// Read the chunk from the file!
QByteArray thisChunk = writeDevice->read(chunkSize);
// If it isn't a WRITE_CHUNK_SIZE chunk, pad the rest of it with 0xFFs (unprogrammed bytes)
// so the total chunk size is WRITE_CHUNK_SIZE, since that's what the programmer board expects.
for (int x = writeLenRemaining; x < WRITE_CHUNK_SIZE; x++)
{
thisChunk.append(0xFF);
}
// Write the chunk out (it's asynchronous so will return immediately)
serialPort->write(thisChunk);
// OK, now we're waiting to hear back from the programmer on the result
qDebug() << "Waiting for status reply...";
curState = WriteSIMMWaitingWriteReply;
writeLenRemaining -= chunkSize;
lenWritten += chunkSize;
emit writeCompletionLengthChanged(lenWritten);
break;
}
case ProgrammerWriteError:
default:
qDebug() << "Error writing to chips.";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteError);
break;
}
break;
}
// Expecting reply from programmer after we told it we're done writing
case WriteSIMMWaitingFinishReply:
switch (c)
{
case ProgrammerWriteOK:
if (verifyMode() == VerifyAfterWrite)
{
isReadVerifying = true;
// Ensure the verify buffer is empty
verifyArray->clear();
verifyBuffer->seek(0);
verifyLength = lenWritten;
// Start reading from the SIMM now!
emit writeStatusChanged(WriteVerifying);
internalReadSIMM(verifyBuffer, writeDevice->size());
}
else
{
curState = WaitingForNextCommand;
qDebug() << "Write success at end";
closePort();
// Emit the correct signal based on how we finished
if (verifyMode() == NoVerification)
{
emit writeStatusChanged(WriteCompleteNoVerify);
}
else
{
emit writeStatusChanged(WriteCompleteVerifyOK);
}
}
break;
case ProgrammerWriteError:
default:
qDebug() << "Write failure at end";
curState = WaitingForNextCommand;
closePort();
emit writeStatusChanged(WriteError);
break;
}
break;
// ELECTRICAL TEST STATE HANDLERS
// Expecting reply from programmer after we told it to run an electrical test
case ElectricalTestWaitingStartReply:
switch (c)
{
case CommandReplyOK:
curState = ElectricalTestWaitingNextStatus;
emit electricalTestStatusChanged(ElectricalTestStarted);
electricalTestErrorCounter = 0;
break;
case CommandReplyError:
case CommandReplyInvalid:
default:
curState = WaitingForNextCommand;
closePort();
emit electricalTestStatusChanged(ElectricalTestCouldntStart);
}
break;
// Expecting info from programmer about the electrical test in progress
// (Either that it's done or that it found a failure)
case ElectricalTestWaitingNextStatus:
switch (c)
{
case ProgrammerElectricalTestDone:
curState = WaitingForNextCommand;
closePort();
if (electricalTestErrorCounter > 0)
{
emit electricalTestStatusChanged(ElectricalTestFailed);
}
else
{
emit electricalTestStatusChanged(ElectricalTestPassed);
}
break;
case ProgrammerElectricalTestFail:
electricalTestErrorCounter++;
curState = ElectricalTestWaitingFirstFail;
break;
}
break;
// Expecting electrical test fail location #1
case ElectricalTestWaitingFirstFail:
electricalTestFirstErrorLoc = c;
curState = ElectricalTestWaitingSecondFail;
break;
// Expecting electrical test fail location #2
case ElectricalTestWaitingSecondFail:
emit electricalTestFailLocation(electricalTestFirstErrorLoc, c);
curState = ElectricalTestWaitingNextStatus;
break;
// READ SIMM STATE HANDLERS
// Expecting reply after we told the programmer to start reading
case ReadSIMMWaitingStartReply:
case ReadSIMMWaitingStartOffsetReply:
switch (c)
{
case CommandReplyOK:
if (!isReadVerifying)
{
emit readStatusChanged(ReadStarting);
}
else
{
emit writeStatusChanged(WriteVerifyStarting);
}
curState = ReadSIMMWaitingLengthReply;