0
\$\begingroup\$

I'm trying to flash Atmega-328P using avrdude. I've written a custom config for my USBASP to be recognised.

programmer
  id    = "usbasp-kt";
  desc  = "usbasp desc";
  type  = "usbasp";
  usbvid     = 0x03EB;
  usbpid     = 0xC8B4;
;

Then I try to flash it.

avr-gcc -Os -mmcu=atmega328p -g -Wall -Wextra main.c -o firmware.o
avr-objcopy -j .text -j .data -O ihex firmware.o firmware.hex
avrdude -C /usr/local/etc/avrdude.conf -B 4 -c usbasp-kt -p m328p -U flash:w:firmware.hex -P usb -vvv

avrdude: Version 6.2
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/usr/local/etc/avrdude.conf"
         User configuration file is "/home/u/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : usbasp-kt
         Setting bit clk period        : 4.0
avrdude: usbasp_open("usb")
avrdude: seen device from vendor ->zhifengsoft<-
avrdude: seen product ->USBHID<-
         AVR Part                      : ATMEGA328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : usbasp
         Description     : Kompiuterines technikos ISP programuotojas

avrdude: usbasp_initialize()
avrdude: usbasp_spi_set_sck_period(4e-06)
avrdude: try to set SCK period to 4e-06 s (= 250000 Hz)
avrdude: set SCK frequency to 187500 Hz
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: usbasp_program_enable()
avrdude: error: program enable: target doesn't answer. 0 
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

avrdude: usbasp_close()

avrdude done.  Thank you.

It doesn't matter if I use -B 4 parameter or not. I've checked wires a few times, everything seems correct. USBASP light is on so it must be working from USB.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Is the microcontroller you are trying to program set up with the right clock source? When they come from the factory, their fuse bits are normally set so that they run at a clock speed of 1 MHz, using the internal RC oscillator. If you have scavenged it from somewhere, it might be set to expect an external clock source or crystal (and won't do anything without one being connected).

As the programmer has to keep its SPI clock speed at or below 1/4 of the target clock speed for successful data transfer, you will have to reduce the SPI clock speed when the target device runs slowly (for example, at the default 1 MHz). AVRDUDE clearly tries to set the SPI clock below 1/4 of 1 MHz

avrdude: usbasp_spi_set_sck_period(4e-06)  
avrdude: try to set SCK period to 4e-06 s (= 250000 Hz)  
avrdude: set SCK frequency to 187500 Hz. 
avrdude: warning: cannot set sck period. please check for usbasp firmware update.  

but reported errors while trying to do so.

Some USBasp -style programmers have a physical switch, pad or jumper for switching to a low SPI clock speed when programming microcontrollers running at slow speeds, and won't accept AVRDUDEs -B sck clock period switch. I suspect that this is the case with yours. You can flash the USBasp with updated firmware using another programmer to avoid messing with the switch/jumper.

\$\endgroup\$
4
  • \$\begingroup\$ I'm trying to flash a brand new Atmega microprocessor. On the other side, my USBASP is from China, so it's 3rd party. Then I should try to set USBASP to slow mode? It came with a case by default, so I can't see what's inside of it. I've no idea how to flash USBASP. \$\endgroup\$
    – user142736
    Commented Mar 21, 2017 at 5:46
  • \$\begingroup\$ @YeeApple Yes, with a brand new AVR you must set the USBasp to slow mode in order to program it. To do so, take apart the case and set the slow sck jumper. If there is just a pair of adjacent pads labeled e.g. "slow sck" instead of a jumper, you will have to bridge two pads with a blob of solder (or solder your own switch/jumper there). Once you flash the fuse bits so that the AVR runs at e.g. 8 MHz, the slow mode won't be necessary anymore. \$\endgroup\$
    – jms
    Commented Mar 21, 2017 at 12:34
  • \$\begingroup\$ @YeeApple You only have to flash the USBasp if you want to use the -B setting instead of the physical switch/jumper; it's not absolutely necessary, and might even break it if the manufacturer chose to make the USBasp non-standard. To update it, you need any other in-circuit serial AVR programmer, even an arduino that has been programmed to act like one. \$\endgroup\$
    – jms
    Commented Mar 21, 2017 at 12:35
  • \$\begingroup\$ I've fixed this by reflashing an USBasp programmer's software. Also official USBasp works fine. \$\endgroup\$
    – user142736
    Commented May 26, 2017 at 9:20

Your Answer

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