Help create a how-to guide: Upload program written for Arduino to ATmega328P

Ok, this seems to be a very common problem and I have not found a good resource for a solution so I'm hoping by compiling all my research and with the help of the following posts we can make some sort of guide for future users. I noticed similar post whose title describes my issue but doesn't explain much here ( http://arduino.cc/forum/index.php/topic,82085.0.html )

Scenario: People like programming in the Arduino / Wiring language. There are built in libraries and users don't have to worry about all the details of setting up timers and other non-trivial tasks. Also, being able to abstract individual pins from ports is nice way to think for newcomers. So for people not worried about maximum efficiency this style of coding is very nice; however, they still recognize that it's very wasteful to keep buying a whole new Arduino board for each project. They want to code in the Arduino language and then port that code to a basic ATmega328 or equivalent chip on their own custom board, but most are unsure how to do this including myself.

Problem Statement: People do not know how to get the 'fancy' Arduino code onto a generic ATmega328 chip.

In my case I thought "ok it's time to step up to the big leagues and get an ISP programmer so I can be a real microcontroller dev. I chose the AVR MKII and got AVR Studio 5 installed and had a Blink sketch from Sparkfun all working and was ready to go. I quickly found out that it is not simple to port your Arduino project to an AVR Studio project. All of the core files in Arduino which make development easy do not exist by default in a basic AVR project and including the whole arduino directory seemed like a mess so here I am wondering how I can get code that I know works on an UNO/Deumillenove and get it on a 328P using MKII.

Now this problem causes people to be opened up to whole set of other problems. I'm taking step back away from my personal story a bit to maybe help some others who have a slightly different case.

There are two most common ways to get code onto a microcontroller: 1. Through standard USB to Serial converter ( This is standard on Arduino boards ) 2. Using AVR ISP MKII

Background knowledge about each transfer method: 1. Using USB-Serial This involves extra hardware on the custom board and is less common for barebones microcontroller programming or custom projects which don't need USB. As far as I know this would work ok with the Arduino IDE however you just have to make sure you setup the Auto-reset correctly otherwise you will always have to reset your board just before uploading. ( I found this out the hard way when trying to use Eclipse to upload code. Even when using an actual UNO, Eclipse does not have the reset functionality built in like Arduino IDE has and you have to manually press the reset button which is quite annoying. )

2. Using AVR ISP MKII This is my particular case and although I did a bit more research about this method I still can not get it to work. Most people who have the AVR ISP MKII also have AVR Studio installed and this brings up another possible problem. Documented briefly here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1228502992 AVR Studio has it's own utility / Jungo driver to work with the ISP MKII and Arduino IDE uses avrdude and libusb and since only one of these drivers can be installed at one time you either have to only use the MKII with AVR Studio or only use it with Arduino IDE.

So within this method of programming we have two more possibilities of Arduino IDE or AVR Studio.

Method 2.a ( Arduino IDE and AVR MK II ): If you're using the Arduino IDE with avrdude / libusb then you should just be able to select Upload with Programmer and it should work. You might want to look at this: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1275160256 which explains how to modify the boards.txt and add another menu with custom settings for programming 328P's with MKII however when I did this I still didn't change the drivers and avrdude could not recognize my MKII usb device so I can't verify either of these solutions, but I believe uninstalling the Jungo driver from AVR Studio and installing the one that comes with the Arduino package would work.

Method 2.b ( AVR Studio with AVR MK II ): I was following this article http://arduino.cc/playground/Learning/Burn168 to use AVR Studio however I do not think it has the whole story. Once you upload the Atmega328 bootloader .hex ( ..\arduino-1.0\hardware\arduino\bootloaders\atmega\ATmegaBOOT_168_atmega328.hex ) and set all the fuses ( 0xF8, 0xDF, 0xFF ) you still need a way to upload an actual program / sketch .hex and this is where I was confused.

From my understanding once you upload the bootloader hex you then change the fuses to control where the memory offset will be for the next hex file so that when you try to upload a .hex from an Arduino sketch it will not over write the bootloader since you've told the microcontroler to skip over that memory region with the fuses.

In summary I tried the following with no success:

1. Developed on Arduino IDE on UNO R2.

Then from my understanding the Arduino IDE goes through the build process ( http://arduino.cc/en/Hacking/BuildProcess ) does a bunch of behind the scene's work to create a .hex file which is basically your sketch with all the includes from the core compiled, linked, and converted down to machine code. * From my understanding this file should be able to run on any similar microcontrollyer namely, the 328P which i'm tyring to use. ) So I just need to get this file upload to the chip.

2. Get the .hex file generated from Arduino IDE and copy it to sketch folder. Apparently the Arduino IDE does a lot of file manipulation under operation and in order to get the .hex file you need to open your sketch and click verify, but DO NOT click save or close the IDE because this will delete it from the temporary location. The .hex file generated will be in %localappdata%/Temp/[sketch_name].hex . ( The %localappdata% is a system variable which goes to the proper directory for Windows 7, it may be in some other Local App Data directory in older versions of windows. ) So I copy this to my sketch folder an then close the Arduino IDE.

3. Open AVR Studio 4. Upload the Arduino Bootloader, set Fuses, and set Lock Bits Again I used ( ..\arduino-1.0\hardware\arduino\bootloaders\atmega\ATmegaBOOT_168_atmega328.hex ) and set fuses and lock bits based off of the 168 section of this article http://arduino.cc/playground/Learning/Burn168

5. Upload the .hex file we copied to our sketch folder. In the same window we jsut used to program the bootloader we just browse to the new .hex file and click "Program"

However these steps didn't work for me and so I have the following questions:

1. Is it correct to assume that the .hex file generated by Arduino IDE can be used on a basic 328P?

2. If I do want to use this .hex file from Arduino to a blank 328P is a Arduino bootloader required to make the .hex file work or should the .hex operate correctly without the bootloader. From my understanding the bootloader was only there to retain serial communication regardless of what program is on the device. If we are using the ISP MKII we don't need USB communication and hence the bootloader so the .hex file form Arduino IDE should work independently. We would just have a little be extra flash memory to work with.

However, I read in another source that the bootloader also sets up timers and other things that are expected to be initialized a certain way by Arduino code so if they are not then perhaps PWM, Tones, and other operations would not function correclty without the bootloader. Hopefully someone can clarify this for people and myself.

3. If we use AVR Studio to program the bootloader should we then uncheck 'Erase device before upload' to upload the next .hex or do the Fuse/Lockbits already restrict what will be erased. My concern is that perhaps the .hex does require the bootloader and that even though the directions I followed were correct, I immediately erased the bootloader before uploading the new 'sketch' .hex and this is why I see no improvment from just uploading the .hex without the bootloader.

4. For someone who has used both methods ( Arduino IDE / ISP MKII OR AVR Studio / ISP MKII ) if I want to write code in the Wiring language but then upload to a blank 328P which is the best method in your opinion?

Sorry for the very long post that probably has an easy answer. I just wanted to be a complete as possible so people dn't have to ask for more information. Also if I mis-stated something let me know and I can edit it for future reference.

Thank you very much!

  • Matt

I think you are making some fundmental mistakes in your analysis of the Arduino.

People like programming in the Arduino / Wiring language.

Arduinos are programmed in the C/C++ language using the gcc compiler. What you are calling the Wiring language are just C and C++ functions that come standard with the Arduino IDE and is core libraries. Things like digitalRead() is just a function that you are free to use or not use as you please. You can program inside the arduino IDE writing all your own low level C or C++ functions or libraries and not use any of the wiring functions if you so desire.

Some have used the AVR studio IDE to program and upload to a arduino board, but It should be for reasons other then the language being used to write the programs, possibly because of the avalible debugger, possibly to drop down to pure assembly language only programming?

So I would ask you what is the objective you have in mind for converting to the AVR studio IDE?

Again there is no 'Wiring language', just C/C++.

Lefty

Well considering Arduino is the most popular and most recommended microcontroller amonst hobbyist and newcomers I think it’s safe to say that people like it. I understand that you don’t need to use digitalWrite. I know that bitwise opperations and such can be done… PORTC |= 1 << 4; I think most people would be fine if they had to do these simple operations without knowing C but it’s the more complex things that allow you to write sophisticated programs on Arduino without known advanced microcontroller programming. Which again is why people enjoy programming for Arduino.

You can program inside the arduino IDE writing all your own low level C or C++ functions or libraries and not use any of the wiring functions if you so desire.

Yes I desire to use these functions and libraries because that’s how i originally wrote the program. For instance my program is using PWM and Tone both of these would have to be rewritten/re-created to work on generic 328P or I would need to include them in my AVR project. Instead of re-creating the whole build process that’s inside the Arduino IDE I just want to take what it does and use AVR Studio to program.

So I would ask you what is the objective you have in mind for converting to the AVR studio IDE?

And the reason I have to use AVR studio to program is because I already have the Jungo driver installed and AVR ISP MKII will not work with Arduino 1.0 IDE as long as that is installed. Since AVR Studio has much more functionality as far as controlling fuses and learning about ISP I thought I would learn to use that to do the programming. I looked around the web for at least 2 hours before writing this and it seems as the Arduino community evolves more and more people are going to be wanting to move to real microcontroller programming and more advanced boards but won’t quite be ready to break away from the coding style which is why I believe this is great skill / technique for people to learn. It is the bridge between being reliant on Arduino IDE and becoming more independent.

Again there is no ‘Wiring language’, just C/C++.

Ok dont think of it as a new language, it’s a new style of programming which abstracts away all the tediousness. I said new language as sort of shortcut, it’s just bad naming. I don’t want people to get caught up on my not writing something in perfect context. The point of the post is to help people learn how to get code compiled with all the standard Arduino includes onto a basic chip most likely through ISP.

There are already 3 other posts similar to this one which we could be helping:

http://arduino.cc/forum/index.php/topic,82085.0.html
http://arduino.cc/forum/index.php/topic,87008.0.html
http://arduino.cc/forum/index.php/topic,58670.0.html

Here are a few more resources I just found from this post:

http://arduino.cc/forum/index.php/topic,80483.0.html

This is a guide for doing a similar process to what I'm asking on an ATmega1284P http://maniacbug.wordpress.com/2011/11/27/arduino-on-atmega1284p-4/

However it says "Uploading a sketch is trivial" and that's the exact part I'm stuck on.

Also there is: http://www.arduino.cc/en/Main/Standalone and http://arduino.cc/en/Tutorial/ArduinoISP

But the top one does not explain how to use AVR Studio to program the bootloader and then sketch.

For now the only solution that seems to have documentation and known success is when using the AVR ISP MKII with Arduino IDE. :(

  • Matt

Ok so I gave up on AVR Studio since no one else seems to know how to use that for uploading sketches properly.

These are the steps i took which I thought would help:

  1. Went to control panel and uninstalled AVR Studio 5, and the AVR Jungo USB Driver.
  2. Downloaded libusb-win32-bin-1.2.5.0
  3. Plugged in my AVR ISP MKII and clicked on \libusb-win32-bin-1.2.5.0\bin\amd64\install-filter-win.exe and installed the filter on AVR MKII device ( amd64 because i'm using 64-bit machine )
  4. Started up Arduino 1.0 IDE and opened the example Blink sketch.
  5. Selected Programmer as AVR ISP MKII
  6. Selected Board as Arduino Deumilanove w/ ATmega328
  7. Selected Tools > Burn Bootloader

I get the same error message I've always getting when using Arduino IDE and AVR ISP MKII 'avrdude: usbdev_open(): did not find any USB device "usb"'

Originally I thought this was due to a conflict with the Jungo driver that AVR Studio uses and was under the impression that I needed to install libusb; however, I did finally uninstall the Jungo and install libusb and it has had no effect.

Any suggestions for myself and others in similar situation?

ok, I have been through this myself and I agree it's not obvious. I can offer you a couple of suggestions: 1) AVR studio will happily burn a sketch that was compiled with the arduino IDE. If when you compile the sketch in the ide you push the shift key while you click the upload button you will see piles of crud in the message window. look hard for the directory where the .hex file is being stored. Fire up avr studio and the programmer dialog and navigate to that directory to select the hex file (I agree that avr studio is great for getting in trouble with fuses) 2) The arduino ide will happily burn a sketch using an isp programmer, you don't need to bother with the boot loader. I hate to throw more links around but I believe I cobbled my setup together using this http://arduino.cc/en/Hacking/Programmer

If you were successfully able to set fuses and upload files from avr studio you might want to go back to that just so you can see what success looks like and build from there.

Good luck!

I posted a guide to porting an Arduino sketch to custom atmega328-based hardware at http://miscsolutions.wordpress.com/2011/08/09/prototyping-small-embedded-projects-with-arduino/ last year. Unfortunately there is now another step involved, because the ArduinoISP sketch doesn't work under Arduino 1.0.

dc42: Unfortunately there is now another step involved, because the ArduinoISP sketch doesn't work under Arduino 1.0.

You can just get download the latest [https://github.com/rsbohn/arduinoisp ArduinoISP] directly. This thing works great. Just don't compile it under a recent Linux distro.

mattm: Well considering Arduino is the most popular and most recommended microcontroller amonst hobbyist and newcomers

Arduino is not a micro-controller. It's a development platform. The micro-controller is an Atmel AVR ATMega168/328p micro-controller.

@maniacbug: The latest ArduinoISP sketch works with Arduino 1.0?

Yup, it's even a dot-ino file.

Did you change the size of the serial receive buffer? Or the baud rate?

No. Seems to work fine out of the box.