6
\$\begingroup\$

I'm new to AVR programming, I got this atmega8A chip and I use avrdude with a USBasp that I made myself, for programming it.

For some reason the chip won't answer if I don't use the -B 3 parameter with avrdude, although I have reset the chip's fuse bits to factory defaults.

Now my question is what is this -B 3 parameter and how can I fix this.

\$\endgroup\$

3 Answers 3

6
\$\begingroup\$

From the documentation:

-B bitclock
        Specify the bit clock period for the JTAG interface or the ISP
        clock (JTAG ICE only).  The value is a floating-point number in
        microseconds.  The default value of the JTAG ICE results in about
        1 microsecond bit clock period, suitable for target MCUs running
        at 4 MHz clock and above.  Unlike certain parameters in the 
        STK500, the JTAG ICE resets all its parameters to default values 
        when the programming software signs off from the ICE, so for MCUs
        running at lower clock speeds, this parameter must be specified on
        the command-line.  You can use the 'default_bitclock' keyword in
        your ${HOME}/.avrduderc file to assign a default value to keep
        from having to specify this option on every invocation.

In my opinion this means it is not so much a 'fix', but regular setting to adjust the clock of the programmer to the clock of the receiving controller. If my memory serves me right, a factory default ATmega8 runs at 1MHz, whereas avrdude is set for 4MHz by default and thus your programmer is just too fast for your controller to keep up. This implies that when you change the clock fuses of your controller, a different -B is required for programming it.

\$\endgroup\$
3
\$\begingroup\$

The B-flag in avrdude is there to set the clock speed of the JTAG/ISP-clock.

Since your microcontroller is factory default it probably runs at 1 MHz and the default setting for the clock speed for the ISP transfer is to high for it to cope.

So the -B 3 parameter is nothing wrong really. But if you configure your controller to run at 8 MHz (remove the divide by 8 in the fuse bits) or connect a faster crystal you probably won't need the -B 3 and can run at the default settings.

It is quite common when starting out using a new controller that you have to configure it initially using a slower ISP-clock since the controller is running at a low speed.

\$\endgroup\$
1
\$\begingroup\$

The B flag's purpose depends on the programmer in use. The avrdude documentation doesn't tell the whole story. Fortunately the source is open, so we can peer beneath the covers.

Essentially avrdude collects the command line arguments, packs them in a struct, and asks the programmer to initialise itself. The USBasp's implementation of initialise ultimately calls set_sck_period and passes it the bitclock setting. The bitclock setting, by this point, is the number you specified (3) interpreted as microseconds and converted to Hertz. So roughly 333kHz.

Now set_sck_period checks whether you're in TinyAVR mode or normal SPI mode. Assuming you're in SPI mode, it then finds the closest supported SCK frequency below the requested bitclock. For -B 3, that's 187.5kHz. From there, your USBasp is told what's up. Interestingly, if no -B flag is specified, then "AUTO" is set, which the USBasp interprets as 375kHz.

So why is 187.5kHz needed and 375kHz wont do? Could be a number of reasons such as borderline signal integrity that is helped by a lower clock speed. But I have noticed that with a ATmega328p at least, the CKDIV8 fuse comes programmed from the factory, which means the internal 8MHz oscillator produces a 1MHz system clock. Section 27.8 of the 328p datasheet indicates that for SPI programming, there must be at least four CPU clock cycles for each SCK period. At a 1MHz system clock, that probably means you need SCK to be no faster than 250kHz. Hence, 375kHz wont do, yet setting it to 333kHz succeeds because it's rounded down to 187.5kHz by the USBasp!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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