I2C sensor sample script compiles for Arduino AVR boards, but not Teensy

Jerry

Member
I'm trying to run a sample script written by Infineon to request pressure readings from their DPS368 pressure sensor over I2C.

Git Hub: https://github.com/Infineon/DPS368-Library-Arduino

The script will compile just fine for any Arduino AVR boards (Due, Uno, Mega), however when attempting to compile the sample script for a Teensy board using Arduino 1.8.19 and Teensy Duino 1.57, it throws errors for unused variables and call for overflow. Any thoughts from the community on how to best troubleshoot this? Thanks.


Example Script:
Code:
#include <Dps368.h>

// Dps368 Opject
Dps368 Dps368PressureSensor = Dps368();

void setup()
{
  Serial.begin(9600);
  while (!Serial);


  //Call begin to initialize Dps368PressureSensor
  //The parameter 0x76 is the bus address. The default address is 0x77 and does not need to be given.
  //Dps368PressureSensor.begin(Wire, 0x76);
  //Use the commented line below instead of the one above to use the default I2C address.
  //if you are using the Pressure 3 click Board, you need 0x76
  Dps368PressureSensor.begin(Wire);

  Serial.println("Init complete!");
}



void loop()
{
  float temperature;
  float pressure;
  uint8_t oversampling = 7;
  int16_t ret;
  Serial.println();

  //lets the Dps368 perform a Single temperature measurement with the last (or standard) configuration
  //The result will be written to the paramerter temperature
  //ret = Dps368PressureSensor.measureTempOnce(temperature);
  //the commented line below does exactly the same as the one above, but you can also config the precision
  //oversampling can be a value from 0 to 7
  //the Dps 368 will perform 2^oversampling internal temperature measurements and combine them to one result with higher precision
  //measurements with higher precision take more time, consult datasheet for more information
  ret = Dps368PressureSensor.measureTempOnce(temperature, oversampling);

  if (ret != 0)
  {
    //Something went wrong.
    //Look at the library code for more information about return codes
    Serial.print("FAIL! ret = ");
    Serial.println(ret);
  }
  else
  {
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" degrees of Celsius");
  }

  //Pressure measurement behaves like temperature measurement
  //ret = Dps368PressureSensor.measurePressureOnce(pressure);
  ret = Dps368PressureSensor.measurePressureOnce(pressure, oversampling);
  if (ret != 0)
  {
    //Something went wrong.
    //Look at the library code for more information about return codes
    Serial.print("FAIL! ret = ");
    Serial.println(ret);
  }
  else
  {
    Serial.print("Pressure: ");
    Serial.print(pressure);
    Serial.println(" Pascal");
  }

  //Wait some time
  delay(500);
}



Errors:
PHP:
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp: In member function 'int16_t DpsClass::readByte(uint8_t)':
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp:567:50: error: call of overloaded 'requestFrom(uint8_t&, unsigned int, unsigned int)' is ambiguous
  if (m_i2cbus->requestFrom(m_slaveAddress, 1U, 1U) > 0)
                                                  ^
In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/Wire.h:26:0,
                 from C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.h:20,
                 from C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp:1:
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/WireIMXRT.h:91:10: note: candidate: uint8_t TwoWire::requestFrom(uint8_t, uint8_t, uint8_t)
  uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
          ^
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/WireIMXRT.h:92:10: note: candidate: uint8_t TwoWire::requestFrom(uint8_t, uint8_t, bool)
  uint8_t requestFrom(uint8_t address, uint8_t quantity, bool sendStop) {
          ^
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/WireIMXRT.h:98:10: note: candidate: uint8_t TwoWire::requestFrom(int, int, int)
  uint8_t requestFrom(int address, int quantity, int sendStop) {
          ^
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp: In member function 'int16_t DpsClass::readBlock(RegBlock_t, uint8_t*)':
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp:777:73: error: call of overloaded 'requestFrom(uint8_t&, uint8_t&, unsigned int)' is ambiguous
  int16_t ret = m_i2cbus->requestFrom(m_slaveAddress, regBlock.length, 1U);
                                                                         ^
In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/Wire.h:26:0,
                 from C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.h:20,
                 from C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp:1:
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/WireIMXRT.h:91:10: note: candidate: uint8_t TwoWire::requestFrom(uint8_t, uint8_t, uint8_t)
  uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
          ^
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/WireIMXRT.h:92:10: note: candidate: uint8_t TwoWire::requestFrom(uint8_t, uint8_t, bool)
  uint8_t requestFrom(uint8_t address, uint8_t quantity, bool sendStop) {
          ^
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/WireIMXRT.h:98:10: note: candidate: uint8_t TwoWire::requestFrom(int, int, int)
  uint8_t requestFrom(int address, int quantity, int sendStop) {
          ^
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp: In member function 'virtual int16_t DpsClass::configTemp(uint8_t, uint8_t)':
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp:505:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp: In member function 'virtual int16_t DpsClass::configPressure(uint8_t, uint8_t)':
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp:521:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp: In member function 'int16_t DpsClass::readByte(uint8_t)':
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp:575:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
Error compiling for board Teensy 4.1.
 
Any thoughts from the community on how to best troubleshoot this? Thanks.
Code:
C:\Users\testuser\Documents\Arduino\libraries\DPS368-Library-Arduino-dps368\src\DpsClass.cpp:567:50: error: call of overloaded 'requestFrom(uint8_t&, unsigned int, unsigned int)' is ambiguous
  if (m_i2cbus->requestFrom(m_slaveAddress, 1U, 1U) > 0)

The "ambiguous" errors occur because calls to requestFrom() in the library contain multiple possible matches to different (overloaded) versions of that function in the Teensy Wire library. There is a version where all 3 arguments are uint8_t, and another version where all 3 arguments are type int, and another where two are uint8_t and one is bool. The calls in the library don't exactly match any of those, and the compiler can't make a decision of which one should be called.

The fix is to modify lines 567 and 777 of Dsp368.cpp to explicitly cast the arguments to type uint8_t, like this:

Code:
	if (m_i2cbus->requestFrom(m_slaveAddress, (uint8_t)1U, (uint8_t)1U) > 0)

	int16_t ret = m_i2cbus->requestFrom(m_slaveAddress, regBlock.length, (uint8_t)1U);
 
The "ambiguous" errors occur because calls to requestFrom() in the library contain multiple possible matches to different (overloaded) versions of that function in the Teensy Wire library. There is a version where all 3 arguments are uint8_t, and another version where all 3 arguments are type int, and another where two are uint8_t and one is bool. The calls in the library don't exactly match any of those, and the compiler can't make a decision of which one should be called.

The fix is to modify lines 567 and 777 of Dsp368.cpp to explicitly cast the arguments to type uint8_t, like this:

Code:
	if (m_i2cbus->requestFrom(m_slaveAddress, (uint8_t)1U, (uint8_t)1U) > 0)

	int16_t ret = m_i2cbus->requestFrom(m_slaveAddress, regBlock.length, (uint8_t)1U);

Thank you so much Joe for taking the time to explain what's going on and for the suggestions to resolve what I believe is casting the unsigned int 1U (16-bit unsigned int teensy 2.0 or 32-bit unsigned int teensy 3.x,4.x) into a fixed unit8_t (8-bit unsigned int) that is one of the three that Teensy Wire library is expecting.

Code:
TwoWire::
                            requestFrom(uint8_t, uint8_t, uint8_t)


I honestly had no idea that parts of the Teensy Wire library gets loaded even though #include "Wire.h" is not explicitly stated in the example script while verifying. Thanks again for the help.
 
I honestly had no idea that parts of the Teensy Wire library gets loaded even though #include "Wire.h" is not explicitly stated in the example script while verifying.

You're welcome, yes, the power of the Arduino platform is the "layers" of software and the capabilities they provide. In the diagram below, the sketch and the Dps368 library are non-platform-specific, meaning the same code can be used on Arduino UNO, Teensy, or other Arduino compatibles. To be Arduino-compatible is to provide (along with many other things) a Wire library that translates non-processor-specific Wire function calls into processor-specific control of the I2C peripheral.

Code:
sketch <--> Dps368 <--> Wire <--> I2C device
 
Back
Top