Yes, yes, I know that it must be between 1 and 4 clock cycles of a target device. But... We don't know it if clock is provided by internal rc generator or external quartz. So is there any possible way to find the fastest allowed spi frequency?

And one more question. I've read that we can provide external clock source when programming. But how can we tell to AVR about this? I thought that it always uses internal rc generator when being programmed.

  • \$\begingroup\$ You have to set the CKSEL fuse bits to choose a clock source, but you have set them only once. So I guess this is the way regarding your second question. But someone correct me if I am mistaken. \$\endgroup\$ – Bence Kaulics May 8 '16 at 20:55
  • \$\begingroup\$ @BenceKaulics so you think I have to write fuses twice? Once before programming to enable external source and once after to write user fuses? \$\endgroup\$ – Long Smith May 8 '16 at 21:39

First question:

You know your board design. If you are using an external clock source (oscillator, crystal, etc.), you know what the frequency is. If you have no external clock source, you know you need to use the internal 8MHz oscillator. If it is a brand new chip (i.e. never programmed) you know that the internal source is selected.

The simplest process is to write the fuse value for the clock source at a low speed (AFAIK there is no limit on how low a frequency you can program the device). Once you have programmed the fuse values, you now know for sure what clock source is being used - because you have just selected it. If you know the clock source, you know what frequency it is, and you can easily work out the fastest possible programming speed.

Second question:

The AVR will use whatever clock source the fuses tell it to use - even when programming. If you program the fuse to use the internal oscillator, then after the next reset it will start using the internal oscillator. If you select an external clock source, then it will use the external clock source for everything, including programming.

In fact this is one of the problems people face. They program the fuses to use an external clock source, but then forget to apply one and wonder why they can't program any more. If your AVR is set to use an external source, you must apply an external source, even for programming (sorry, I just want to keep reiterating that point).

Third question (from comments)

No, you don't need to program the fuses twice. Once you have programmed the fuse, it is programmed. You can change it again if you want, but you don't need to program it twice to make it stick. However, what you do need to do is reset the IC. While in programming mode, if you change the fuses, the value is not updated until after the chip undergoes a reset (either from the reset pin, or a power-on reset).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.