earlephilhower / arduino-pico

Raspberry Pi Pico Arduino core, for all RP2040 and RP2350 boards
GNU Lesser General Public License v2.1
2.03k stars 422 forks source link

Serial interfering with i2C on same pin #1307

Closed risq closed 1 year ago

risq commented 1 year ago

Hello,

I just resolved a problem with an i2c device but I don't understand why my initial code did not work. Sorry in advance if this is the expeded behavior or if this is not linked to arduino-pico core but I would like to be sure that I did everything right. And maybe this could help someone who would have the same problem in the future.


Here is a simplified version of my code :

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI1);

void setup()
{
  // ...

  // initializing i2c
  Wire.setSDA(SDA_PIN);
  Wire.setSCL(SCL_PIN);
  Wire.begin();

  // ...

  // initializing MIDI
  Serial1.setTX(12);
  MIDI1.begin(MIDI_CHANNEL_OMNI);

  // ...

  writeEEPROM(0x50, 1, 123); // not working
}

Here by investigating a little I got a Wire error 4 (other twi error (lost bus arbitration, bus error, ..)). I finally found that removing MIDI initialization line would fix the issue. As Serial1 default TX pin is GP0 it looks that it is interfering with i2c on the same pin, even if TX pin was changed before begining MIDI transmission. How is it possible ?

The only way I found to make it work was to move my Wire initialization code after MIDI initialization. Is it the expected behavior ?

Thanks for the clarification.

earlephilhower commented 1 year ago

I have no idea what library you're using for MIDI or what it's doing behind the scenes, but justby inspection it seems you have not set the Serial1.setRX() pin to something not overlapping your Wire and hence, when Serial1.begin(xxx) is called the shared Wire/Serial GPIO is redirected from Wire to Serial1. I suggest adding a setRX() to some legal, unused pin.

risq commented 1 year ago

You are right ! I don't know I could not see it... Indeed I only use MIDI out so I don't use Serial RX at all, but I totally forgot to change it. Thanks a lot.

PS : sorry for not having linked the MIDI library I use. For the record, here it is : arduino_midi_library and the relevant file : https://github.com/FortySevenEffects/arduino_midi_library/blob/master/src/serialMIDI.h#L56