$Id: README.stk500,v 1.4 2006-12-12 18:23:00 vlahan Exp $

NOTE: Atmel has finally released the specs for the protocol for talking to the
stk500 over a serial line. Here's where to get the files:

    ftp://www.atmel.com/pub/atmel/avr061.zip
    http://www.atmel.com/atmel/acrobat/doc2525.pdf

STK500 Commands  **preliminary information**
---------------

Extracted by Jason Kyle 20010701
Updated 200201

The latest version of this document can be found at www.avr1.org.

Notes:
- Incomplete but sufficient to make progress on free tools.
- All commands are terminated by 0x20
- All successful responses are framed 0x14 data 0x10 (i.e. NULL response is 
  0x14 0x10)
- Commands invalid in current mode (i.e not in pgm mode, pgm page) give 
  0x14 0x13 response.


STK500 BOARD COMMANDS
---------------------

** ?STK500
Command		0x30 0x20
Response	0x14 0x10
Use		Establish STK500 presence and UART sync. Re-send n times until
		response.
Notes		STK500 won't respond to other commands unless this has been
		sent.

** BoardID
Command		0x31 0x20
Response	0x14 "AVR STK" 0x10
Use		Identify board.
Notes

** VTargetSet
Command		0x40 0x84 VTarget 0x20
Response	0x14 0x10
Use		Set VTarget voltage.
Notes		0x33 = 5.1V , 0x34 = 5.2V , 0x32 = 5.0V

** ARefSet
Command		0x40 0x85 ARef 0x20
Response	0x14 0x10
Use		Set ARef voltage.
Notes		Same count/V relationship as VTargetRead

** POscSet
Command		0x40 0x86 POsc 0x20
Response	0x14 0x10
Use		Set P parameter.
Notes

** NOscSet
Command		0x40 0x87 NOsc 0x20
Response	0x14 0x10
Use		Set N parameter.
Notes

** SDOscSet
Command		0x40 0x89 SDOsc 0x20
Response	0x14 0x10
Use		Set SD parameter.
Notes

** HWver
Command		0x41 0x80 0x20
Response	0x14 0x02 0x10
Use		Hardware version identification.
Notes

** SWmajor
Command		0x41 0x81 0x20
Response	0x14 0x01 0x10
Use		Software major version identification.
Notes

** SWminor
Command		0x41 0x82 0x20
Response	0x14 0x07 0x10
Use		Software minor version identification.
Notes

** VTargetRead
Command		0x41 0x84 0x20
Response	0x14 0x33 0x10
Use		Query present VTarget voltage.
Notes		0x33 = 5.1V , 0x34 = 5.2V , 0x32 = 5.0V

** ARefRead
Command		0x41 0x85 0x20
Response	0x14 0x33 0x10
Use		Query present ARef voltage.
Notes		Same count/V relationship as VTargetRead

** POscRead
Command		0x41 0x86 0x20
Response	0x14 0x01 0x10
Use		Query present P parameter.
Notes

** NOscRead
Command		0x41 0x87 0x20
Response	0x14 0x00 0x10
Use		Query present N parameter.
Notes

** SDOscRead
Command		0x41 0x89 0x20
Response	0x14 0x01 0x10
Use		Query present SD parameter.
Notes

** DeviceParam
Command		0x42 (20 device specific bytes) 0x20
Response	0x14 0x10
Use		Tell programmer how to program device, i.e data polling byte
		returns.
Notes		See table for more details

** MagicNumber
Command		0x45 0x03 0x00 0xD7 0xA0 0x20
Response	0x14 0x10
Use		Ask Atmel.  Sent before entering programming mode.
Notes		This may well be a code checksum, software version requested
		first.


STK500 PROGRAMMING COMMANDS
---------------------------

** EnterPgmMode
Command		0x50 0x20
Response	0x14 0x10
Use		Enter programming mode, nRESET = low.
Notes

** LeavePgmMode
Command		0x51 0x20
Response	0x14 0x10
Use		Leave programming mode, nRESET = high.
Notes

** EraseDevice
Command		0x52 0x20
Response	0x14 0x10
Use		Erase device.
Notes

** SetAddress
Command		0x55 AddrL AddrH 0x20
Response	0x14 0x10
Use		Set address for read/write operation
Notes		Word address FLASH, Byte address EEPROM

** UniversalCmd
Command		0x56 0xAC 0xFD 0x00 0xFF 0x20
Response	0x14 0x00 0x10
Use		Sending lock and fuse bits.
Notes		Could probably be used for byte by byte programming of devices.
		The 0x00 returned is read from byte 4 SPI return value, i.e. if
		read program memory SPI command sent then this is the byte
		returned.

** WriteMemory
Command		0x64 LenH LenL 0x46 DataBytes 0x20
Response	0x14 0x10
Use		Programming FLASH/EEPROM/PageBuffer
Notes		Change 0x46 ("F") to 0x45 ("E") for EEPROM. Length in bytes.
		Written in 0x0100 chunks for 8515


** ReadMemory
Command		0x74 LenH LenL 0x46 0x20
Response	0x14 DataBytes 0x10
Use		Reading FLASH/EEPROM
Notes		Change 0x46 ("F") to 0x45 ("E") for EEPROM. Length in bytes.
		Read in 0x0100 chunks for 8515.

** GetSignature
Command		0x75 0x20
Response	0x14 0x1E 0x93 0x01 0x10
Use		Requesting device signature bytes
Notes		Response shown for 8515

** ReadOscCal
Command		0x76 0x20
Response	0x14 0x42 0x10
Use		Read current oscillator calibration value
Notes		Varies between devices, 0x42 in an ATmega163L-4PI 0035



COMMAND SEQUENCES
-----------------

Startup:
?STK500 , BoardID , HWver , SWmajor , SWminor

ReadSignature:
?STK500 , DeviceParam , SWminor , SWmajor, MagicNumber , EnterPgmMode ,
GetSignature , LeavePgmMode

WriteLockBits:
?STK500 , DeviceParam , SWminor , SWmajor , MagicNumber , EnterPgmMode ,
UniversalCmd , LeavePgmMode

WriteMemory:
?STK500 , DeviceParam , SWminor , SWmajor , MagicNumber , EnterPgmMode ,
EraseDevice , SetAddress 0x0000 , PgmBlockMode n bytes , SetAddress 0x0000 ,
ReadBlockMode n bytes , LeavePgmMode

ReadMemory:
?STK500 , DeviceParam , SWminor , SWmajor , MagicNumber , EnterPgmMode ,
SetAddress 0x0000 , ReadBlockMode n bytes , LeavePgmMode

Normally memory is programmed or read in 0x0100 byte chunks, obviously if a
program doesn't align to an 0x0100 length then there will be a < 0x0100 chunk
at the end.


Writing mega163.  Set address to 0x0000, write 0x0100 bytes, set address to
0x0080, write 0x0100 bytes etc...  STK500 must auto detect end of page boundry
and write current page to address specified in last setaddress cmd.


Set device paramters and programming mode command for STK500

8515
42 60 00 00 01 01 00 01 01 7f 7f 80 7f 00 00 02 00 00 00 20 00 20

4414
42 50 00 00 01 01 00 01 01 7f 7f 80 7f 00 00 01 00 00 00 10 00 20

2313
42 40 00 00 01 01 00 01 01 7f 7f 80 7f 00 00 00 80 00 00 08 00 20

1200
42 33 00 00 01 01 00 01 01 ff ff 00 ff 00 00 00 40 00 00 04 00 20

2323
42 41 00 00 00 01 00 01 01 ff ff 00 ff 00 00 00 80 00 00 08 00 20

2343
42 43 00 00 00 01 00 01 01 ff ff 00 ff 00 00 00 80 00 00 08 00 20

2233
42 42 00 00 01 01 00 01 01 ff ff 00 ff 00 00 00 80 00 00 08 00 20

4433
42 51 00 00 01 01 00 01 01 ff ff 00 ff 00 00 01 00 00 00 10 00 20

4434
42 52 00 00 01 01 00 01 01 ff ff 00 ff 00 00 01 00 00 00 10 00 20

8535
42 61 00 00 01 01 00 01 01 ff ff 00 ff 00 00 02 00 00 00 20 00 20

tiny11
42 11 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 04 00 20

tiny12
42 12 00 00 00 01 01 01 01 ff ff ff ff 00 00 00 40 00 00 04 00 20

tiny15
42 13 00 00 00 01 01 01 01 ff ff ff ff 00 00 00 40 00 00 04 00 20

tiny22
42 20 00 00 00 01 00 01 01 ff ff 00 ff 00 00 00 80 00 00 08 00 20

tiny28
42 22 00 00 01 00 01 01 01 00 00 00 00 00 00 00 00 00 00 08 00 20

mega32
42 90 00 00 01 01 01 01 02 ff ff ff ff 00 80 04 00 00 00 80 00 20

mega161
42 80 00 00 01 01 01 01 01 ff ff ff ff 00 80 02 00 00 00 40 00 20

mega163
42 81 00 00 01 01 01 01 02 ff ff ff ff 00 80 02 00 00 00 40 00 20

mega103
42 b1 00 00 01 00 00 01 01 00 00 00 00 01 00 10 00 00 02 00 00 20

mega128
42 b2 00 00 01 01 01 01 03 ff ff ff ff 01 00 10 00 00 02 00 00 20
