Atmel Studio vs. Arduino IDE [SOLVED]

Hi All,

(edit SOLVED: ** check the solution below **)

I have an Arduino Uno (atmega 328p). It does not say what rev.
I am experiencing the following, which is an issue:
The same code, compiled by the toolchain in Atmel Studio 6 runs FASTER than the code compiled with Arduino IDE 0023.

Atmel Studio 6 configuration:

  • I configured the symbol: “F_CPU=16000000L”, to define the speed the hardware is running on.
  • I included all the files from “arduino-0023\hardware\arduino\cores\arduino” (cpp and h files) in my project (unnecessarily increases the hex file size of course, but for this test it does not matter).
  • I turned on this linker option: “-Wl, –gc-sections” which decreases the size of the hex file dramatically!
  • In the C++ compiler I choose “Optimize for size (-Os)”. -ffunction-sections / -fpack-struct / -fshort-enums are also checked.
  • I upload code with avrdude. With these parameters (probably not important for my issue, but anyway:) : “-CC:\arduino-0023\hardware\tools\avr\etc\avrdude.conf -patmega328p -cstk500v1 -PCOM3 -b115200 -Uflash:w:"$(ProjectDir)Debug$(ItemFileName).hex":i

I also tried to compile in Atmel Studio 6 with symbol F_CPU set to “F_CPU=8000000L”. But the led blinks at the same rate as with 16000000L.
The difference is that “test” DOES appear on the Serial Monitor with F_CPU set to 16000000L whereas it does NOT when set to 8000000L.
With F_CPU set to 8000000L some weird characters appear in the Serial Monitor when baud set to “9600” in Serial Monitor, but when I double this to “19200” the string “test” appears again. Though I left the configuration of “9600” baud in the code the same.

Can anyone shine any light on this?
See the very simple code below.

BTW: The reason I wish to use Atmel Studio 6 is because it (should) give me more control over the microcontroller. And I wish to run a OS (preferably a RTOS) on it.

Code in Atmel Studio 6:

#include "WProgram.h"

//To sh*t up the compiler complaining about something I do not care about :)
extern "C" void __cxa_pure_virtual() { while (1); }
	
int main(void)
{
	//Lets do the same as Arduino IDE (but in Arduino IDE GUI you do not get to see this)
	init();
	
    while(1)
    {
          	DDRB = 0xFF;
          	PORTB = 0xFF;
          	Serial.begin(9600);
          	while(1)
          	{
	          	Serial.println("test");
	          	for(int i=0; i<1000; i++)
	          	{
		          	delayMicroseconds(1000);
	          	}
	          	PORTB ^= (1 << 5);
          	} 
    }
}

Code in Arduino IDE 0023:

void setup()
{
}

void loop()
{
        	DDRB = 0xFF;
        	PORTB = 0xFF;
        	Serial.begin(9600);
        	while(1)
        	{
	          	Serial.println("test");
	          	for(int i=0; i<1000; i++)
	          	{
		          	delayMicroseconds(1000);
	          	}
	          	PORTB ^= (1 << 5);
        	} 
}

Thanks in advance!

Solution:
Actually I am not sure what solved it. I changed some settings for the compiler.
Anyway this is what the C Compiler flags look like:

-DF_CPU=16000000L  -Os -ffunction-sections -Wall -c -fdata-sections -g -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)"  -mmcu=atmega328p

This is wat the C++ compiler flags look like:

-DF_CPU=16000000L  -I"C:\arduino-0023\hardware\arduino\cores\arduino"  -Os -ffunction-sections -w -c -fdata-sections -g -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)"  -mmcu=atmega328p

BTW: The reason I wish to use Atmel Studio 6 is because it (should) give me more control over the microcontroller.

Relative to what?

And I wish to run a OS (preferably a RTOS) on it.

With only 2K of RAM? Why?

The same code, compiled by the toolchain in Atmel Studio 6 runs FASTER than the code compiled with Arduino IDE 0023.

Measured how?

@ James C4S: 1. I might have put it wrong. Atmel Studio 6 IDE is much better than Arduino IDE. Of course you can compile the same code with both. 2. Because FemtoOs claims to be able to do so. And an OS facilitates multitasking. No spaghetti code required with an OS. 3. Seeing the LED blink with approx. twice the rate. I did not put my osciloscope on port 13 ;)

It is convenient to use the Arduino libraries. And I am very curious about why it runs faster with Atmel Studio 6 toolchain compiled code.

Edit: I intercepted the commandline from Arduino IDE to avr-gcc.exe. Nothing special actually. The 'mmcu' is defined by the device choice in the project in Atmel Studio 6. The F_CPU was set as a defined symbol, hence the notation 'DF_CPU'. I also defined the 'ARDUINO'. Still no luck.

-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=22 -IC:\arduino-0023\hardware\arduino\cores\arduino C:\arduino-0023\hardware\arduino\cores\arduino\wiring_shift.c -oC:\Users\Mike\AppData\Local\Temp\build1472107583140147487.tmp\wiring_shift.c.o