PDA

View Full Version : Wire compatibility



sumotoy
11-24-2012, 06:13 PM
There's any plan to complete wire functions? I noticed that slave mode is still not implemented, endTrasmission not return any code (apart 0) and looking inside wire.cpp I just discovered that wire works only at 48 or 24 Mhz and I2C speed cannot be set apart 100Khz (at list by reading comments inside code, dunno if it's true).

PaulStoffregen
11-24-2012, 08:08 PM
I do indeed have a plan regarding the Wire library slave mode... but that plan is basically to prioritize work on Wire after a few of the USB types (especially MIDI), speeding up analogRead, implementing analogWriteFrequency (that's what I'm working on today), and other more urgent stuff.

Are there any published programs that depend upon endTransmission's return code? I can probably look at this sooner, if it's really needed. Unless you can point me to at least one widely published program which actually uses the return value, I'm not going to work on endTransmission until getting into the slave mode stuff.

Like most peripherals in this chip, the I2C is clocked from the bus clock. When you set the CPU to 96 MHz, the bus clock is still 48 MHz. In theory, the PLL and clock dividers could be programmed to generate many different clocks, but Teensyduino doesn't support any others, because they don't work with USB. That's why the Tools > USB Type menu is populated with only 3 choices. The main PLL has to be a multiple of 24 MHz for the USB to work, and the CPU, Bus and Flash clocks have to be power of 2 divisions of the PLL and integer multiples of each other.

Just like the Wire library when used on Arduino, the bit rate is fixed at 100 kHz. At least some programs directly write to the AVR TWBR register, because Wire doesn't provide an API to set the speed. I'm considering adding code to emulate TWBR.

If you look in Wire.cpp, I left some code in there to set other speeds. For example:



#if F_BUS == 48000000
I2C0_F = 0x27; // 100 kHz
// I2C0_F = 0x85; // 400 kHz
// I2C0_F = 0x0D; // 1 MHz


If you want those other speeds, just uncomment the line you want.

sumotoy
11-24-2012, 10:34 PM
Thanks a lot Paul!!!.
I think the most urgent is Slave Mode since endTransmission return code have some utility only because it's the only way I know to scan the I2C bus in search of devices (twi utility are not present in teensy3).
I finally find out a lot of useful infos!

DougALug
12-07-2012, 03:17 AM
Hey Paul,

I could also use the I2c Slave. I've had a bear of a time making my Arduino Nano interface via i2c (Nano being the slave) with a PIC18F. For some reason, using the wire libraries, my Nano wants to keep the SDA line low after receiving a write bit. This makes the PCI think it is in a multi-master configuration and it stops communication. Unfortunately the Nano is just one of about 5 peripherals on the bus so I can't make it a master. I was fiddling with my teensy3 only to discover no slave functionality in the Teensy libs. :(

-Doug

el_supremo
12-07-2012, 03:15 PM
My patch described in this thread: http://forum.pjrc.com/threads/562-TwoWire-endTransmission-always-return-0 will make a bus scanner work.

Pete

PaulStoffregen
01-19-2013, 11:39 PM
Just a quick followup for anyone watching this old thread....

Beta12 has an improved Wire library which should fix the return status codes and should fully support slave mode.