1.07.2013

Atmel Studio for Arduino-guys - Part1: precompile Arduino core

Introduction


Hello everybody!

I'm back to my blog with several interesting articles. Now I'm working on telepresence robot, actually I'm using it from 04/12 and doing improvements all the time. Well.. long story short - my mother survived a stroke and now as a reault she has troubles with health, so I need an ability to stay at home, but I'm working at Betsson Tech in Kiev and it is nearly 400km from home. I love, yeah.. really love, my job so after discussion with management I had an approval to work remotely from home (thank you very much guys!).

It is cool, but new trouble arised - I need an ability to attend daily standups, plannings etc in Betsson Tech in Kiev. That is how idea to create telepresence robot arised. I'll write about the robot with some video and description of social aspects of using such a telebot in one of the future articles.

And today I want to start serie of articles about using Atmel Studio 6 with Arduino Mega 2560 development board. The Plan is to write about:

Part 1. Common project configuration + precompilation of Arduino Core and some Libraries
Part 2. freeRTOS for Arduino
Part 3. Google Protocol Buffers for Arduino + STL
Part 4. NanoPB for Arduino

So, lets start.

Common project configuration + precompilation of Arduino Core


First of all you need to download and install Atmel Studio 6. Then create a solution named Arduino.Precompiled (File -> New project... -> Atmel studio solution -> Blank Solution) and one static library named Arduino.Precompiled.Core (Right click on solution in Solution Explorer -> Add -> New project):

Create new project
Create new project

Then you will be asked to select your device. I'm using Arduino Mega 2560 board with MCU ATmega2560:

Select device
Select device
Press OK and now you have created your first project for AVR MCU in Atmega Studio! Now, lets add Arduino core files. To do this you need to download and unpack Arduino Software package (press on link for Windows). I unzipped package at the same level where Arduino.Precompiled folder located. Now go to Solution Explorer, right click on project and select Add -> Existing Item.. Go to the folder where all core files located: <arduino>\hardware\arduino\cores\arduino\ (see image below):

Add core files
Add core files
Select all files (Ctrl+A) and from drop down menu select Add As Link. When doing so you will add all selected files to your project as links, without physically copying them to project folder as Add does. Now lets do some configuration to our project. Need to say that configuration options I took from logs from Arduino IDE. So, right click on project -> Properties:

Set library name
Set library name
On Build tab you could set file name of your library, I named it libarduinocore. Please don't forget to do this for all configurations. Also, seems like Atmel Studio have some bug with saving properties for all configuration, so please recheck that name was really changed for both configuration by selecting Release and then Debug from drop down and checking actual Artifact Name.

Now go to Build Events tab:

Setting post-build event
Setting post-build event
I configured copy of resulting static library to Arduino.Precompiled.Release or Arduino.Precompiled.Debug folders located in solution folder, depending on build mode. Just to separate concerns...

Now we are ready to configure our Toolchain. Go to Toolchain tab, and select Symbols item for C or C++ compiler:

Configure toolchain: symbols
Configure toolchain: symbols
I configured both C\C++ for all configurations, all symbols I took from Arduino IDE logs:
  1. F_CPU - frequency of MCU you use. In case of Arduino Mega 2560 it is 16MHz or 16,000,000 Hz.
  2. ARDUINO is for version of Arduino framework, as you can see I use 1.0.3. Used for examle in Firmata library. But in our case it is optional and you can skip this definition at all.
  3. USB_VID and USB_PID is for vendor ID and peripheral ID, we are not using it. You can search for actual values in USBCore.cpp.
  4. __AVR_LIBC_DEPRECATED_ENABLE__ - use this definition to prevent compilation errors with Arduino libraries. Sorry, had no time to dig into it...
Next configure include directories (all configurations too):

Configure toolchain: directories
Configure toolchain: directories
In case you use another board, not Mega 2560, choose appropriate subfolder in variants folder.
Now we are ready to configure optimization section for Debug and Release modes separately:

Configure toolchain: optimizations
Configure toolchain: optimizations
And finally in Miscellaneous section add -fno-exceptions option:

Configure toolchain: miscellaneous
Configure toolchain: miscellaneous

With compillers we done, and now we should configure linker. Only in Release mode do next:

Configure toolchain: linker
Configure toolchain: linker
It looks like long and, may be, complicated, but after 2-3 projects you will do this configuration in 2-3 minutes. Description of all compiler and linker options you can find on official GCC site. Also, for AVR specific things you could visit AVR Libc site.

So, we are nearly done. Just remove unneccessary file that Atmel Studio created automagically:

Remove unneccessary file
Remove unneccessary file
When asked, press 'Delete' to permanently delete this file from your disk. Now you can try to compile... If you will see that output file is still named not like libarduinocore.a you can try next:
  1. Right click on project and choose Unload Project
  2. Right click on project and choose Edit Arduino.Precompiled.Core.cppproj
  3. Change your project file as shown on the image below:
Change name of library in project file
Change name of library in project file
After you done, just right click again and choose Reload Project. Thats it, now you have precompiled arduino static library in your Arduino.Precompiled.Release and\or Debug folders:

Library in the folder
Library in the folder
And here is small video that show you how to create project with precompiled Arduino Core library in Atmel Studio 6:


And here is flushing and testing:



Do you noticed memory statistics in the log? It shows how much you program takes in Flash (Program Memory Usage) and in RAM (Data Memory Usage). So make sure that Data Memory Usage is not more than you board can handle. For Arduino Mega it is 8KB.

Also I'm using avrdude to flush program into board, here is contents of flush.cmd:
c:\Users\User1\Downloads\arduino-1.0.1\hardware\tools\avr\bin\avrdude.exe -CC:\Users\User1\Downloads\arduino-1.0.1\hardware\tools\avr\etc\avrdude.conf -p m2560 -c wiring -P COM4 -b 115200 -F -U flash:w:%1%:i
pause

That's all for today. Next article will be about precompiling Arduino Libraries.

5 comments:

  1. Nice article, very detailed!
    Have you seen the add-in for Arduino programming with Atmel Studio 6.1 over at visualmicro.com?
    It includes a software debugger that is sweet.

    ReplyDelete
  2. Thank you for this article, I learned a lot!

    I was trying to build the Arduino 1.5.5-r2 core library with Atmel Studio 6.1, but it seems that the Atmel Studio's native avr-g++ compiler is not happy about the sources. I get strage Errors on WString.cpp:
    WString.cpp(189,26): ISO C++ forbids declaration of 'type name' with no type [-fpermissive]

    I tried to run the WinAVR's avr-g++ compiler with the same arguments, Atmel Studio generated, and that worked (in a terminal window).
    There is an option in Tools->Options...->Toolchain to change the Toolchain Path. For AVR 8-bit C language, there even was a predefined Toolchain Flavor called WinAVR. I added one for the CPP language and pushed the "Set As Default" button, but Atmel Studio does not seem to care about that.

    Do you have any idea, how to get the Atmel Studio g++ compiler compile this library's version?

    cheers, Niun

    ReplyDelete
    Replies
    1. I just discoverd that there is an "Advanced" tab in project properties. There you can change the Toolchain Flavor. Setting this to WinAVR makes my Arduino 1.5.5-r2 core compiling.

      Delete
  3. Is there a way to get this to work with the Arduino Due?

    I have tried to do it but keep getting errors. I changed all of the locations to the correct Due folders but still nothing.

    ReplyDelete