Skip to content

Instantly share code, notes, and snippets.

@t-nissie
Last active March 24, 2024 11:53
Show Gist options
  • Save t-nissie/a9c81376cf0898c0ec0b453d5a385d33 to your computer and use it in GitHub Desktop.
Save t-nissie/a9c81376cf0898c0ec0b453d5a385d33 to your computer and use it in GitHub Desktop.
Write a program on AVR with USBtiny and a 6-pin cable from Linux

Write a program on AVR with USBtiny and a 6-pin cable from Linux

USBtiny is a writer/reader for in-system programming (ISP) or in-circuit serial programming (ICSP) for AVR. Although there is no drivers for Windows 10 nor 11, we can still use USBtiny with Linux. My USBtiny is HP-AUSB-ISP.

I could write some programs into some boards through a 6-pin cable/connector:

  • led.c into Arduino NANO with ATmega168P
  • Klipper into ET-4000 V2 with ATmega1284P, a 3D Printer main board.

Original location of this MEMO is https://gist.github.com/t-nissie/a9c81376cf0898c0ec0b453d5a385d33 .

Preparation for Ubuntu and Debian GNU/Linux

I had to install some packages with apt(1) command. My Ubuntu is 22.04 LTS.

$ sudo apt install gcc-avr avr-libc binutils-avr avrdude

After I connected USBtiny to my Linux box with a USB cable, I got some system log messages.

$ sudo dmesg -e
     :
[ 3/23 04:41] usb 1-3: new low-speed USB device number 25 using xhci_hcd
[  +0.155004] usb 1-3: New USB device found, idVendor=1781, idProduct=0c9f, bcdDevice= 1.04
[  +0.000050] usb 1-3: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[  +0.000021] usb 1-3: Product: USBtiny
     :

compile led.c and flash the code

Compile led.c. Flash led.elf.hex to the board with avrdude(1) command. led.c and Makefile are in this Gist. After the flashing, verification will be done by avrdude(1).

$ make
avr-gcc -g -Wall -O2 -mmcu=atmega168   -c -o led.o led.c
avr-gcc -g -Wall -O2 -mmcu=atmega168 -Wl,-Map,led.map -o led.elf led.o
avr-objcopy -j .text -j .data -O ihex led.elf led.elf.hex
$ sudo avrdude -c usbtiny -p atmega168p -U flash:w:led.elf.hex

Do not forget sudo for avrdude(1), or you will get a verification error.

Writing Klipper into ET-4000 V2

klipper.elf.hex can be written into ET-4000 V2 as:

$ sudo avrdude -c usbtiny -p atmega1284p -U flash:w:out/klipper.elf.hex:i

Connection can be checked with et4000v2initialtest.cfg as printer.cfg. serial should be changed for your system.

[include mainsail.cfg]
[mcu]
serial: /dev/ttyUSB1

[virtual_sdcard]
path: /home/pi/printer_data/gcodes
on_error_gcode: CANCEL_PRINT

[printer]
kinematics: none
max_velocity: 1000
max_accel: 1000

Trouble shooting

Verification error

avrdude(1) gives an error of avrdude: verification error, first mismatch at byte 0x0006 after writing and reading.

$ sudo avrdude -p atmega168p -c usbtiny -D -U flash:w:demo.elf.hex
                :
Writing | ################################################## | 100% 0.58s
                :
Reading | ################################################## | 100% 0.34s
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0006
         0x06 != 0x46
avrdude: verification error; content mismatch

There may be some reasons:

  • You forgot sudo before avrdude(1).
  • Power supply is not enough. Disconnect servos, motors, etc.
  • avrdude(1) is too old. See Appendix below.

See also: https://synapse.kyoto/blog/electronic_handicraft/arduino/how-to-fix-failure-in-burning-boot-loader-with-arduino-ide-1-6-10 .

Link error

You may encounter link errors like:

/usr/lib/avr/bin/ld: address 0x800931 of out/klipper.elf section `.bss' is not within region `data'

In this case reinstallations of gcc-avr avr-libc and binutils-avr may solve the problem. See https://gist.github.com/t-nissie/81bc471cd543e92d5cd1c3d36474c8b1

Appendix: Install the latest avrdude-7.3

If you want to install the latest avrdude(1), you need some packages for development.

$ sudo apt remove avrdude
$ sudo apt install cmake flex bison libelf-dev libusb-dev libhidapi-dev libftdi1-dev libreadline-dev libserialport-dev ninja-build
$ tar xf avrdude-7.3.tar.gz
$ cd avrdude-7.3.tar.gz
$ ./build.sh
$ sudo cmake --build build_linux --target install
[include mainsail.cfg]
[mcu]
serial: /dev/ttyUSB1
[virtual_sdcard]
path: /home/pi/printer_data/gcodes
on_error_gcode: CANCEL_PRINT
[printer]
kinematics: none
max_velocity: 1000
max_accel: 1000
/* led.c
* Blink the onboard LED at PB5.
* I do not know what value should be set to F_CPU.
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB = _BV(PB5);
PORTB |= _BV(PB5);
while (1) {
_delay_ms(300); /* wait for 300 ms */
PORTB ^= _BV(PB5); /* Flip the bit */
}
}
# -*-Makefile-*- for led.c
##
PRG = led
CC = avr-gcc
CFLAGS = -g -Wall -O2 -mmcu=atmega168
LDFLAGS = -Wl,-Map,$(PRG).map
all: $(PRG).elf.hex
$(PRG).o: $(PRG).c
$(PRG).elf: $(PRG).o
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
%.elf.hex: %.elf
avr-objcopy -j .text -j .data -O ihex $< $@
@echo " Flash the AVR as: sudo avrdude -c usbtiny -p atmega168p -U flash:w:$@"
@echo ' Do not forget `sudo` !!!'
flash: $(PRG).elf.hex
avrdude -P usb -p atmega168p -c usbtiny -D -U flash:w:$<
clean:
rm -f *.o $(PRG).elf *.eps *.png *.pdf *.bak *.hex *.bin *.srec
rm -f *.lst *.map $(EXTRA_CLEAN_FILES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment