Professional Documents
Culture Documents
http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio-6-ide-w...
Directories
Add two entries to locate the source files for the Arduino core and be sure to un-check the relative path option. These are to be found
wherever you installed the Arduino IDE. For me, and for the Uno, they are:
C:\Program Files\Arduino\hardware\arduino\cores\arduino
C:\Program Files\Arduino\hardware\arduino\variants\standard
The second of these would have leonardo instead of standard for the Leonardo board.
If you use any of the Arduno libraries, you must add additional entries to Directories for each one: e.g. C:\Program Files\Arduino
\libraries\EEPROM
Optimisation
Set the compiler to optimise for size and check the -ffunction-sections option.
In addition, under AVR/GNU Linker, set the optimisation to garbage collect unused sections.
Symbols
Add two entries (these are applicable for Uno and Leonardo; the first denotes Arduno 1.0 libraries and the second denotes a 16MHz
clock):
ARDUINO=100
F_CPU=16000000L
sketch.cpp
If you already have an arduino sketch (.ino or .pde) then the content of it may just be copied and renamed sketch.cpp.
There are, however, two small extras that may be required at the top of the sketch:
1 of 8
10/27/2014 8:02 PM
Adam @ Hilltop Cottage Using Atmel Studio 6 IDE with Arduino (U...
http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio-6-ide-w...
1. if there are any functions other than void setup() and void loop() then it is necessary to add a function prototype. This means that if
there is a function void serialMessage() then you must add void serialMessage(); at the head of sketch.cpp in addition to the
function itself. Google arduino function prototype to find out more.
2. if a library such as EEPROM is used then it may be necessary to change the #include to point to the .cpp file rather than the .h file.
If EEPROM.h does NOT contain a #include <EEPROM.cpp> then you need to point to EEPROM.cpp from the sketch otherwise
the compiler will not find the definition of EEPROM. Wire and SD libraries are even more tedious in that there are 1 or more
additional files to include check the error messages, add the #include and if necessary also add a new directory in the C++
Compiler options.
Keep these extras together and above the body of the sketch with an appropriate comment line if you intend to share the code (or just
want to be neat, keep sane).
As an alternative to adding prototypes manually, it is possible to compile and upload in the Arduino IDE and then to grab the C++ code
that the IDE creates during its compile process. To find where this is, go to File|Preferences and set the Arduino IDE to Show verbose
output during: [x] compilation. This will cause the temporary directory containing the C++, all compiled intermediates and the .hex file
used by the uploader to be revealed. The .cpp should be the same as the sketch but with a few extra lines near the top of the listing.
Replace the template sketch.cpp with this.
main.cpp
This is really just a combination of two of Elco Jacobs files with a few edits. Since things are a little different for Uno and Leonardo, the
main.cpp file differs between the two versions. Remember that the end point is a separate template for each board so two versions are
created rather than having to comment out or uncomment code blocks according to the board in use.
NB: You may have to uncomment or add one or mode .cpp or .h files from the Arduino core libraries. See the "//Unused source
files" but also be aware there may be some not listed in that section.
main.cpp for Uno
1 #define ARDUINO_MAIN
2
3 // Disable some warnings for the Arduino files
4 #pragma GCC diagnostic push
5 #pragma GCC diagnostic ignored "-Wsign-compare"
6 #pragma GCC diagnostic ignored "-Wattributes"
7 #pragma GCC diagnostic ignored "-Wunused-variable"
8 #pragma GCC diagnostic ignored "-Wuninitialized"
9
10 #include <Arduino.h>
11 extern "C"{
#include <pins_arduino.h>
12
13 }
14
15
16
17
18 // Standard Arduino source files for serial:
19 #include <HardwareSerial.cpp>
20
21 // Other source files, depends on your program which you need
22 #include <Print.cpp>
23 #include <New.cpp>
24 #include <wiring.c>
25 #include <wiring_digital.c>
26 #include <wiring_analog.c> //analog read/write functions
27 #include <WString.cpp>
28 #include <WMath.cpp>
29 #include <Stream.cpp>
30
31 // Unused source files:
32 //#include <WInterrupts.c>
33 //#include <wiring_pulse.c>
34 //#include <wiring_shift.c>
35 //#include <IPAddress.cpp>
36 //#include <Tone.cpp>
37
38 // Restore original warnings configuration
39 #pragma GCC diagnostic pop
40
41
42
43
44 int main(void)
45 {
46
init();
47
48
setup();
49
50
for (;;) {
51
loop();
52
if (serialEventRun) serialEventRun();
53
}
54
return 0;
55 }
2 of 8
10/27/2014 8:02 PM
Adam @ Hilltop Cottage Using Atmel Studio 6 IDE with Arduino (U...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma
#pragma
#pragma
#pragma
GCC
GCC
GCC
GCC
diagnostic
diagnostic
diagnostic
diagnostic
ignored
ignored
ignored
ignored
http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio-6-ide-w...
"-Wsign-compare"
"-Wattributes"
"-Wunused-variable"
"-Wuninitialized"
#include <Arduino.h>
extern "C"{
#include <pins_arduino.h>
}
int main(void)
{
init();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
Try it!
Given the above, it should be possible to cut and paste the blink sketch into sketch.cpp and compile using F7 or the Build menu.
2 uploading
Uploading can be a bit of a pig. The smooth way the Arduino IDE works is, for me, its main redeeming feature. There are several options
and I began by using the rather nice MegunoLink tool (which may be downloaded for free and a donation made). MegunoLink allows you
to locate the .hex file that is created after compilation and to upload it with ease. It uses avrdude behind the scenes, just as the Arduino
IDE does. MegunoLink also includes a rather nice plotting feature, where you can send formatted data over Serial and plot it in real time.
Nice! The MegunoLink site also gives an alternative recipe for using AtmelStudio alongside MegunoLink.
For better integration of avrdude with Atmel Studio, you have to do a bit of fiddling. A lot more fiddling was required to get a usable
approach for the Leonardo. The end point is upload over the normal USB connection by the click of a mouse or keyboard shortcut inside
Atmel Studio.
3 of 8
10/27/2014 8:02 PM
Adam @ Hilltop Cottage Using Atmel Studio 6 IDE with Arduino (U...
http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio-6-ide-w...
A new external tool should be accessible from the Tools menu on saving this data. In use it is essential to first select (click on) the project
in the Project Explorer window. This is so that the $(ItemFilenName) is correct. If you have sketch.cpp selected then Atmel Studio tries to
invoke avrdude to upload sketch.hex, which does not exist.
The idea is to call this bit of python code as an External Tool, similar to the way avrdude was called in the Uno example. You will need to
install Python and pySerial. You may also need to change the PATH to include the directory into which Python was installed. I put the
Serial Uploader.py in the Atmel Studio solutions directory.
This time set up the external tool like this:
Command = python.exe
Arguments = C:\Documents and Settings\Adam\My Documents\Atmel Studio\Serial Uploader.py C:\Program Files\Arduino
4 of 8
10/27/2014 8:02 PM
Adam @ Hilltop Cottage Using Atmel Studio 6 IDE with Arduino (U...
http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio-6-ide-w...
Licence etc
Elco made his code available under GPL v3 and you should consider the source code given above to be distrubuted under the same terms
since it is a derivative of his work.
* Copyright 2012 Adam Cooper, based heavily on the work of Elco Jacobs.
* See http://www.elcojacobs.com/easy-to-use-atmel-studio-project-for-arduino-and-programming-the-arduinofrom-python/
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See: GNU General Public License
This was written by Adam. Posted on Saturday, December 1, 2012, at 9:23 pm.
Filed under Arduino, Microcontrollers. Bookmark the permalink. Follow
comments here with the RSS feed. Post a comment or leave a trackback.
14 Comments
1.
2.
Adam wrote:
@Elco no I did not fork it. I should have done but I wasnt thinking properly!
Adam
Saturday, December 1, 2012 at 10:04 pm | Permalink
5 of 8
10/27/2014 8:02 PM
Adam @ Hilltop Cottage Using Atmel Studio 6 IDE with Arduino (U...
http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio-6-ide-w...
Pete wrote:
3.
Hey this is a pretty good walkthrough, Im struggling with step 1b I think. I am struggling to see how to fit the sketch harness into
the main program. I tried building the main program (using leonardo) but get undefined reference to setup/loop error. Is there
perhaps an example of how you include sketch.cpp or you could possibly give a bit more guidance at these steps. Thank you
Wednesday, December 26, 2012 at 1:20 pm | Permalink
4.
Adam wrote:
@Pete
OK Ill have a go tho its always hard to diagnose problems remotely and I might have got the wrong end of the stick!
To add sketch.cpp (or any number of supplementary files, for that matter) you can use the menu: Project | Add New Item . Then
choose CPP file and give it a name. You can also get the same result other ways.
You should also be able to just paste the same sketch code into the end of main.cpp instead of having a separate sketch.cpp
(remembering the notes about function prototypes).
Cheers, Adam
Wednesday, December 26, 2012 at 9:00 pm | Permalink
Richard Waterman wrote:
5.
Only had Arduino a few days and hate its IDE. Ive programmed in other languages and miss their editors. The python script is just
used for uploading? You can still program with regular example code of Arduino? (other than the function prototype) I was using a
sikuli library in java and would want to continue to use it here. Should be no problem? And also what about Processing? Can this be
done for that as well? I really cant see how people like these editors lol.
Saturday, February 23, 2013 at 12:58 am | Permalink
Adam wrote:
6.
Yes, the python is just for uploading. There are other approaches you can use to upload too: have a look at Megunolink. Yes, regular
arduino code will work so long as you add the function prototypes and make sure the libraries are referenced. This is dependent on
the compiler and linker rather than the IDE per se. As for the rest, I have no idea
Adam
Saturday, February 23, 2013 at 5:44 pm | Permalink
7.
8.
Antonello wrote:
With the blink sketch Ive received the following errors:
OUTPUT was not declared in this scope
pinMode was not declared in this scope
HIGH was not declared in this scope
digitalWrite was not declared in this scope
delay was not declared in this scope
LOW was not declared in this scope
Then Ive include the Arduino.h inside sketch.cpp and everything worked.
Is that normal ?
Tuesday, June 18, 2013 at 9:22 am | Permalink
9.
Adam wrote:
Yes, you do need to include the Arduino libraries; these functions and constants are not considered standard by the compiler. This
is the case for all of the functions documented on the Arduino Language Reference page http://arduino.cc/en/Reference
/HomePage.
Thursday, June 20, 2013 at 1:59 pm | Permalink
10.
6 of 8
10/27/2014 8:02 PM
Adam @ Hilltop Cottage Using Atmel Studio 6 IDE with Arduino (U...
http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio-6-ide-w...
Adam wrote:
Chris Im a bit hazy about the workings of avrdude; I follow standard recipes.
In your case, it looks like the baud rate might be wrong. I think you should be using 115200 baud. See the section Setting up for
Uno on my post.
I think when you upload from the Arduino IDE it picks this up from a boards.txt (somewhere inside C:\Program Files\Arduino)
otherwise getting default values from C:\avrdude.conf if it exists.
Sometimes Windows messes up COM ports. Try a reboot, check port assignments etc if the above does not get you there.
hth, Adam
Thursday, January 23, 2014 at 9:29 pm | Permalink
12.
7 of 8
10/27/2014 8:02 PM
Adam @ Hilltop Cottage Using Atmel Studio 6 IDE with Arduino (U...
http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio-6-ide-w...
13.
Hmmm
well, one thing I failed to notice at first was that you are not using an Uno, so my comments on baud rate were wrong.
Maybe try to run avrdude from a command line (DOS Window) and see if that works.
This looks a bit odd: CANNOT Find output file C:\Users\C\Documents\Atmel Studio\6.1\P1BLINKAM2\P1BLINKAM2
\scripts\output.txt Aborting script
It might be a symptom of anti-virus software getting in the way. Ive certainly had problems with compilation due to my AV. Try
disabling it and see if things improve.
Adam
Saturday, February 1, 2014 at 5:49 pm | Permalink
14.
4 Trackbacks/Pingbacks
1. Adam @ Hilltop Cottage Simulating Arduino Code using Atmel Studio 6 on Saturday, May 11, 2013 at 2:20 pm
[...] This starts off as just a case of compiling the code in Atmel Studio so you can set break-points or step line-by-line through the
code to see what happens. See an earlier post of mine for how to get an Arduno sketch to become an Atmel Studio 6 project. [...]
2. Adam @ Hilltop Cottage Debugging Arduino using debugWire (+ Atmel Studio and an AVR Dragon) on Saturday, May 11, 2013
at 4:14 pm
[...] is basically just a case of compiling the code for the Arduino in Atmel Studio as Ive previously described. It is NOT necessary
to upload the compiled code using avrdude because Atmel Studio will do this [...]
3. How to: Step by step guide to setting up Atmel Studio (AVR Studio 6) for Arduino | aSensar on Sunday, June 23, 2013 at 3:23 pm
[...] Using Atmel Studio 6 IDE with Arduino (Uno and Leonardo) http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio6-ide-with-arduino-uno-and-leonardo/ [...]
4. Step by step guide to setting up Atmel Studio for Arduino development - Inspired by Nature on Monday, July 15, 2013 at 5:38 am
[...] Using Atmel Studio 6 IDE with Arduino (Uno and Leonardo) http://www.hilltop-cottage.info/blogs/adam/using-atmel-studio6-ide-with-arduino-uno-and-leonardo/ [...]
8 of 8
10/27/2014 8:02 PM