Minor bug in optiboot boot.c and question

I managed to get an Uno running with 10MHz system clock. Obviously it required a mod to the bootloader; in the process I found what I think is a minor bug in optiboot.c and have a question regarding it too. Here's the relevant piece of code:

#include "pin_defs.h"

... then, later in the file ...

/* Switch in soft UART for hard baud rates */
#if (F_CPU/BAUD_RATE) > 280 // > 57600 for 16MHz
#ifndef SOFT_UART
#define SOFT_UART
#endif
#endif

The bug is that this code occurs after the file pin_defs.h is included, but pin_defs.h needs to see this macro, so the include should be moved after SOFT_UART is defined.

The reason I found this is that with a 10MHz clock, the upload will not work at 115,200 baud and I had to lower it to 28,800 baud. That change triggered this bug and optiboot.c would not compile.

This leads to my question. In comments above the indicated code, it says that SOFT_UART is triggered for hard baud rates. The macro's condition is only triggered for slow baud rates. So, what's so hard about slow baud rates? I would have thought this would apply to higher baud rates where you have trouble exactly matching the desired rate with an integer division of the clock.

Anyway, for me it worked just fine to leave SOFT_UART off at 28,800 baud -- at 10MHz the divisor is 43 and it doesn't overflow the low byte and works just fine. So, just curious about the purpose of SOFT_UART in addition to reporting the bug.

I think I got the file name wrong in a couple spots...it is optiboot.c, not boot.c. Sorry for the confusion.

That issue no longer exists in the latest optiboot code: GitHub - Optiboot/optiboot: Small and Fast Bootloader for Arduino and other Atmel AVR chips but there hasn't been a production release in 4 years so I don't know how long it will take for the optiboot source used in the Arduino IDE bootloader files to be updated. You could try submitting an issue report or pull request to the Arduino IDE repository. I'm not sure if the developers will be receptive to changing it.

pert answered faster, while I was typing up this, but I'll post this anyway:

The Optiboot code which is in the IDE and still installed in new boards is stable and gets the desired results when used at the default Arduino baud rates for boards with the default crystals. The source code is included for reference, but the fact that it just plain works is probably why it has not been updated to the most recent version of Optiboot. This would follow the "don't fix it if it ain't broke" and "keep it simple silly" principles. So, I don't know if it's terribly important for the IDE to get up-to-date. The above sentences are my opinion and I can't substantiate it, but I just said it to give you a little bit of background. For new projects such as yours, I would recommend getting the latest Optiboot version source, and compile from that:

There has been quite a bit of revision. The IDE and Uno boards come with version 4.4 of Optiboot, but Optiboot is now on version 6.2. So it is possible the bug you are talking about is already fixed. If it is not, I suggest open a Github account and report any bugs there.

If you have just plain old question about what you see in Optiboot 6.2, post a thread in this website's Microcontrollers section, and people who know a lot about Optiboot or a lot about programming are likely to be able to answer. In your question, it would be a good idea to mention the specific version of Optiboot you're talking about, and give the link to it like I have above.

Question answered, thanks!

I did not realize that Optiboot was a separate project. Checked there and they have indeed fixed the bug and added improvements to boot (ha, ha, pun intended). There's even a comment in there about autodetect of SOFT_UART:

/* June 2011: /
/
4.4 WestfW: remove automatic soft_uart detect (didn't /
/
know what it was doing or why.)

Don't know that it really matters if the Arduino folks fix/change it; I'm happy with having found the Optiboot project -- they even have Atmel studio projects in there which makes it easier to build the bootloader on Windows. The 1.6 version of the Arduino IDE seems to have broken the ability to build Optiboot in the install directory, so the Atmel Studio projects are a nice plus.

Thanks again.

Also useful in the Optiboot project on github:

  1. quite a bit of documentation in the wiki.
  2. actively maintained "issue" list.

The Atmel Studio projects are quite new, BTW.