Serial problem on Mega2560 still continues on Gentoo

Hi everyone,

I've just started developing with Arduino. Have a Deumilanove and Mega2560.

Just faced a weird problem with Mega. Checked this forum and others. Some people had such a problem before but dunno if there are others nowadays.

Simply, any code contains serial connection won't work on Mega, but works fine on Deumilanove.

An example

void setup(){
  Serial.begin(9600);
  Serial.println("Hello Computer");
}
void loop() {
}

This simple code compiles and uploads fine but makes Mega blink randomly on pin13 led and sends nothing through serial port. Even though there is nothing about the pin13 in the code. But works fine with Deumilanove.

And yes, I correctly selected the board model from GUI.

Also updated the serial firmware from 0.000 to 0.001 for Mega. I've read something like this is a problem with gcc itself on Linux and tried several configurations to overcome this stupid error. In some forums, they recommended to downgrade gcc version to 4.3.3 but that version does not support for Mega2560. Though it would be OK for Mega 1280. The earliest version I could install is 4.3.4 supporting 2560. And that won't solve the problem. The latest version neither. (4.5.3-r1)

Here is the system information I work on,

uname -a
Linux tarik 2.6.39-gentoo-r3 #1 SMP Fri Oct 7 00:57:27 JST 2011 i686 Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz GenuineIntel GNU/Linux
USE="-openmp" crossdev  --target avr --gcc 4.4.4-r2 --binutils 2.19.1-r1 --libc 1.6.8 -s1 --without-headers
USE="-openmp" crossdev  --target avr --gcc 4.4.4-r2 --binutils 2.19.1-r1 --libc 1.6.8 -s4 --without-headers
ln -s /usr/i686-pc-linux-gnu/avr/lib/ldscripts /usr/avr/lib/ldscripts
ln -s /usr/avr/lib/avr6/crtm2560.o /usr/avr/lib/crtm2560.o
ln -s /usr/avr/lib/avr6/crtm2561.o /usr/avr/lib/crtm2561.o

These compile the code fine, "Blink" works OK. But for the codes contains anything about serial makes the board act weird.

Also tried with the stable versions like,

USE="-openmp" crossdev  --target avr -S -s4 --without-headers

but this won't even compile, because avr-libc-1.6.2 has a serious problem with compiling. (see https://bugs.gentoo.org/show_bug.cgi?id=384605)

Other versions of gcc and libc are already tried a couple of times, but just no luck. With the stable versions of gcc and binutils and 1.6.8 version of libc (it's the earliest version that can be compiled), here is the output,

/usr/libexec/gcc/avr/ld: skipping incompatible /usr/lib/gcc/avr/4.5.3/../../../../avr/lib/libm.a when searching for -lm
/usr/libexec/gcc/avr/ld: cannot find -lm
/usr/libexec/gcc/avr/ld: skipping incompatible /usr/lib/gcc/avr/4.5.3/libgcc.a when searching for -lgcc
/usr/libexec/gcc/avr/ld: cannot find -lgcc
/usr/libexec/gcc/avr/ld: skipping incompatible /usr/lib/gcc/avr/4.5.3/../../../../avr/lib/libc.a when searching for -lc
/usr/libexec/gcc/avr/ld: cannot find -lc
/usr/libexec/gcc/avr/ld: skipping incompatible /usr/lib/gcc/avr/4.5.3/libgcc.a when searching for -lgcc
/usr/libexec/gcc/avr/ld: cannot find -lgcc
collect2: ld returned 1 exit status

As a result, I'm not be able to run serial connection on Mega2560. I guess this is a problem with the Linux side but still annoying. Anyone else having the same problem or knowing the solution. Any idea will be welcomed.

Thanks in advance.

What platform IDE are you using? Looks like Linux, and I am using the Linux version. When I first received my Mega2560, it would not run anything but the blink example. If it required "Serial.begin()" or "Ethernet.begin()", the sketch would fail.

I upgraded the avr-gcc and avr-libc, and all works now, after some small editing in the runtime library. The details of the upgrade are in reply #5 here: http://arduino.cc/forum/index.php/topic,71182.0.html

Now I am using avr-gcc-4.5.1 and avr-libc-1.7.1 and all is fine.

Edit: Looks like you are using avr-libc-1.6.8. Good thing you weren't using avr-libc-1.7.0. It won't even compile and run the blink example. That was the trouble the OP in that thread was having.

These are the "signature errors" for these avr-libc versions: 1.6.8 - Serial.begin(9600) causes a crash. 1.7.0 - delay(1000) causes a crash. 1.7.1 - compiler "no declaration for fabs and ceil" error (prior to editing delay.h as described in the thread link above).

Thank you for your answer.

At least there's a light at the end of the tunnel :sweat_smile:

I've upgraded avr-gcc and avr-libc to the following versions,

  • binutils: binutils-2.20.1-r1
  • gcc: gcc-4.5.1-r1
  • libc: avr-libc-1.7.1

and modified the delay.h and wiring.h headers according to your previous post. Now I'm getting the following error while compiling blink

/usr/libexec/gcc/avr/ld: skipping incompatible /usr/lib/gcc/avr/4.5.1/../../../../avr/lib/libm.a when searching for -lm
/usr/libexec/gcc/avr/ld: cannot find -lm
collect2: ld returned 1 exit status

This error occurs for all sample codes regardless of having delay or serial functions.

That is a GCC linker error. What version of GCC are you using? You can find that from a command line shell. gcc --version

Mine is v4.5.2 Did you get the upgrade from the Atmel website?

My binutils is v2.21. I did not need to upgrade that. I'll check.

Add: I compiled and installed binutils v2.21.1 after downloading from here, and the Arduino IDE still compiles ok: http://ftp.gnu.org/gnu/binutils/binutils-2.21.1a.tar.bz2

Download and extract into a local directory, then in that directory from a shell: ./configure make make install

There is a README file there too if you have questions. The "make" command takes a while.

"make install" required me to be logged in as root.

Opss, maybe a misunderstanding,

The versions I wrote are for the avr versions,

  • binutils: binutils-2.20.1-r1
  • gcc: gcc-4.5.1-r1
  • libc: avr-libc-1.7.1

These are not for the system ones, but the avr packages; cross-avr/gcc-4.5.1-r1 , cross-avr/binutils-2.20.1-r1 , cross-avr/avr-libc-1.7.1

The gcc version I use to compile the entire system is sys-devel/gcc-4.4.5 and sys-devel/binutils-2.20.1-r1.

Do they have to match with avr versions?

On the other hand, avr toolchain from Atmel website, works fine. But I want to create the environment by using the gentoo's crossdev tool. It compiles all the necessary packages and creates the cross compile environment automatically. What I need is the working combination of the versions. The above mentioned versions have the problem I sent,

/usr/libexec/gcc/avr/ld: skipping incompatible /usr/lib/gcc/avr/4.5.1/../../../../avr/lib/libm.a when searching for -lm
/usr/libexec/gcc/avr/ld: cannot find -lm
collect2: ld returned 1 exit status

It isn't exactly "matching", all versions of all that stuff must work. One fails, all fail. That "weakest link" thing. I am using binutils 2.21.1

Ok I've found what the last problem is.

Caution! This is only for Gentoo. To overcome that, "multilib" must be enabled. Open /usr/portage/eclass/toolchain.eclass and change --disable-multilib to --enable-multilib

Then, compile your avr environment by USE="-openmp" crossdev --target avr --gcc 4.5.1-r1 --binutils 2.21.1 --libc 1.7.1 -s4 --without-headers ln -s /usr/i686-pc-linux-gnu/avr/lib/ldscripts /usr/avr/lib/ldscripts ln -s /usr/avr/lib/avr6/crtm2560.o /usr/avr/lib/crtm2560.o ln -s /usr/avr/lib/avr6/crtm2561.o /usr/avr/lib/crtm2561.o

DO NOT FORGET to take the modification back at /usr/portage/eclass/toolchain.eclass

Then modify /usr/avr/include/util/delay.h according to @SurferTim's entry.

This configuration compiles fine and and gives no error. BUT! It still creates faulty binaries with the serial functions. Blink compiles and runs fine but a basic serial code won't run in any way, but that compiles fine too.

The versions I've installed are verified by you as working fine, but those won't work in my case. Any idea?

That is good to know about the Gentoo version.

Your serial symptoms sound exactly like mine were. The code would compile and upload ok, but would crash as soon as it started "running". It would do nothing. I could try to activate the pin 13 LED prior to the Serial.begin(9600) call, but even that would not work. The LED would not light, despite being before the Serial.begin(9600) routine call.

Have you tried it with the Arduino IDE?

Yep, I’ve tried it with Arduino IDE.

And unfortunately, activating the LED on pin13 prior to Serial.begin won’t even have an effect. The LED blinks randomly, when Serial.begin is called.
I can’t see any relation with the LED on Pin13 and the serial interface. Why they affect each other?

Any idea how to compare the binary code created by the Atmel toolchain and the system one?

There must be a point causing this error because the versions of gcc, libc and binutils are matching in those two.

Similar symptoms here with gentoo amd64 and a Mega 2560.

Thanks to this thread I could compile the avr toolchain and make the necessary adjustments (multilib compile, ldscripts and crtm*.o symlinks). The Basics/Blink example compiles, uploads and runs fine. The Communication/ASCIITable compiles and uploads, but does not do what is expected (LED13 blinks in some strange way, but there is no output in the serial console).

I uploaded the same example from a Windows computer, it runs fine. Once the board is programmed with the ASCIITable sketch, I can also plug it into the linux computer, and check on the output of the serial console that it also works there.

Configuration:

  • linux-3.1.6-gentoo x86_64 (modules cdc_acm, ftdi_sio, usbserial)
  • avr-libc-1.7.1, binutils-2.21.1-r1, gcc-4.5.3-r2
  • arduino-1.0-linux64.tgz binary from arduino.cc

I finally had the arduino IDE 1.0 working on my gentoo amd64 with Serial function. More precisely, Blink and ASCIITable sketches upload and run. All using portage.

I followed the howto http://www.arduino.cc/playground/linux/gentoo with some alterations.

You can start by emerging arduino-0021 to pull all the java dependencies, and check it really does not work on your Mega 2560 as soon as Serial is involved. The howto suggests sun-jre-bin, but the JRE is not enough to compile some dependencies of ant. You should emerge dev-java/sun-jdk if you like official binaries or icedtea6-bin, which is free software. Use eselect java-vm list to see what is installed on your system.

Contents of /etc/portage/package.keywords (with the most relevant rxtx-2.2_pre2)

dev-embedded/arduino ~amd64
=dev-embedded/uisp-20050207-r1 ~amd64
=cross-avr/avr-libc-1.7.1 ~amd64
=dev-embedded/avrdude-5.11.1 ~amd64
=dev-java/rxtx-2.2_pre2 ~amd64

Toolchain was compiled with the following combination:

USE="-openmp" crossdev --target avr --gcc 4.6.2 --binutils 2.21.1 --libc 1.7.1 -s4 --without-headers

Rationale:

  • I tested gcc 4.4.6 (because atmel toolchain uses gcc 4.4) and 4.5.3 (current stable). They both fail on Serial.
  • binutils 2.22 causes a ld segfault when compiling a sketch.
  • avr-libc, as mention by previous posters, fails to compile in its 1.6.2 (stable) version.

Before compilation, I manually altered /usr/portage/eclass/toolchain.eclass, around line 830 (see https://bugs.gentoo.org/show_bug.cgi?id=147155#c72 )

gcc-multilib-configure() {
    # if multilib is disabled, get out quick!
    if ! is_multilib ; then
        confgcc+=" --enable-multilib" # this was --disable-multilib
        return
    else
        confgcc+=" --enable-multilib"
    fi

Do not forget to revert when you finish with the toolchain compilation. After compilation, create missing symlinks for the linker you compiled (see http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1202087650/8#8 ) and for the boards you actually want to use.

cd /usr/avr/lib/
ln -s /usr/lib/binutils/avr/2.21.1/ldscripts/  .
ln -s /usr/avr/lib/avr6/crtm2560.o .
ln -s /usr/avr/lib/avr6/crtm2561.o .
ln -s /usr/avr/lib/avr5/crtm328p.o .

Run gcc-config -l to see which avr-gcc versions are installed. As root, run gcc-config avr-4.6.2. You need to launch a new terminal or type source /etc/profile in the current one for the modification to take effect. gcc-config -c is not sufficient, you need to run avr-gcc -v to really know what version is going to be used in a specific shell or console.

Now that the toolchain is ready, fetch the unofficial arduino overlay (from the playground howto) and duplicate the ebuild to alter it. Keep it in a local overlay (follow https://forums.gentoo.org/viewtopic-t-827407.html ). Actually I used the ebuild from https://scm.narf.ssji.net/svn/gentoo-portage/browser/dev-embedded/arduino/arduino-0023.ebuild?rev=117 (it does not add version numbers to documentation files, this is convention on gentoo from un-slotted ebuilds) but the one from the overlay in the playground should work as well.

Alteration to arduino-0021.ebuild is the following:

-EGIT_COMMIT="4c26e071c8ced7b1bf3c884550d4d77a4c42c554"
+EGIT_COMMIIT="f520bb505134893b36182085fc1bfdc301d5bf89"

Say, you call the new file arduino-0100.ebuild.

The modification will update to the version of 30th Dec. 2011. Later alterations will appear at page https://github.com/arduino/Arduino/commits/master . (Select the topmost alteration, the commit number appears in its URL and is referenced in the content of the linked page.) I guess removing totally the line will always update to the latest master (as in -9999 ebuilds), but the patch below is not guaranteed to work in all future versions.

There should be a patch in the files/ directory. Its content needed small adaptations. Here is the version that made it in my system. Call it arduino-0100.patch

--- build/linux/work/arduino.orig       2011-12-31 23:25:05.771005662 +0000
+++ build/linux/work/arduino    2011-12-31 23:41:49.500993674 +0000
@@ -10,13 +10,12 @@
     lib/*.jar \
     ;
 do
-    CLASSPATH="${CLASSPATH}:${LIB}"
+    CLASSPATH="${CLASSPATH+$CLASSPATH:}${APPDIR}/${LIB}"
 done
+CLASSPATH="${CLASSPATH+$CLASSPATH:}$(java-config -dp antlr)"
 export CLASSPATH

-LD_LIBRARY_PATH=`pwd`/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
+LD_LIBRARY_PATH="$(java-config -di rxtx-2)${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}"
 export LD_LIBRARY_PATH

-export PATH="${APPDIR}/java/bin:${PATH}"
-
 java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel processing.app.Base

Create the Manifest for the ebuild and the patch with ebuild arduino-0100.ebuild digest. You should now be able to emerge --ask arduino and compile the new version.

Before launching the IDE, double-check avr-gcc -v really is 4.6.2, as sketch compilation fails with other versions.

tarik: Ok I've found what the last problem is.

Caution! This is only for Gentoo. To overcome that, "multilib" must be enabled. Open /usr/portage/eclass/toolchain.eclass and change --disable-multilib to --enable-multilib

Then, compile your avr environment by USE="-openmp" crossdev --target avr --gcc 4.5.1-r1 --binutils 2.21.1 --libc 1.7.1 -s4 --without-headers ln -s /usr/i686-pc-linux-gnu/avr/lib/ldscripts /usr/avr/lib/ldscripts ln -s /usr/avr/lib/avr6/crtm2560.o /usr/avr/lib/crtm2560.o ln -s /usr/avr/lib/avr6/crtm2561.o /usr/avr/lib/crtm2561.o

DO NOT FORGET to take the modification back at /usr/portage/eclass/toolchain.eclass

Then modify /usr/avr/include/util/delay.h according to @SurferTim's entry.

This configuration compiles fine and and gives no error. BUT! It still creates faulty binaries with the serial functions. Blink compiles and runs fine but a basic serial code won't run in any way, but that compiles fine too.

The versions I've installed are verified by you as working fine, but those won't work in my case. Any idea?

Modifing eclass is a bad idea

sudo  USE="-openmp multilib" crossdev --target avr --gcc 4.5.3-r1 --binutils 2.21.1-r1 --libc 1.7.1 --without-headers

Work for me