AZTEEG X3 PRO: ATmega 2560 Bootloader MCUSR always 0

Hi,

I'm using at AZTEEG X3 PRO board with ATmega 2560. When reading the MCUSR register, it is always 0. This prevent me from diagnostic a reboot problem as I have no insight on the reboot source.

So, my questions are:

  • How can I know which bootloader is installed on my board (I'm using avrdude to update the firmware on it)?
  • Does someone know where can I found the bootloader.hex for the AZTEEG X3 PRO board or simply for ATmega 2560?
  • Does the MCUSR always 0 is a known issue and if there is a fix available (or bootloader version fixing it)?
  • Where can I find the source code for the bootloader so I can fix it?

Thanks in advance,

Sylvain

SylvainAON3D:
How can I know which bootloader is installed on my board

The easy way to be sure of which is installed is to install it yourself. Then there's no doubt.

SylvainAON3D:
Does someone know where can I found the bootloader.hex for the AZTEEG X3 PRO board

Are you using a 3rd party hardware package to add support to the Arduino IDE for the AZTEEG X3 PRO, or do you just use it as if it was an Arduino Mega 2560?

SylvainAON3D:
or simply for ATmega 2560?

Here's the one used for the Arduino Mega 2560:

SylvainAON3D:
Does the MCUSR always 0 is a known issue and if there is a fix available (or bootloader version fixing it)?

Most likely. There has been a lot of discussion of this issue in the optiboot repository, which you can find by following the links off of:

It looks like that has been solved to some extent in the latest version of optiboot. My quick experiments indicate optiboot is an improvement over the standard STK500V2 bootloader used on the Arduino Mega 2560, but it still doesn't preserve WDRF.

If you want to give optiboot a try, you'll be happy to know that the latest version of optiboot also supports the ATmega2560. The easiest way to put optiboot on your board is via MegaCore:

As for WDRF, I use this library to detect watchdog resets and even to be able to determine the program address where the reset occurred:

Thanks for the quick reply, I will look into this and come back to you.

pert:
Are you using a 3rd party hardware package to add support to the Arduino IDE for the AZTEEG X3 PRO, or do you just use it as if it was an Arduino Mega 2560?

We are using the bootloader as shipped with the AZTEEG X3 PRO board. We do not for now update the bootloader.

pert:
The easy way to be sure of which is installed is to install it yourself. Then there's no doubt.

I tried the following avrdude command to verify if the bootloader found at ArduinoCore-avr/bootloaders/stk500v2 at master · arduino/ArduinoCore-avr · GitHub was the same as on the board but it does not seem to support boot for 2560:

sudo avrdude -p m2560 -b 115200 -c avrisp2 -D -P /dev/ttyUSB0 -U boot:v:stk500boot_v2_mega2560.hex -V

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e9801 (probably m2560)
"boot" memory type not defined for part "ATmega2560"

avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF)

avrdude done.  Thank you.

Also note that we are not using the Arduino IDE.

SylvainAON3D:
Also note that we are not using the Arduino IDE.

OK, well I think all the information I provided in my previous reply still applies, except for the MegaCore installation and usage. You can still get the bootloaders from the MegaCore repository:

Optiboot fits in a 1 kB boot section, so you'll want to adjust your fuses accordingly.

I look at the fix in optiboot and it is simply to pass MCUSR value in parameter to app_start(), so as we are using the stk500boot, we have decided that it was quicker to keep using it.

First step it to compile the stk500boot_v2_mega2560.hex file. I successfully done it but have a footprint difference with the stk500boot_v2_mega2560.hex provided:

/source/ArduinoCore-avr/bootloaders/stk500v2$ make mega2560
-rw-r--r-- 1 sylvain sylvain  16976 Apr 18 11:02 stk500boot_v2_mega2560.hex
-rw-r--r-- 1 sylvain sylvain  21017 Apr 18 11:02 stk500boot_v2_mega2560.hex.old

Even using the source provided by Arduino result in a smaller hex file:

/usr/share/arduino/hardware/arduino/bootloaders/stk500v2$ sudo make mega2560
-rw-r--r-- 1 root root  16743 Apr 18 10:49 stk500boot_v2_mega2560.hex

Is it normal that my hex file is ~5K smaller then the provided one? Am I missing compile flags or Makefile parameters?

This is the compile output:

-------- begin --------
avr-gcc (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Compiling: stk500boot.c
avr-gcc -c -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=16000000UL  -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -fno-jump-tables -Wall -Wstrict-prototypes -Wa,-adhlns=stk500boot.lst  -std=gnu99 -D_MEGA_BOARD_ -D_MEGA_BOARD_ stk500boot.c -o stk500boot.o 

Linking: stk500boot.elf
avr-gcc -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=16000000UL  -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -fno-jump-tables -Wall -Wstrict-prototypes -Wa,-adhlns=stk500boot.o  -std=gnu99 -D_MEGA_BOARD_ -D_MEGA_BOARD_ stk500boot.o --output stk500boot.elf -Wl,-Map=stk500boot.map,--cref    -lm -Wl,--section-start=.text=3E000

Creating load file for Flash: stk500boot.hex
avr-objcopy -O ihex -R .eeprom stk500boot.elf stk500boot.hex

Creating load file for EEPROM: stk500boot.eep
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex stk500boot.elf stk500boot.eep
avr-objcopy: --change-section-lma .eeprom=0x0000000000000000 never used

Creating Extended Listing: stk500boot.lss
avr-objdump -h -S stk500boot.elf > stk500boot.lss

Creating Symbol Table: stk500boot.sym
avr-nm -n stk500boot.elf > stk500boot.sym

Size after:
AVR Memory Usage
----------------
Device: atmega2560

Program:    5928 bytes (2.3% Full)
(.text + .data + .bootloader)

Data:         18 bytes (0.2% Full)
(.data + .bss + .noinit)



-------- end --------

mv stk500boot.hex stk500boot_v2_mega2560.hex

Second step, it to flash it I tried two commands but failed to succeed:

  1. Using the hex file that I made, update fails:
sudo avrdude -p m2560 -b 115200 -c stk500v2 -D -P /dev/ttyUSB0 -U flash:w:stk500boot_v2_mega2560.hex -V

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "stk500boot_v2_mega2560.hex"
avrdude: input file stk500boot_v2_mega2560.hex auto detected as Intel Hex
avrdude: writing flash (259880 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 259880 bytes of flash written
avrdude: verifying flash memory against stk500boot_v2_mega2560.hex:
avrdude: load data flash data from input file stk500boot_v2_mega2560.hex:
avrdude: input file stk500boot_v2_mega2560.hex auto detected as Intel Hex
avrdude: input file stk500boot_v2_mega2560.hex contains 259880 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x3e0e4
         0x41 != 0x2a
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF)

avrdude done.  Thank you.
  1. Done it using the elf, as I saw a post that was suggesting it, but still fails:
sudo avrdude -p m2560 -b 115200 -c stk500v2 -D -P /dev/ttyUSB0 -U flash:w:stk500boot.elf -V

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "stk500boot.elf"
avrdude: input file stk500boot.elf auto detected as ELF
avrdude: writing flash (259880 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 259880 bytes of flash written
avrdude: verifying flash memory against stk500boot.elf:
avrdude: load data flash data from input file stk500boot.elf:
avrdude: input file stk500boot.elf auto detected as ELF
avrdude: input file stk500boot.elf contains 259880 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x3e0e4
         0x41 != 0x2a
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF)

avrdude done.  Thank you.
  1. Update it successfully using the 21KB hex file, so I can conclude that a missing something in the Makefile configuration:
sudo avrdude -p m2560 -b 115200 -c stk500v2 -D -P /dev/ttyUSB0 -U flash:w:stk500boot_v2_mega2560.hex -V

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "stk500boot_v2_mega2560.hex"
avrdude: input file stk500boot_v2_mega2560.hex auto detected as Intel Hex
avrdude: writing flash (261406 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 261406 bytes of flash written
avrdude: verifying flash memory against stk500boot_v2_mega2560.hex:
avrdude: load data flash data from input file stk500boot_v2_mega2560.hex:
avrdude: input file stk500boot_v2_mega2560.hex auto detected as Intel Hex
avrdude: input file stk500boot_v2_mega2560.hex contains 261406 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 261406 bytes of flash verified

avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF)

avrdude done.  Thank you.

Last step will be to do the following modification in stk500boot.c and flash the new version:

527c527
< void (*app_start)(void) = 0x0000;
---
> void (*app_start)(uint8_t mcuStatusReg) = 0x0000;
575c575
< 		app_start();
---
> 		app_start(mcuStatusReg);

I just compiled the unmodified STK500V2 bootloader and got a similar size discrepancy. You need to understand that the original .hex file was compiled many years ago using a much older version of avr-gcc. It's not surprising there is some difference. I flashed the newly compiled bootloader to my Arduino Mega 2560 and I am able to upload with no problem. So I suspect the problem is related to your modifications. Try compiling the unmodified bootloader source and flash it to your board to see whether you can upload with that bootloader.

I did not do any modification prior to updating the bootloader, I want to first be able to flash an unmodified version.

So it seems as the boorloader is probably protected on the AZTEEG X3 PRO board. I will investigate this.

Can this be related to FUSE settings?

SylvainAON3D:
Can this be related to FUSE settings?

Possibly. It's worth making sure the BOOTSZ and BOOTRST fuses are correctly set. You can see the Arduino Mega 2560's fuse values here:

lfuse: 0xFF
hfuse: 0xD8
efuse: 0xFD
lock: 0x0F

Hi Pert,

Thanks for your support.

My lfuse, hfuse and efuse seem to be correctly set:

avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF)

I tried to only set the lock bit but the verification also failed:

sudo avrdude -p m2560 -b 115200 -c stk500v2 -D -P /dev/ttyUSB0 -Ulock:w:0x3F:m -V

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "0x3F"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.03s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x3F:
avrdude: load data lock data from input file 0x3F:
avrdude: input file 0x3F contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0xcf != 0x3f
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF)

avrdude done.  Thank you.

I tried both 0x0F and 0x3F as mega.bootloader.unlock_bits=0x3F and mega.bootloader.lock_bits=0x0F but the lock is always set to 0xCF.

sudo avrdude -p m2560 -b 115200 -c stk500v2 -D -P /dev/ttyUSB0 -Ulock:w:0x0F:m -V

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "0x0F"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.03s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0F:
avrdude: load data lock data from input file 0x0F:
avrdude: input file 0x0F contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0xcf != 0x0f
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF)

avrdude done.  Thank you.

I did even try to erase the chip but this also fails:

sudo avrdude -v -p m2560 -b 115200 -c stk500v2 -D -P /dev/ttyUSB0 -e

avrdude: Version 6.3
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/etc/avrdude.conf"
         User configuration file is "/home/sylvain/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : stk500v2
         Overriding Baud Rate          : 115200
         AVR Part                      : ATmega2560
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
           flash         65    10   256    0 yes    262144  256   1024  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500V2
         Description     : Atmel STK500 Version 2.x firmware
         Programmer Model: AVRISP
         Hardware Version: 15
         Firmware Version Master : 2.10
         Vtarget         : 0.0 V
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as FD
avrdude: erasing chip
avrdude: stk500v2_command(): command failed

avrdude done.  Thank you.

I did successfully update the bootloader using AVR Studio on Windows. Seems as avrdude is broken on Linux for updating the bootloader as we use it every day for updating the application.