Hardwire problems

Post Reply
alex9x
Posts: 3
Joined: Tue Oct 04, 2016 10:23 pm

Hardwire problems

Post by alex9x » Sun Oct 22, 2017 12:30 pm

Hello!
I`m working with F103 family and faced with hardwire problems.

Hardwire library from STM32F1\libraries\Wire works fine if make Hardwire declaration in main .ino file before setup(), but failed if move to library and make Hardwire declaration in function with passed to function port. Need to use I2C1 and I2C2 depends on function parameter, for example:

Code: Select all

void Scan_I2C(int port) {
  HardWire HWire(port, I2C_FAST_MODE);
  HWire.begin();
........
  HWire.end();
}
It don`t work, HWire.endTransmission() always return 1.

Other method - send HWire to function:

Code: Select all

#include <Wire.h>
HardWire HWire1(1, I2C_FAST_MODE);
HardWire HWire2(2, I2C_FAST_MODE);

void setup() {
..........
HWire1.begin();
HWire2.begin();
}

void loop() {
Scan_I2C(&HWire1);
Scan_I2C(&HWire2);
..........
}


void Scan_I2C(HardWire *port) {
HardWire HWire = *port;
.........
HWire.beginTransmission(address);
int error = HWire.endTransmission();
..........
}
works, but only one time. Next time function calling halt on HWire.endTransmission()

If add HWire.end() in the end of function - it also halt on it.

Any ideas about this?
Thanks!

alex9x
Posts: 3
Joined: Tue Oct 04, 2016 10:23 pm

Re: Hardwire problems

Post by alex9x » Sun Oct 29, 2017 6:27 pm

Nobody have any ideas? :|
Maybe anybody know any other libraries, which use Hardwire library except LCD libraries?

victor_pv
Posts: 1745
Joined: Mon Apr 27, 2015 12:12 pm

Re: Hardwire problems

Post by victor_pv » Tue Oct 31, 2017 3:05 am

I have used if for eeprom and fram memories without an issue, but I don't declare hardwire within the library code. In fact, if I remember right, I don't declare it in main, since i2c1 I believe is declared within the wire library code (but may be wrong, if not, I declared it as global outside on Main).

What about this?

Code: Select all

void Scan_I2C(HardWire *port) {
HardWire HWire = *port;
Shouldn't that be rather like this?

Code: Select all

void Scan_I2C(HardWire *port) {
HardWire *HWire = port;
Or another option:

Code: Select all

#include <Wire.h>
HardWire HWire1(1, I2C_FAST_MODE);
HardWire HWire2(2, I2C_FAST_MODE);

void setup() {
..........
HWire1.begin();
HWire2.begin();
}

void loop() {
Scan_I2C(HWire1);
Scan_I2C(HWire2);
..........
}


void Scan_I2C(HardWire &port) {
.........
port.beginTransmission(address);
int error = port.endTransmission();
..........
}
I think part of the problem with your other code (the one that declares the object within a function) is that you would be calling a constructor and a destructor each time the function is called, and perhaps the destructor in the wire library doesn't do it's job correctly. I haven't looked at the code, just an idea...

Post Reply