# Using Arduino Mega as Atmega2560 AVR development board with Atmel Studio 7

(updates at end)

Qualifier: I am posting this here and not on the Arduino Stack Exchange because my question is about firmware development and not Arduino development.

Project: I am designing a PCB that will use an ATmega2560 as the CPU.

Why: I am a recently minted BSEE with much more experience and knack for hardware than firmware so I'm challenging myself with the limited resources I have available to better my understanding of uCs and firmware development.

Resources: a bunch of Arduino Megas, some LEDs, jumper wires, Atmel Studio 7 (AS7), Arduino IDE, WinAVR, the internet.

Done so far: I have gone through the ArduinoISP process successfully. These are the steps I followed using the Arduino IDE...

1. Uploaded ArduinoISP sketch to Programmer Mega.
2. Attached 10uF cap between reset & gnd on Programmer Mega.
3. Wired Target Mega ICSP connections to Programmer Mega.
4. Left COMM port set to Programmer Mega.
5. Choose "Arduino as ISP" as programmer and clicked "Burned Bootloader".

Thoughts: I realize now as I was typing this that all I did was re-burn the Arduino Bootloader onto the Target Mega using the "Arduino as ISP" Programmer Mega.

Done so Far Again: Written simple "Blink" program in AS7. It builds.

Wanted Outcome: My Target Mega to no longer be an Arduino Mega and be an ATmega2560 development board so that I can do pure AVR firmware development forcing me to do all the hard stuff that Arduino takes care of. I am trying to eliminate everything Arduino from the development process other than the hardware used for firmware development and "Arduino as ISP" for flash programming.

Research: Everything I have found online has to do with using AS7 to code an "Arduino" AVR uC.

Questions:

1. Can I used the "Arduino as ISP" Mega programmer to flash an ATMega2560 on an Arduino Mega without the Arduino Bootloader on said ATMega2560?
2. Is AVR-GCC what I'll use to generate the binary file from the C source code that I will then flash?
3. Is my "Blink" example above using anything "Arduino"?
4. Am I correct in my thinking that if I can get my Arduino Mega to not be recognized by the Arduino IDE but still successfully flash code to it and control it I've achieved my "wanted outcome"?

UPDATE: I was having issues setting up AVRDude as an External Tool on AS7. I followed all the directions I could find carefully and the output would just be blank. I figured out it was because I had the Arduino IDE installed as a Windows App so AS7 could not follow the AVRdude.exe path. I'm now having issues with opening AVRdude.conf but ecstatic I at least had one small success.

UPDATE2: Had the path wrong for AVRdude.conf. Using the Arduino IDE with "verbose output for upload" selected I uploaded blank code and the top line of the output message contained the paths I needed.

UPDATE3: I was getting the classic stk500_getsync():not in sync error and it was a baud rate issue. I had 115200 which I'm pretty sure is the baud rate that Arduino IDE uses to upload a sketch to a Mega but the bootloader is burned at a 19200 baud rate. Since I'm using "Arduino as ISP" it's burning the entire flash memory not just uploading the source code. (note:I was still having issues syncing even after changing the baud rate and it was because the decoupling cap connections on the reset pin of the Programmer Mega were sketchy. Bent the ends of the legs so it would fit more snuggly in the Mega headers.)

UPDATE4: My ArduinoISP programmer runs but my target still isn't blinking. Studied the AVRdude output and saw that it could not find the main.hex file. I looked in the project directory of Atmel studio and there is no main.hex file. In the external tool argument had to change ItemFileName.hex to TargetName.hex. So upload successful but my target still doesn't blink.

UPDATE5: IT BLINKS!!!!! So I gave up on my initial pursuit and attempted to just make one Arduino Mega blink using the other as an ISP programmer. I noticed that AVRdude reads the fuse values before it writes to them so I took an Arduino Mega that I knew was still configured as an Arduino and uploaded my blink program to it. It didn't start blinking, but I took the fuse values it read and included those in my external tool argument. Re-uploaded and huzzah my mega blinks. This is my argument (also found this great link that helps to understand everything in the argument)

-U lfuse:w:0xFF:m -U hfuse:w:0xD8:m -U efuse:w:0xFD:m -e -v -patmega2560 -carduino -PCOM11 -b19200 -D -Uflash:w:"$(ProjectDir)Debug\$(TargetName).hex":i -C"C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

Next Steps: Figure out how to get an active debugging session in AS7 with on-chip debugger of ATmega2560 (heading to datasheet now)

• You'll likely generate code using Atmel Studio for the AVR, the documentation for that will have many answers you seek. You can use an Arduino as an ISP, see instructables.com/id/Integrate-ArduinoISP-and-Atmel-Studio but USBasp programmers are available for a few bucks too. Mar 5 '19 at 16:46
• Questions do not get edited when resolved, that is what accepting an answer does. Mar 18 '19 at 12:47
• Again, the rules are that edits are not used to indicate a question has been resolved. Nor may you change the focus of a question after accepting an answer. Mar 18 '19 at 13:01
• No, you may not change what you are asking. That is both unfair to those who answered and contrary to the requirement of specificity. You're trying to treat this like a forum or wiki, while stack exchange is only for singular specific questions, not evolving projects. You should reaccept the answer to your original question. Mar 18 '19 at 13:09
• @ChrisStratton ok thanks for clarifying and sorry. I get know that I was trying to turn my question into a psuedo-Instructable, which I get now isn't ok. I humbly receive the -1 as repentance. Mar 18 '19 at 13:30

## 2 Answers

1. You definately can. I have never done this myself, but Arduino-ISP is supported by AVRDude. Have a look at this question for further details. Even the correct commandline is mentioned there.

2. Yes, you can do that of course. Personally, I even prefer the GNU toolchain (in which avr-gcc is included) over Atmel Studio. If you choose to call the compiler manually, you will probably want to use a Makefile as well. There are examples in the WinAVR folder.

3. Your example does not use any Arduino source code or function calls. It seems perfectly fine exept for this one line: PORTB &= (1 << PB7). It should be like this: PORTB &= ~(1 << PB7). I think you should be able to figure out why.
Since somebody mentioned the predefined symbols and libraries: The libraries you use are not Arduino-specific, but commonly used for AVRs in general. They define the addresses of processor registers and other useful stuff. Since Arduino is built on top of these same libraries, it might be a little confusing to figure this out, but you can test yourself: If your code compiles fine with avg-gcc or in Atmel Studio and you did not explicitly include #include <Arduino.h>, you did it "the hard way" without the help of Arduino.

4. I think, your Arduino Mega will always be detected as such by your computer, since the USB communication is done by a separate chip on the Arduino board. However, if you are able to compile and upload the code above to the ATmega2560 outside the Arduino IDE and it runs as expected, you can be shure that you successfully eliminated Arduino.

Some sidenotes:

• AVR development WITHOUT Atmel Studio seems to be easier on a Linux machine, but is not impossible in Windows. If you decide to use Atmel Studio, you are bound to Windows, but don't need to handle the build process completely on your own.
• A dedicated programmer makes things easier, but your Arduino-ISP solution works as well.
• You might want to try out some other AVRs on a breadboard as well. That way, you will see the hardware differences between a pure AVR and an Arduino board easily. I would recommend an ATmega328p, similar to the one used by the Arduino Uno.
• Referencing answer to 3: 1&1=1 (what am i doing with my life) 1&0=0 (hello darkness my old friend) thanks! Mar 6 '19 at 13:19

Once you have the .hex file created, you can upload it via the SPI pins (SCK, MOSI, MISO, Reset, Power, Gnd) using a Programmer. A Programmer takes Reset low and then accesses memory & fuses more or less directly. The fuses need to be set up to match the harware environment for clock source for one, and for the starting address to use for another thing (software only, bootloader + software, bootloader size).