Skip to content

Wire support needs improvement to do minimal checks. #257

Closed
@RobTillaart

Description

@RobTillaart

I have several I2C based libraries that therefor use the Wire library.
These are hard to test as there is minimal support in GODMODE.

One of the functions I would like to see supported is connection test from which I expect a time out in fact.
It does a sort of "Ping" over I2C and is code wise core in every I2C scanner.
Unfortunately it doesn't give the expected result.

Example snippet from - https://github.com/RobTillaart/MCP4725

bool MCP4725::isConnected()
{
  Wire.beginTransmission(_deviceAddress);
  return (Wire.endTransmission() == 0);
}

Currently this function returns true currently in the unittest.
It should return false as the end transmission should get a NACK on transmission of the address.
// https://www.arduino.cc/en/Reference/WireEndTransmission

Furthermore it would be useful to be able to add a device ID in GODMODE I2C, something like

GodMode->Wire->add(deviceAddress);

So that at least such basic ping could work.

A more elaborated Wire implementation could set and get register values so I could do something like

GodMode->Wire->add(deviceAddress);
GodMode->Wire->setRegister(1, 0xF5); // loads value F5 in a virtual register

n = WireRequestFrom(addr, 1);  // pseudo 
 assertEqual(0xF5, n);

// and to check a written value
value = GodMode->Wire->getRegister(1); 

No priority as I am not blocked by it.

A small test program to see status when (not) connected.

//    FILE: WireConnectionTest.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.0

#include "Wire.h"

void setup()
{
  Serial.begin(115200);
  Serial.println("Wire connection test");

  Wire.begin();

  uint8_t addr = 0x62;
  Wire.beginTransmission(addr);
  int rv = Wire.endTransmission();

  // https://www.arduino.cc/en/Reference/WireEndTransmission

  Serial.print("ADDRESS:\t");
  Serial.println(addr, HEX);
  Serial.print("RETURNS:\t");
  Serial.print(rv);
  switch (rv)
  {
    case 0:
      Serial.println("\tsuccess");
      break;
    case 1:
      Serial.println("\tdata too long to fit in transmit buffer");
      break;
    case 2:
      Serial.println("\treceived NACK on transmit of address");  // NOT CONNECTED, ADDRESS NOT FOUND
      break;
    case 3:
      Serial.println("\treceived NACK on transmit of data");
      break;
    case 4:
    default:
      Serial.println("\tother error");
      break;
  }
}

void loop()
{
}

// -- END OF FILE --

Metadata

Metadata

Assignees

No one assigned

    Labels

    arduino mocksCompilation mocks for the Arduino libraryenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions