@@ -169,7 +169,6 @@ static BitBangedSPI SPI;
169169void setup () {
170170 SERIAL.begin (19200 );
171171
172-
173172 pinMode (LED_PMODE, OUTPUT);
174173 pulse (LED_PMODE, 2 );
175174 pinMode (LED_ERR, OUTPUT);
@@ -348,21 +347,33 @@ void set_parameters() {
348347}
349348
350349void start_pmode () {
351- SPI.begin ();
352- SPI.beginTransaction (SPISettings (SPI_CLOCK, MSBFIRST, SPI_MODE0));
353- // SPI.begin() has configured SS as output,
350+
351+ // Reset target before driving SCK or MOSI
352+
353+ // SPI.begin() will configure SS as output,
354354 // so SPI master mode is selected.
355355 // We have defined RESET as pin 10,
356356 // which for many arduino's is not the SS pin.
357357 // So we have to configure RESET as output here,
358- // (reset_target() first sets the level correct)
359- reset_target (false );
358+ // (reset_target() first sets the correct level )
359+ reset_target (true );
360360 pinMode (RESET, OUTPUT);
361+ SPI.begin ();
362+ SPI.beginTransaction (SPISettings (SPI_CLOCK, MSBFIRST, SPI_MODE0));
361363
364+ // See avr datasheets, chapter "SERIAL_PRG Programming Algorithm":
365+
366+ // Pulse RESET after SCK is low:
362367 digitalWrite (SCK, LOW);
363- delay (20 );
368+ delay (20 ); // discharge SCK, value arbitrally chosen
369+ reset_target (false );
370+ // Pulse must be minimum 2 target CPU clock cycles
371+ // so 100 usec is ok for CPU speeds above 20KHz
372+ delayMicroseconds (100 );
364373 reset_target (true );
365- delay (50 );
374+
375+ // Send the enable programming command:
376+ delay (50 ); // datasheet: must be > 20 msec
366377 spi_transaction (0xAC , 0x53 , 0x00 , 0x00 );
367378 pmode = 1 ;
368379}
0 commit comments