   if (hbval < 32) hbdelta = -hbdelta;
   hbval += hbdelta;
   analogWrite(LED_HB, hbval);
+}
+
+static bool rst_active_high;
+
+void reset_target(bool reset)
+{
+  digitalWrite(RESET, ((reset && rst_active_high) || (!reset && !rst_active_high)) ? HIGH : LOW);
 }
 
 void loop(void) {


void end_pmode() {
  SPI.end();
  digitalWrite(RESET, HIGH);
  pinMode(RESET, INPUT);
  pmode = 0;
}

                    + buff[18] * 0x00000100
                    + buff[19];

  // avr devices have active low reset, at89sx are active high
  rst_active_high = (param.devicecode >= 0xe0);
}

void start_pmode() {
  SPI.begin();
  // SPI.begin() has configured SS as output,
  // so SPI master mode is selected.
  // We have defined RESET as pin 10,
  // which for many arduino's is not the SS pin.
  // So we have to configure RESET as output here,
  // (reset_target() first sets the level correct)
  reset_target(false);
  pinMode(RESET, OUTPUT);

  digitalWrite(SCK, LOW);
  delay(20);
  reset_target(true);
  delay(50);
  spi_transaction(0xAC, 0x53, 0x00, 0x00);
  pmode = 1;
