Making bootloader for Atmega162

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

I've made a bit of progress, it turns out that the NWRRSTART should be 0x3800, and not 0x1C00. Just for curiosities sake, does anyone know where this comes from (originally I used a value found in the datasheet).
Also, the Low Fuse byte should be 0xD8 rather than 0xDA otherwise there was only 256Bytes of bootloader space. Woops!

Now I get three quick blinks of the LED as expected when downloading begins, however at the moment it is still not responding (not in sync: resp=0x00).

WOOO! Got it :smiley:

The hardware serial port of the Atmega162 is slightly different, so the initialisation of it needs to be changed to:

#ifndef SOFT_UART
#ifdef __AVR_ATmega8__
  ...
#elif defined(__AVR_ATmega162__)
  UCSR0A = _BV(U2X0); //Double speed mode USART0
  UCSR0B = _BV(RXEN0) | _BV(TXEN0);
  UCSR0C = _BV(URSEL0) | _BV(UCSZ00) | _BV(UCSZ01);
  UBRR0L = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
#else
  ...
#endif
#endif

Also, i'm guessing that the NRWWSTART value = 'program memory space' - 'bootloader size' = 16k - 1k = 0x3C00

Seems to be working nicely. Got the blink sketch to upload and run.
I did need to decrease to 57600 BAUD from 115200 to get a reliable connection (keeps timing out otherwise).

Tom

optiboot_atmega162.hex (1.41 KB)

pins_arduino.h (5.07 KB)

boards.txt (13.5 KB)

My pins_arduino.h file was wrong. I have attached a corrected version.

pins_arduino.h (5.06 KB)

TCWORLD:
My pins_arduino.h file was wrong. I have attached a corrected version.

Cool, I've got your files. I'll try to make it work, but this is my FIRST arduino project...
any help would be welcome

I also have a bootloader hex (x0xb0x_boot.HEX) file for the 162. Its from adafruit, for the x0xb0x.
I guess its more or less the same as yours.

But... I really dont know what to do with yout files :frowning:

YES! It worked.
I renamed my bootloader.hex file ant put it in the optiboot map.
COOOLLL

Now I can burn the firmware of the x0xb0x to it, and finaly play with my x0xb0x
XD
THANKS
:smiley:

To bad... The bootloader doesn't work. It won't load the firmware... Maybe because i first loaded a false programme to the 162 and after that the bootloader.
Or because i renamed the .HEX Bootloader file...
I dont get it...

Whats the problem with it?

Here's what I did:

In the directory:
..\arduino-1.0\hardware\arduino\variants

  1. Add a folder called Atmega162 (I called it AstroEQ for various reasons, but doesnt matter).
  2. Put the "pins_arduino.h" inside this directory.

In the directory:
..\arduino-1.0\hardware\arduino\

  1. Add the following to "boards.txt":
##############################################################
atmega162.name=ATmega162

atmega162.upload.protocol=arduino
atmega162.upload.maximum_size=15360
atmega162.upload.speed=57600

atmega162.bootloader.low_fuses=0xFF
atmega162.bootloader.high_fuses=0xD8
atmega162.bootloader.extended_fuses=0xFB
atmega162.bootloader.path=optiboot
atmega162.bootloader.file=optiboot_atmega162.hex
atmega162.bootloader.unlock_bits=0x3F
atmega162.bootloader.lock_bits=0x0F

atmega162.build.mcu=atmega162
atmega162.build.f_cpu=16000000L
atmega162.build.core=arduino
atmega162.build.variant=Atmega162

##############################################################

Note that the last line of that (before the #####) has the name of the folder you created in step 1. If you used the boards.txt file I uploaded, these lines are already at the bottom of the file. You just need to edit the last line to match the correct folder name as mentioned.
The first line of the above is the what you want it to appear as in the IDE.

In the directory:
..\arduino-1.0\hardware\arduino\bootloaders\optiboot\

4)Copy the file "optiboot_atmega162.hex" to this folder.

5)Restart the Arduino IDE (close all open windows, and reopen).

  1. Select "Tools->Board->'name from step 4' "

  2. Select "Tools->Programmer->'Arduino as ISP' "

  3. Click "Tools->'Burn Bootloader' "

If the bootloader burns sucessfully, it should now work. Just upload a sketch as normal.

I have just looked up 'x0xb0x'. Am I right in thinking you are trying to use this:

If so, they have given you a .hex file, so you don't actually need Arduino IDE, or the bootloader. Perhaps I had misunderstood what you are trying to do.

If you are just trying to load their hex file, you can do it following the steps above only in step 4 instead of using the bootloader file I made, download the x0xb0x hex file and rename it to "optiboot_atmega162.hex", then it should burn that hex file.

I did all the above, and the bootloader burned.
But i wonder if the name in step 4 :
4)Copy the file "optiboot_atmega162.hex" to this folder.

Could be changed to x0xb0x_boot.HEX
?

You beat me too it, I was just editing my post above.

If you rename the file, it will trick the arduino IDE into uploading that file.

TCWORLD:
You beat me too it, I was just editing my post above.
Jj
I did just that, but doesn't the name mess
If you rename the file, it will trick the arduino IDE into uploading that file.

Hehehe, nice :stuck_out_tongue:

I did just that.
But i still cant load the firmware on it... No correct device name...
I'll look into it again tomorrow. I need sleep now...
:sleeping:

Just to be shure,
how did you hookup the 162?

UNO -> Atmega162
Miso -> Miso
Mosi -> Mosi
SS -> Reset
+5v -> VCC
GND -> GND

What happens when you try and program it?

OK that's what I did.

I think the problem is in my x0xb0x.

I put a brand new CPU in today, with firmware, and still... no play time.
So I'll first have to fix the x0xb0x...

I think the bootloader worked just fine.

Hi TCWORLD
i just want to know, can you run bootloader on ATMega162 to work with Arduino IDE?

Yes, that is the purpose of it. It is just a modified version of the Arduino Optiboot bootloader to support the Atmega162.

The code supports both serial ports - Serial.begin() and Serial1.begin(). I have checked and they do work. It should also support PWM, but I haven't checked that. See the pins_arduino.h file to find out how the arduino pin numbers (0 to 34 inclusive) relate to the atmega162 chip pins.
Note: The atmega162 has no Analog inputs, but it does have 35 digital outputs, 6 of which work as PWMs. It has 2 serial ports, SPI, and I2C. There is however only 14kB of program space, half what an Uno has.

I found some bugs in the pins_arduino.h file, and there is also a file in the avr folder which doesn't work without a small modification. I have attached a full set of files to this post.

optiboot_atmega162.hex needs to be saved to:
\hardware\arduino\bootloaders\optiboot\optiboot_atmega162.hex

iom162.h needs to be saved to:
\hardware\tools\avr\avr\include\avr\iom162.h
(This replaces the existing one. There is a bug which prevents code from compiling otherwise. Changing the file won't affect anything other than the Atmega162).

pins_arduino.h needs to be saved to:
\hardware\arduino\variants\atmega162\pins_arduino.h

Then in the \hardware\arduino\boards.txt file, add these lines to the end:

##############################################################

atmega162.name= ATmega162

atmega162.upload.protocol=arduino
atmega162.upload.maximum_size=14336
atmega162.upload.speed=57600

atmega162.bootloader.low_fuses=0xFF
atmega162.bootloader.high_fuses=0xD8
atmega162.bootloader.extended_fuses=0xFB
atmega162.bootloader.path=optiboot
atmega162.bootloader.file=optiboot_atmega162.hex
atmega162.bootloader.unlock_bits=0x3F
atmega162.bootloader.lock_bits=0x0F

atmega162.build.mcu=atmega162
atmega162.build.f_cpu=16000000L
atmega162.build.core=arduino
atmega162.build.variant=atmega162

##############################################################

pins_arduino.h (5.16 KB)

iom162.h (20.4 KB)

optiboot_atmega162.hex (1.41 KB)

Hi Tom!

You're doing an awesome Job!

I'm trying to program an ATmega162 using the Arduino IDE and the ArduinoISP, so I dont need the bootloader, but i'm having some problems that you had, for instance the problem of the missing definition of USART0_RX_vect.

Your solution is awesome, but I'd recommend to use another definition inside of Arduino.h or even in pins_arduino.h to avoid changes in iom162.h.

I made the following definitions:

#define USART0_RX_vect USART0_RXC_vect
#define USART1_RX_vect USART1_RXC_vect
#define USART0_TX_vect USART0_TXC_vect
#define USART1_TX_vect USART1_TXC_vect

Worked nicely to compile the affected files, like HardwareSerial.cpp.

Now I "just" need to be able to burn the generated code to the MCU. lol

Regards,
Yuri I.

y3i12:
Your solution is awesome, but I'd recommend to use another definition inside of Arduino.h or even in pins_arduino.h to avoid changes in iom162.h.

I made the following definitions:

#define USART0_RX_vect USART0_RXC_vect

#define USART1_RX_vect USART1_RXC_vect
#define USART0_TX_vect USART0_TXC_vect
#define USART1_TX_vect USART1_TXC_vect

The reason I changed the iom162.h file directly is because Arduino doesn't actually support the 162 natively, so changing it wont do any harm to any of the other processors and examples. It just makes it easier than having to remember to #define them. But either way works :slight_smile: Perhaps though you are correct, the pins_arduino.h file for it would be an ideal place to define them as you would only have to change it once...