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