USBasp & USBtinyISP very slow programming speed

Ok so here is additional information on USBasp.
So I decided to dig deeper and really see what is going on as to why the USBasp is not faster.
Is it the USBasp device, USBasp firmware, avrdude, the IDE or some combination?
I have some answers.

The the newer USBasp firmware (since 2009) supports setting the SCK clock speed over the USB.
There is a default clock rate built into the firmware.
It is 375 kHz.
The USBasp interface supports telling the device to automatically pick a clock.
The newer avrdude uses this when connecting to the USBasp device.
The USBasp firmware doesn't really auto detect the SCK rate, instead it just
sets it the default clock rate which again is 375 kHz.

The USBasp message interface supports setting higher clock rates over the USB interface.
It supports 750 kHz and 1.5 Mhz.

So now the trick is how to get the USBasp device to run the higher clock rates.
Before we go into that, I went and looked up just how fast is the fastest SCK can be clocked.
With clocks > 12Mhz which the 16Mhz AVR is, you can't clock it faster than 6 clocks which means
no faster than 2.666Mhz.
So great I thought I'd just whip up special version of USBasp firmware that defaults
to running the SCK clock at a frequency faster than the current default of 375Khz
and right up as close to 2.666 as I can get, assuming I should be able to get 2Mhz.
Well you can't do 2Mhz. The USBasp device uses AVR SPI hardware clocked from power of 2 divisors off
the main clock. The main clock on USBasp is 12Mhz so you can do /8 to get 1.5Mhz and then /4 to get 3Mhz
but nothing in between.

So now I thought about burning a new version of the firmware with a faster SCK at 1.5Mhz but
first starting looking at using -B options to set the clock faster, as it is a bit of pain to get
things setup to burn new firmware into the USBasp device I have.
While I was running avrdude from the commandline for my testing,
I was a bit confused at first since the -B option value is not passed directly to the USBasp
firmware. I was assuming -B10 or -B12 was passed in directly through the lowlevel USBasp messages.
It was a user error on my part. The -B option is actually a floating point value
that represents the bit clock frequency in microseconds. This is actually nice since it is consistent
on all devices regardless of how the low level messages are implemented.
To to get a frequency of 1.5Mhz on you use -B0.666
(which avrdude turns into a low level USBasp message clock value of 12)

Testing showed that the using -B0.666 is much faster than using the defaults.

I went looking to see how the IDE runs avrdude and see what -B options it is using.
Well it turns out that the IDE is not using a -B option.
However, after looking at the java code there is a way to trick it to allow setting a -B option.
What you need to do is go into your programmers.txt file and modify the entry for the USBasp programmer.
change this line:

usbasp.protocol=usbasp

to this:

usbasp.protocol=usbasp -B0.666

Alternatively you could create new programmers for fast/slow programming etc..:

usbaspSlow.name=USBaspSlow
usbaspSlow.communication=usb
usbaspSlow.protocol=usbasp -B10

usbaspFast.name=USBaspFast
usbaspFast.communication=usb
usbaspFast.protocol=usbasp -B0.666

I would have preferred to put the -B option on the .communications line
but the java code doesn't use it. It looks for "usb" on the line then hardcodes the option to "usb"
rather than just using the string.
But you can trick it by adding an additional option to the protocol field
as the java code just blindly uses whatever you put there "as is".

So by using these modified USBasp faster programmer types,
the USBasp SCK clock will be 8 times faster than the current default clock.
Before you jump up and down, that doesn't translate into an upload time that is 8 times shorter.
However it does drop a full 32k 25 second upload down to about 12 seconds.
While still not quite as fast as the Serial upload it is pretty close.
And when using the new avrdude burning a optiboot bootloader drops from
around 40 seconds down to about 0.5 seconds.

Just keep in mind that you have to have the newer USBasp firmware that
supports setting the clock over the USB message interface in order to do this.
And if you are building your own USBasp firmware image, I recommend going
in and changing the default clock rate to be 1.5Mhz instead of 375Khz.
It is a one line change in isp.c
Change this:

	if (option == USBASP_ISP_SCK_AUTO)
		option = USBASP_ISP_SCK_375;

to this:

	if (option == USBASP_ISP_SCK_AUTO)
		option = USBASP_ISP_SCK_1500;

If you make this firmware change, you won't need to modify your boards.txt file
to get the faster clock as the default SCK clock will be 1.5Mhz

--- bill