Using 2nd I2C peripheral with STM32

Hello,

I don't want to mess with existing data flow of 1st I2C peripheral of STM32 (blue pill), it's busy enough already, and trying to make 2nd one to work, but I haven't had much luck with that. I'm not looking for other workaround, I need to make 2nd I2C to work.

Maybe community will be able to help?

EDIT, solution:

Treat "wire2" as regular "wire".

TwoWire Wire2 (2, I2C_FAST_MODE); // I2C_2 (PB11-SDA, PB10-SCL )

//use IIC2
TwoWire WIRE2 (2,I2C_FAST_MODE);
#define Wire WIRE2

//TwoWire WIRE(1,I2C_FAST_MODE); // I2c1
//TwoWire WIRE(2,I2C_FAST_MODE); // I2c2

The "REPORT TO MODERATOR" button is NOT for reply's LOL

Bob.

Jakkrapong:
//use IIC2
TwoWire WIRE2 (2,I2C_FAST_MODE);
#define Wire WIRE2

//TwoWire WIRE(1,I2C_FAST_MODE); // I2c1
//TwoWire WIRE(2,I2C_FAST_MODE); // I2c2

I am trying to use I2C1 with STM32 (SCL1-PB8,SDA1-PB9). I am testing the i2c_scanner_wire example. I get no I2C device found, softwire example detects the i2c device correctly.
Here is my code.

#include <Wire.h>
#define I2C_SDA PB9
#define I2C_SCL PB8
//use IIC2
TwoWire WIRE1 (1,I2C_FAST_MODE);
#define Wire WIRE1


void setup() {
  delay(5000*2);
  Serial.begin(115200);
 
  Wire.begin();
  Serial.println("\nI2C Scanner");
}


void loop() {
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.

    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) 
        Serial.print("0");
      Serial.println(address, HEX);

      nDevices++;
    }
    else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) 
        Serial.print("0");
      Serial.println(address, HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found");
  else
    Serial.println("done");

  delay(5000);           // wait 5 seconds for next scan
}

It must depend whether youre using the 'official' ST core, or the Maple core.

TwoWire WIRE2 (2,I2C_FAST_MODE);

complains that I2C_FAST_MODE is undefined, and when I dig through the headers, it appears that TwoWire only needs the pin no for SDA and SCL that you want to use.

On 'official,' this works to use I2C-2 on PB10 and 11:

TwoWire Wire1(PB11, PB10);// Use STM32 I2C2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, OLED_RESET);

...however, I'm still sorting out a few things. trying to use something like 'Wire1.setClock(400000)' complains that ''Wire1' does not name a type' on compile.