Search code examples
c++arduinoi2c

Problems with libraries for BQ769x0


Im reverse engineering a bms. i have all the hardware connections down. it has a stm32f103c8, and bq7693003 with CRC. there are 2 libraries for this chip but both are not working! this is a picture of the bms. and this is the model of the bms. I'm using the example code in the github repo. the library I'm using is called: 10s_Ti_BQ76930_BMS

enter image description here

enter image description here

after having one of the gpios as the boot pin for the BQ7693003 it boots up and says this in the serial console:

8
0x08
Starting BMS object
Starting wire.begin
starting variable initialization
Starting boot pin check
Determining i2c address and whether CRC is enabled
Address and CRC detection successful
Address:  8
CRC Enabled:  1

And it gets stuck there.

In the bq769x0.cpp i uncommented the BootPin code to start my bq7693003 automatically. before it was not turning on. but now it does and gets stuck as mentioned above.

  // Boot IC if pin is defined (else: manual boot via push button has to be 
  // done before calling this method)
  Serial.println("Starting boot pin check");
  if (bootPin >= 0)
  {
    pinMode(bootPin, OUTPUT);
    digitalWrite(bootPin, HIGH);
    delay(5);   // wait 5 ms for device to receive boot signal (datasheet: max. 2 ms)
    //pinMode(bootPin, INPUT);     // don't disturb temperature measurement
    delay(1000);  // wait for device to boot up completely (datasheet: max. 10 ms)
  }

Theres also another problem it will not compile because of this:

      // check for overrun of millis() or very slow running program
      if (abs(secSinceInterrupt - secSinceErrorCounter) > 2) {
        secSinceErrorCounter = secSinceInterrupt;
      }

I think i fixed this by using fabs instead of abs.

i added various delays to the code but still it isint working properly.

Im really not sure what to do, as i have no idea how to code for I2C and this chip. also, i have tried the LibreSolar library it gets far to the clearing of various errors such as XR, Alert, UV, OV.. etc but gets stuck after clearing the XR error.

Regards


Solution

  • So after partially re-writing the whole library from scratch, I realized that the original author had some mistakes or something I'm not sure... if anyone knows please share so that there is a reason behind this fix.

    First i changed byte bq769x0::updateBalancingSwitches(void) to void bq769x0::updateBalancingSwitches(void) inbq769x0.cpp

    then I changed byte updateBalancingSwitches(void); to void updateBalancingSwitches(void); in bq769x0.h

    and then finally i changed this portion of code from this:

    if (determineAddressAndCrc())
        {
            //debug prints to show that initial comms were successful
            Serial.println("Address and CRC detection successful");
            Serial.print("Address:  ");
            Serial.println(I2CAddress, HEX);
            Serial.print("CRC Enabled:  ");
            Serial.println(crcEnabled);
            
    
            // initial settings for bq769x0
            writeRegister(SYS_CTRL1, 0b00010000);  // Turn ADC on and use internal die temp
            writeRegister(SYS_CTRL2, 0b01000000);  // switch CC_EN on
    
                // attach ALERT interrupt to this instance
        instancePointer = this;  //taken from arduino compatible code
        attachInterrupt(digitalPinToInterrupt(alertPin), bq769x0::alertISR, RISING); //taken from arduino compatible code
            
            // get ADC offset and gain
            adcOffset = (signed int) readRegister(ADCOFFSET);  // convert from 2's complement
            adcGain = 365 + (((readRegister(ADCGAIN1) & 0b00001100) << 1) |
                ((readRegister(ADCGAIN2) & 0b11100000) >> 5)); // uV/LSB
        }
        else {
            // TODO: do something else... e.g. set error flag
    #if BQ769X0_DEBUG
            Serial.println("BMS communication error\n");
    #endif
        }
    
    
    
    /*  // From original arduino startup code
      if (readRegister(CC_CFG) == 0x19)
      {
          Serial.println("Inside readregister for cc_cfg if");
        // initial settings for bq769x0
        writeRegister(SYS_CTRL1, B00011000);  // switch external thermistor and ADC on
        writeRegister(SYS_CTRL2, B01000000);  // switch CC_EN on
    
        // attach ALERT interrupt to this instance
        instancePointer = this;
        attachInterrupt(digitalPinToInterrupt(alertPin), bq769x0::alertISR, RISING);
    
        // get ADC offset and gain
        adcOffset = (signed int) readRegister(ADCOFFSET);  // convert from 2's complement
        adcGain = 365 + (((readRegister(ADCGAIN1) & B00001100) << 1) | 
          ((readRegister(ADCGAIN2) & B11100000) >> 5)); // uV/LSB
        
        return 0;
      }
    

    to this in the bq769x0.cpp file:

     if (determineAddressAndCrc())
        {
            //debug prints to show that initial comms were successful
            Serial.println("Address and CRC detection successful");
            Serial.print("Address:  ");
            Serial.println(I2CAddress, HEX);
            Serial.print("CRC Enabled:  ");
            Serial.println(crcEnabled);
        }
    
    
    
      // From original arduino startup code
      if (readRegister(CC_CFG) == 0x19)
      {
          Serial.println("Inside readregister for cc_cfg if");
        // initial settings for bq769x0
        writeRegister(SYS_CTRL1, B00011000);  // switch external thermistor and ADC on
        writeRegister(SYS_CTRL2, B01000000);  // switch CC_EN on
    
        // attach ALERT interrupt to this instance
        instancePointer = this;
        attachInterrupt(digitalPinToInterrupt(alertPin), bq769x0::alertISR, RISING);
    
        // get ADC offset and gain
        adcOffset = (signed int) readRegister(ADCOFFSET);  // convert from 2's complement
        adcGain = 365 + (((readRegister(ADCGAIN1) & B00001100) << 1) | 
          ((readRegister(ADCGAIN2) & B11100000) >> 5)); // uV/LSB
        
        return 0;
      }