compiling optiboot bootloader for atmega16

hi all I have a few atmega16's lying around and was wanting to use these chips withe the arduino boot loader and i found this thread which was very helpful http://forum.arduino.cc/index.php?topic=172881.msg1283986#msg1283986

a guy called "hiduino" did attach a mega16 boot-loader but i want to compile the bl myself and need a little help in what changes need to be made in optiboot.c and makefile and pin_defs.h to get it to compile

so far i have added this to the make file

atmega16: TARGET = atmega16
atmega16: MCU_TARGET = atmega16
atmega16: CFLAGS += '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200' '-DBIGBOOT'
atmega16: AVR_FREQ = 16000000L
atmega16: LDSECTIONS  = -Wl,--section-start=.text=0xfc00
atmega16: $(PROGRAM)_atmega16.hex
atmega16: $(PROGRAM)_atmega16.lst

atmega16_isp: atmega16
atmega16_isp: TARGET = atmega16
atmega16_isp: MCU_TARGET = atmega16
# 1024 byte boot
atmega16_isp: HFUSE = D9
# Low power xtal (16MHz) 16KCK/14CK+65ms
atmega16_isp: LFUSE = FF
# 2.7V brownout
atmega16_isp: isp

and added this to pin_def.h

#if defined(__AVR_ATmega16__)
  //Name conversion R.Wiersma
  #define UCSR0A   UCSRA
  #define UDR0         UDR
  #define UDRE0    UDRE
  #define RXC0     RXC
  #define FE0           FE
  #define TIFR1    TIFR
  #define WDTCSR   WDTCR
#endif

but all this is just a guess

I'm not too sure on what else i have to change/add Iknow that im trying to reinvent the wheel :) but it's just how i like to learn.

so hope you guys can help.

Mike.

Optiboot includes atmega32 stuff (latest versions). So just add where you see "atmega32". As far as I can remember there are some addons in the code as well..

I would recommend starting with the latest Optiboot5.0a. The latest version supports command line build options for easier customizations. https://code.google.com/p/optiboot/downloads/list

These changes below are for Optiboot5.0a.

Note: Optiboot builds within 512 bytes/256 words, so the start address for the mega16 will be 0x3e00 (byte address). Also the hfuse byte should be changed to 0xDC, for the boot start 0x1F00 (word address) and boot reset vector option. Makefile:

atmega16: TARGET = atmega16
atmega16: MCU_TARGET = atmega16
atmega16: CFLAGS += $(COMMON_OPTIONS)
atmega16: AVR_FREQ ?= 11059200L
atmega16: LDSECTIONS  = -Wl,--section-start=.text=0x3e00 -Wl,--section-start=.version=0x3ffe
atmega16: $(PROGRAM)_atmega16.hex
atmega16: $(PROGRAM)_atmega16.lst

atmega16_isp: atmega16
atmega16_isp: TARGET = atmega16
atmega16_isp: MCU_TARGET = atmega16
# No OCD or JTAG, SPIEN, CKOPT (for full swing xtal), Bootsize=512B
atmega16_isp: HFUSE ?= DC
# 2.7V brownout, 16MHz Xtal, 16KCK/14CK+65ms
atmega16_isp: LFUSE ?= FF
atmega16_isp: isp

pin_defs.h (some additional register name changes are needed for the mega16):

#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__)
  //Name conversion R.Wiersma
  #define UCSR0A   UCSRA
  #define UDR0         UDR
  #define UDRE0    UDRE
  #define RXC0     RXC
  #define FE0      FE
  #define TIFR1    TIFR
  #define WDTCSR   WDTCR
#endif
#if defined(__AVR_ATmega16__)
  #define MCUSR        MCUCSR
#endif
#if defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__)
  #define WDCE     WDTOE
#endif

optiboot.c (this requires many changes):

#if defined(__AVR_ATmega168__) || defined (__AVR_ATmega16__)
#define RAMSTART (0x100)
#define NRWWSTART (0x3800)
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega32__)

Where ever you see this line:

#if defined(__AVR_ATmega8__) || defined (__AVR_ATmega32__)

change it to:

#if defined(__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)

Building optiboot from command line:

> make atmega16 AVR_FREQ=16000000L BAUD_RATE=115200 UART=0 LED=B7 LED_START_FLASHES=3

This should get you going.

There is one caveat with Optiboot5.0a it defaults to using Double speed U2X USART Baud rates. This creates problems for slower CPU clock rates working reliably at 115200 or even 57600 bauds. I have made additional mods to turn off Double Speed. I can go into more details if you are having problems with getting the baud rate to work with slower CPU clock speeds.

Thanks Hiduino your help is much appreciated this info is great and worked a treat :)

I've got another small problem. I've tried to up load a sketch which uses timer 2 overflow interrupt but when i upload it i get a error

error: 'TIMSK2' was not declared in this scope

but when i upload on to my uno it works so not a problem with the sketch but with the core-files im using. are there any core-files already customised ? or do i have to edit them myself?

The ATmega16 only has one register TIMSK to access all timer masks. You will need to reference the datasheet for details. http://www.atmel.com/Images/doc2466.pdf

The core files from mighty-1284p do not have full support for the mega16. From what I can tell only the Tone library is referencing TIMSK2. Are you trying to use tone()?

I'm using a library for rotary encoders

it is working now just changed timsk2 to timsk

i really should have checked the datasheet as i would have found out there was no tmsk2 :blush:

and thanks alot hiduino your a star