Atmel Studio 6 and Arduino

Home / Atmel Studio 6 and Arduino

Atmel Studio 6 and Arduino

December 11, 2015 | Article | No Comments

Atmel Studio, formerly known as AVR Studio, is a good development application to create program for Atmel’s processor (Atmel’s AVR and Atmel’s ARM). The 6th version promises to be better, faster, and easier to use than before. Here, we will discuss about the way to set up the platform for the use with Arduino.

In this article, I use:

  1. Windows 8.1 64-bit
  2. Atmel Studio 6.2
  3. Arduino 1.5.8 (BETA)

Our objective:

  1. Successfully create binary file for Arduino
  2. Successfully flash the image to Arduino
  3. Know some of Arduino core library

It is recommended that we install both Atmel Studio and Arduino IDE.

Introduction to Atmel Studio 6

This Integrated Development Platform is a rich feature development platform for developing and debugging Atmel ARM Cortex-M and Atmel AVR microcontroller (MCU) based application.

Atmel Studio 6 is released in 2011 and based on Microsoft Visual Studio. The latest version at the time of writing this article is Atmel Studio 6.2 which can be downloaded from here.

Atmel studio 6 is free of charge and is integrated with the Atmel Software Framework (ASF), a large library of free source code with 1600 ARM and AVR project examples. In summary, what Atmel Studio 6 offers in addition to creating new software for MCU:

  1. Facilitates reuse of existing software and, by doing so, enables design differentiation.
  2. Supports the product development process with easy access to integrated tools and software extensions through Atmel Gallery.
  3. Reduces time to market by providing advanced features, an extensible software eco-system, and powerful debug integration.

Why Switching from Arduino IDE to Atmel Studio 6?

Arduino is a great tools. But, it does so much under the hood that it can actually be quite limiting for experienced programmers. The lacks of compiler warnings and debugging capabilities make life hard when working on advanced projects. And here Atmel Studio comes to the rescue.

Atmel Studio is a huge step up from those limitations. If you have a big project, porting the entire thing to pure C can be a daunting task. Plus, some of those Arduino libraries are just so darn convenient.

So, why not have the best of both worlds? Arduino is basically a wrapper on top of C/C++, technically it’s possible to combine any Arduino sketch or library with your own custom code (and that it is). The trick is in setting up your project properly. Once accomplished, you can keep access to the huge Arduino user-contributed code library, but enjoy all the features of advanced AVR and a real IDE.

Arduino Core Library

Arduino is an open-source physical computing platform based on simple I/O board. Stripped the IDE part, Arduino has a library which consists of seberal things like: EEPROM, Esplora, Ethernet, Firmata, etc. But down to the innermost, Arduino has a set of library known as the Arduino core. This Arduino core is the software library which is compiled to any board of Arduino and make Arduino as Arduino.

The Arduino core can be found at “<ArduinoPath>\hardware\arduino\cores\arduino\” where <ArduinoPath> is the path you install Arduino IDE. Well, if you don’t install Arduino IDE, you can clone it from git

git clone git://github.com/arduino/Arduino

and head to “hardware\arduino\cores\arduino”. You can read the content of Arduino core library in the appendix A below.

In order to build Arduino projects, we need to grab the arduino core library. Normally, the Arduino IDE compiles this for us whenever we compile a sketch. To use it in Atmel Studio we need to compile it first.

Steps:

  1. Open Arduino IDE. Open preferences (File -> Preferences) and check on “compile” next to “Show verbose output during:”.
  2. Make sure Arduino IDE is set to whichever chip you want to use with Atmel Studio.
  3. Compile any example sketch, for example “01.Basics\Blink”. In output window at the bottom of the IDE window you should see information on where the IDE put the temporary build output. It will look something like this: “C:\Users\{Your User}\AppData\Local\Temp\build1249632912679374352.tmp”
  4. Copy the “core.a” file and paste it as “libcore.a” so Atmel Studio (and GCC toolchain) can recognizes it as a standard library.

Converting the Sketch

Now the sketch part. There are two things you can do:

  1. If you are converting an existing sketch called Sketch, open the Sketch.cpp file from temporary build directory in text editor. You can then simply copy-paste the code into AVR Studio project. This file is generated by Arduino from our sketch. Arduino automatically fix the messes for us, for example we can define function anywhere in the sketch but Arduino would make appropriate function prototype for us. This is a solution if you are compiling the sketch via Arduino IDE.
  2. Copy and paste the source code from sketch file into the project’s main .cpp file. You can also copy-paste the source from Arduino .pde sketch. If you do this, you should define the function prototypes by yourself. Adding function prototypes is easy actually. Remember to incluse prototypes for setup() and loop() and then add #include “Arduino.h” to the very beginning.

Compiler and Linker Setup

In this section we will setup the compiler and linker setup for any of our project. Our objective:

  • Define Symbols for compilation, especially F_CPU and Arduino software version.
  • Point additional Include directory.
  • Set some Optimization
  • Set Linker to include our libcore
atmel-arduino
Picture 1

Steps:

  1. Open the Project Properties (Project -> <ProjectName> Properties or press Alt+F7), then click on “Toolchain”. We need to setup a bunch of compiler options here. Select “All Configuration from the Configuration drop-down menu to modify both release and debug configuration at same time.
  2. Click on “Symbols” in the left-hand “AVR/GNU C++ Compiler” dropdown menu (see number 1 on picture 1). We need to tell some symbols to our compiler. To do it, click the green plus icon and then enter the symbol. Here is what you need to enter:
    • F_CPU=16000000L
    • ARDUINO=158
  3. Click on “Directories” in the same C++ Compiler menu (see number 2 on picture 1). We need to add the include directories, the directories that contain our Arduino core code and libraries. For any Arduino project, we’ll need to tell the compiler where to find “Arduino.h” and “pins_arduino.h”. In my case I install Arduino on C:\Arduino so the library should be on “C:\Arduino\hardware\arduino\cores\arduino” and “C:\Arduino\hardware\arduino\variants\standard”.
  4. Click on “Optimization” immediately after “Directories” (see number 3 on picture 1). Choose “Optimize for size” under “Optimization Level”. Add “-fdata-sections” to “other optimization flags” and check the box for “prepare functions for garbage collection”.
  5. Next click on “Miscellaneous” (see number 4 on picture 1). Add “-fno-exceptions” to the “Other flags” field.
  6. Now we will move to the linker. Click on “Libraries” in the left hand “AVR/GNU Linker” dropdown menu (see number 5 on picture 1).
    1. In the Libraries section you should already see an entry for “m” which is the AVR math library. Add an entry called core, which is our libcore.a file that we grabbed earlier.
    2. We also need to tell where to find the libcore.a, so add that directory path under “Library search path”
  7. Click on “Optimization” (see number 6 on picture 1) and check the box for “Garbage Collect unused sections (-Wl,-gc-sections)”. This tells the linker to leave out unused portions of each library, which reduces the final code size.

Build Project

Building process is similar like other project. Hit the “F7” button to build our solution and watch the output window.

AVRDude and Flashing

The instruction in this section is similar to other “Adding ISP Programmer to Atmel Studio”.

Once you have build the project, we need to upload it. We can achieve this using the similar method as the Arduino IDE. Arduino uses the avrdude utility to flash via serial, and we will do the same.

atmel-exttools
picture 2
atmel-exttools2
picture 3

Ignore entries other than Arduino on picture 3.

Steps:

  1. Open External Tools by clicking Tools -> External Tools (see picture 2)
  2. When the window popups, click “Add” (see number 1 on picture 3)
  3. Note which COM port Arduino uses and make the title like “Arduino on COM24” for easy identification (see number 2 on picture 3)
  4. In the “Command field” (see number 3 on picture 3), put the path to avrdude.exe in Arduino installation. For example in my case: “C:\arduino\hardware\tools\avr\bin\avrdude.exe” without the quotes.
  5. In the “Arguments” (see number 4 on picture 3), paste this line.
-CC:\arduino\hardware\tools\avr\etc\avrdude.conf -patmega328p -carduino -P\\.\COM24 -b57600 -D -Uflash:w:"$(ProjectDir)Debug\$(ItemFileName).hex":i

Edit the path to Arduino installation and change the COM port, chip target, and baud rate if necessary.

 

Appendix A: Arduino Core Library files

The Arduino core library is consists of following file:

utility function

Arduino.h
binary.h
main.cpp
new.cpp
new.h
Platform.h
WCharacter.h
WInterrupts.c
WMath.cpp
WString.cpp
WString.h

CDC communication.

CDC.cpp

Human Interface Device (HID) communication.

HID.cpp

Hardware serial, for serial communication capability.

HardwareSerial.cpp
HardwareSerial.h

Internetwork connection.

Client.h
IPAddress.cpp
IPAddress.h
Server.h
udp.h

Printing.

Print.cpp
Print.h
Printable.h

Stream oriented communication

Stream.cpp
Stream.h

USB serial communication.

USBAPI.h
USBCore.cpp
USBCore.h
USBDesc.h

Tone Generator.

Tone.cpp

Wiring project

wiring_analog.c
wiring_digital.c
wiring_private.h
wiring_pulse.c
wiring_shift.c

, ,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial