Pages: 1 2 [3]   Go Down
Author Topic: Why not (finally) expose main.cpp to the user ?  (Read 4208 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 200
Posts: 12782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do that?

I believe it is an attempt to make the core more portable.  The idea is to have the pins defined in a header file that is located and included when a sketch is built. 

But I don't know why main.cpp was used to pull in the pin definitions.

Quote
In fact why not change main.cpp from ... Then it compiles on its own without any header files, and will only be used if the user doesn't supply their own main function.

I almost agree.  There is some safety in using a common header file for function prototypes.  Well, with overloading, maybe there isn't.  Let me check ... never mind.  I must be thinking of ObjectPascal.

I agree.  The second version is a good alternative.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe it is an attempt to make the core more portable.  The idea is to have the pins defined in a header file that is located and included when a sketch is built. 

Since the developers control what the IDE does when it compiles, why not just have it copy the pins file anyway? I mean, if they are going to copy main.cpp, why not copy pins_arduino.h as well? This seems a convoluted way of doing it.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 200
Posts: 12782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


I have no idea why it was done the way it was.
Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 606
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe the idea was to allow 3rd party boards to use the standard Arduino core more easily.  All the pinout-specific stuff is supposed to go into a single header file, and boards.txt can specify the directory.  So in theory, a board which only needs to customize pins could just add a directory and its own copy of pins_arduino.h, and not need to modify any files, except perhaps boards.txt.

Compiling the code into main.cpp was probably just an oversight?  It very likely belongs in either wiring_digital.c (where it's actually used), or in its own dedicated .c file (as it was in prior to 1.0).

If you delete the ARDUINO_MAIN line from main.cpp, and add it into wiring_digital.c like this:

Code:
#define ARDUINO_MAIN
#include "wiring_private.h"
#include "pins_arduino.h"

I believe that solves this problem.
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 11
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

We can certainly move the pins_arduino.h include from main.cpp to some other file, if it helps.  Just submit a issue / patch to the Google Code list: http://code.google.com/p/arduino/issues/list
Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 606
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Done, issue #691, including a patch!  smiley

http://code.google.com/p/arduino/issues/detail?id=691

Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 606
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Bump.

This patch became part of 1.0-rc2.  Anyone who really cares about main() might want to give 1.0-rc2 a try before 1.0 officially releases?

http://code.google.com/p/arduino/wiki/Arduino1
Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This example compiles and works:

Code:
int main(void) {
  init();
  Serial.begin(9600);
  pinMode(2, INPUT);
  while(1) {
    int sensorValue = digitalRead(2);
    Serial.println(sensorValue);
  }
}

I see that all compiler warnings have been turned on, probably only for testing.

Some observations:

Code:
avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega168 -DF_CPU=16000000L -DARDUINO=100 -I/***/arduino-1.0-rc2/hardware/arduino/cores/arduino -I/***/arduino-1.0-rc2/hardware/arduino/variants/standard /***/arduino-1.0-rc2/hardware/arduino/cores/arduino/IPAddress.cpp -o/tmp/build4743758903281560802.tmp/IPAddress.cpp.o
In file included from /***/arduino-1.0-rc2/hardware/arduino/cores/arduino/IPAddress.cpp:3:
/***/arduino-1.0-rc2/hardware/arduino/cores/arduino/IPAddress.h: In member function 'IPAddress::operator uint32_t()':
/***/arduino-1.0-rc2/hardware/arduino/cores/arduino/IPAddress.h:51: warning: dereferencing type-punned pointer will break strict-aliasing rules
/***/arduino-1.0-rc2/hardware/arduino/cores/arduino/IPAddress.h: In member function 'bool IPAddress::operator==(const IPAddress&)':
/***/arduino-1.0-rc2/hardware/arduino/cores/arduino/IPAddress.h:52: warning: dereferencing type-punned pointer will break strict-aliasing rules
/***/arduino-1.0-rc2/hardware/arduino/cores/arduino/IPAddress.h:52: warning: dereferencing type-punned pointer will break strict-aliasing rules

Code:
avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega168 -DF_CPU=16000000L -DARDUINO=100 -I/***/arduino-1.0-rc2/hardware/arduino/cores/arduino -I/***/arduino-1.0-rc2/hardware/arduino/variants/standard /***/arduino-1.0-rc2/hardware/arduino/cores/arduino/Tone.cpp -o/tmp/build4743758903281560802.tmp/Tone.cpp.o
/***/arduino-1.0-rc2/hardware/arduino/cores/arduino/Tone.cpp:108: warning: only initialized variables can be placed into program memory area

The last one can be silenced by using 'const prog_uint8_t var_name' instead of 'const uint8_t PROGMEM var_name'.

See: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=57011

I'm using:

* avr-gcc (GCC) 4.4.3
* avr-libc 1.7.1

Thanks for making it work.
« Last Edit: October 27, 2011, 05:20:33 pm by madworm » Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The PROGMEM issue seems identical to

http://code.google.com/p/arduino/issues/attachmentText?id=688&aid=6880000003&name=0004-Avoid-spurious-pgmspace-warnings.-See-http-gcc.gnu.o.patch&token=9f0e77f0e7d48a831b326a901e2ed403
Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Pages: 1 2 [3]   Go Up
Jump to: