Thought I would post back here with the results of my experimentation with Peter Fleury's STK500v2-compatible bootloader. Not really on-topic any more, as the subject matter is no longer Optiboot, but I still thought it may be informative.
There are a number of modifications I ended up making to the bootloader code:
- Added support for LIN-based UARTs. It wasn't quite as straight-forward as I had hoped, and involved re-jigging of some of the existing code that handles other types of UARTs, but the changes are still quite straightforward.
- The LED indicator stuff was hard-coded to active-low, so I added an option to make the polarity configurable (I needed active-high).
- I had to change the device signature string from "AVRISP_2" to "STK500_2".
One thing I realised while changing the UART configuration code was that because my device is running at only 8 MHz, I can't get a proper 115.2Kbaud out of it with the common 8/16 bit-sampling timing that is fixed (toggle-able by the 'U2X' flag) in other UARTs. However, because the ATmegaXXM1 series actually has a configurable bit timing (anywhere from 8 to 63), I
can if I set it to an uncommon value. So rather than complicate the code, I just for now hard-coded it to 23 (and prescaler of 2), which gets me within 1% of 115.2K. In the long run I'll plan to switch to a 12 MHz crystal on this board, though, so I can get bang on 115.2K at 8/16 timing.
So, I now have a bootloader that satisfies my criteria of working with both avrdude
and Atmel Studio 7!
Incidentally, that is the reason why I had to change the signature string - AS7 didn't handle it being "AVRISP_2", for whatever reason. But avrdude doesn't seem to care either way when telling it you have an 'stk500v2' programmer.
Also, one odd thing I have noticed - that may or may not be of concern - is the behaviour when reading flash memory of avrdude versus AS7. I noticed that the size of the read data from avrdude was
not the full 32KB.
Comparing in a hex editor, it appears as though when reading with avrdude, it stops at the end of the bootloader, rather than at the end of flash. The discrepancy in size comes about because the bootloader does not occupy the full 1024 bytes (only 940). The un-read bytes are just 0xFF emptiness, so don't
really matter at the end of the day. But I've no idea what's causing this. From reading the code and the protocol documentation, it appears the programming application specifies the number of bytes to read. So why does avrdude specify a reduced value?
As the bootloader is GPL, I'll probably put my changes on GitHub at some point.