Mega - serial1, 2 und 3 - initialization fails

I was also having this problem with gcc 4.4.3 in Linux.. Downgrading to 4.3.4 fixed it for me..

Before I tried that I found that I could comment out all but one Serial port declaration at the bottom of arduino-0018/hardware/arduino/cores/arduino/HardwareSerial.cpp, and that port would work. It didn't matter which port, but only one could be uncommented.

Eg:

#if defined(__AVR_ATmega8__)
HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
#else
// HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
#endif


#if defined(__AVR_ATmega1280__)
// HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
//HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
#endif

This would allow Serial3 to function.

Ok. Perhaps there´s a problem with the preprocessor-commands?
Because if there´s only one condition after the #if- or #else-command, it seems to work.
But there are arduinos where the serial communication works with all 4 ports. So that can´t be the solution.

i just got my arduino mega today.

my os is osx 10.5.8
i am using arudino 18

i am trying to get newsoftware serial and/or serial1, serial2, serial3 to work.

but i am getting mixed results.
TX works but RX gives me nothing back.
can someone tell me if they extra serial ports work for them on osx ?

thanks,
stephan.

update:
i did some more tests. just using the regular hardware serial and softserial.
i ran the same code on an arduino duemilanove and it transmits and receives just fine.
i am connecting the softserial (and hopefully serial1) to a TTL to rs485 shifter, which sends and receives 8byte arrays from a trinamic motor controller.

i tried commenting out the lines in hardwareSerial.cpp suggested earlier. but no change.

thx.

here is some code that tries software serial and hardware Serial1.

this code works great for software serial on a duemilanove but not a mega.

this code sends a 9 byte command to a motor controller via RS485 chip and wants to receive a 9 byte command back from the motor controller. But never receives anything with the Mega.

byte MotorSerInStr[32];


#define rxPin 2
#define txPin 3
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial =  SoftwareSerial(rxPin, txPin);


int dirPin = 12;
int module = 1; //module# of motor board, check motor board for number

byte command[9];
byte checksum;

int sensorTimer = 0;
int receiveTimer =0;
int newSpeed = 1;

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  Serial.begin(9600);
 // Serial1.begin(9600);
  SoftSerial.begin(9600);
}

void loop() {
  if(millis()-sensorTimer > 200){
    sensorTimer = millis(); 
    cmd_setTargetSteps(newSpeed*200);
    send_cmd();
  }


  if(millis() - receiveTimer > 1000){ 

    receiveTimer = millis();

    cmd_getPosition();
    send_cmd();
    
  // readMotorCommand();  //hardware serial
     readMotorCommand2(); //software serial
  }

 
  if (Serial.available()) {
    newSpeed = Serial.read();
  }
  
}

//send 8bit command to spinmaster
void send_cmd(){

  checksum = 0;
  for(int i=0; i<8; i++){
    checksum = checksum + command[i]; 
  }
  command[8]=checksum;

  digitalWrite(dirPin, 1); //direction pin of RS485 chip
  for(int i=0; i<9; i++){
   //Serial1.print(command[i],BYTE);
    SoftSerial.print(command[i],BYTE);
  }
  digitalWrite(dirPin, 0);

}

void readMotorCommand() //hardware serial1
{ 
  
  Serial.println("readMotorCommand");
  byte str[9];
  digitalWrite(dirPin, 0);  

  if(Serial1.available() ){

   int b = Serial1.read();

    str[0] = b;
    Serial.print(str[0],BYTE);
 
    for(int i=1; i<9; i++){ //
      str[i] = Serial1.read();       // fill it up
      Serial.print(" + ");
      Serial.print(str[i],BYTE); //echo back to ipod
    }
  }
  Serial.println("---");
  
}

void readMotorCommand2() //software serial
{
  Serial.println("readMotorCommand");
   byte str[9];
  digitalWrite(dirPin, 0);


 int b = SoftSerial.read();

  str[0] = b;
  Serial.print(str[0],DEC); //,BYTE);

  for(int i=1; i<9; i++){ //
    str[i] = SoftSerial.read();       // fill it up
    Serial.print(str[i],DEC); //,BYTE); //echo back to ipod
  }

  Serial.println("---");
}




int cmd_setTargetSteps(int temp_value){
  command[0] = module; //module address
  command[1] = 65; // instruction #
  command[2] = 0; // type, changes depending on command
  command[3] = 0; //or 1 motor/bank/axis #
  command[4] = 0; //(temp_value >> 24) & 0xFF; // byte 3
  command[5] = 0; //(temp_value >> 16) & 0xFF; // byte 2
  command[6] = (temp_value >> 8) & 0xFF; // byte 1
  command[7] = temp_value & 0xFF; // byte 0 
}

void cmd_getPosition(){
  Serial.println("--cmd_getPosition--"); //<<endl;
  command[0] = module; //module address
  command[1] = 6; // instruction #
  command[2] = 1; // type, changes depending on command
  command[3] = 0; //or 1 motor/bank/axis #
  command[4] = 0; //(temp_value >> 24) & 0xFF; // byte 3
  command[5] = 0; //(temp_value >> 16) & 0xFF; // byte 2
  command[6] = 0; // byte 1
  command[7] = 0; // byte 0 
}

I did some more tests.

I connect the serial3 from my mega to a serial cable back to my computer and check what was send out through serial3.
the data send was correct, arduino mega serial does work.
BUT still I am only able to successfully communicate with my motor controller, if i use software serial (and newsoftware serial).

does anyone know what the difference between all those serial options are?

thx,
stephan.

Anyone know if there are changes I can make to the HardwareSerial source code to get all the Serial objects working with the avr-gcc 4.4.x compilers?

I didn't read the entire thread. However, FWIW, using an Arduino Mega with Serial for serial communication and Serial3 for a Parallax RFID reader works just fine.

I have a Fedora 13 system and it came with avc-gcc-4.5.0 and avc-gcc-g++-4.5.0 which for whatever reason break all serial output on the Mega (but not on the Duememilnove). I removed those versions and installed the versions from Fedora 10 and now I can get serial output on my Mega.

This is what is current and working on my Fedora 13 x86_64 system.

avr-libc-1.6.7-2.fc13.noarch
avr-binutils-2.20-2.fc13.x86_64
avr-gcc-c++-4.3.3-2.fc11.x86_64
avrdude-5.10-2.fc13.x86_64
avr-gcc-4.3.3-2.fc11.x86_64

For the record commenting the serial port lines out of Hardware.cpp did not work for me. I had to revert avr-gcc

Does any know if this has been filed as a bug? It's seems like a pretty obvious regression if we can pinpoint it. I opened a bug here:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44617

I'm a little over my head in providing them the debugging info they need. Hoping to get this fix for the next release.

Posting one normal message so i can post links.

Had a similar problem with Sanguino (Google Code Archive - Long-term storage for Google Code Project Hosting.). When calling Serial.begin it would hang, replacing SIGNAL with ISR corrected the behaviour.

I don't know if this resolves the problem for the Arduino Mega as i don't have one. Code is untested but compiles.
Patch: http://dpaste.com/hold/211172/

According to the docs (avr-libc: <avr/interrupt.h>: Interrupts) the incorrect interrupts we're being used for 1280 before. I changed those to the correct but don't know if it works. I also replaced all other SIGNAL's with ISR.

same problem here:
arduino programming interface version 18

kernel compiler and libraries info

uname -a
Linux TOOLBOX 2.6.35-rc3 #6 SMP Thu Jun 17 23:16:48 CEST 2010 x86_64 Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz GenuineIntel GNU/Linux



avr-gcc --version
avr-gcc (Gentoo 4.4.3-r2 p1.2) 4.4.3
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gcc --version
gcc (Gentoo 4.4.3-r2 p1.2) 4.4.3
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


/usr/lib64/rxtx-2/librxtxRS485-2.2pre1.so
/usr/lib64/rxtx-2/librxtxRS485.so
/usr/lib64/rxtx-2/librxtxSerial-2.2pre1.so
/usr/lib64/rxtx-2/librxtxI2C.so
/usr/lib64/rxtx-2/librxtxSerial.so
/usr/lib64/rxtx-2/librxtxParallel.so
/usr/lib64/rxtx-2/librxtxParallel-2.2pre1.so
/usr/lib64/rxtx-2/librxtxRaw.so
/usr/lib64/rxtx-2/librxtxI2C-2.2pre1.so
/usr/lib64/rxtx-2/librxtxRaw-2.2pre1.so

java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode)

emerge --info

Portage 2.1.8.3 (default/linux/amd64/10.0, gcc-4.4.3, glibc-2.11.1-r0, 2.6.35-rc3 x86_64)
=================================================================
System uname: Linux-2.6.35-rc3-x86_64-Intel-R-_Core-TM-_i7_CPU_920_@_2.67GHz-with-gentoo-2.0.1
Timestamp of tree: Thu, 24 Jun 2010 16:45:01 +0000
app-shells/bash:     4.0_p37
dev-java/java-config: 2.1.11
dev-lang/python:     2.6.5-r2, 3.1.2-r3
dev-util/cmake:      2.8.1-r2
sys-apps/baselayout: 2.0.1
sys-apps/openrc:     9999
sys-apps/sandbox:    1.6-r2
sys-devel/autoconf:  2.13, 2.65
sys-devel/automake:  1.8.5-r4, 1.9.6-r3, 1.10.3, 1.11.1
sys-devel/binutils:  2.20.51.0.9
sys-devel/gcc:       4.4.3-r2
sys-devel/gcc-config: 1.4.1
sys-devel/libtool:   2.2.8-r1
virtual/os-headers:  2.6.30-r1
ACCEPT_KEYWORDS="amd64"
ACCEPT_LICENSE="*"
CBUILD="x86_64-pc-linux-gnu"
CFLAGS="-O2 -pipe"
CHOST="x86_64-pc-linux-gnu"
CONFIG_PROTECT="/etc /usr/share/X11/xkb /usr/share/config /var/lib/hsqldb"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/env.d /etc/env.d/java/ /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/php/apache2-php5/ext-active/ /etc/php/cgi-php5/ext-active/ /etc/php/cli-php5/ext-active/ /etc/revdep-rebuild /etc/sandbox.d /etc/splash /etc/terminfo /etc/texmf/language.dat.d /etc/texmf/language.def.d /etc/texmf/updmap.d /etc/texmf/web2c"
CXXFLAGS="-O2 -pipe"
DISTDIR="/usr/portage/distfiles"
FEATURES="assume-digests distlocks fixpackages news parallel-fetch protect-owned sandbox sfperms strict unmerge-logs unmerge-orphans userfetch"
GENTOO_MIRRORS="http://mirror.gentoo.no/"
LDFLAGS="-Wl,-O1"
PKGDIR="/usr/portage/packages"
PORTAGE_CONFIGROOT="/"
PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --compress --force --whole-file --delete --stats --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages"
PORTAGE_TMPDIR="/var/tmp"
PORTDIR="/usr/portage"
PORTDIR_OVERLAY="/usr/local/portage"
SYNC="rsync://rsync.ie.gentoo.org/gentoo-portage"
USE="3dnow 3dnowext X Xaw3d a52 aac aalib acl acpi addns ads aio akonadi alisp alsa amd64 amr archive autoipd avahi bash-completion beagle berkdb bidi big-tables bl blender-game bluetooth bonjour bookmarks bs2b bzip2 c3p0 cairo caps cdda cdparanoia cli cluster consolekit contrast cracklib crypt cscope cups cupsd curl cxx daap dbus derby device-mapper dga dhcp dhcpcd dirac directfb disk-partition diskio djvu dri dvb dvd dxr3 eap-sim eds elf emacs examples exchange exif expat extensible extraengine faac faad fasteap fax fbcon ffmpeg fftw fits flac fontforge foomaticdb fortran fpx ftp fuse gcj gconf gdal gdbm gdu ggi gif gimp gnome gnome-keyring google-gadgets gpc gphoto2 gpm gps graphite graphviz gs gsm gstreamer gtk gunit gzip-el hal hardware-carrier hdri hesiod hotpixels hpijs iconv ieee1394 imagemagick imlib inotify ipod ipv6 irda java javascript jbig jce joystick jpeg jpeg2k kde kdrive kismet ladspa lame lastfm latex lcms ldap ldb lensfun libcaca libgda libnl libnotify lirc lm_sensors log4j lqr lua lzo m17n-lib mad madwifi mapnik md5sum mfd-rewrites mmap mmx mmxext mng modules motif mp3 mp3tunes mpeg mtp mudflap multilib multimedia mysql mythtv nas nautilus ncurses network networkmanager nis nls nptl nptlonly nsplugin nut objc objc++ objc-gc odbc odbcmanual odk ogg okular openal openexr opengl openmp pam parport pbxt pch pcre pda pdf perl phonon php plotutils png pnm policykit ppds pppd pulseaudio pvr python q32 q8 qimageblitz qscintilla qt3support qt4 quota qwt radio raster raw readline reflection rle rpm ruby samba scanner schroedinger scripts sdk sdl secure-delete session slang slp smbsharemodes smp smux snmp soap sound soup speech speex spl sql sqlite sse sse2 ssl ssse3 startup-notification static-ppds svg swat swig sysfs syslog system-sqlite taglib tcl tcpd tdbtest teletext templates tga theora thumbnail tiff timezone tk toolkit-scroll-bars tools trace tracker transmitter truetype tslib udev-acl unicode usb util v4l v4l2 vcd vdpau verse vim-pager vim-syntax vorbis wavpack webkit wicd wifi winbind wmf wps wxwidgets x264 xanim xemacs xft xinerama xml xmp xorg xtradb xv xvid xvmc zlib zoran" ALSA_CARDS="ali5451 als4000 atiixp atiixp-modem bt87x ca0106 cmipci emu10k1x ens1370 ens1371 es1938 es1968 fm801 hda-intel intel8x0 intel8x0m maestro3 trident usb-audio via82xx via82xx-modem ymfpci" ALSA_PCM_PLUGINS="adpcm alaw asym copy dmix dshare dsnoop empty extplug file hooks iec958 ioplug ladspa lfloat linear meter mmap_emul mulaw multi null plug rate route share shm softvol" APACHE2_MODULES="actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" CAMERAS="adc65 agfa_cl20 aox barbie canon casio_qv clicksmart310 digigr8 digita dimagev dimera3500 directory enigma13 fuji gsmart300 hp215 iclick jamcam jd11 jl2005a kodak_dc120 kodak_dc210 kodak_dc240 kodak_dc3200 kodak_ez200 konica konica_qm150 largan lg_gsm mars mustek panasonic_coolshot panasonic_dc1000 panasonic_dc1580 panasonic_l859 pccam300 pccam600 polaroid_pdc320 polaroid_pdc640 polaroid_pdc700 ptp2 ricoh ricoh_g3 samsung sierra sipix_blink sipix_blink2 sipix_web2 smal sonix sony_dscf1 sony_dscf55 soundvision spca50x sq905 stv0674 stv0680 sx330z topfield toshiba_pdrm11" ELIBC="glibc" INPUT_DEVICES="evdev keyboard mouse acecad aiptek joystick penmount synaptics tslib void wacom" KERNEL="linux" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" LIRC_DEVICES="all" RUBY_TARGETS="ruby18" USERLAND="GNU" VIDEO_CARDS="nvidia" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq steal rawnat logmark ipmark dhcpmac delude chaos account" 
Unset:  CPPFLAGS, CTARGET, EMERGE_DEFAULT_OPTS, FFLAGS, INSTALL_MASK, LANG, LC_ALL, LINGUAS, MAKEOPTS, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS

bump ?

bump ?

The solution has been mentioned several times already :
downgrade to a avr-gcc 4.3.x version and it works.

Eberhard

isn't there a patch to make everything work with the latest avr-gcc ?

Problem found, I think.
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=736620

Yep, downgraded to avr-gcc-4.3.3 and it worked.

j/k what kind of a giant a55h0le would I be if thats all i posted.

google and download avr-gcc-c++-4.3.3-1.fc10 rpm and avr-gcc-4.3.3-1.fc10 rpm into /home/your-user-name

in a terminal type

su
your-root-password

rpm -i --oldpackage --replacefiles /home/your-user-name/rpm-file-you-downloaded

rpm -i --oldpackage --replacefiles /home/your-user-name/other-rpm-file-you-downloaded

If there's a faster way you should post it. If there isn't... :cry:

Hi,

I am running a seeduino MEGA with only one serial (under windows). My code is running at 4800bps because of the device I am communicating with. If I use 017, everything works A1. If I switch to 021, it look like the RX queue is corrupted and the code does not work properly. I noticed that both windows version 017 and 021 are using avr-gcc 4.3.2. I read that the solution is to go to 4.3.x but I guess it wont help in my case.

Anyone had that problem or may have a clue?

thanx

Mart