Using the internal oscillator with a bootloader

It seems all Arduinos use crystals or resonators, even the 8 MHz 3.3V varieties, mainly because the internal 8 MHz oscillator is only "calibrated" to within 10%, which doesn't work for UART communications used in bootloaders. I assume a programmer would still work, but bootloaders are easier to use.

The OSCCAL register allows for fine tuning the frequency, and I've come across several ways to fine tune the calibration, but they typically change OSCCAL after the sketch starts to run, and the bootloader knows nothing about such an adjustment. Then I found this project:

which uses a programmer, an RTC and a sketch to determine the proper OSCCAL value, then it embeds that value into the bootloader for burning to the chip. The bootloader is also modified so as to load the saved value into OSCAL immediately on powerup - before attempting any communications with the host. However, this project deals with the old bootloader, and all the software appears to be for Linux. Not wanting to reinvent the wheel, I wondered if a more up-to-date version of this for Optiboot, which also works in Windows, already exists. It looks like the latest Optiboot version has something like 38 bytes left. :slight_smile:

I also want to ask about another solution that actually could work for both internal and external clocks, and that is to bit-bang the serial communications, including a way to determine the bit period accurately no matter what the clock is. As used in another brand of processors' bootloader, the conversation begins with the host sending a 0X80 byte to the target. Counting the start bit, the target's RxD line goes low for exactly 8 bit periods, so the timer count for that 8-bit period can be right-shifted three times to get the time for one bit, and shifted again to get it for 1/2 bit, which is needed for receiving. It seems this would work at least at lower baud rates, including 9600. That process would eliminate the need to adjust OSCCAL for the bootloader, but that adjustment could be done later after the sketch starts up if a calibrated clock is needed.

It's also interesting to me that both Digispark and Adafruit have bootloaders for their Attiny85 boards even though the same 10% problem seems to exist for those parts. But then I remembered that those bootloaders aren't really dealing with UART communications, but instead are directly reading the USB data lines. I believe USB uses NRZI, which may be more forgiving of uncalibrated clocks. But they also use their own virtual COM port drivers, so there's no telling what protocol they're actually using. Anyway, for Arduinos I think we would need to use existing USB adapters and their drivers, which means UART communications for bootloaders.

Well, I would appreciate any thoughts on this, and links to any successful projects dealing with bootloaders when internal oscillators are used.

ShermanP:
the internal 8 MHz oscillator is only "calibrated" to within 10%, which doesn't work for UART communications used in bootloaders.

I have used the internal oscillator quite a bit and I think only once have I had a problem using the bootloader. It was using a suboptimal baud rate with a high error rate. Combine that with an internal oscillator that is off enough in the right direction and it could result in a failure to communicate.

That said, I do think what you're talking about is interesting, though I don't have any knowledge on the topic.