Moving from IDE to Atmel Studio

Hi all!

I designed a board with an atmega328p on it and I have a simple code running on it, everything's good. I would like to "rewrite" the code on Atmel Studio, but without the Arduino main libraries. Aside from digitalWrite and similar functions that are pretty easy to rewrite working directly on the registers, there should be a lot more behind that is unseen by the IDE users (I/O for sure and more I guess).

Is there any guide to follow (or a template of "blink" for example, even better) or should I figure all by myself?

Well, you have all the source code for the arduino libraries...

And its just possible that those that have had the most success in doning this are over in the Atmel Studio support forums.

I'm very new to this myself, making a part migration from the Arduino IDE to Atmel Studio so I can use an interactive debugger, data visualiser etc., so I am joining this thread to see what comes out of it.

With atmega328p arduino applications, I believe you have the option of importing an Arduino application directly. With the board I am using, an atmega4809 (megaAVR-0 series) eXplained, as i discovered, you can't.

Then you have to decide if you use the ATMEL START development tool or directly use raw gcc C++. I've just about got to the stage of roughing out bare metal equivalents of Serial.print, port access (digitalWrite() etc. ) and a millis() equivalent.

I feel I'm pioneering a bit because I am finding bugs in the official "getting started" examples for my board which indicates that nobody has really used them, such as forgetting to activate the pullup resistor on a read button example, or not noticing that the built in led is wired on the high side so everything is inverted.

What the exercise does make you realise, though, is how much you take for granted with the Arduino IDE, especially a high degree of source code portability across platforms, functions for the commonly used activities, and libraries for a huge number of peripherals. However, you then also hit the limitations of not having professional development tools like an integrated debugger.

c0rsa1r:
. . .
Is there any guide to follow (or a template of “blink” for example, even better) or should I figure all by myself?

Just on that point, you can run raw gcc C++ in the Arduino IDE, defining your own main() function, to get a taste of what to expect when move away to the Atmel Studio.

There is an example here: https://forum.arduino.cc/index.php?topic=687482.0 (post #4) which you can easily adapt to blink the built in led using direct port manipulation and _delay_ms(<not too big!>) . For more realism, rename setup() and loop() and drop the #include <Arduino.h>

The standard init() does not run which would configure timers ADC, etc etc. so millis() won’t work.

Thank you for all the answers! I’ve been able to import the whole .ino in Atmel Studio with the visual micro plugin. All of the libraries have been imported automatically and the sketch (now cpp in the project folder) compiles fine.

It does compile because

#include <Arduino.h>

has been automatically added, which includes all of the sub-lib necessaries to compile the code (wiring.h, pins_arduino.h, all of the variants and the core files).

This is, however, much more than what is needed in order to run on the simple 328p (I suppose that importing variants will include everything from the Arduino lib, regardless of the microcontroller used).

Digging inside the imported libs, I’ve found all the references that I was looking for (avr/io.h, util/delay.h and so on).

I think that I should extract only the functions that are needed, say for a “Blink.cpp” code, without the inclusion of Arduino.h

A successfull compile of that cpp without including Arduino.h and replacing digitalWrite and all the Arduino functions with direct registers manipulation should mean that the “conversion” is done.

Am I right?

c0rsa1r: . . . I think that I should extract only the functions that are needed, say for a "Blink.cpp" code, without the inclusion of Arduino.h

A successfull compile of that cpp without including Arduino.h and replacing digitalWrite and all the Arduino functions with direct registers manipulation should mean that the "conversion" is done.

Am I right?

The blink sketch uses delay() which will certainly drag in a lot of clutter, and will need timer0 initialised. I suppose it all depends on how free of the Arduino environment you want to be. But I believe that it would be nice to have a tiny subset of the Arduino environment around so that you can move sketches without too much pain between the two development environments.

6v6gt: ...I believe that it would be nice to have a tiny subset of the Arduino environment around so that you can move sketches without too much pain between the two development environments.

that's exactly what I'm looking for. I love the IDE and how simple it is, and I like the Arduino libs which are somehow simpler than looking into registers and so on (maybe that's because I'm used to them, I don't know), but I feel that having a code free of those libraries will be more professional.

Digging into the libs, I see that thay are pretty solid, at least from my point of view, and they are updated along with the boards definitions and so on, so they are not bad, at all.

Hower, as you correctly pointed out, moving from the two environment is a pain, because that forces me to install visual micro (god bless the import .ino function). Doing the other way around is much easier (from AS to IDE).