I have a project which involves talking to OneWire chips (DS2431) through a I2C switch (PCA9548). What I want to do is configure the switch properly (just write a byte to its state register) then use the mbed SDA pin for OneWire communication. The switch doesn't care about the SDA line (it can even pass DC in both directions) and I've tested it with OneWire successfully. The problem is switching in software from I2C to OneWire on the same pin.
I've tried it the easy way : making a global I2C instance, then a OneWire instance, but the last one always busts the previous one so that I can either have I2C working or OneWire. Is there a way to destruct one instance and create it again?
You can go about it in a few ways, but one of the simpliest ways is to declare the I2C and OneWire instance inside of your main function.
If you need to access the OneWire instance outside of main, you can assign it to a pointer.
Here's some pseudocode:
OneWire *oneWireGlobal;
void func1() {
oneWireGlobal->writeBit(0xFF);
}
void main() {
I2C i2c(I2C_SDA, I2C_SCL);
[I2C operations here...]
OneWire oneWire(I2C_SDA);
oneWireGlobal = &oneWire; // Be sure to do this before accessing "oneWireGlobal"
[oneWire operations here...]
func1();
}