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.
Best Answer
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
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.