Should Teensy 4.0 / 4.1 have WIRE_INTERFACES_COUNT defined?

atok

Member
Hi,

I've spent some time making two of my SSD1306 displays work with u8g2 library using two different i2c ports (Wire & Wire1) on Teensy 4.1 and wanted to share my experience.

The author of this library seems to assume that there is a #define WIRE_INTERFACES_COUNT 3 somewhere in the board definition and only then enables the functionality. Without it the U8G2_SSD1306_128X64_NONAME_F_2ND_HW_I2C constructor creates a no-op object.

The issue is mentioned here but in the context of Teensy 3.x and i2c_t3 (that does not support Teensy 4.x).

I worked around the problem by commenting out the relevant check in U8x8lib.cpp:
Code:
extern "C" uint8_t u8x8_byte_arduino_2nd_hw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
{
// #ifdef U8X8_HAVE_2ND_HW_I2C
  switch(msg)
  {
...
// #endif
  return 1;
}

Not sure if #define WIRE_INTERFACES_COUNT 3 is a standard practice but maybe it is worth adding it? I took a shot at it but I'm not sure where to put it, and platformio does not make it easy to edit those files.
 
Should Teensy's Arduino.h have #define WIRE_INTERFACES_COUNT?

Reposing this question. I ran into this same issue with the U8g2 display library. Apparently some Arduino board packages define macro WIRE_INTERFACES_COUNT to specify the number of hardware I2C ports available. U8g2 expects this macro to exist and allows (or disallows) use of Wire, Wire1, and Wire2 based on its value. I did some searching and couldn't find anything about it being "standard" for Arduino, but I did find a couple of other libraries that expect it to exist. The code below is an example of how the macro is used in TwoWire.h from Arduino Due by C. Maglie.

Code:
#if WIRE_INTERFACES_COUNT > 0
extern TwoWire Wire;
#endif
#if WIRE_INTERFACES_COUNT > 1
extern TwoWire Wire1;
#endif
 
Why not put the following code at the start of your code.
Code:
#if defined(ARDUINO_TEENSY31) || defined(ARDUINO_TEENSY32) || defined(ARDUINO_TEENSYLC)
uint8_t WIRE_INTERFACES_COUNT = 2;
#elif defined(ARDUINO_TEENSY35) || defined(ARDUINO_TEENSY41) || defined(ARDUINO_TEENSY40)
uint8_t WIRE_INTERFACES_COUNT = 3;
#elif defined(ARDUINO_TEENSY36)
uint8_t WIRE_INTERFACES_COUNT = 4;
#else
uint8_t WIRE_INTERFACES_COUNT = 0;
#endif
 
Why not put the following code at the start of your code.

That wouldn't solve the problem, because it's the external library that needs the #define. My work-around is to edit the library source, but I'd have to do that each time a library update is released.
 
Sounds like we should probably do this.

Which other boards define WIRE_INTERFACES_COUNT? Or to be more specific, can anyone point to the specific file and line numbers where they define WIRE_INTERFACES_COUNT? Would like to see this so I can know we'll do it the same way.
 
Sounds like we should probably do this.

Which other boards define WIRE_INTERFACES_COUNT? Or to be more specific, can anyone point to the specific file and line numbers where they define WIRE_INTERFACES_COUNT? Would like to see this so I can know we'll do it the same way.

So far I have found it in these board packages on my own computer.

Adafruit SAMD 1.7.10
Arduino SAMD 1.8.13
GrumpyOldPizza STM32L4 0.0.28
Arduino-nRF5

They all seem to do it the same way. Each variant.h file has #define WIRE_INTERFACES_COUNT (value) for that variant, and then Wire.h and Wire.cpp use the macro.

As a specific example, arduino\hardware\samd\1.8.13\mkrzero\variant.h, contains these lines

Code:
  94   #define SPI_INTERFACES_COUNT 2
 132  #define WIRE_INTERFACES_COUNT 1
 150  #define I2S_INTERFACES_COUNT 1

adafruit\hardware\samd\1.7.10\variants\feather_m4\variant.h is very similar.

Let me know if this is what you're looking for. Also, the library where I first encountered it is U8g2.
 
Ok, looks like they're expecting to it be in the core library, not Wire.h (where I would have preferred to put this).

We don't have a variant.h file. I'm guessing pins_arduino.h is probably our best option. Looks like u8g2 is including Arduino.h to get this define. Seems like it should work...
 
Ok, looks like they're expecting to it be in the core library, not Wire.h (where I would have preferred to put this).

We don't have a variant.h file. I'm guessing pins_arduino.h is probably our best option. Looks like u8g2 is including Arduino.h to get this define. Seems like it should work...

Yes, that sounds right. Is there anything else I can do?
 
Hello,

I ran into the same problem, with u8g2 libary on a Teensy4.1, but using SPI1 instead of there here mentionend Wire1 interface.

Could
#define SPI_INTERFACES_COUNT 2
be added in the same manner?

Thanks for all the great Teensy stuff!

Bob.
 
Back
Top