// Simple DigiSpark (ATTing85) fuse setting for P5 (chip pin 1) to either Reset or I/O
// Condensed into small hand-held stand-alone device with OLED display.   Phil_G 16/02/2019
// Initially displays current DigiSpark fuses. If power held on, hfuse is set to DF, then 5F.
// Remove power when desired setting is shown. DF means P5 is RST and DigiSpark can be programmed, 5F means P5 is I/O
//
// Based on sketch originally created by Paul Willoughby 03/20/2010
// www.rickety.us slash 2010/03/arduino-avr-high-voltage-serial-programmer/
// Inspired by Jeff Keyzer mightyohm.com
// Serial Programming routines from ATtiny25/45/85 datasheet

#include <U8glib.h>
#define logo_width 16  // (in bytes)
#define logo_height 64 // in bits :-)
static unsigned char phils_logo[]  = 
{
0x0,0x0,0x0,0x0,0x0,0x0,0x3,0xff,0xff,0xc0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1f,0xff,0xff,0xf8,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0xfe,0x0,0x0,0x7f,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x3,0xe0,0x0,0x0,0x7,0xc0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xe,0x0,
0x0,0x0,0x0,0x78,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1c,0x0,0x0,0x0,0x0,0x3c,0x0,0x0,0x0,0x0,0x0,0x0,0x1f,0xc0,0x0,0x0,0x38,0x0,0x0,0x0,0x0,
0x1e,0x0,0x0,0x7,0xf0,0x0,0x0,0x10,0x0,0x0,0x0,0x70,0x0,0x0,0x0,0x0,0x6,0x0,0x0,0x4,0x18,0x0,0x0,0x10,0x0,0x0,0x0,0xc0,0x0,0x0,0x0,0x0,0x3,0x80,0x0,
0x4,0x8,0x0,0x0,0x10,0x0,0x0,0x1,0x80,0x0,0x0,0x0,0x0,0x1,0x80,0x0,0x4,0x8,0x0,0x0,0x10,0x0,0x0,0x1,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0x3,0x80,0x78,0x1,0xff,0xff,0x80,0x1f,0x0,0xe0,0x2,0x4,0x0,0x0,
0x10,0x0,0x1,0xc1,0xe0,0x0,0x7f,0xfe,0x0,0x7,0x81,0xc0,0x3,0xc,0x0,0x0,0x1f,0xe0,0x0,0xc1,0xc3,0x80,0x7,0xe0,0x7,0x1,0xc1,0x80,0x0,0xf0,0x0,0x0,0x0,
0x0,0x0,0xc3,0x82,0x40,0x0,0x0,0x8,0x80,0xc3,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xc6,0x2,0x40,0x0,0x0,0x8,0x0,0x73,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xee,
0x3,0x80,0x0,0x0,0x9,0xc0,0x3b,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x7c,0x2,0x0,0x0,0x0,0x8,0x80,0x1f,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x30,0x2,0x0,0x0,0x0,
0x7,0x0,0xc,0x0,0x0,0x0,0x0
};

U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI

// Desired fuse configuration
#define  HFUSE_RST  0xDF   // Defaults for ATtiny25/45/85 RST enabled.  Set to 5F for GPIO5
#define  HFUSE_P5   0x5F   // Defaults for ATtiny25/45/85 RST enabled.  Set to 5F for GPIO5
#define  LFUSE  0xF1       // 62 for Attiny85 default 1Mhz

#define  RST     3     // Output to level shifter for !RESET from transistor to Pin 1
#define  CLKOUT  9     // Connect to Serial Clock Input (SCI) Pin 2
#define  DATAIN  10    // Connect to Serial Data Output (SDO) Pin 7
#define  INSTOUT 11    // Connect to Serial Instruction Input (SII) Pin 6
#define  DATAOUT  12   // Connect to Serial Data Input (SDI) Pin 5
#define  VCC      6    // Connect to VCC Pin 8

int inData_l = 0;         // low fuse incoming serial byte AVR
int inData_h = 0;         // high fuse incoming serial byte AVR
int inData_e = 0;         // extended fuse incoming serial byte AVR

int targetValue = HFUSE_RST;
char stringbuff[50] = "";

void setup() {
  // Display something to prove that the OLED is working
  u8g.firstPage();
  do {
  u8g.setColorIndex(1);
  u8g.drawBitmap( 0, 0, logo_width, logo_height, phils_logo);  
  } while ( u8g.nextPage() );
    
  // Set up control lines for HV parallel programming
  pinMode(VCC, OUTPUT);
  pinMode(RST, OUTPUT);
  pinMode(DATAOUT, OUTPUT);
  pinMode(INSTOUT, OUTPUT);
  pinMode(CLKOUT, OUTPUT);
  pinMode(DATAIN, OUTPUT);  // configured as input when in programming mode

  // Initialize output pins as needed
  digitalWrite(RST, HIGH);  // Level shifter is inverting, this shuts off 12V
}

void draw(void) {
  // graphic commands to redraw the complete screen should be placed here
  u8g.setFont(u8g_font_6x10);
  u8g.drawStr( 0, 9,  "Present fuses:");
  u8g.drawStr( 0, 21,  stringbuff);
  u8g.drawStr( 0, 33,  "DF=RST, 5F=P5 as I/O");
  u8g.drawStr( 0, 45,  "Hold to flip H:DF/5F");
  u8g.drawStr( 0, 57,  "release when correct.");
}

void loop() {

  // Initialize pins to enter programming mode
  pinMode(DATAIN, OUTPUT);  //Temporary
  digitalWrite(DATAOUT, LOW);
  digitalWrite(INSTOUT, LOW);
  digitalWrite(DATAIN, LOW);
  digitalWrite(RST, HIGH);  // Level shifter is inverting, this shuts off 12V
  delay(2000);
  // Enter High-voltage Serial programming mode
  vcc_on();
  readFuses();
  delay(1000);
  vcc_off();
  delay(9000);

  vcc_on();
  writefuse_df(); // DF is reset
  readFuses();
  delay(1000);
  vcc_off();
  delay(9000);

  vcc_on();
  writefuse_5f(); // 5F is P5
  readFuses();
  delay(1000);
  vcc_off();

  while (1);
}

int shiftOut2(uint8_t dataPin, uint8_t dataPin1, uint8_t clockPin, uint8_t bitOrder, byte val, byte val1) {
  int i;
  int inBits = 0;
  //Wait until DATAIN goes high
  while (!digitalRead(DATAIN)) {
    // Nothing to do here
  }

  //Start bit
  digitalWrite(DATAOUT, LOW);
  digitalWrite(INSTOUT, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);

  for (i = 0; i < 8; i++) {

    if (bitOrder == LSBFIRST) {
      digitalWrite(dataPin, !!(val & (1 << i)));
      digitalWrite(dataPin1, !!(val1 & (1 << i)));
    }
    else {
      digitalWrite(dataPin, !!(val & (1 << (7 - i))));
      digitalWrite(dataPin1, !!(val1 & (1 << (7 - i))));
    }
    inBits <<= 1;
    inBits |= digitalRead(DATAIN);
    digitalWrite(clockPin, HIGH);
    digitalWrite(clockPin, LOW);

  }

  //End bits
  digitalWrite(DATAOUT, LOW);
  digitalWrite(INSTOUT, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);

  return inBits;
}

// Returns the value of the HFUSE
int readFuses() {
  //Read lfuse
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x04, 0x4C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x68);
  inData_l = shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x6C);

  //Read hfuse
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x04, 0x4C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x7A);
  inData_h = shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x7E);
  int hFuse = inData_h;

  //Read efuse
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x04, 0x4C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x6A);
  inData_e = shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x6E);

  sprintf(stringbuff, "L:%02X  H:%02X  E:%02X", inData_l, inData_h, inData_e);
  
  u8g.firstPage();
  do {
    draw();
  } while ( u8g.nextPage() );
}

void writefuse_df() {
  //Write hfuse to 0xDF (to allow RST on pin 1)
  delay(1000);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x40, 0x4C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, HFUSE_RST, 0x2C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x74);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x7C);

  //Write lfuse
  delay(1000);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x40, 0x4C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, LFUSE, 0x2C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x64);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x6C);
}

void writefuse_5f() {
  //Write hfuse to 0x5F (to allow pin 1 to be P5 I/O)
  delay(1000);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x40, 0x4C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, HFUSE_P5, 0x2C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x74);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x7C);

  //Write lfuse
  delay(1000);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x40, 0x4C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, LFUSE, 0x2C);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x64);
  shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x6C);
}

void vcc_on() {
  // Enter High-voltage Serial programming mode
  digitalWrite(VCC, HIGH);  // Apply VCC to start programming process
  delayMicroseconds(70);    // originally 20
  digitalWrite(RST, LOW);   //Turn on 12v
  delayMicroseconds(10);
  pinMode(DATAIN, INPUT);   //Release DATAIN
  delayMicroseconds(300);
}

void vcc_off() {
  digitalWrite(CLKOUT, LOW);
  digitalWrite(VCC, LOW);
  digitalWrite(RST, HIGH);   //Turn off 12v
}
