forked from microtherion/ScratchMonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMoHVPP.cpp
747 lines (672 loc) · 20 KB
/
SMoHVPP.cpp
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
// -*- mode: c++; tab-width: 4; indent-tabs-mode: nil -*-
//
// ScratchMonkey 0.1 - STK500v2 compatible programmer for Arduino
//
// File: SMoHVPP.cpp - High Voltage Parallel Programming
// (for MCUs with 20 pins and more)
//
// Copyright (c) 2013 Matthias Neeracher <microtherion@gmail.com>
// All rights reserved.
//
// See license at bottom of this file or at
// http://opensource.org/licenses/bsd-license.php
//
// Modified by Hisashi Ito <info at mewpro.cc> (c) 2015
// in order to support HVprog2, an STK500 clone open hardware that you can buy or make.
// http://mewpro.cc/2016/01/20/how-to-use-hvprog2/
#include "SMoHVPP.h"
#include "SMoCommand.h"
#include "SMoGeneral.h"
#include "SMoConfig.h"
#ifdef DEBUG_HVPP
#include "SMoDebug.h"
#endif
#include <Arduino.h>
#include <SPI.h>
enum {
HVPP_RESET = SMO_HVRESET,
HVPP_VCC = SMO_SVCC,
#if SMO_LAYOUT==SMO_LAYOUT_STANDARD
HVPP_RCLK = A1,
HVPP_XTAL = A2,
#define HVPP_TOGGLE_XTAL do { PORTC |= _BV(2); PORTC &= ~_BV(2); } while (0)
#define HVPP_RDY 12
#define ISREADY (digitalRead(HVPP_RDY))
#elif SMO_LAYOUT==SMO_LAYOUT_LEONARDO
HVPP_XTAL = 13,
#define HVPP_TOGGLE_XTAL do { PORTC |= _BV(7); PORTC &= ~_BV(7); } while (0)
#define HVPP_RDY 12
#define ISREADY (digitalRead(HVPP_RDY))
#elif SMO_LAYOUT==SMO_LAYOUT_MEGA
HVPP_XTAL = 13,
#define HVPP_TOGGLE_XTAL do { PORTB |= _BV(7); PORTB &= ~_BV(7); } while (0)
#define HVPP_RDY 12
#define ISREADY (digitalRead(HVPP_RDY))
#elif SMO_LAYOUT==SMO_LAYOUT_HVPROG2
HVPP_XTAL = 15,
#define HVPP_TOGGLE_XTAL do { PORTD &= ~_BV(7); PORTD |= _BV(7); } while (0)
#define ISREADY (PINC & _BV(SMoGeneral::gControlStack[kRdyBsyBit]))
#endif
};
//
// HVPP, for 28 pins and more, requires 8 output signals and 1 input
// signal. 20 pin MCUs multiplex some of the output signals, and they
// don't do it all the same way. We rely on the control stack uploaded
// by avrdude to tell us what signals to set, when.
//
// This implementation uses a 74HV595 shift register for the output
// signal, but if you're using a board with more output pins, or are
// willing to have the TX/RX pins do double duty, you may be able to
// dispense with that.
//
// Control patterns, expressed as index values for the control stack
// (Adopted from Simon Quiang's AVRminiProg implementation)
//
enum {
kLoadAddr = 0,
kLoadData = 4,
kLoadCommand = 8,
kDone = 12,
kCommitData = 16,
kEnableRead = 20,
kPageLoad = 24,
kRdyBsyMask = 25,
kOEdelay = 26, // Atmel's newest firmware 2.10 seems to ignore this value
kRdyBsyBit = 27,
kInit = 28,
// 0x00
// 0x00
kPoll = 31, // 0x00 for all but 0x01 CAN32/64/128 and 4414/4434, 0x02 m644/1284 family and m2560/2561
kLowByte = 0,
kHighByte = 1,
kExtByte = 2,
kExt2Byte = 3,
};
#define HVPPControlPattern(c, b) (SMoGeneral::gControlStack[c + b])
// Command Byte Bit Coding
#define HVPP_FLASH 0x00
#define HVPP_EEPROM 0x01
#define HVPP_ChipErase 0x80
#define HVPP_WriteFuseBits 0x40
#define HVPP_WriteLockBits 0x20
#define HVPP_WriteMemory 0x10
#define HVPP_WriteFlash (HVPP_WriteMemory | HVPP_FLASH)
#define HVPP_WriteEEPROM (HVPP_WriteMemory | HVPP_EEPROM)
#define HVPP_ReadSignature 0x08
#define HVPP_ReadFuseLock 0x04
#define HVPP_ReadMemory 0x02
#define HVPP_ReadFlash (HVPP_ReadMemory | HVPP_FLASH)
#define HVPP_ReadEEPROM (HVPP_ReadMemory | HVPP_EEPROM)
#define HVPP_NoOperation 0x00
//
// Control/Data access
//
#if SMO_LAYOUT==SMO_LAYOUT_STANDARD
//
// Delegate controls to auxiliary 74HC595 shift register, but
// can transfer data pretty easily
//
enum {
PORTD_MASK = 0xFC,
PORTB_MASK = 0x03,
PORTD_SHIFT = 2,
PORTB_SHIFT = 6
};
inline void
HVPPSetControlSignals(uint8_t signals)
{
digitalWrite(HVPP_RCLK, LOW);
SPI.transfer(signals);
digitalWrite(HVPP_RCLK, HIGH);
}
inline void
HVPPInitControlSignals()
{
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV2);// Pedal to the metal
digitalWrite(HVPP_RCLK, LOW);
pinMode(HVPP_RCLK, OUTPUT);
pinMode(HVPP_RDY, INPUT);
}
inline void
HVPPEndControls()
{
SPI.end();
}
inline void
HVPPSetDataMode(uint8_t mode)
{
if (mode == OUTPUT) {
DDRD |= PORTD_MASK;
DDRB |= PORTB_MASK;
} else {
DDRD &= ~PORTD_MASK;
DDRB &= ~PORTB_MASK;
}
}
inline void
HVPPSetDataBits(uint8_t dataOut)
{
PORTD = (PORTD & ~PORTD_MASK) | ((dataOut << PORTD_SHIFT) & PORTD_MASK);
PORTB = (PORTB & ~PORTB_MASK) | ((dataOut >> PORTB_SHIFT) & PORTB_MASK);
}
inline uint8_t
HVPPGetDataBits()
{
// No need for masking
return (PINB << PORTB_SHIFT | PIND >> PORTD_SHIFT) & 0xFF;
}
#elif SMO_LAYOUT==SMO_LAYOUT_LEONARDO
//
// Leonardos don't have 8 contiguous pins anywhere, so we split the
// control signals across two ports. The data signals are not as
// critical, so we just use digitalRead (we'd have to split them
// across at least three ports).
//
enum {
PORTF_MASK = 0xF1,
PORTD_MASK = 0x0C,
};
inline void
HVPPSetControlSignals(uint8_t signals)
{
PORTF = (PORTF & ~PORTF_MASK) | (signals & PORTF_MASK);
PORTD = (PORTD & ~PORTD_MASK) | (signals & PORTD_MASK);
}
inline void
HVPPInitControlSignals()
{
DDRF |= PORTF_MASK;
DDRD |= PORTD_MASK;
pinMode(HVPP_RDY, INPUT);
}
inline void
HVPPEndControls()
{
}
inline void
HVPPSetDataMode(uint8_t mode)
{
for (uint8_t pin=2; pin<10; ++pin)
pinMode(pin, mode);
}
inline void
HVPPSetDataBits(uint8_t dataOut)
{
for (uint8_t pin=2; pin<10; ++pin) {
digitalWrite(pin, dataOut & 1);
dataOut >>= 1;
}
}
inline uint8_t
HVPPGetDataBits()
{
uint8_t dataIn;
for (uint8_t pin=9; pin >= 2; --pin)
dataIn = dataIn << 1 | digitalRead(pin);
return dataIn;
}
#elif SMO_LAYOUT==SMO_LAYOUT_MEGA
//
// Megas have lots of contiguous pins, so we just use two full ports.
//
inline void
HVPPSetControlSignals(uint8_t signals)
{
PORTF = signals;
}
inline void
HVPPInitControlSignals()
{
DDRF = 0xFF;
pinMode(HVPP_RDY, INPUT);
}
inline void
HVPPEndControls()
{
}
inline void
HVPPSetDataMode(uint8_t mode)
{
if (mode == OUTPUT) {
DDRK = 0xFF;
} else {
DDRK = 0x00;
}
}
inline void
HVPPSetDataBits(uint8_t dataOut)
{
PORTK = dataOut;
}
inline uint8_t
HVPPGetDataBits()
{
return PINK;
}
#elif SMO_LAYOUT==SMO_LAYOUT_HVPROG2
//
// DIP-40 AVRs have enough numbers of contiguous pins
//
inline void
HVPPSetControlSignals(uint8_t signals)
{
PORTC = signals & SMoGeneral::gControlStack[kRdyBsyMask];
}
inline void
HVPPInitControlSignals()
{
// disable pullups as SPI.end() doesn't restore them
DDRB = 0xFF;
// set data directions
DDRC = SMoGeneral::gControlStack[kRdyBsyMask];
}
inline void
HVPPEndControls()
{
}
inline void
HVPPSetDataMode(uint8_t mode)
{
if (mode == OUTPUT)
DDRB = 0xFF;
else
DDRB = 0x00;
}
inline void
HVPPSetDataBits(uint8_t dataOut)
{
PORTB = dataOut;
}
inline uint8_t
HVPPGetDataBits()
{
return PINB;
}
#endif
static void
HVPPControls(uint8_t c)
{
#ifdef DEBUG_HVPP
SMoDebug.print("Ctrl ");
SMoDebug.print(c, BIN);
SMoDebug.println();
#endif
HVPPSetControlSignals(c);
}
static void
HVPPControls(uint8_t c, uint8_t data)
{
#ifdef DEBUG_HVPP
SMoDebug.print("Ctrl ");
SMoDebug.print(c, BIN);
SMoDebug.println();
#endif
HVPPSetControlSignals(c);
#ifdef DEBUG_HVPP
SMoDebug.print("Data<");
SMoDebug.println(data, HEX);
#endif
HVPPSetDataBits(data);
HVPP_TOGGLE_XTAL;
}
static void
HVPPControls(uint8_t c, uint8_t *p)
{
#ifdef DEBUG_HVPP
SMoDebug.print("Ctrl ");
SMoDebug.print(c, BIN);
SMoDebug.println();
#endif
HVPPSetControlSignals(c);
// very short delay for t2313A
switch (SMoGeneral::gControlStack[kOEdelay]) {
case 4:
__asm__ __volatile__ ("nop\n\t");
case 3:
__asm__ __volatile__ ("nop\n\t");
case 2:
__asm__ __volatile__ ("nop\n\t");
case 1:
__asm__ __volatile__ ("nop\n\t");
default:
break;
}
*p = HVPPGetDataBits();
#ifdef DEBUG_HVPP
SMoDebug.print("Data>");
SMoDebug.println(*p, HEX);
#endif
}
inline void
HVPPInitControls()
{
HVPPInitControlSignals();
HVPPSetControlSignals(HVPPControlPattern(kInit, kLowByte)); // Set all control pins to zero
}
static bool
HVPPPollWait(uint8_t pollTimeout)
{
uint32_t time = millis();
delayMicroseconds(1);
while (!ISREADY)
if (millis() - time > (uint32_t)pollTimeout)
return false;
return true;
}
void
SMoHVPP::EnterProgmode()
{
#ifdef DEBUG_HVPP
SMoDebugInit();
#endif
const uint8_t stabDelay = SMoCommand::gBody[1];
const uint8_t progModeDelay = SMoCommand::gBody[2];
const uint8_t latchCycles = SMoCommand::gBody[3];
const uint8_t toggleVtg = SMoCommand::gBody[4];
const uint8_t powerOffDelay = SMoCommand::gBody[5];
const uint8_t resetDelayMs = SMoCommand::gBody[6];
const uint8_t resetDelayUs = SMoCommand::gBody[7];
delay(progModeDelay);
// power off target
#ifdef SMO_AVCC
analogWrite(HVPP_VCC, 0);
#else
digitalWrite(HVPP_VCC, LOW);
pinMode(HVPP_VCC, OUTPUT);
#endif
delayMicroseconds(250);
// target RESET = 0V
digitalWrite(HVPP_RESET, HIGH);
pinMode(HVPP_RESET, OUTPUT);
// set control pins
HVPPInitControls();
HVPP_TOGGLE_XTAL;
pinMode(HVPP_XTAL, OUTPUT);
#if SMO_LAYOUT==SMO_LAYOUT_HVPROG2
digitalWrite(SMO_HVENABLE, HIGH);
pinMode(SMO_HVENABLE, OUTPUT); // enable 12V
#endif
// make sure HVPP_VCC is 0V
if (toggleVtg) {
#ifdef SMO_AVCC
uint32_t time = millis();
while (analogRead(SMO_AVCC) > 10) { // wait until HVPP_VCC becomes lower than 0.06V
if (millis() - time > DEFAULTTIMEOUT)
break;
}
#endif
delay(powerOffDelay);
}
// power on target
#ifdef SMO_AVCC
analogWrite(HVPP_VCC, 255);
{
uint32_t time = millis();
while (analogRead(SMO_AVCC) < 743) { // wait until HVPP_VCC becomes higher than 4.5V
if (millis() - time > DEFAULTTIMEOUT)
break;
}
}
analogWrite(HVPP_VCC, FIVEVOLT);
#else
digitalWrite(HVPP_VCC, HIGH);
#endif
delay(resetDelayMs);
delayMicroseconds(resetDelayUs * 10);
// toggle XTAL1
for (uint8_t i=0; i<latchCycles; ++i) {
HVPP_TOGGLE_XTAL;
}
// apply 12V to RESET
digitalWrite(HVPP_RESET, LOW);
delay(stabDelay);
HVPPSetDataMode(OUTPUT);
HVPPControls(HVPPControlPattern(kDone, kLowByte));
SMoCommand::SendResponse();
}
void
SMoHVPP::LeaveProgmode()
{
const uint8_t stabDelay = SMoCommand::gBody[1];
const uint8_t resetDelay = SMoCommand::gBody[2];
#ifdef SMO_AVCC
analogWrite(HVPP_VCC, 0);
#else
digitalWrite(HVPP_VCC, LOW);
#endif
digitalWrite(HVPP_RESET, HIGH);
delayMicroseconds(resetDelay * 10);
#ifdef SMO_AVCC
digitalWrite(SMO_HVENABLE, LOW); // disable 12V
analogWrite(HVPP_VCC, 255);
{
uint32_t time = millis();
while (analogRead(SMO_AVCC) < 743) { // wait until HVPP_VCC becomes higher than 4.5V
if (millis() - time > DEFAULTTIMEOUT)
break;
}
}
analogWrite(HVPP_VCC, FIVEVOLT);
digitalWrite(HVPP_RESET, LOW);
#endif
delayMicroseconds(stabDelay * 10);
SMoCommand::SendResponse();
}
void
SMoHVPP::ChipErase()
{
const uint8_t pulseWidth = SMoCommand::gBody[1];
const uint8_t pollTimeout = SMoCommand::gBody[2];
HVPPControls(HVPPControlPattern(kLoadCommand, kLowByte), (uint8_t)HVPP_ChipErase);
HVPPControls(HVPPControlPattern(kCommitData, kLowByte));
delay(pulseWidth);
HVPPControls(HVPPControlPattern(kDone, kLowByte));
// SMoGeneral::gControlStack[kPoll] is simply ignored. :)
if (pollTimeout)
HVPPPollWait(pollTimeout);
SMoCommand::SendResponse();
}
static void
ProgramMemory(bool flash)
{
uint16_t numBytes = SMoCommand::gBody[1] << 8 | SMoCommand::gBody[2];
const uint8_t mode = SMoCommand::gBody[3];
const uint8_t pollTimeout = SMoCommand::gBody[4];
const uint8_t * data = &SMoCommand::gBody[5];
uint16_t pageMask = (1 << ((mode & 0x0E ? mode & 0x0E : 0x10) >> 1) - (flash ? 1 : 0)) - 1;
int8_t b;
HVPPControls(HVPPControlPattern(kLoadCommand, kLowByte), (uint8_t)(flash ? HVPP_WriteFlash : HVPP_WriteEEPROM));
if (SMoGeneral::gAddress.d.extH & 0x80)
HVPPControls(HVPPControlPattern(kLoadAddr, kExtByte), SMoGeneral::gAddress.d.extL);
HVPPControls(HVPPControlPattern(kLoadAddr, kHighByte), SMoGeneral::gAddress.c[1]);
HVPPControls(HVPPControlPattern(kLoadAddr, kLowByte), SMoGeneral::gAddress.c[0]);
do {
b = (!(numBytes & 1) || !flash) ? kLowByte : kHighByte;
HVPPControls(HVPPControlPattern(kLoadData, b), *data);
if (!(mode & 1)) { // Byte mode
HVPPControls(HVPPControlPattern(kCommitData, b));
HVPPControls(HVPPControlPattern(kDone, b));
if (!HVPPPollWait(pollTimeout))
goto TIMEOUT_ProgramMemory;
}
data++;
if (!(--numBytes & 1) || !flash) {
SMoGeneral::gAddress.d.addr++;
if (mode & 1) { // Page mode
HVPPControls(HVPPControlPattern(kPageLoad, kLowByte)); // assert PAGEL
if (!(SMoGeneral::gAddress.d.addr & pageMask) && mode & 0x80 || (numBytes == 0 && mode & 0x40)) { // Write page to memory
HVPPControls(HVPPControlPattern(kCommitData, kLowByte));
HVPPControls(HVPPControlPattern(kDone, kLowByte));
if (!HVPPPollWait(pollTimeout))
goto TIMEOUT_ProgramMemory;
}
}
if (!numBytes)
break;
if (SMoGeneral::gAddress.c[0] == 0) {
if (SMoGeneral::gAddress.c[1] == 0)
HVPPControls(HVPPControlPattern(kLoadAddr, kExtByte), ++SMoGeneral::gAddress.d.extL);
HVPPControls(HVPPControlPattern(kLoadAddr, kHighByte), SMoGeneral::gAddress.c[1]);
}
HVPPControls(HVPPControlPattern(kLoadAddr, kLowByte), SMoGeneral::gAddress.c[0]);
}
} while (numBytes);
if (mode & 0x40)
HVPPControls(HVPPControlPattern(kLoadCommand, kLowByte), (uint8_t)HVPP_NoOperation);
SMoCommand::SendResponse();
return;
TIMEOUT_ProgramMemory:
SMoCommand::SendResponse(STATUS_RDY_BSY_TOUT);
}
static void
ReadMemory(bool flash)
{
uint16_t numBytes = SMoCommand::gBody[1] << 8 | SMoCommand::gBody[2];
uint8_t * dataOut = &SMoCommand::gBody[2];
int8_t b;
HVPPControls(HVPPControlPattern(kLoadCommand, kLowByte), (uint8_t)(flash ? HVPP_ReadFlash : HVPP_ReadEEPROM));
if (SMoGeneral::gAddress.d.extH & 0x80)
HVPPControls(HVPPControlPattern(kLoadAddr, kExtByte), SMoGeneral::gAddress.d.extL);
HVPPControls(HVPPControlPattern(kLoadAddr, kHighByte), SMoGeneral::gAddress.c[1]);
HVPPControls(HVPPControlPattern(kLoadAddr, kLowByte), SMoGeneral::gAddress.c[0]);
HVPPSetDataMode(INPUT);
do {
b = (!(numBytes & 1) || !flash) ? kLowByte : kHighByte;
HVPPControls(HVPPControlPattern(kEnableRead, b), (uint8_t *)dataOut++);
if (!(--numBytes & 1) || !flash) {
HVPPControls(HVPPControlPattern(kDone, b));
SMoGeneral::gAddress.d.addr++;
if (!numBytes)
break;
HVPPSetDataMode(OUTPUT);
if (SMoGeneral::gAddress.c[0] == 0) {
if (SMoGeneral::gAddress.c[1] == 0)
HVPPControls(HVPPControlPattern(kLoadAddr, kExtByte), ++SMoGeneral::gAddress.d.extL);
HVPPControls(HVPPControlPattern(kLoadAddr, kHighByte), SMoGeneral::gAddress.c[1]);
}
HVPPControls(HVPPControlPattern(kLoadAddr, kLowByte), SMoGeneral::gAddress.c[0]);
HVPPSetDataMode(INPUT);
}
} while (numBytes);
*dataOut++ = STATUS_CMD_OK;
SMoCommand::SendResponse(STATUS_CMD_OK, dataOut - &SMoCommand::gBody[0]);
HVPPSetDataMode(OUTPUT);
}
void
SMoHVPP::ProgramFlash()
{
ProgramMemory(true);
}
void
SMoHVPP::ReadFlash()
{
ReadMemory(true);
}
void
SMoHVPP::ProgramEEPROM()
{
ProgramMemory(false);
}
void
SMoHVPP::ReadEEPROM()
{
ReadMemory(false);
}
static void
ProgramFuseLock(uint8_t command, uint8_t byteSel)
{
const uint8_t value = SMoCommand::gBody[2];
const uint8_t pulseWidth = SMoCommand::gBody[3];
const uint8_t pollTimeout = SMoCommand::gBody[4];
HVPPControls(HVPPControlPattern(kLoadCommand, kLowByte), command);
HVPPControls(HVPPControlPattern(kLoadData, kLowByte), value);
HVPPControls(HVPPControlPattern(kCommitData, byteSel));
delay(pulseWidth);
HVPPControls(HVPPControlPattern(kDone, byteSel));
// AT90S1200 and AT90S4414/8515 don't generate any activity on RDY/BSY pin.
// So just ignore the return value even if timeout occurs.
HVPPPollWait(pollTimeout);
SMoCommand::SendResponse();
}
static void
ReadFuseLock(uint8_t byteSel)
{
uint8_t * dataOut = &SMoCommand::gBody[2];
HVPPControls(HVPPControlPattern(kLoadCommand, kLowByte), (uint8_t)HVPP_ReadFuseLock);
HVPPSetDataMode(INPUT);
HVPPControls(HVPPControlPattern(kEnableRead, byteSel), (uint8_t *)dataOut);
HVPPControls(HVPPControlPattern(kDone, byteSel));
SMoCommand::SendResponse(STATUS_CMD_OK, 3);
HVPPSetDataMode(OUTPUT);
}
void
SMoHVPP::ProgramFuse()
{
const uint8_t byteSel = SMoCommand::gBody[1];
ProgramFuseLock(HVPP_WriteFuseBits, byteSel);
}
void
SMoHVPP::ReadFuse()
{
const uint8_t byteSel = SMoCommand::gBody[1];
ReadFuseLock(byteSel == kHighByte ? kExt2Byte : byteSel);
}
void
SMoHVPP::ProgramLock()
{
ProgramFuseLock(HVPP_WriteLockBits, kLowByte);
}
void
SMoHVPP::ReadLock()
{
ReadFuseLock(kHighByte);
}
static void
ReadSignatureCal(uint8_t byteSel, uint8_t addr)
{
uint8_t * dataOut = &SMoCommand::gBody[2];
HVPPControls(HVPPControlPattern(kLoadCommand, kLowByte), (uint8_t)HVPP_ReadSignature);
HVPPControls(HVPPControlPattern(kLoadAddr, kLowByte), addr);
HVPPSetDataMode(INPUT);
HVPPControls(HVPPControlPattern(kEnableRead, byteSel), (uint8_t *)dataOut);
HVPPControlPattern(kDone, byteSel);
SMoCommand::SendResponse(STATUS_CMD_OK, 3);
HVPPSetDataMode(OUTPUT);
}
void
SMoHVPP::ReadSignature()
{
const uint8_t addr = SMoCommand::gBody[1];
ReadSignatureCal(kLowByte, addr);
}
void
SMoHVPP::ReadOscCal()
{
ReadSignatureCal(kHighByte, 0x00);
}
//
// LICENSE
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//