jbilander / sdbox

A parallel to sd-card project for the Amiga
GNU General Public License v3.0
94 stars 14 forks source link

Atmega328PB #14

Open SukkoPera opened 2 years ago

SukkoPera commented 2 years ago

I had a broken Nano and i repaired it by replacing the MCU with an ATmega328PB I had laying around. Said MCU is not 100% compatible with the 328P, but I managed to build an hex that seems to work fine.

I'm attaching the modified Makefile and the resulting hex file just for reference. No guarantees ;) Makefile.328pb.txt main.hex.txt (Remove .txt etension)

RetepV commented 4 months ago

Thanks SukkoPera, I was able to program my SDBOX MINI with your makefile changes. The resulting binary seems to be running well.

There are some big changes to the oscillator, the PB does not have a full-swing oscillator anymore. But I used the values for the ATMEGA328P to program the fuses, and it all seems to still work. I recommend to add the fuse values to the avrdude call:

PORT = /dev/ttyUSB0
BAUD = 115200
MCU = atmega328pb
PROGRAMMER = arduino

all: build flash

main.elf: main.c
    avr-gcc -Os -DSPSR=SPSR0 -DSPCR=SPCR0 -DSPDR=SPDR0 -mmcu=$(MCU) main.c -o main.elf

main.hex: main.elf
    avr-objcopy -O ihex main.elf main.hex

build: main.hex

flash: main.hex
    avrdude -p$(MCU) -c$(PROGRAMMER) -P$(PORT) -b$(BAUD) -D -Uflash:w:main.hex:i -Ulfuse:w:0xe6:m -Uhfuse:w:0xdf:m -Uefuse:w:0xfc:m

clean:
    rm main.elf
    rm main.hex

This programs the fuses for full-swing oscillator, even though the PB does not support it. But apparently, with 16MHz the oscillator still works. Going 20MHz or higher, it stops working.

https://omzlo.com/articles/using-an-external-crystal-on-the-atmega328pb

If in doubt, you could also replace the crystal with a canned oscillator, but it's not drop-in (the oscillator needs a 5V vcc and you will have to change the PCB or cut traces and solder wires).

SukkoPera commented 4 months ago

Yes, you are correct. My experience says that 16 MHz works "well enough" on the PB... unless you touch the caps :). But yeah, while most of the other changes wrt the P are negligible when trying to do a drop-in replacement, this one is a real PITA :(.

Anyway lol, I coudln't even remember I had done this! 😅