Teensy 4.0, use of 2 different i2c bus

Status
Not open for further replies.

Guigui56

Member
Hello,

Looking to switch from Teensy 3.6 to 4.0. T4(s) ordered today.


In my T3.6 code, i use 2 i2c bus, with the i2c_t3.h library.

When i switch Arduino software to teensy 4.0, i can't compile anymore. it's logical as the i2c_t3.h library is only for Teensy card up to 3.6... so i get a nice "Wire was not declared in this scope"...

What is the right library to use for i2c to enjoy all the i2c ports ?

Thanks for your help :)
 
As far as I know, the i2c_t3 library has not been ported over to the T4.

However the Wire library does (and has) support all of the I2C busses on all of the Teensy boards.

But the I2C_t3 library was not developed to be 100% compatible with the Wire library.

With Wire library, you use the Wire object to talk to the first one, Wire1, Wire2, ... for the other Wire objects.

To use alternative pins (not sure if there are any on T4), you can do like:
Code:
Wire.setSCL(19);
Wire.setSDA(18);
But this will NOT work on T4 (however would work on T3.6 as these are Alternative pins on 3.6 for the Wire object)
Code:
Wire.setSCL(16);
Wire.setSDA(17);

Instead on T4, you would do:
Code:
Wire1.setSCL(16);
Wire1.setSDA(17);

But again these are the default and I think only pins for these two.
 
ok so switched back to Wire.h and it does compile, now let's wait the cards for real testing :) I will use Default pins.

Thanks a lot
 
According to the pinout chart, there are no alternate I2C pins, but there are 3 I2C buses on the Teensy 4.0:

  • Wire: Pin 19/A5 is SCL0, pin 18/A4 is SDA0;
  • Wire1: Pin 16/A2 is SCL1, pin 17/A3 is SDA1; (and)
  • Wire2: Pad (underneath the Teensy) 24/A10 is SCL2, and pad 25/A11 is SDA2.

If your device does not have pull-up resistors, you will need a pull-up resistor between each of the 2 I2C pins for a particular bus and 3.3v. A 2.2k resistor is often the best choice for a 3.3v system for pull-up resistors.

I recall that previous versions of Teensydunio also made Wire1, Wire2, etc. work on the other Teensy LC/3.x platforms.
 
The pins associated with Wire1 and Wire2 have the same internal Pull up capabilities as other IO pins on the T4. So the main question may be are the Internal PU resistors or the possible PU on a device you wish to use sufficient or do you need external ones? And same answer as Wire... It may or may not and as MichaelMeissner mentioned above an external 2.2K resistor may be a good choice
 
do you know whether wire1 has pull up resistors or not?

Internally, the ARM Teensies do not have pull-up resistors, and you need resistors somewhere on the I2C bus. This isn't unique to Teensys, the M0/M4 ARM platforms from Adafruit that I've used, typically need resistors as well. Of the chips I've used, only the Arduino AVR processors had strong enough internal pull-up resistors that you did not need to add additional resistors.

Some devices provide the resistors, some do not. The usual way to see if the resistors are needed is to run the Examples -> Wire -> Scanner sketch and see if the sketch hangs. If it hangs, you need external pull-up resistors. For the second and third I2C buses, you would need to clone the Scanner script, and change it to use Wire1 or Wire2 instead of Wire. When I'm laying out prototype boards for Teensies, I always include space for the resistors, even though I rarely use I2C these days.

If the device provides the resistors, it can be helpful if there is a solder pad to enable/disable the resistors, since you only need one set of resistors on the bus. From what I've read, having too large of a resistance can make using higher speed I2C transactions impossible.

IIRC, all new Adafruit and Sparkfun I2C devices have resistors, but some of the earlier devices might not have the resistors.
 
Last edited:
Hi @MichaelMeissner - As I mentioned I do believe we do enable the IO Pads Pull up resistors when we do Wire.begin().

That is the begin method:
Code:
#define PINCONFIG (IOMUXC_PAD_ODE | IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(4) | IOMUXC_PAD_SPEED(1) | IOMUXC_PAD_[COLOR="#FF0000"]PKE[/COLOR] | IOMUXC_PAD_[COLOR="#FF0000"]PUE[/COLOR] | IOMUXC_PAD_[COLOR="#FF0000"]PUS(3)[/COLOR])

void TwoWire::begin(void)
{
	// use 24 MHz clock
	CCM_CSCDR2 = (CCM_CSCDR2 & ~CCM_CSCDR2_LPI2C_CLK_PODF(63)) | CCM_CSCDR2_LPI2C_CLK_SEL;
	hardware.clock_gate_register |= hardware.clock_gate_mask;
	port->MCR = LPI2C_MCR_RST;
	setClock(100000);

	// Setup SDA register
	*(portControlRegister(hardware.sda_pins[sda_pin_index_].pin)) = PINCONFIG;
	*(portConfigRegister(hardware.sda_pins[sda_pin_index_].pin)) = hardware.sda_pins[sda_pin_index_].mux_val;
	if (hardware.sda_pins[sda_pin_index_].select_input_register) {
		*(hardware.sda_pins[sda_pin_index_].select_input_register) =  hardware.sda_pins[sda_pin_index_].select_val;
	}

	// setup SCL register
	*(portControlRegister(hardware.scl_pins[scl_pin_index_].pin)) = PINCONFIG;
	*(portConfigRegister(hardware.scl_pins[scl_pin_index_].pin)) = hardware.scl_pins[scl_pin_index_].mux_val;
	if (hardware.scl_pins[scl_pin_index_].select_input_register) {
		*(hardware.scl_pins[scl_pin_index_].select_input_register) =  hardware.scl_pins[scl_pin_index_].select_val;
	}
}
SO the PKE -> Enables Pull/Keep Enable, PUE - Pull UP, PUS(3) - 22K Ohm Pull up
But again it is pretty weak (options are 100K, 47K or 22K)
 
Hi @MichaelMeissner - As I mentioned I do believe we do enable the IO Pads Pull up resistors when we do Wire.begin().
SO the PKE -> Enables Pull/Keep Enable, PUE - Pull UP, PUS(3) - 22K Ohm Pull up
But again it is pretty weak (options are 100K, 47K or 22K)
I missed that. I wonder whether it would help in the general case, or whether it is better to always put in the resistors. However those values look high, since the normal 5v pull-up is 4.7K, 3.3v pull-up is 2.2K, and I've seen some devices that use 10K. But that is what page 1016 of the T4 datasheet says. I don't see any reference to internal pull-ups for I2C in the 3.2 and 3.6 datasheets.
 
I missed that. I wonder whether it would help in the general case, or whether it is better to always put in the resistors. However those values look high, since the normal 5v pull-up is 4.7K, 3.3v pull-up is 2.2K, and I've seen some devices that use 10K. But that is what page 1016 of the T4 datasheet says. I don't see any reference to internal pull-ups for I2C in the 3.2 and 3.6 datasheets.

I agree with you on this, that 22KB is probably way too high, but may keep it slightly happy if nothing hooked up to the pins.

As for T3.x... I believe you are correct. Again from the Wire.begin is the comment:
Code:
// On Teensy 3.0 external pullup resistors *MUST* be used
	// the PORT_PCR_PE bit is ignored when in I2C mode
	// I2C will not work at all without pullup resistors
	// It might seem like setting PORT_PCR_PE & PORT_PCR_PS
	// would enable pullup resistors.  However, there seems
	// to be a bug in chip while I2C is enabled, where setting
	// those causes the port to be driven strongly high.
I wonder if it is still true for the other T3.x boards or not? Probably not worth trying to change it now.
 
Status
Not open for further replies.
Back
Top