Arduino Mega 2560

Support for the Arduino Mega 2560 board. More...

Detailed Description

Support for the Arduino Mega 2560 board.

Overview

The Arduino Mega2560 is one of the larger Arduino boards. It is based on Atmel's AVR architecture and sports an ATmega2560 MCU. It is like many Arduinos extensible by using shields.

NOTE: In case you are wondering if flashing RIOT on your Arduino Mega2560 will overwrite the Arduino bootloader: you can be reassured. After flashing RIOT you can without any intermediate steps just go over to flashing Arduino sketches again.

Hardware

Arduino Mega2560

MCU

MCU ATmega2560
Family AVR/ATmega
Vendor Atmel
RAM 8Kb
Flash 256Kb
Frequency 16MHz
Timers 6 (2x 8bit, 4x 16bit)
ADCs 14 analog input pins (10bit resolution
UARTs 4
SPIs 1
I2Cs 1 (called TWI)
Vcc 5.0V
Datasheet / Reference Manual Datasheet and Reference Manual
Board Manual Board Manual

Flashing RIOT on the Arduino Mega2560 is quite straight forward, just connect your Arduino Mega2560 using the programming port to your host computer and type:

make BOARD=arduino-mega2560 flash

This should take care of everything!

We use the open avrdude tool to write the new code into the ATmega2560's flash

State

While there is basic support in RIOT, there are still some parts missing: Timer implementation needs love (ideally simulate a 32bit timer by adding an overflow counter to the implementation) LPM driver missing ~~SPI driver missing~~ (See https://github.com/RIOT-OS/RIOT/pull/4045) I2C/TWI driver missing ADC driver missing PWM driver missing

Debugging (WIP)

The ATmega2560 MCU supports JTAG debugging. To use the JTAG debugging on the Arduino Mega 2560 an external JTAG debugger is required. There are several options for this MCU/board: AVR JTAGICE mkII JTAGICE3 AVR Dragon

There may be other options as well, but I can't comment on how well they work. I tested debugging RIOT on the Arduino Mega 2560 using an AVR Dragon.

Important:** To use a JTAG Debugger in conjunction with the ATmega2560 it is required to change the fuses of the MCU. Additionally it seems to be required to overwrite the bootloader on the MCU. Because of that it is a necessity to use an ISP (in system programmer) to do the debugging. This isn't an issue because all of the afore mentioned devices have ISP capabilities, but it requires some additional steps to get back normal operation after debugging: flash a new arduino bootloader on the device, e.g. [this one](https://raw. githubusercontent.com/arduino/Arduino/master/hardware/arduino/bootloaders/stk500 v2/stk500boot_v2_mega2560.hex) restore the fuses to the default state.

Wiring for the AVR Dragon

Wiring for dubugging

In contrast to normal use (USB only), for debugging there are two separate wiring changes to do: connecting the ISP headers (picture: orange cables). connecting the JTAG header to the respective pins on the Arduino (picture: blue cables)

Connecting the ISP headers is straight forward: Pin1 on the Dragon connects to Pin1 on the Arduino Mega2560 and so on. Connecting the JTAG header needs the following pin mapping:

AVR Dragon Arduino Mega 2560Signal
JTAG1 A4 TCK
JTAG2 o. 10GND GND
JTAG3 A6 TDO
JTAG4 +5V +5V
JTAG5 A5 TMS
JTAG9 A7 TDI

Ax refers to the analog in pins on the Arduino Mega 2560. JTAG2 and JTAG10 on the AVR Dragon are both GND, one connection suffices. All other JTAG Pins are not needed for debugging the Arduino Mega2560

Additional information can be found here or here.

Fuses

default:**

Fuse Setting
Low Fuse 0xFF
High Fuse 0xD8
Extended Fuse 0xFD

avrdude arguments: -U lfuse:w:0xff:m -U hfuse:w:0xD8:m -U efuse:w:0xfd:m

debugging:**

Fuse Setting
Low Fuse 0xFF
High Fuse 0x18
Extended Fuse 0xFD

(Both OCDEN and JTAGEN fuse bits are enabled)

avrdude arguments: -U lfuse:w:0xff:m -U hfuse:w:0x18:m -U efuse:w:0xfd:m

A useful tool to calculate fuse settings yourself is this fuse calculator, which also works with other AVR MCUs.

Debugging RIOT on the Arduino Mega 2560

With PR #1696 merged the following commands should work for debugging:

make BOARD=arduino-mega2560 debug-server: starts an avarice (avarice needs to be installed) server that avr-gdb can connect to.

make BOARD=arduino-mega2560 debug: starts an avarice server and connects avr-gdb to it.

Note:** To flash the board via the ISP while debugging the additional flag PROGRAMMER=dragon_isp is required. For a full rebuild and debug cycle use the following command:

make BOARD=arduino-mega2560 PROGRAMMER=dragon_isp clean all flash debug

Mac OSX El Capitan users

Mac users can flash this Arduino board by installing avr-gcc and avrdude from brew. Debug is possible but is not covered in this wiki, if you need it, please refer to this page.

Toolchain installation

You should have installed brew to follow this instructions. Otherwise, install it.

Then, add a repository:

$ brew tap osx-cross/avr

Afterwards, install avr-gcc:

$ brew install avr-libc

And finally:

$ brew install avrdude --with-usb

With this you should be allowed to compile and to flash code to the Arduino Mega.

Troubleshooting for serial connection

In OSX El Capitan, there is no native driver working for the serial connection.

In order to install it, you must download and install a CDC-ACM driver from here (Go to Resources/Software & Driver/Mac Software).

A reboot should be enough to find your Arduino on /dev/tty.usbmodem*

Files

file  arduino-mega2560/include/board.h
 Board specific definitions for the Arduino Mega 2560 board.