Sunday, June 23, 2013

Announcing ScratchMonkey 1.0

Among the sketches bundled with the Arduino IDE is  ArduinoISP, which only becomes relevant once you buy your second Arduino or other AVR processor. ArduinoISP turns an Arduino into a programmer, communicating with avrdude on a PC through the STK500 protocol, and with a  target AVR processor through an SPI based protocol (known as In-System Programming a.k.a ISP).

Earlier this year, I decided to expand on this idea, and ended up writing ScratchMonkey, a more versatile programmer sketch emulating the STK500v2 protocol.

A Better ISP Programmer

ArduinoISP is perfectly well suited for programming other Arduinos, or many AVR processors with factory fuse settings. However, the version bundled with the Arduino IDE has some limitations (some of which are addressed in forks of the standard version, to be fair):
  • Communication speed with the computer is limited to 19200 baud for protocol compatibility reasons. ScratchMonkey is emulating a newer protocol and thus runs at 115000 baud.
  • The protocol used limits flash programming to 16 bit addresses, which prevents programming the ATmega2560 used in Arduino Mega 2560s. ScratchMonkey supports the address extensions needed for larger flash memories.
  • If the fuse settings of the target processor specify an external clock source, it is necessary to supply one i.e. by installing a crystal on the bread board (this is not a problem when reprogramming Arduinos, as the board already contains a clock source). ScratchMonkey provides a clock source by generating a PWM wave on one of the timer output pins.
  • If the fuse settings specify a very slow internal clock source (e.g. the 128kHz internal source available on some processors), or a slow external source is installed on an AVR board (e.g. a 32.768kHz watch crystal), hardware SPI is too fast to communicate with the target. ScratchMonkey automatically falls back to software emulated SPI, which can be made arbitrarily slow if necessary.

High Voltage Programming

With some fuse settings, ISP ceases to be capable of reprogramming an AVR processor: The RSTDISBL fuse turns the RESET pin into a regular input/output pin (an attractive proposition, e.g., on the 8 pin ATtiny85), and the SPIEN fuse can disable the use of the SPI protocol. For such cases, AVR offers a bigger hammer allowing to reprogram the processor anyway: High voltage programming. 
High Voltage Programming (Artist’s Impression)
While the term may evoke images of Tesla or Frankenstein, the reality is considerably more prosaic, involving the application of a mere 12V signal to the RESET pin of the target processor.

Along with this signal, one of two programming protocols needs to be used: AVR processors with fewer than 20 pins use High Voltage Serial Programming (HVSP), a protocol using 4 signals, while processors with 20 pins or more use one of the High Voltage Parallel Programming (HVPP) protocols, of which there are two 15 signal variants for 20 pin processors, and a 17 signal variant for processors with more than 20 pins.

For some reason, most of the open source projects discussing high voltage programming limit themselves to reprogramming the fuses into a state where ISP can be used again. ScratchMonkey, on the other hand, supports full programming functionality for both HVSP and HVPP.

Supported Systems

Both an Arduino Uno and an Arduino Micro were tested as the programmer. Other Arduinos mostly should work as well, but have not been tested. I tested a wide variety of AVR processors as targets:

Untitled
Top row: ATtiny1634, ATtiny84, ATtiny85
Bottom row: ATtiny4313, ATtiny861, ATmega328, ATmega1284
Not shown: ATmega32u4, ATmega2560 

Availability

ScratchMonkey is available on GitHub. An extensive (arguably excessive) manual is also available there.

4 comments:

  1. I tried to usescratchmonkey on Leonardo as ISP programmer to burn bootloader into Atmega328P but did not success

    ReplyDelete
  2. Can you mail me the debug output you’re getting (With upload debug option turned on in Arduino IDE, or -v -v -v -v on the command line), or post the output here?

    ReplyDelete
  3. When I burn bootloader there is no error comment arise in Arduino IDE, even debug option turned on. I tried to recognise the target IC using avrdude and the result is

    C:\Users\PC>avrdude -P COM12 -b 19200 -c stk500v2 -p atmega328p -n
    avrdude: stk500_2_ReceiveMessage(): timeout
    avrdude: stk500_2_ReceiveMessage(): timeout
    avrdude: stk500_2_ReceiveMessage(): timeout
    avrdude: stk500_2_ReceiveMessage(): timeout
    ^C

    but when i use ArduinoISP sketch, avrdude succes to recognise as shows below

    C:\Users\PC>avrdude -P COM12 -b 19200 -c arduino -p atmega328p -n

    avrdude: AVR device initialized and ready to accept instructions

    Reading | ################################################## | 100% 0.02s

    avrdude: Device signature = 0x1e950f

    avrdude: safemode: Fuses OK

    avrdude done. Thank you.

    is the any suggestion to solve?

    ReplyDelete
  4. ScratchMonkey operates at the standard baud rate for stk500v2, which is 115200 baud. Can you just omit the -b 19200 argument from the command line?

    As for the IDE, there are two debug options, one for compilation, one for upload. You need to turn the latter on.

    ReplyDelete