Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 678c79d

Browse files
Support automatically starting programming
This reads from the target's RESET pin to detect whether a target chip or board is present, and automatically starts programming when a chip is inserted. This requires a big pulldown resistor on the programmer side, so the RESET pin reads low when no chip is inserted. This pulldown must be big enough to not interfere with the target's pullup (so something like 1M). Because a pullup is required, this feature is disable by default. It can be enabled by changing the AUTOSTART define in the .ino file.
1 parent 498dcfb commit 678c79d

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Standalone-Arduino-AVR-ISP-programmer.ino

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ byte pageBuffer[128]; /* One page of flash */
4141
#define BUTTON A1
4242
#define PIEZOPIN A3
4343

44+
// Set to 1 to enable autostart. This automatically starts programming
45+
// when a chip is inserted. How this detection happens is determined by
46+
// the DETECT_* constants below. This is disabled by default, since the
47+
// default settings require adding a pulldown (see below).
48+
#define AUTOSTART 0
49+
50+
// Pin to use for detecting chip presence. This defaults to the
51+
// (target) RESET pin, which detects the (internal) reset pullup on the
52+
// target chip or board. For this to work, there should be a pulldown on
53+
// the programmer side (which is big enough to not interfere with the
54+
// target pullup). Alternatively, for self-powered boards, this could be
55+
// set to an I/O pin that is connected to the targets VCC (still
56+
// requires a pulldown on the programmer side).
57+
#define DETECT RESET
58+
59+
// The DETECT pin level that indicates a chip is present.
60+
#define DETECT_ACTIVE HIGH
61+
62+
// Debounce delay: The DETECT pin must be stable for this long before it
63+
// is seen as changed. This applies both after removing and after
64+
// re-inserting the chip.
65+
#define DETECT_DEBOUNCE_MS 500
66+
4467
void setup () {
4568
Serial.begin(57600); /* Initialize serial for status msgs */
4669
Serial.println("\nAdaBootLoader Bootstrap programmer (originally OptiLoader Bill Westfield (WestfW))");
@@ -66,9 +89,55 @@ void setup () {
6689

6790
}
6891

92+
#if AUTOSTART != 0
93+
bool detect_chip() {
94+
// The most recently read detection state (true is chip present, false is no
95+
// chip present). Default is to assume a chip is present, to prevent
96+
// autostarting on powerup, only after *inserting* a chip.
97+
static bool detected = true;
98+
// The most recent detection state that has been stable.
99+
static bool debounced = detected;
100+
// The timestamp of the most recent change, or 0 when the pin is
101+
// stable.
102+
static unsigned long last_change = 0;
103+
104+
// See if the pin changed
105+
unsigned long now = millis();
106+
bool prev_detected = detected;
107+
detected = (digitalRead(DETECT) == DETECT_ACTIVE);
108+
109+
if (detected != prev_detected)
110+
last_change = now;
111+
112+
if (detected != debounced && (now - last_change) > DETECT_DEBOUNCE_MS) {
113+
// If stable for long enough, update the debounced state and clear
114+
// the last_change value. Keep the previous value, to detect changes
115+
last_change = 0;
116+
debounced = detected;
117+
118+
// Detect when a chip is inserted
119+
if (debounced)
120+
return true;
121+
}
122+
123+
// No change, or chip removed
124+
return false;
125+
}
126+
#endif
127+
69128
void loop (void) {
129+
#if AUTOSTART != 0
130+
Serial.println("\nInsert next chip to autostart or type 'G' or hit BUTTON to force start");
131+
#else
70132
Serial.println("\nType 'G' or hit BUTTON for next chip");
133+
#endif
71134
while (1) {
135+
#if AUTOSTART != 0
136+
if (detect_chip()) {
137+
Serial.println("Chip detected, autostarting..");
138+
break;
139+
}
140+
#endif
72141
if ((! digitalRead(BUTTON)) || (Serial.read() == 'G'))
73142
break;
74143
}

0 commit comments

Comments
 (0)