Here's what I think happens and how long it takes (FIXME means I don't know). I'd like to end up with solid max times for everything if possible.
Power-on, input caps to the 328P begin charging and Vcc to the 328P rising
From the time it first comes to life, internal 328P hardware holds an
internal reset line low.
Vcc to 328P climbs past the power-on reset voltage threshold V_pot, which has
a maximum value of 1.6V (see the ATmega328P_datasheet_revision_DS40002061A.pdf
Table 29-11). For a 9V battery with a typical internal resistance of 2 ohms,
the time constant with the ~100 uF of input capacitance on the Arduino
is RC = (2 * 0.0001) = 0.0002 s. So this normally happens pretty fast,
but note that if additional loads are connected to the Arduino Vcc or Vin
things might take longer.
A reset timer on 328P begins running. When it expires the internal reset
line will be released. How long this timer will run depends on fuse settings.
For the default Arduino fuse settings the timer will run for about 65 ms.
For a 3.3V Arduino variant the typical time will be slightly longer (~69 ms).
See ATmega328P_datasheet_revision_DS40002061A.pdf Table 29-11 Chapter 9
for details.
The PD7 line of the ATMEGA16U2 chip on on the Arduino holds the external
reset line low past the point in time at which the internal reset is released,
which causes the EXTRF bit of the MCUSR register on 328P the 328P to end up
set (indicating an external reset). FIXME: does it actually work like this
(hold it low longer)? FIXME: How long exactly?
The external reset is released by the ATMEGA16U2
The bootloader on the 328P begins executing
The bootloader examines the MCUSR and finds that EXTRF is set, but WDRF
of MCUSR is not set. It therefore quickly flashes the on-board LED several
times and the attempts to download a new program over the serial line.
This process takes about 1.6 s total.
The download attempt terminates with a watchdog timer timeout and consequent
watchdog timer reset. The EXTRF bit of MCUSR remains set (it is sticky and
is only cleared by a power-on reset or explicit 0 write). Time: pretty quick
The bootloader starts executing (again). Time: pretty quick
The bootloader examines the MCUSR and finds that the EXTRF and WDRF bits
of the MCUSR are both set. It interprets this to mean that the bootloader
just executed and terminated due to a watchdog timeout, i.e. it decides that
an upload attempt has already been made. It therefore clears the WDRF flag
(since it thinks it probably set it itself) and jumps to the start of the
application flash instead of making an upload attempt. Time: pretty quick
All the setup code required to provide the C environment your main()
program requires runs. This includes for example copying global variable
intial values from flash to RAM, etc. Time: FIXME?
I mean an upper bound on how long each of these steps can take. I guess I'm particularly interested in pre-main() C setup time as that seems likely to be the third-biggest delay (after bootloader download attemp and power ramp time) both of which can be greatly reduced (by cutting RESET EN and by reprogramming SUT0 fuse for fast rising power).
It is my understanding that on power-up, Optiboot recognises that it is not an external reset and bypasses the code upload wait.
I've made a shield that acts as power on-off supervisor to a stock arduino, to let it run for years on battery in monitoring applications. It also supports wake on user signal (which can be powered from controller's low-Iq regulator) and then propagates that signal once the controlled Arduino is safely on. I'm interested in the maximum time until the controlled Arduino can see that propagated signal (including when RESET EN is cut and SUT0 fuse is reprogrammed to mean "fast rising power").
The optiboot logic is somewhat convoluted, it still skips the bootloader when EXTRF is set if the watchdog reset flag is also set. I believe this is because EXTRF is sticky across all but power-on resets and optiboot generally tries to preserve MCUSR bits. So it has this logic to determine that it was probably responsible for the last reset.
Unless you plan to use several different types of Arduino it will probably be sufficient to measure the startup time once or twice and add 50% for a safety margin.
Or have some code in setup() that causes an I/O pin to go HIGH (or to "blink") so that your other device could detect when the Arduino startup has completed?
Or you could program the Arduino using ICSP thus overwriting the bootloader. Then the startup will only depend on the processes within the Atmega chip.
You can always program your Arduino
Via ICSP , in which case the bootloader is over written by your program and startup would be quicker as there is then no “ bootloader”
The PD7 line of the ATMEGA16U2 chip on on the Arduino holds the external
reset line low past the point in time at which the internal reset is released,
which causes the EXTRF bit of the MCUSR register on 328P the 328P to end up
set (indicating an external reset). FIXME: does it actually work like this
(hold it low longer)? FIXME: How long exactly?
You are exhibiting a lot of faith that the 16u2 resets and begins acting "deliberately" before the 328p does.
The external reset is released by the ATMEGA16U2
I am pretty sure that the 328 RESET is only capacatively coupled to the 16u2, via the nominal "DTR" output (backward compatible with non-smart USB controllers.)
So this is less deterministic than you think. (let's see - ~8k pullup for the 10k external + 40k internal reset pullup, 0.1uF capacitor...)
The bootloader examines the MCUSR and finds that EXTRF is set, but WDRF
of MCUSR is not set. It therefore quickly flashes the on-board LED several
times and the attempts to download a new program over the serial line.
This process takes about 1.6 s total.
Sounds about right. It flashes the LED 3 times over a short period (0.6s?), and then enables a 1s watchdog timeout.
The download attempt terminates with a watchdog timer timeout and consequent
watchdog timer reset. The EXTRF bit of MCUSR remains set (it is sticky and
is only cleared by a power-on reset or explicit 0 write). Time: pretty quick
The bootloader starts executing (again). Time: pretty quick
The bootloader examines the MCUSR and finds that the EXTRF and WDRF bits
of the MCUSR are both set. It interprets this to mean that the bootloader
just executed and terminated due to a watchdog timeout, i.e. it decides that
an upload attempt has already been made. It therefore clears the WDRF flag
(since it thinks it probably set it itself) and jumps to the start of the
application flash instead of making an upload attempt. Time: pretty quick
For this stuff, you need to start knowing exactly which version of the bootloader you are running.
Older versions of Optiboot would reset all of the MCUSR bits.
Newer versions attempt to preserve them with a sort of complicated dance to detect the "HW Reset= RUN bootloaer" vs "Watchdog Reset = run application" differences, while still leaving the bits for the application to check.
All the setup code required to provide the C environment your main()
program requires runs. This includes for example copying global variable
intial values from flash to RAM, etc. Time: FIXME?
main() begins executing.
Sounds right. Note that there is only ~2k of RAM. With a couple of cycles to read and a couple to write and a couple to loop, you're probably still looking at times in the "pretty quick" range.