earlephilhower / arduino-pico

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

TalkToMySelf.ino and I2C issues #1865

Closed victoryred closed 9 months ago

victoryred commented 9 months ago

Hi Everyone, I'm trying to get the Wire/I2C libraries working with both the TalkToMySelf.ino and I2C_Scan.ino sketches on a Raspberry Pi Pico. Long story short, I believe the the Wire.endTransmission() function/subfunction chain has issues. I'm using version 3.6.1 from github.

Indeed, the endTransmission() function exits with a value of 4. "4: other error. Either !running || !txBegin"

I used the connection setup as instantiated here in Wire.cpp; TwoWire Wire(__WIRE0_DEVICE, PIN_WIRE0_SDA, PIN_WIRE0_SCL); TwoWire Wire1(__WIRE1_DEVICE, PIN_WIRE1_SDA, PIN_WIRE1_SCL);

I made no pin changes in the code. BTW what is the clock speed? Can't verify it.

Regards

earlephilhower commented 9 months ago

What exactly is your output from TalkingToMyself.ino? It is working just fine and unchanged from 3.6.0 to 3.6.1 here:

3.6.1:

Sending...
buff: 'pass 0'

recv: '0002FD'
Sending...
buff: 'pass 1'

recv: '0002FE'
Sending...
buff: 'pass 2'

recv: '0002FF'
Sending...
buff: 'pass 3'

recv: '000300'
Sending...
buff: 'pass 4'

recv: '000301'
Sending...
buff: 'pass 5'

recv: '000302'
Sending...
buff: 'pass 6'

recv: '000303'
Sending...
buff: 'pass 7'

recv: '000304'
Sending...
buff: 'pass 8'

recv: '000305'
Sending...
buff: 'pass 9'

recv: '000306'

3.6.0:

Sending...
buff: 'pass 0'

recv: '0002FD'
Sending...
buff: 'pass 1'

recv: '0002FE'
Sending...
buff: 'pass 2'

recv: '0002FF'
Sending...
buff: 'pass 3'

recv: '000300'
Sending...
buff: 'pass 4'

recv: '000301'
Sending...
buff: 'pass 5'

recv: '000302'
Sending...
buff: 'pass 6'

recv: '000303'
Sending...
buff: 'pass 7'

recv: '000304'
Sending...
buff: 'pass 8'

recv: '000305'
Sending...
buff: 'pass 9'

recv: '000306'

You obviously need to read the sketch and do the hookup of GPIOs, or it won't be able to talk to itself. 0->2, 1->3 (note on the RP2040 the GPIO # is not the same as the board pinout, there's a GND between GP1 and GP2).

victoryred commented 9 months ago

Hi Earle, Thank you for your help and advice. This is the output from my testing.

12:36:58.037 -> 12:36:58.037 -> Wire receiving from Wire1: '' recv: Cnt= 0 12:36:59.037 -> Beginning transmission. Error code- 0:Success 12:36:59.037 -> Finished transmission. Error code- 4: other error. Either !running || !txBegin 12:37:00.035 -> Wire1 receiving from Wire. 12:37:00.035 -> buff: '' 12:37:00.035 ->

But this is what is peculiar, I have Wire.cpp open in VSCode and I hover over the passed initialization parameters.

TwoWire Wire(__WIRE0_DEVICE, PIN_WIRE0_SDA, PIN_WIRE0_SCL); TwoWire Wire1(__WIRE1_DEVICE, PIN_WIRE1_SDA, PIN_WIRE1_SCL);

The devices are i2c0, i2c1 The data and clock pins are (GPIO) 4u, 5u for i2c0 and (GPIO) 26u, 27u for i2c1. I don't mind redefining the pins in the .ino file. But what forces that to be necessary to make the change to 0,1 and 2,3.

I made the change with the same result.

earlephilhower commented 9 months ago

Your output doesn't match what the sketch prints at all.

That said, generally printing an empty buff means you've got a wiring issue. I would not trust VSCode to display the proper PIN_WIRE0_SDA/etc. pins. There are literally dozens of places those values are defined, and the right one will depend on the board configuration and not the 1st one that comes into scope in the tree. They're part of the variant config.

Can you please run the unmodified sketch with the GPIOs hooked up as requested in it? There's nothing special about the GPIOs 0/1,2/3, they're just simple to wire up.

victoryred commented 9 months ago

Hi Earle, I'm including some snippets of code.

From /home/penta/Arduino/hardware/pico/rp2040/variants/rpipico/pins_arduino.h // Wire

define PIN_WIRE0_SDA (4u)

define PIN_WIRE0_SCL (5u)

define PIN_WIRE1_SDA (26u)

define PIN_WIRE1_SCL (27u)

From /home/penta/.arduino15/packages/rp2040/hardware/rp2040/3.6.1/libraries/Wire/src/Wire.cpp

My changes-

uint8_t TwoWire::beginTransmission(uint8_t addr) { if (!_running || _txBegun) { // ERROR return 4; } _addr = addr; _buffLen = 0; _buffOff = 0; _txBegun = true; return 0; }

and the source for the error codes......

// Errors: // 0 : Success // 1 : Data too long // 2 : NACK on transmit of address // 3 : NACK on transmit of data // 4 : Other error // 5 : Timeout uint8_t TwoWire::endTransmission(bool stopBit) {

earlephilhower commented 9 months ago

Sorry, you have a wiring problem. GPIO and pinout on the PCB are not 1:1 (there are gnd/vcc/etc. intersped).

I just took the TalkingToMyself sketch on a plain Pico PCB and did the following change to setup():

void setup() {
  Serial.begin(115200);
  delay(5000);
//  Wire.setSDA(0);
//  Wire.setSCL(1);
  Wire.begin();
//  Wire1.setSDA(2);
//  Wire1.setSCL(3);
  Wire1.begin(0x30);
  Wire1.onReceive(recv);
  Wire1.onRequest(req);
}

Wired GP4->GP26, GP5->GP27cand ran and it's all good:

Sending...
buff: 'pass 0'

recv: '0002FD'
Sending...
buff: 'pass 1'

recv: '0002FE'
Sending...
buff: 'pass 2'

recv: '0002FF'
Sending...
buff: 'pass 3'

recv: '000300'
Sending...
buff: 'pass 4'

recv: '000301'
Sending...
buff: 'pass 5'

recv: '000302'
Sending...
buff: 'pass 6'

recv: '000303'
Sending...
buff: 'pass 7'

recv: '000304'
Sending...
buff: 'pass 8'
....
victoryred commented 9 months ago

I thank you, Earle! I know you are right...

I ran the original .ino file with the GPIO 0/2 ,1/3

13:35:34.725 -> Sending... 13:35:36.722 -> buff: '' 13:35:36.722 -> 13:35:36.722 -> recv: '' 13:35:36.722 -> Sending... 13:35:38.717 -> buff: '' 13:35:38.717 -> 13:35:38.717 -> recv: '' 13:35:38.717 -> Sending... 13:35:40.712 -> buff: '' 13:35:40.712 -> 13:35:40.712 -> recv: '' 13:35:40.712 -> Sending... 13:35:42.741 -> buff: '' 13:35:42.741 ->

Ouch! I'll double check the new breakout board from China. Don't hit me.......;) I'll let you know.

victoryred commented 9 months ago

Like Phil Mickelson said at Winged Foot- "I'm an idiot!" Earle, you have great patience.

earlephilhower commented 9 months ago

No worries! Good luck.