Bug in VirtualWire

Hi, I have been trying to get VirtualWire working with my Arduino and have been having a very frustrating time, with the chip crashing and breaking any timing functions (delay(), millis(), etc). I finally narrowed the problem down to the use of digitalRead() inside the Interrupt Service Routine. I’m not sure why it doesn’t work, but digitalRead seems to break when called from within an ISR.
Does anybody have any experience with this? I have been able to fix the problem in a hackish way by hardcoding the pin read using boolean logic.
This code snippit starts on line 408 of VirtualWire.cpp
(I think this is a super useful library and it’s a shame that it breaks your Arduino!)

// This is the interrupt service routine called when timer1 overflows
// Its job is to output the next bit from the transmitter (every 8 calls)
// and to call the PLL code if the receiver is enabled
    // My change here: Read from pin 3 on PORTD (Pin 11)
    vw_rx_sample = (PIND & (1 << 3))>>3;//digitalRead(vw_rx_pin);
    // End change

    // Do transmitter stuff first to reduce transmitter bit jitter due 
    // to variable receiver processing
    if (vw_tx_enabled && vw_tx_sample++ == 0)
        // Send next bit
      // Symbols are sent LSB first
        // Finished sending the whole message? (after waiting one bit period 
      // since the last bit)
        if (vw_tx_index >= vw_tx_len)
          digitalWrite(vw_tx_pin, vw_tx_buf[vw_tx_index] & (1 << vw_tx_bit++));
          if (vw_tx_bit >= 6)
              vw_tx_bit = 0;
    if (vw_tx_sample > 7)
      vw_tx_sample = 0;

    if (vw_rx_enabled)

Which version of the Arduino software are you using? If 0013, try it with 0012.

No, I'm running 0012. Also, the transmitter is working, but it appears that somewhere the receive code is still broken. I'm working on it, but I'm not sure what I'm looking for.

Well, there's apparently a problem in the version of avr-gcc (4.3.0) included with Arduino 0012 with functions called from interrupts. We've seen it show up in Arduino 0013 (which has the same version of avr-gcc), but it could also be a problem in Arduino 0012. I'm hoping to release Arduino 0014, with a new version of avr-gcc, soon.

If that's the case, then I'm happy. Only question is how do I get the new version of avr-gcc on ubuntu intrepid? Is this the tool here? http://www.avrfreaks.net/index.php?module=Freaks%20Tools&func=viewItem&item_id=145

I took the time to install avr-gcc 4.3.2 from the Ubuntu Jaunty repositories and can confirm that this fixes the problem. avr-gcc 4.3.0 apparently has a bug w/ integer multiplication or something…

For people running Intrepid, the repository I used was:
deb http://mirrors.kernel.org/ubuntu jaunty main universe

Just mark gcc-avr for upgrade and make sure that you install avr-libc since the upgrade seems to remove it. That fixed my problem.

Thanks to mellis for the tip!
– John


also, I found that using arduino 13 with ubuntu 8.10 and updated gcc-avr (4.3.2) ended up with badly broken code. Not sure of how or why - don't have the time to get into this now.

I just went back to arduino 12 and that fixed it.