What's behind the Wiring

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi!
I'm using my Arduino with the Atmel Studio 6 since few months. For some reasons, I'm not always have the strength to use pure C. So, in these days, I came back at Wiring.
But because I studied the Atmel datasheet I've acquired a little sensibility about optimisation issues and low-level programming. That being said, I feel frustrated to see the 1,8Kb of code to blanking a LED or more than 3K for the serial initialisation. So I started to search for some documentation, at least, of what are the default values of registers used in the Wiring environment. But people on Arduino forum are saying that there's no documentation on what are the default vaules of registers (i.e. pwm modes, parity bit on uart and stuff like that). I mean, if I need to change something from the default I'm ok to the need to study the datasheet and C and so on, but I don't think it's too much ask for knowing what I downloaded without became an engineer in software to program an 8 bit MCU (btw, I'm a student in electronic engineering, and we used C to program Freescale MCUs, so I know that you don't have to became a Java expert :D)
And since I realized that those 1,8Kb are beacuse of (I think!) the perfect initialisation of everything and to keep a sort of order in programs made in C++, I try to ask even what includes this increase of memory usage. But the answer on Arduino forum is the same, paraphrasing: "do programs by your self in C if you want to know".
PARAPHRASING everything above:
There's no documentation on Arduino software in a deep analysis? Or on what are the defaults settings loaded when I use a function pre-writed by them? Or something that explain how is built the final sketch?

I hope to make the main idea of my thought.

Thanks to all.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why did you post here? This is for YOU to TEACH others by means of tutorials not ask questions.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you have installed the Arduino software all the source code seems to be available. I believe that is as close as you will come to a documentation of the inner workings of the Arduino "Wiring" libraries.

For example, the source code for digitalWrite() is in the file

C:\arduino-1.0\hardware\arduino\cores\arduino\wiring_digital.c

If you start looking at what that code does, you will also find why setting a single digital output pin using

digitalWrite(arduino_pin_number)

takes a lot more time than for the very efficient C code

port |= 1<<port_pin_number;

I am not expressing an opinion of whether this is a good or a bad thing, just talking about the technical facts.

-----

One reason for the simple setting of digital I/O pins takes so much code is that everything including determining port address, and pin number within the port, from the Arduino boards pin number is done dynamically at run time. It has been shown here that this produces code that is a magnitude ten (or worse IIRC) larger than if the port and port-pin-number is known at compile time. This is a sacrifice that the Arduino people chose in order to do the users code writing a bit easier. They do not need to look up stuff in the data sheet in the same extent as us plain C programmers do. The Arduino software framework strives to create a layer of abstraction making the teck details of the AVR hardware as invisible as possible.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ops, I don't know!
I will post on AVR Forum. Sorry.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I will post on AVR Forum. Sorry.
No please don't. I have already signalled for the moderators on this forum to move it for you.

We don't want doubling up on threads or it gets confusing.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
There's no documentation on Arduino software in a deep analysis?

Correct. Read the source code; it's not like there is a lot of it. Also, there are assorted discussions in the Arduino forums: http://arduino.cc/forum/index.ph... for example (a bit dated by now, I guess. I should do it again.)
Occasionally documentation gets added. http://code.google.com/p/arduino... is an example.

Quote:
Or something that explain how is built the final sketch?

Did you see http://arduino.cc/en/Hacking/Bui... ?
Did you turn on verbose compilation output ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

And since I realized that those 1,8Kb are beacuse of (I think!) the perfect initialisation of everything and to keep a sort of order in programs made in C++, I try to ask even what includes this increase of memory usage.

If you are building in AS6 so that a
.elf file is being created in the ./Debug/ output directory then using a Command Prompt at that directory (or CD to it if you know how) use the command:

avr-nm --size-sort .elf

(replacing "
" with the real file name) The functions towards the end of the list are the ones using all the space (the numbers are the size of each function - that is " T " entries - in hex bytes).

If I use the Arudino IDE and do something similar I get:

C:\Documents and Settings\asl\Local Settings\Temp\build22122800012078905.tmp>avr-size Blink.cpp.elf
   text    data     bss     dec     hex filename
    832       0       9     841     349 Blink.cpp.elf

C:\Documents and Settings\asl\Local Settings\Temp\build22122800012078905.tmp>avr-nm --size-sort Blink.cpp.elf
00000001 b timer0_fract
00000004 B timer0_millis
00000004 B timer0_overflow_count
00000008 T setup
0000000a T port_to_input_PGM
0000000a T port_to_mode_PGM
0000000a T port_to_output_PGM
00000014 T digital_pin_to_bit_mask_PGM
00000014 T digital_pin_to_port_PGM
00000014 T digital_pin_to_timer_PGM
00000016 T main
00000022 T loop
00000040 T init
0000004c T pinMode
00000072 T digitalWrite
00000090 T __vector_9
000000b6 T delay

Actually, though not the most efficient program in the entire world (pure C could blink and LED in 200 bytes) the 832 bytes there doesn't seem too bad.

When you say 1.8kB are you measuring the binary size or the size of a .hex file? If I look at the .hex file this generated:

C:\Documents and Settings\asl\Local Settings\Temp\build22122800012078905.tmp>dir Blink.cpp.hex
 Volume in drive C has no label.
 Volume Serial Number is E0DD-96FE

 Directory of C:\Documents and Settings\asl\Local Settings\Temp\build22122800012078905.tmp

08/10/2012  10:13             2,353 Blink.cpp.hex
               1 File(s)          2,353 bytes

But that doesn't mean I will be using 2,353 bytes within the AVR. As seen above that 2,353 bytes holds the information to program 832 bytes in the AVR.

BTW for comparison, if I write the equivalent of Arduino's "Blink" example in straight C using AS6 I get:

C:\Documents and Settings\asl\My Documents\Atmel Studio\GccApplication1\GccApplication1\Debug>avr-size GccApplication1.e
lf
   text    data     bss     dec     hex filename
     84       0       0      84      54 GccApplication1.elf

C:\Documents and Settings\asl\My Documents\Atmel Studio\GccApplication1\GccApplication1\Debug>avr-nm --size-sort GccAppl
ication1.elf
0000002e T main

C:\Documents and Settings\asl\My Documents\Atmel Studio\GccApplication1\GccApplication1\Debug>dir GccApplication1.hex
 Volume in drive C has no label.
 Volume Serial Number is E0DD-96FE

 Directory of C:\Documents and Settings\asl\My Documents\Atmel Studio\GccApplication1\GccApplication1\Debug

08/10/2012  10:23               280 GccApplication1.hex
               1 File(s)            280 bytes

It's certainly true that an 84 byte program (creating a 280 byte .hex) is considerably smaller than the 832 bytes above!