I have a chip now and am trying to get it to work, but have so far been unsuccessful.
I have tried to adapt the Makefile, optiboot.c, and boot.h files to suit the 162, and can get it to compile to hex.
I can then upload the hex file using another arduino as an isp fine.
However each time I try to upload a sketch to the Atmega162, I just get that it is not in sync, and it would appear that the chip is not comunicating.
Furthermore, the LED I have connected to what I have called Pin 13 doesn't seem to behave correctly. Initially it just wouldn't turn on at all. Based on the datasheet, I couldn't find any reference to the ability to toggle an output by writing 1 to PINxn, whereas in the 328 datasheet it is mentioned. As such I have changed any reference of it to:
#if defined(__AVR_ATmega162__) || defined(__AVR_ATmega8__)
LED_PORT ^= _BV(LED);
#else
LED_PIN |= _BV(LED);
#endif
which I believe will do the job. However this doesn't appear to work. Sometimes when I try to program the LED comes on very dimly and nothing after that. Other times it blinks once. Some times it doesn't do anything until Arduino closes the COM port, and then it blinks once.
The next change is that the Atmega162 doesn't have an MCUSR register, but the 4 bits in it are contained as the least significant 4 bits in the MCUCSR register, so I believe this change should be appropriate
#ifdef __AVR_ATmega162__
ch = MCUCSR;
ch &= 0x0F;
MCUCSR &= 0xF0;
#else
ch = MCUSR;
MCUSR = 0;
#endif
if (!(ch & _BV(EXTRF))) appStart();
The Watchdog timer register has a different name, so I have changed that. Similarly the timers have different registers, so I have updated those with #if define()'s.
I am not entirely sure what I have missed, so if anyone could help point me in the right direction that would be great. I am happy to provide more info if needed.
Tom
p.s. quick thought, could it be something to do with the wrong register numbers in the inline assembler bits?
optiboot.c:
http://www.railways-in-miniature.co.uk/electronics/FileDump/optiboot/optiboot.c
boot.h:
http://www.railways-in-miniature.co.uk/electronics/FileDump/optiboot/boot.h
lines added to Makefile
atmega162: TARGET = atmega162
atmega162: MCU_TARGET = atmega162
atmega162: CFLAGS += '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200'
atmega162: AVR_FREQ = 16000000L
atmega162: $(PROGRAM)_atmega162.hex
atmega162: $(PROGRAM)_atmega162.lst
atmega162_isp: atmega162
atmega162_isp: TARGET = atmega162
# 2.7V brownout
atmega162_isp: HFUSE = DA
# Low power xtal (16MHz) 16KCK/14CK+65ms
atmega162_isp: LFUSE = FF
# 512 byte boot
atmega162_isp: EFUSE = 1A
atmega162_isp: isp