[SOLVED] I2C2 on STM32F103C8T6 and HWire

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
Winnie_The_Pooh
Posts: 5
Joined: Mon Oct 24, 2016 10:31 am

Re: I2C2 on STM32F103C8T6 and HWire

Post by Winnie_The_Pooh » Fri Jun 16, 2017 7:14 am

Morning, Luca!

here it is https://yadi.sk/d/NyZlmcEs3KBF3w

But smth tells me, that it will work absolutely the same :)

luca_stm32
Posts: 17
Joined: Wed Apr 26, 2017 12:39 pm

Re: I2C2 on STM32F103C8T6 and HWire

Post by luca_stm32 » Sun Jun 18, 2017 6:30 pm

Hi Winni_The_Pooh
Yesterday I test the code using your repository image, but, as you already said me, it was the same.
But I noticed a thing: in your second working examples you call HWire.begin() function twice, one time in setup function and again in write_dac function.
If I call HWire.begin() twice, the second time I call it the code stop working (I tested it using Serial.print (" TPX ") before and after the HWire.begin() function).
I feel like that I2C2 in my case is not configured in a corrected way and when I try to write on it the code stuck.
So I have two questions: what kind of board are you using? I'm using Blue Pill.
The second question: I would like to use Serial.print in i2c.c library to make more deep debug. How can I use it? I tried but I have a compiler error. Or is another way to debug in i2c.c library without stlink?

Thanks again.
Luca.

User avatar
Pito
Posts: 1498
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: I2C2 on STM32F103C8T6 and HWire

Post by Pito » Sun Jun 18, 2017 6:47 pm

@Luca: do you have wired 4k7 resistor pullups on the SDA and SCL??
I used to use (now not handy however) two I2C at once with MapleMini (or BP), ie:

Code: Select all

HardWire HWire(1, I2C_FAST_MODE); // I2C1
HardWire HWire2(2, I2C_FAST_MODE); // I2C2
Mind you have to use "HWire..." everywhere in the respective drivers with all devices hanging on the I2C1, and, you have to use "HWire2..." everywhere in the respective drivers with all devices hanging on the I2C2, also you need 2x4k7 (or similar value) pullup resistors on SDA and SCL on both I2C1 and I2C2.
Pukao Hats Cleaning Services Ltd.

luca_stm32
Posts: 17
Joined: Wed Apr 26, 2017 12:39 pm

Re: I2C2 on STM32F103C8T6 and HWire

Post by luca_stm32 » Mon Jun 19, 2017 12:54 pm

@Pito: I wired 4k7 resistors on SDA and SCL lines and I'm quite sure hardware is ok. Infact, if I don't change hardware and I use software implementation of I2C on pins PB10 and PB11, I can read/write external EEProm.
On the same hardware, if I use the hardware implemetation of I2C2 (located on pins PB10 e PB11), I can't write and read from external EEProm.

If I connect external EEProm on pins PB6 and PB7 (I2C1) I can read and write external EEProm with software AND hardware I2C library (Twowire and Hwire).

My declaration for I2C2 is:

Code: Select all

HardWire H2Wire(2, I2C_FAST_MODE); // I2C2
and then I use the functions:

Code: Select all

H2Wire.begin();
....
H2Wire.beginTransmission(deviceaddress);
temp = eeaddress >> 8;
H2Wire.write((uint8_t)temp);   // MSB
temp = eeaddress & 0xFF;
H2Wire.write((uint8_t)temp); // LSB
H2Wire.endTransmission();
H2Wire.requestFrom(deviceaddress,1);

zmemw16
Posts: 1369
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: I2C2 on STM32F103C8T6 and HWire

Post by zmemw16 » Mon Jun 19, 2017 1:40 pm

software i2c must be slower than HW fast mode, so try HW slow mode ?
srp

User avatar
Pito
Posts: 1498
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: I2C2 on STM32F103C8T6 and HWire

Post by Pito » Mon Jun 19, 2017 1:41 pm

This is how I read register(s) off the BMP085 pressure sensor, while on I2C2 (Maple Mini):

Code: Select all

..
HardWire HWire(2, I2C_FAST_MODE); // I2C2
..
 HWire.begin();
..
 // Read 1 byte from the BMP085 at 'address'
char bmp085Read(unsigned char address)
{
  unsigned char data;

  HWire.beginTransmission(BMP085_ADDRESS);
  HWire.write(address);
  HWire.endTransmission();

  HWire.requestFrom(BMP085_ADDRESS, 1);
  while(!HWire.available())
    ;

  return HWire.read();
}

// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(unsigned char address)
{
  unsigned char msb, lsb;

  HWire.beginTransmission(BMP085_ADDRESS);
  HWire.write(address);
  HWire.endTransmission();

  HWire.requestFrom(BMP085_ADDRESS, 2);
  while(HWire.available()<2)
    ;
  msb = HWire.read();
  lsb = HWire.read();

  return (short int) msb<<8 | lsb;
}
In parallel I ran DS3231 RTC on I2C1 (HWire1).

Your 24LCxx code for example
http://playground.arduino.cc/Code/I2CEEPROM
http://gammon.com.au/forum/?id=10896&reply=2#reply2
Pukao Hats Cleaning Services Ltd.

luca_stm32
Posts: 17
Joined: Wed Apr 26, 2017 12:39 pm

Re: I2C2 on STM32F103C8T6 and HWire

Post by luca_stm32 » Mon Jun 19, 2017 7:34 pm

Hi Pito.
The first link is exactly the code I used. Infact it works (on I2C1 hardware and software version and I2C2 only software version).
I saw also the second link, but the function to read/write the EEProm are the same.

I tried also changing board (another Blue Pill) but with the same result.

I follow also the Winni_The_Pooh instructions changing variabile type, but doesn't work.

The only main difference is that I use a Blue PIll (STM32F103C8) and you are using a MIni Maple (STM32F103CB), but the only difference seems to be flash memory...

I tried also to write I2C2 registers with Serial.print on the sketch, but nothing appares strange.

@zmemw16: how can I put hardware I2C in slow mode?

Luca

luca_stm32
Posts: 17
Joined: Wed Apr 26, 2017 12:39 pm

Re: I2C2 on STM32F103C8T6 and HWire

Post by luca_stm32 » Tue Jun 20, 2017 11:39 am

Hi everybody.
Today a made a simple test: using the SAME code, but compiling choosing "MiniMaple" instead "Generic STM32F103C Series", the hardware I2C2 works :D

Now I have to understand why.

Luca.

User avatar
RogerClark
Posts: 6675
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: I2C2 on STM32F103C8T6 and HWire

Post by RogerClark » Tue Jun 20, 2017 12:02 pm

luca_stm32 wrote:
Tue Jun 20, 2017 11:39 am
Hi everybody.
Today a made a simple test: using the SAME code, but compiling choosing "MiniMaple" instead "Generic STM32F103C Series", the hardware I2C2 works :D

Now I have to understand why.

Luca.
There is some speculation that some STM32F103's are clones and not official ones. In which case it could explain why one board works and another doesnt.

I will PM @fpSTM and ask him if he knows of any clones

Winnie_The_Pooh
Posts: 5
Joined: Mon Oct 24, 2016 10:31 am

Re: I2C2 on STM32F103C8T6 and HWire

Post by Winnie_The_Pooh » Tue Jun 20, 2017 12:05 pm

Hi, Luca!

Yes, I've used blue pill.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests