I have a really weird problem with a Mega640. It only runs properly when the JTAG ICE is plugged in.
I have put a GPIO toggle in the main loop which goes at 307kHz. The main loop is incredibly simply. All it does is wait for serial comms which is interrupt driven.
If I boot without the JTAG plugged in it [the GPIO] toggles at about 10kHz. I can get it going at the right speed by simply plugging and unplugging the JTAG header of the ICE - you can see the GPIO suddenly toggling at the right speed.
The crystal seems to be oscillating at the correct frequency from the word go, brown out is disabled.
This is not a new design, it's a very low volume production board and I don't have any more boards.
Anyone got any suggestions please?