AVR

Powering on my STK 500

You can use any polarity, but if you can, please use a negative center connector 10-15V DC.

Background from STK500 Manual:

An external 10 - 15V DC power supply is required. The input circuit is a full bridge rectifier, and the STK500 automatically handles both positive or negative center connectors. If a positive center connector is used, it can be impossible to turn the STK500 off since the power switch disconnects the GND terminal. In this case, GND can be supplied through the RS-232 cable shield if connected or through alternative GND connections.

Getting on and connecting the STK500

$ dmesg 
pl2303 converter now attached to ttyUSB0

Which Socket to use?

STK500 Jumper Settings

Clock Settings

AVRDUDE commands

List supported MCUs:

avrdude  -c stk500v2 -p ?

Read lfuse:

avrdude -c stk500v2 -p m16 -P /dev/ttyUSB0 -U lfuse:r:lfuse.hex:b

Read hfuse:

avrdude -c stk500v2 -p m16 -P /dev/ttyUSB0 -U hfuse:r:hfuse.hex:b

Set lfuse:

avrdude -c stk500v2 -p m16 -P /dev/ttyUSB0 -U lfuse:w:0b11110000:m

ATMEGA16L Fuses

Read the datasheet, and search for "Fuse Low Byte" and "Fuse High Byte". For my configuration, for an external clock, 0000 needs to be written to CKSEL3...CKSEL0

ATMEGA8 Fuses

STK500 is having a resonator on it's own, so AVR has to be configured to use an external clock source. For this, the external clock options needs to be selected, and that is the same as in the case of ATMEGA16L.

Trying out V-USB

This is using an ATTINY2313 chip. First I want to see if I can get the lfuse to read.

Read lfuse:

$ avrdude -c stk500v2 -p t2313 -P /dev/ttyUSB0 -U lfuse:r:lfuse.hex:b
$ cat lfuse.hex
0b1100100

Read hfuse:

avrdude -c stk500v2 -p t2313 -P /dev/ttyUSB0 -U hfuse:r:hfuse.hex:b
$ cat hfuse.hex
0b11011111

This more or less matches the datasheet. I say more or less, as one section suggests that the default value for CKSEL3..1 is 001 whereas the document also mentions:

The device is shipped with CKSEL = 0100, SUT = 10, and CKDIV8 programmed.

Clock configuration

For an xtal configuration, I need CKSEL3..1 to be 111 for crystal above 8 Mhz. CKSEL0 fuse together with SUT1..0 determines startup.

Also looking at examples with V-USB:

$(AVRDUDE) -U hfuse:w:0xdb:m -U lfuse:w:0xef:m

Meaning hfuse to be on 0b11011011 and lfuse to be on 0b11101111

ATTINY2313 HFUSE

What changes in VUSB configuration is that Brown-Out detection voltage is put to 2.7V

ATTINY2313 LFUSE

This means that we will not use a prescaler and CKSEL3..1 will be 111 meaning external Xtal above 8 MHz. CKSEL0 becomes 1 which togther with SUT0 being 0 and SUT1 being 1 means Crystal Oscillator, fast rising power configuration.

Let's program those bytes:

avrdude -c stk500v2 -p t2313 -P /dev/ttyUSB0 -U hfuse:w:0xdb:m -U lfuse:w:0xef:m

And now let's try to blink it. This is blink.c

#define F_CPU 12000000
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    uint8_t led;

    DDRB = 0xff;
    led = 0;

    PORTB = ~led;

    for(;;) {
        _delay_ms(500);
        if(led) {
            led = 0;
        } else {
            led = 255;
        }
        PORTB = ~led;
    }
}

And this is my Makefile:

MMCU=attiny2313
AVRDUDE_DEVICE=t2313

CC=avr-gcc
OBJCOPY=avr-objcopy
CMPNAME=blink

STKPORT=/dev/ttyUSB0

CFLAGS=-g -mmcu=$(MMCU) -v

$(CMPNAME).hex : $(CMPNAME).out
    $(OBJCOPY) -j .text -O ihex $(CMPNAME).out $@

$(CMPNAME).out : $(CMPNAME).o
    $(CC) $(CFLAGS) -o $@ -Wl,-Map,$(CMPNAME).map $<

$(CMPNAME).o : $(CMPNAME).c
    $(CC) $(CFLAGS) -Os -c $<

$(CMPNAME).s: $(CMPNAME).c
    $(CC) -S $(CFLAGS) -o $<

program: $(CMPNAME).hex
    avrdude -p $(AVRDUDE_DEVICE) -c stk500v2 -P $(STKPORT) -U flash:w:$(CMPNAME).hex:i

fuses:
    avrdude -p $(AVRDUDE_DEVICE) -c stk500v2 -P $(STKPORT) -U hfuse:w:0xdb:m -U lfuse:w:0xef:m

clean:
    rm -f *.o *.out *.map *.hex

.PHONY: clean program