After several success attempt with arduino mini and nano (both using ATMega328), I’m curious about programming some chip in assembler. Yeah, I know we could use arduino board to do it. But I prefer to do it the “right way”, as pure assembler novice, :)
So I grab the 1$ ATTiny13A and 2$ USBASP knockoff. Almost forgot the second as I think I already have one used on my Arduino Pro Mini (no, it’s USB-to-TTL, PL2303HX, only have vcc-gnd-tx-rx pins, completely different device).
Turn out that my ‘other’ have some use in my system (my usbasp couldnt provide 5v power, atleast at my system, so I use usb-to-ttl instead)
avrdude done. Thank you.
Create the file main.c, a code to blink LED on Port B0. (Okay, it's not assembly yet, just to test if my minimal system works, will do in assembler next)
}
compile it
Nugrohos-MacBook-Air:ATTiny nugroho$
Nugrohos-MacBook-Air:ATTiny nugroho$
And we're good to go
Setelah beberapa sukses dengan Aarduino pro mini dan nano (keduanya menggunakan ATMega328), saya tertarik dengan memprogram beberapa chip menggunaka bahasa assembly (atau assembler). Tentu saja kita bisa menggunakan arduino untuk melakukan itu namun saya ingin merasakan pemrograman asembler dengan cara klasik, menggunakan sistem minimal dan upload manual, :)
(di post ini saya masih menggunakan c untuk mengetes bahwa sistem telah bekerja, dapat mengupload hex file)
Jadilah saya memakai ATTiny13A, ISP programmer atau downloader SPI palsu, USBASP. Yang ini hampir lupa karena saya pikir telah punya PL2303HX ,USB-To-TTL yang biasa saya gunakan untuk mengupload sketch arduino, dengan pin vcc-gnd-tx-rx.
Ternyata USB-to-TTL saya memang berguna juga karena USBASP saya tak bisa memberi tegangan 5v ke sistem minimal, hanya dapat digunakan untuk upload file hex ke ATTiny13A.
Test Programmer and connectivity
Nugrohos-MacBook-Air:ATTiny nugroho$ avrdude -p t13 -c usbasp
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e9007
avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)
Create the file main.c, a code to blink LED on Port B0. (Okay, it's not assembly yet, just to test if my minimal system works, will do in assembler next)
#include <avr/io.h>
#include <util/delay.h>
/*
Blinks an LED connected to pin B0.
Assumes the positive pin of the LED is connected to B0.
The negative pin is connected through a resistor to GND.
A 1K resistor works well for most LEDs.
Decrease if too dim.
*/
int main(void) {
DDRB = 255U; // Make all PB* -- PORT B -- pins output
PORTB = 0x0; // turn all PB* -- PORT B -- pins off.
while (1) {
PORTB = 0x1; // high
_delay_ms(250); // 1/4 second on
PORTB = 0X0; // low
_delay_ms(2500); // 2.5 seconds off
}
}
create the Makefile
PRG = main
OBJ = main.o
PROGRAMMER = usbasp
PORT = usb
MCU_TARGET = attiny13
AVRDUDE_TARGET = attiny13
OPTIMIZE = -Os
DEFS =
LIBS =
HZ = 9600000
CC = avr-gcc
override CFLAGS = -g -DF_CPU=$(HZ) -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
override LDFLAGS = -Wl,-Map,$(PRG).map
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
all: $(PRG).elf lst text #eeprom
$(PRG).elf: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
clean:
rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak *.hex *.bin *.srec
rm -rf *.lst *.map $(EXTRA_CLEAN_FILES)
lst: $(PRG).lst
%.lst: %.elf
$(OBJDUMP) -h -S $< > $@
text: hex bin srec
hex: $(PRG).hex
bin: $(PRG).bin
srec: $(PRG).srec
%.hex: %.elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@
%.srec: %.elf
$(OBJCOPY) -j .text -j .data -O srec $< $@
%.bin: %.elf
$(OBJCOPY) -j .text -j .data -O binary $< $@
eeprom: ehex ebin esrec
ehex: $(PRG)_eeprom.hex
esrec: $(PRG)_eeprom.srec
%_eeprom.hex: %.elf
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
%_eeprom.bin: %.elf
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@
install: $(PRG).hex
avrdude -p $(AVRDUDE_TARGET) -c $(PROGRAMMER) -P $(PORT) -v \
-U flash:w:$(PRG).hex
(note: although we use ATTiny13A, we have to declare it as ATTiny13 or it won’t upload :) )
Nugrohos-MacBook-Air:ATTiny nugroho$ make
avr-gcc -g -DF_CPU=9600000 -Wall -Os -mmcu=attiny13 -c -o main.o main.c
avr-gcc -g -DF_CPU=9600000 -Wall -Os -mmcu=attiny13 -Wl,-Map,main.map -o main.elf main.o
avr-objdump -h -S main.elf > main.lst
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-objcopy -j .text -j .data -O binary main.elf main.bin
avr-objcopy -j .text -j .data -O srec main.elf main.srec
Nugrohos-MacBook-Air:ATTiny nugroho$
upload it to ATTiny13 using make install
Nugrohos-MacBook-Air:ATTiny nugroho$ make install
avrdude -p attiny13 -c usbasp -P usb -v \
-U flash:w:main.hex
avrdude: Version 6.0.1, compiled on Dec 16 2013 at 17:26:24
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2009 Joerg Wunsch
System wide configuration file is "/usr/local/CrossPack-AVR-20131216/etc/avrdude.conf"
User configuration file is "/Users/nugroho/.avrduderc"
User configuration file does not exist or is not a regular file, skipping
Using Port : usb
Using Programmer : usbasp
AVR Part : ATtiny13
Chip Erase delay : 4000 us
PAGEL : P00
BS2 : P00
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 5 4 0 no 64 4 0 4000 4000 0xff 0xff
flash 65 6 32 0 yes 1024 32 32 4500 4500 0xff 0xff
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
calibration 0 0 0 0 no 2 0 0 0 0 0x00 0x00
lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
Programmer Type : usbasp
Description : USBasp, http://www.fischl.de/usbasp/
avrdude: auto set sck period (because given equals null)
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e9007
avrdude: safemode: lfuse reads as 6A
avrdude: safemode: hfuse reads as FF
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: auto set sck period (because given equals null)
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "main.hex"
avrdude: input file main.hex auto detected as Intel Hex
avrdude: writing flash (88 bytes):
Writing | ################################################## | 100% 0.08s
avrdude: 88 bytes of flash written
avrdude: verifying flash memory against main.hex:
avrdude: load data flash data from input file main.hex:
avrdude: input file main.hex auto detected as Intel Hex
avrdude: input file main.hex contains 88 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.05s
avrdude: verifying ...
avrdude: 88 bytes of flash verified
avrdude: safemode: lfuse reads as 6A
avrdude: safemode: hfuse reads as FF
avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)
avrdude done. Thank you.
And we're good to go
Setelah beberapa sukses dengan Aarduino pro mini dan nano (keduanya menggunakan ATMega328), saya tertarik dengan memprogram beberapa chip menggunaka bahasa assembly (atau assembler). Tentu saja kita bisa menggunakan arduino untuk melakukan itu namun saya ingin merasakan pemrograman asembler dengan cara klasik, menggunakan sistem minimal dan upload manual, :)
(di post ini saya masih menggunakan c untuk mengetes bahwa sistem telah bekerja, dapat mengupload hex file)
Jadilah saya memakai ATTiny13A, ISP programmer atau downloader SPI palsu, USBASP. Yang ini hampir lupa karena saya pikir telah punya PL2303HX ,USB-To-TTL yang biasa saya gunakan untuk mengupload sketch arduino, dengan pin vcc-gnd-tx-rx.
Ternyata USB-to-TTL saya memang berguna juga karena USBASP saya tak bisa memberi tegangan 5v ke sistem minimal, hanya dapat digunakan untuk upload file hex ke ATTiny13A.
Tak perlu driver, OS X telah mendeteksi USBASP saya secara otomatis