Part 1 focused on installing and setting up Atmel Studio 6 for Arduino Boards.

Now Part 2 will tell you how to integrate and use Arduino code with Atmel Studio 6!
There are a lot of things to do, but each one is fast and easy. Just follow the steps!

Step 1: Having gone through Part 1

You especially need to have installed the Terminal Window, and have a working Tool to upload your sketches to Arduino boards. (Part 1 - steps 4 and 5)

Step 2: Compile the Arduino Core

  • Open Arduino IDE


Make sure you have the verbose activated (File -> Preferences and check "Show verbose output during compilation").

  • Open Arduino IDE and compile any example sketch, eg. the classic blink one.
  • In the verbose, you should see something like:


C:UsersxxxxxxxAppDataLocalTempbuild3173545040878149377.tmpBlink.cpp.hex
Binary sketch size: 1026 bytes (of a 30720 byte maximum)

  • In your Arduino, copy the part that corresponds to what I put in blue and go to that folder.

Don't close Arduino IDE yet as it will delete the temporary folder
 

Step 3: Create your Arduino Library in Atmel Studio 6

Import the linker core:

  • Open your working directory for Atmel Studio 6 (by default DocumentsAtmel Studio).
  • Create a new folder, and name it ArduinoCore for example.
  • Go back to the arduino temporary build folder, and locate "core.a".
  • Copy "core.a" to your ArduinoCore Atmel Studio folder.
  • Rename it to "libcore.a".
  • Close Arduino IDE.

Import the libraries' headers:

  • Go to your Arduino installation folder and browse to hardware/arduino/cores/arduino.
  • Select everything and copy it to your ArduinoCore Atmel Studio folder.
  • In the ArduinoCore folder, delete the .c and .cpp files. Only the header files are needed.

Import the Arduino pins configuration:

  • Go back to your Arduino installation folder and browse to hardware/arduino.
  • There is a "boards.txt" text file. You can look at it to know which variant you need for your Arduino board.
  • Now go to the variants folder, open the folder corresponding to your variant and copy the pins_arduino.h header.

I myself use the standard one, as it corresponds to the Arduino Uno and Pro.

  • Paste it in your ArduinoCore Atmel Studio folder.

Step 4: Configure your project in Atmel Studio 6

Create your project

  • Open Atmel Studio
  • Hit File -> New -> Project... and select GCC C++ executable project.
  • Name it, TutorialArduino for example and hit OK.
  • Select your chip, Atmega328P most likely.

Configure your project

  • Go to Project -> TutorialArduino Properties...
  • Go to the Toolchain tab:

AVR/GNU C Compiler

  • Directories


Click add, uncheck "Relative Path", browse to your newly created ArduinoCore folder and hit OK

  • Optimization

    Optimization level: Optimize for size (-Os) (otherwise delay functions won't work correctly)
    Check Prepare functions for garbage collection

AVR/GNU C++ Compiler

  • Directories

    Click add, uncheck "Relative Path", browse to your ArduinoCore folder and hit OK.
  • Optimization

    Optimization level: Optimize for size (-Os) (otherwise delay functions won't work correctly)
    Check Prepare functions for garbage collection.

AVR/GNU C++ Linker

  • Libraries -> Libraries -> add:  enter libcore

Libraries -> Library search path -> add: uncheck "Relative Path", browse to your ArduinoCore folder and hit OK.


 

Optimization: check Garbage collect unused sections.

Save the properties and go back to your TutorialArduino.cpp file

Step 5: Write your Arduino code in Atmel Studio 6!

You're all set! Now write your Arduino code (almost) just like you would in Arduino IDE!
Almost, because you just need to add those lines at the beginning of your code:
F_CPU is your clock speed. Change it if you need (this is 16 MHz). ARDUINO 100 means we are using library files relative to Arduino 1.00
#include "Arduino.h" has to be there.

#define F_CPU 16000000
#define ARDUINO 100
#include "Arduino.h"

void setup();
void loop();

Now try it! Write some Arduino code! You can also try with this one:

#define F_CPU 16000000
#define ARDUINO 100
#include "Arduino.h"

void setup();
void loop();

void setup() {
    pinMode(13, OUTPUT);
    Serial.begin(9600);
}

void loop() {
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    delay(500);
    Serial.println("Hello World!");
}

Build it, upload it, and connect the Terminal Window!


This is a "basic" recipe to get that working. You can experiment on yourself and play around with the different optimization settings.

You can also include custom Arduino libraries, just add their path in the Compiler Directories!
For more details about including custom libraries, check out Part 3!

Now you can mix pure c/c++ code and Arduino code with Atmel Studio 6!
And if you feel like it, you can take advantage of the newly integrated QTouch development tools to build an Arduino-code powered touch-screen application!