tiny-mega-i2c icon indicating copy to clipboard operation
tiny-mega-i2c copied to clipboard

Implementation help for SparkFun_RV-8803_Library using this i2c lib instead of std Wire lib

Open dattasaurabh82 opened this issue 4 years ago • 0 comments

Hello! First of all, thank you for this low mem lib implementation as alt to std wire lib.

This is not really an issue report.
I'm trying to implement SparkFun_RV-8803_Arduino_Library using this library:

I'm wondering, form having read a bit this repo, some method comparison would be, for example, from SparkFun_RV8803.cpp, for some functions:

//--- READ OPERATIONS [ as laid out in datasheet 6.8 ]  ---//
uint8_t RV8803Tiny::readRegister(uint8_t addr) {
  //------------------------------------------------------------------------------------//
  //--- using std wire library in custom class as depicted from the sparkfun library ---//
  //------------------------------------------------------------------------------------//
  // ---- [ lines 693-706 in sparkfun library ]
  
  //  _i2cPort->beginTransmission(RV8803_ADDR);
  //  _i2cPort->write(addr);
  //  _i2cPort->endTransmission();
  //
  //  //typecasting the 1 parameter in requestFrom so that the compiler
  //  //doesn't give us a warning about multiple candidates
  //  if (_i2cPort->requestFrom(static_cast<uint8_t>(RV8803_ADDR), static_cast<uint8_t>(1)) != 0)
  //  {
  //    return _i2cPort->read();
  //  }
  //  return false;



  //------------------------------------------------------------------------------------//
  //------------------ using tiny-mega-wire library in custom class --------------------//
  //------------------------------------------------------------------------------------//
  //  Master sends out the START condition, signifyig it'll be a write operation [ R/W bit is a 0 ]
  if (TinyMegaI2C.start(RV8803_ADDR, 0)) { //  if Acknowledged from RV-8803
    //  Master sends out the Register Address to RV-8803-C7.
    if (TinyMegaI2C.write(addr)) { //  if Acknowledged from RV-8803
      //  Master sends out the Repeated START condition (or STOP condition followed by START condition)
      //  Master sends out Slave Address, 65h for the RV-8803 [ R/W bit is 1 ] indicating a read operation
      if (TinyMegaI2C.restart(RV8803_ADDR, 1)) { //Acknowledgement from RV-8803-C7
        // The Slave sends out the Data from the Register Address
        uint8_t res = TinyMegaI2C.read();
        // 1. [NO NEED] Acknowledgement from Master.
        // 2. [NO NEED] The above Steps can be repeated if necessary and the address bit increments automatically.
        // 3. ** The Master, addressed as Receiver, can stop data transmission by not generating an acknowledge on the
        // last byte that has been sent from the Slave {thaat's why we didn't do step 1 here}.
        // In this event, the Slave-Transmitter must leave the data line HIGH to enable the Master to generate a STOP condition.
        TinyMegaI2C.stop();
        return (res);
      } else {
        return (false);
      }
    } else {
      return (false);
    }
  } else {
    return (false);
  }
}


bool RV8803Tiny::readMultipleRegisters(uint8_t addr, uint8_t * dest, uint8_t len) {
  //  i2cPort->beginTransmission(RV8803_ADDR);
  //  _i2cPort->write(addr);
  //  if (_i2cPort->endTransmission() != 0)
  //    return (false); //Error: Sensor did not ack
  //
  //  _i2cPort->requestFrom(static_cast<uint8_t>(RV8803_ADDR), len);
  //  for (uint8_t i = 0; i < len; i++){
  //    dest[i] = _i2cPort->read();
  //  }
  //  return (true);


  //  Master sends out the START condition, signifyig it'll be a write operation [ R/W bit is a 0 ]
  if (TinyMegaI2C.start(RV8803_ADDR, 0)) { //  if Acknowledged from RV-8803
    //  Master sends out the Register Address to RV-8803-C7.
    if (TinyMegaI2C.write(addr)) { //  if Acknowledged from RV-8803
      //  Master sends out the Repeated START condition (or STOP condition followed by START condition)
      //  Master sends out Slave Address, 65h for the RV-8803 [ R/W bit is -1 ] indicating unknown number of read operation
      if (TinyMegaI2C.restart(RV8803_ADDR, -1)) { //Acknowledgement from RV-8803-C7
        // The Slave sends out the Data from the Register Address
        // 1. [NO NEED] Acknowledgement from Master.
        // 2. [NO NEED] The above Steps can be repeated if necessary and the address bit increments automatically.
        // 3. ** The Master, addressed as Receiver, can stop data transmission by not generating an acknowledge on the
        // last byte that has been sent from the Slave {thaat's why we didn't do step 1 here}.
        // In this event, the Slave-Transmitter must leave the data line HIGH to enable the Master to generate a STOP condition.

        for (uint8_t i = 0; i < len; i++) {
          dest[i] = TinyMegaI2C.read();
        }
        
        TinyMegaI2C.stop();
        return (res);
      } else {
        return (false);
      }
    } else {
      return (false);
    }
  } else {
    return (false);
  }
}

Am I in the right direction. Some help would be really great here. Here is the datasheet of the RTC clock.

If we look at the dataSheet at points (6.8 & 6.9 - read operation methods), it's a bit confusing..

Best Datta

dattasaurabh82 avatar Jun 21 '21 16:06 dattasaurabh82