What library to include for command "readfuse"

I made a little High Voltage Programmer to fix my broken ATTiny85s. I copied a sketch but a few commands are not declared, among them readfuse and writefuse. What library should be included?

Code

    Processing megaatmega2560 (platform: atmelavr; board: megaatmega2560; framework: arduino)
---------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/megaatmega2560.html
PLATFORM: Atmel AVR (3.3.0) > Arduino Mega or Mega 2560 ATmega2560 (Mega 2560)
HARDWARE: ATMEGA2560 16MHz, 8KB RAM, 248KB Flash
DEBUG: Current (avr-stub) On-board (avr-stub, simavr)
PACKAGES:
 - framework-arduino-avr 5.1.0
 - toolchain-atmelavr 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 5 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\megaatmega2560\src\main.cpp.o
Compiling .pio\build\megaatmega2560\FrameworkArduino\HardwareSerial3.cpp.o
Compiling .pio\build\megaatmega2560\FrameworkArduino\IPAddress.cpp.o
Compiling .pio\build\megaatmega2560\FrameworkArduino\PluggableUSB.cpp.o
src\main.cpp: In function 'void loop()':
src\main.cpp:57:21: error: 'readSignature' was not declared in this scope
  unsigned int sig = readSignature();
                     ^~~~~~~~~~~~~
src\main.cpp:62:2: error: 'readFuses' was not declared in this scope
  readFuses();
  ^~~~~~~~~
src\main.cpp:67:2: error: 'writeFuse' was not declared in this scope
  writeFuse(LFUSE, 0x6A);
  ^~~~~~~~~
src\main.cpp:82:2: error: 'writeFuse' was not declared in this scope
  writeFuse(LFUSE, 0x62);
  ^~~~~~~~~
*** [.pio\build\megaatmega2560\src\main.cpp.o] Error 1
========================================= [FAILED] Took 4.62 seconds =========================================
The terminal process "C:\Users\hans\.platformio\penv\Scripts\platformio.exe 'run'" terminated with exit code: 1.

PlatformIO.ini

    [env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino

.

You mean this one? That’s an .ino sketch so conversion is necessary if you store the code in a main.ino file. Pre-declare the used functions after including the Arduino.h header with

void writeFuse (unsigned int fuse, byte val);
void readFuses ();

That is another version. The one I used was this one. When I look in the project folders I can’t see any .ino file. How can you tell it’s a .ino sketch?

The same applies here with the function predeclarations though, with exactly the same code.

I can tell the code is supposed to be an .ino file because it’s wildly invalid C++ code due to the missing function declarations before the call of the function and no #include <Arduino.h> at the top, since the Arduino IDE allows exactly that (by means of preprocessing the .ino file into a valid .cpp file).

OK, the sketch runs but in the code there is a test for an incoming character, " if (Serial.available() > 0)" and in Serial Monitor how do I enter a character? I could remove the line but I suppose it’s there for a reason.

    // AVR High-voltage Serial Fuse Reprogrammer
// Adapted from code and design by Paul Willoughby 03/20/2010
// http://www.rickety.us/2010/03/arduino-avr-high-voltage-serial-programmer/
// Fuse Calc:
// http://www.engbedded.com/fusecalc/

#include <Arduino.h>

#define RST 13 // Output to level shifter for !RESET from transistor
#define SCI 12 // Target Clock Input
#define SDO 11 // Target Data Output
#define SII 10 // Target Instruction Input
#define SDI 9  // Target Data Input
#define VCC 8  // Target VCC

#define HFUSE 0x747C
#define LFUSE 0x646C
#define EFUSE 0x666E

// Define ATTiny series signatures
#define ATTINY13 0x9007 // L: 0x6A, H: 0xFF 8 pin
#define ATTINY24 0x910B // L: 0x62, H: 0xDF, E: 0xFF 14 pin
#define ATTINY25 0x9108 // L: 0x62, H: 0xDF, E: 0xFF 8 pin
#define ATTINY44 0x9207 // L: 0x62, H: 0xDF, E: 0xFFF 14 pin
#define ATTINY45 0x9206 // L: 0x62, H: 0xDF, E: 0xFF 8 pin
#define ATTINY84 0x930C // L: 0x62, H: 0xDF, E: 0xFFF 14 pin
#define ATTINY85 0x930B // L: 0x62, H: 0xDF, E: 0xFF 8 pin

byte shiftOut(byte val1, byte val2)
{
    int inBits = 0;
    //Wait until SDO goes high
    while (!digitalRead(SDO))
        ;
    unsigned int dout = (unsigned int)val1 << 2;
    unsigned int iout = (unsigned int)val2 << 2;
    for (int ii = 10; ii >= 0; ii--)
    {
        digitalWrite(SDI, !!(dout & (1 << ii)));
        digitalWrite(SII, !!(iout & (1 << ii)));
        inBits <<= 1;
        inBits |= digitalRead(SDO);
        digitalWrite(SCI, HIGH);
        digitalWrite(SCI, LOW);
    }
    return inBits >> 2;
}

void readFuses()
{

    Serial.println("Reading fuse settings from connected ATtiny.......");

    byte val;
    shiftOut(0x04, 0x4C); // LFuse
    shiftOut(0x00, 0x68);
    val = shiftOut(0x00, 0x6C);
    Serial.print("LFuse: ");
    Serial.print(val, HEX);
    shiftOut(0x04, 0x4C); // HFuse
    shiftOut(0x00, 0x7A);
    val = shiftOut(0x00, 0x7E);
    Serial.print(", HFuse: ");
    Serial.print(val, HEX);
    shiftOut(0x04, 0x4C); // EFuse
    shiftOut(0x00, 0x6A);
    val = shiftOut(0x00, 0x6E);
    Serial.print(", EFuse: ");
    Serial.println(val, HEX);
    Serial.println("Reading complete..");
}

void writeFuse(unsigned int fuse, byte val)
{

    Serial.println("Writing correct fuse settings to ATtiny.......");

    shiftOut(0x40, 0x4C);
    shiftOut(val, 0x2C);
    shiftOut(0x00, (byte)(fuse >> 8));
    shiftOut(0x00, (byte)fuse);

    Serial.println("Writing complete..");
}



unsigned int readSignature()
{
    unsigned int sig = 0;
    byte val;
    for (int ii = 1; ii < 3; ii++)
    {
        shiftOut(0x08, 0x4C);
        shiftOut(ii, 0x0C);
        shiftOut(0x00, 0x68);
        val = shiftOut(0x00, 0x6C);
        sig = (sig << 8) + val;
    }
    return sig;
}


void setup()
{
    pinMode(VCC, OUTPUT);
    pinMode(RST, OUTPUT);
    pinMode(SDI, OUTPUT);
    pinMode(SII, OUTPUT);
    pinMode(SCI, OUTPUT);
    pinMode(SDO, OUTPUT);    // Configured as input when in programming mode
    digitalWrite(RST, HIGH); // Level shifter is inverting, this shuts off 12V
    Serial.begin(9600);
    Serial.println("Code is modified by Rik. Visit riktronics.wordpress.com and electronics-lab.com for more projects");
    Serial.println("-------------------------------------------------------------------------------------------------");
    Serial.println("Enter any character to start process..");
}

void loop()
{
    if (Serial.available() > 0)
    {
        Serial.read();
        pinMode(SDO, OUTPUT); // Set SDO to output
        digitalWrite(SDI, LOW);
        digitalWrite(SII, LOW);
        digitalWrite(SDO, LOW);
        digitalWrite(RST, HIGH); // 12v Off
        digitalWrite(VCC, HIGH); // Vcc On
        delayMicroseconds(20);
        digitalWrite(RST, LOW); // 12v On
        delayMicroseconds(10);
        pinMode(SDO, INPUT); // Set SDO to input
        delayMicroseconds(300);
        unsigned int sig = readSignature();
        Serial.println("Reading signature from connected ATtiny......");
        Serial.println("Reading complete..");
        Serial.print("Signature is: ");
        Serial.println(sig, HEX);
        readFuses();
        if (sig == ATTINY13)
        {

            Serial.println("The ATtiny is detected as ATtiny13/ATtiny13A..");
            Serial.print("LFUSE: ");
            writeFuse(LFUSE, 0x6A);
            Serial.print("HFUSE: ");
            writeFuse(HFUSE, 0xFF);
            Serial.println("");
        }
        else if (sig == ATTINY24 || sig == ATTINY44 || sig == ATTINY84 ||
                 sig == ATTINY25 || sig == ATTINY45 || sig == ATTINY85)
        {

            Serial.println("The ATtiny is detected as ");
            if (sig == ATTINY24)
                Serial.println("ATTINY24..");
            else if (sig == ATTINY44)
                Serial.println("ATTINY44..");
            else if (sig == ATTINY84)
                Serial.println("ATTINY84..");
            else if (sig == ATTINY25)
                Serial.println("ATTINY25..");
            else if (sig == ATTINY45)
                Serial.println("ATTINY45..");
            else if (sig == ATTINY85)
                Serial.println("ATTINY85..");

            writeFuse(LFUSE, 0x62);
            writeFuse(HFUSE, 0xDF);
            writeFuse(EFUSE, 0xFF);
        }

        Serial.println("Fuses will be read again to check if it's changed successfully..");
        readFuses();
        digitalWrite(SCI, LOW);
        digitalWrite(VCC, LOW);  // Vcc Off
        digitalWrite(RST, HIGH); // 12v Off

        Serial.println("");
        Serial.println("");
        Serial.println("");
        Serial.println("");
    }
}

Do you see this output on the serial monitor?

When the serial monitor is open and you see that text, just press any character key. miniterm.py by default will not echo back what you’ve sent, so you won’t see your own key, but you should see the next output then.

If you want the local echo you can set monitor_flags = --echo per docs and docs.

Yes maxgerhardt, that worked. I tested with my newly made HV Programmer and all three ATTiny85 were OK. At the beginning of the Serial output there were two full lines of garbled characters. I’ve seen them when there is a mismatch of baud rate. I’ve set COM5 baud rate at 19200 both in Device Manager and in platformio.ini so what else could it be?

Serial Output

    --- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at http://bit.ly/pio-monitor-filters
--- Miniterm on COM5  19200,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
��␀␀�␀���␀��␀␀�␀��␀�␀��␀����␀�␀␀�␀␀�␀�␀���␀�␀␀�␀�␀���␀�␀␀��␀��␀�␀����␀�␀␀�␀␀␀�����␀␀�␀�␀���␀�␀␀�␀��␀␀␀�␀␀�����␀���␀��␀��␀�␀�␀␀�␀��␀␀�␀���␀��␀␀�␀��␀�␀��␀����␀�␀␀�␀␀�␀�␀���␀�␀␀�␀�␀���␀�␀␀��␀��␀�␀����␀�␀␀�␀␀␀�����␀␀�␀�␀���␀�␀␀�␀��␀␀␀�␀␀�����␀���␀��␀��␀�␀�␀␀�␀��␀␀�␀Code is modified by Rik. Visit riktronics.wordpress.com and electronics-lab.com for more projects
-------------------------------------------------------------------------------------------------
Enter any character to start process..
cReading signature from connected ATtiny......
Reading complete..
Signature is: 930B
Reading fuse settings from connected ATtiny.......
LFuse: 62, HFuse: DF, EFuse: FF
Reading complete..
The ATtiny is detected as 
ATTINY85..
Writing correct fuse settings to ATtiny.......
Writing complete..
Writing correct fuse settings to ATtiny.......
Writing complete..
Writing correct fuse settings to ATtiny.......
Writing complete..
Fuses will be read again to check if it's changed successfully..
Reading fuse settings from connected ATtiny.......
LFuse: 62, HFuse: DF, EFuse: FF
Reading complete..

Why 19200? The sketch uses

No, I changed it to 19200 in the source. Does it matter what speed it is?

It has to match the monitor_speed in the platformio.ini so that the COM adapter is set to receive at the correct baud rate (this also ignores the standard settings in the device manager).