Difference between revisions of "Arduino Behind the Scenes"

From Interaction Station Wiki
Jump to: navigation, search
m
m
Line 10: Line 10:
 
The first sugar cube is quite close and is revealed by first thing you'll see when starting a new sketch in the Arduino IDE. Each new sketch will start with the standard template:
 
The first sugar cube is quite close and is revealed by first thing you'll see when starting a new sketch in the Arduino IDE. Each new sketch will start with the standard template:
  
<syntaxhighlight lang="Arduino" line start="1">
+
<syntaxhighlight lang="Arduino" line start="1" enclose="div">
 
void setup() {
 
void setup() {
 
   // put your setup code here, to run once:
 
   // put your setup code here, to run once:
Line 21: Line 21:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Now this looks easy enough and we all know how what to do here. However, of course there is more to this and to understand what, it is important to know the language we are actually writing our Arduino (Wiring) code in. The language used by the Wiring language (and with that the Arduino language) is C / C++. Actually this is a combination of the languages [https://en.wikipedia.org/wiki/C_%28programming_language%29 C] and [https://en.wikipedia.org/wiki/C%2B%2B C++] which can be intermingled. From here on, for brevity, everywhere C is mentioned it can be read as C / C++ unless noted otherwise.
+
Now this looks easy enough and we all know how what to do here. However, of course there is more to this and to understand what, it is important to know the language we are actually writing our Arduino (Wiring) code in. The language used by the Wiring language (and with that the Arduino language) is C / C++ with an additional sugar topping. Actually this is a combination of the languages [https://en.wikipedia.org/wiki/C_%28programming_language%29 C] and [https://en.wikipedia.org/wiki/C%2B%2B C++] which can be intermingled. From here on, for brevity, everywhere C is mentioned it can be read as C / C++ unless noted otherwise.
  
Every program written in C should contain a main routine. This is the main entry point for code execution. The simplest C program that does nothing at all looks like this:
+
Every program written in C should contain a main routine. This is the main entry point for code execution, without your code won't run (it will not even compile). The simplest C program that does nothing at all looks like this:
<syntaxhighlight lang="C" line start="1">
+
<syntaxhighlight lang="C" line start="1" enclose="div">
 
void main() {
 
void main() {
 
   // put your code here
 
   // put your code here
Line 30: Line 30:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
If Arduino code is just C code, where is the <code>main</code> routine in the above example? It turns out the <code>main</code> routine does exist but is hidden below the surface. When we press the ''Verify'' or ''Upload'' button in the IDE a lot of things are set in motion to get your code translated into a format the &mu;Controller on the Arduino board does understand. Later we will see what exactly does happen but for now it is enough to understand that our <code>setup()</code> and <code>loop()</code> routines are ''added'' to the <code>main</code> routine.
 +
 +
In the arduino core<ref>https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/</ref> there exists a file main.cpp<ref name="main_current>https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/main.cpp</ref>. This is the the file where the <code>main</code> entry point resides. If you open this file in your text editor you will see something along these lines<ref>Shown code is from an earlier version. The current version is a bit more complicated. However the overall structure remains the same</ref>
 +
 +
<syntaxhighlight lang="C" line start="1" highlight="7,10" enclose="div">
 +
#include <WProgram.h>
 +
 +
int main(void)
 +
{
 +
init();
 +
 +
setup();
 +
   
 +
for (;;)
 +
loop();
 +
       
 +
return 0;
 +
}
 +
</syntaxhighlight>
  
 
[[Category:Arduino]]
 
[[Category:Arduino]]
 
[[Category:Advanced]]
 
[[Category:Advanced]]

Revision as of 01:57, 14 April 2015

Too much sugar

In the Arduino Introduction you could read that Arduino is in principle an attempt to make physical computing easier accessible for the average user. It did this by creating a hardware platform (development board) based on the Atmel AVR 8- and 32-bit family of μControllers. You also got introduced to the Wiring language framework Arduino implemented for their hardware platforms.

When we advance in our explorations at some point it is important to realise that making things easier usually means making compromises on other levels. In case of the Arduino and Wiring language framework this specifically means we compromise memory space and execution speed for ease of programming. In many cases this is perfectly fine but there are certain situations where it is important to understand these compromises and their ways around it (on the compromise of ease of use).

In principle Arduino adds a lot of sugar to the μController it is based on. The sugar makes it a lot sweeter, but we all know what too much sugar is not very good for your run-time health.

Where did all that sugar go

The first sugar cube is quite close and is revealed by first thing you'll see when starting a new sketch in the Arduino IDE. Each new sketch will start with the standard template:

  1. void setup() {
  2.   // put your setup code here, to run once:
  3.  
  4. }
  5.  
  6. void loop() {
  7.   // put your main code here, to run repeatedly:
  8. }

Now this looks easy enough and we all know how what to do here. However, of course there is more to this and to understand what, it is important to know the language we are actually writing our Arduino (Wiring) code in. The language used by the Wiring language (and with that the Arduino language) is C / C++ with an additional sugar topping. Actually this is a combination of the languages C and C++ which can be intermingled. From here on, for brevity, everywhere C is mentioned it can be read as C / C++ unless noted otherwise.

Every program written in C should contain a main routine. This is the main entry point for code execution, without your code won't run (it will not even compile). The simplest C program that does nothing at all looks like this:

  1. void main() {
  2.   // put your code here
  3. }

If Arduino code is just C code, where is the main routine in the above example? It turns out the main routine does exist but is hidden below the surface. When we press the Verify or Upload button in the IDE a lot of things are set in motion to get your code translated into a format the μController on the Arduino board does understand. Later we will see what exactly does happen but for now it is enough to understand that our setup() and loop() routines are added to the main routine.

In the arduino core[1] there exists a file main.cpp[2]. This is the the file where the main entry point resides. If you open this file in your text editor you will see something along these lines[3]

  1. #include <WProgram.h>
  2.  
  3. int main(void)
  4. {
  5.         init();
  6.  
  7.         setup();
  8.    
  9.         for (;;)
  10.                 loop();
  11.        
  12.         return 0;
  13. }
  1. https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/
  2. https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/main.cpp
  3. Shown code is from an earlier version. The current version is a bit more complicated. However the overall structure remains the same