Smart battery (DJI) reading/writing

Hi all, I am not very good at Arduino but I can get something out. I am currenlty messing around with smart batteries from my drone DJI mini 2. I found a code to get battery parameters and next spet would be to change some of them reason being replacement of internal akkus. The value that I need to change is the cycli counter. So far my understanding is that the value is stored into a specific memory location, so I wonder how I could write the new value. Following two codes, first one for reading (tested and working), second one for write (yet to test). Any help would be much appreciated.
reading sketch

**********************************
 * SMBus FOR 3DR SOLO with Arduino UNO R3
 * STAVROPOULOS
 * Code Version 0.02 beta
 * MUCH OF THIS CODE WAS COPIED FROM
 * https://github.com/PowerCartel/PackProbe/blob/master/PackProbe/PackProbe.ino
 * https://github.com/ArduPilot/PX4Firmware/blob/master/src/drivers/batt_smbus/batt_smbus.cpp
 * 
 **********************************
 **********************************
 * Configured for Arduino UNO R3
 * you will need to use external pull up resistors of 
 * 4.7k-ohm to pull the SDA and SCL lines up to 3.3v
 **********************************
 **********************************
 * CONFIGURE I2C/SERIAL ON ARDUINO
 **********************************
 */
//DEFINE SDA AND SCL PINS
  #define SCL_PIN 5                 // ARDUINO UNO PIN A5 - COMMUNICATION PIN 5 ON MEGA
  #define SCL_PORT PORTC
  #define SDA_PIN 4                 // ARDUINO UNO PIN A4 - COMMUNICATION PIN 6 ON MEGA
  #define SDA_PORT PORTC

//CONFIGURE I2C MODES
  #define I2C_TIMEOUT 100           //PREVENT SLAVE DEVICES FROM STRETCHING LOW PERIOD OF THE CLOCK INDEFINITELY AND LOCKING UP MCU BY DEFINING TIMEOUT
  //#define I2C_NOINTERRUPT 1       //SET TO 1 IF SMBus DEVICE CAN TIMEOUT
  //#define I2C_FASTMODE 1          //THE STANDARD I2C FREQ IS 100kHz.  USE THIS TO PERMIT FASTER UP TO 400kHz.
  //#define I2C_SLOWMODE 1            //THE STANDARD I2C FREQ IS 100kHz.  USE THIS TO PERMIT SLOWER, DOWN TO 25kHz.
  #define BAUD_RATE 9600
  #include <SoftI2CMaster.h>

/**********************************
 * CONFIGURE SERIAL LIBRARY
 **********************************/
  //#include <SoftwareSerial.h>
  //#include <Serial.h>
  #include <Wire.h>

/**********************************
 * DEFINE VARIABLES AND SMBus MAPPINGS
 **********************************/
  #define BATT_SMBUS_ADDR                     0x0B                ///< I2C address
  #define BATT_SMBUS_ADDR_MIN                 0x08                ///< lowest possible address
  #define BATT_SMBUS_ADDR_MAX                 0x7F                ///< highest possible address
//BUS MAPPINGS FROM DEV.3DR
  #define BATT_SMBUS_TEMP                     0x08                ///< temperature register
  #define BATT_SMBUS_VOLTAGE                  0x09                ///< voltage register
  #define BATT_SMBUS_REMAINING_CAPACITY       0x0f                ///< predicted remaining battery capacity as a percentage
  #define BATT_SMBUS_FULL_CHARGE_CAPACITY     0x10                ///< capacity when fully charged
  #define BATT_SMBUS_DESIGN_CAPACITY          0x18                ///< design capacity register
  #define BATT_SMBUS_DESIGN_VOLTAGE           0x19                ///< design voltage register
  #define BATT_SMBUS_SERIALNUM                0x1c                ///< serial number register
  #define BATT_SMBUS_MANUFACTURE_NAME         0x20                ///< manufacturer name
  #define BATT_SMBUS_MANUFACTURE_DATA         0x23                ///< manufacturer data
  #define BATT_SMBUS_MANUFACTURE_INFO         0x25                ///< cell voltage register
  #define BATT_SMBUS_CURRENT                  0x2a                ///< current register
  #define BATT_SMBUS_MEASUREMENT_INTERVAL_US  (1000000 / 10)      ///< time in microseconds, measure at 10hz
  #define BATT_SMBUS_TIMEOUT_US               10000000            ///< timeout looking for battery 10seconds after startup
  #define BATT_SMBUS_BUTTON_DEBOUNCE_MS       300                 ///< button holds longer than this time will cause a power off event
  
  #define BATT_SMBUS_PEC_POLYNOMIAL           0x07                ///< Polynomial for calculating PEC
  #define BATT_SMBUS_I2C_BUS                  PX4_I2C_BUS_EXPANSION
//BUS MAPPINGS FROM SMBus PROTOCOL DOCUMENTATION
#define BATTERY_MODE             0x03
#define CURRENT                  0x0A
#define RELATIVE_SOC             0x0D
#define ABSOLUTE_SOC             0x0E
#define TIME_TO_FULL             0x13
#define CHARGING_CURRENT         0x14
#define CHARGING_VOLTAGE         0x15
#define BATTERY_STATUS           0x16
#define CYCLE_COUNT              0x17
#define SPEC_INFO                0x1A
#define MFG_DATE                 0x1B
#define DEV_NAME                 0x21   // String
#define CELL_CHEM                0x22   // String
#define CELL4_VOLTAGE            0x3C   // Indidual cell voltages don't work on Lenovo and Dell Packs
#define CELL3_VOLTAGE            0x3D
#define CELL2_VOLTAGE            0x3E
#define CELL1_VOLTAGE            0x3F
#define STATE_OF_HEALTH          0x4F
//END BUS MAPPINGS
  
  #define bufferLen 32
  uint8_t i2cBuffer[bufferLen];

// standard I2C address for Smart Battery packs
  byte deviceAddress = BATT_SMBUS_ADDR;

void setup()
{

  //INITIATE SERIAL CONSOLE
    Serial.begin(BAUD_RATE);
    Serial.println(i2c_init());

  //SETUP I2C INPUT PINS
    //pinMode(27,INPUT_PULLUP);                             //use external pull up resistor instead
    //pinMode(28,INPUT_PULLUP);                             //use external pull up resistor instead

    Serial.flush();

    while (!Serial) {    
    ;                                                       //wait for Console port to connect.
    }

    Serial.println("Console Initialized");
  
    i2c_init();                                             //i2c_start initialized the I2C system.  will return false if bus is locked.
    Serial.println("I2C Inialized");
    scan();
}

int fetchWord(byte func)
{
    i2c_start(deviceAddress<<1 | I2C_WRITE);                //Initiates a transfer to the slave device with the (8-bit) I2C address addr.
                                                            //Alternatively, use i2c_start_wait which tries repeatedly to start transfer until acknowledgment received
    //i2c_start_wait(deviceAddress<<1 | I2C_WRITE);
    i2c_write(func);                                        //Sends a byte to the previously addressed device. Returns true if the device replies with an ACK.
    i2c_rep_start(deviceAddress<<1 | I2C_READ);             //Sends a repeated start condition, i.e., it starts a new transfer without sending first a stop condition.
    byte b1 = i2c_read(false);                              //i2c_read Requests to receive a byte from the slave device. If last is true, 
                                                            //then a NAK is sent after receiving the byte finishing the read transfer sequence.
    byte b2 = i2c_read(true);
    i2c_stop();                                             //Sends a stop condition and thereby releases the bus.
    return (int)b1|((( int)b2)<<8);
}

uint8_t i2c_smbus_read_block ( uint8_t command, uint8_t* blockBuffer, uint8_t blockBufferLen ) 
{
    uint8_t x, num_bytes;
    i2c_start((deviceAddress<<1) + I2C_WRITE);
    i2c_write(command);
    i2c_rep_start((deviceAddress<<1) + I2C_READ);             
    num_bytes = i2c_read(false);                              //num of bytes; 1 byte will be index 0
    num_bytes = constrain(num_bytes,0,blockBufferLen-2);      //room for null at the end
    for (x=0; x<num_bytes-1; x++) {                           //-1 because x=num_bytes-1 if x<y; last byte needs to be "nack"'d, x<y-1
      blockBuffer[x] = i2c_read(false);
    }
    blockBuffer[x++] = i2c_read(true);                        //this will nack the last byte and store it in x's num_bytes-1 address.
    blockBuffer[x] = 0;                                       // and null it at last_byte+1
    i2c_stop();
    return num_bytes;
}

void scan()                                        // cerca la batteria
{
    byte i = 0;
    for ( i= 0; i < 127; i++  )
    {
      Serial.print("Address: 0x");
      Serial.print(i,HEX);
      bool ack = i2c_start(i<<1 | I2C_WRITE); 
      if ( ack ) {
        Serial.println(": OK");
        Serial.flush();
      }
      else {
        Serial.println(": -");
        Serial.flush();      
      }
      i2c_stop();
          }
}

void loop()
{
   delay(10000);
    uint8_t length_read = 0;
  
    Serial.print("Manufacturer Name: ");
    length_read = i2c_smbus_read_block(BATT_SMBUS_MANUFACTURE_NAME, i2cBuffer, bufferLen);
    Serial.write(i2cBuffer, length_read);
    Serial.println("");
  
    Serial.print("Manufacturer Data: ");
    length_read = i2c_smbus_read_block(BATT_SMBUS_MANUFACTURE_DATA, i2cBuffer, bufferLen);
    Serial.write(i2cBuffer, length_read);
    Serial.println("");
  
    Serial.print("Manufacturer Info: ");
    length_read = i2c_smbus_read_block(BATT_SMBUS_MANUFACTURE_INFO, i2cBuffer, bufferLen);
    Serial.write(i2cBuffer, length_read);
    Serial.println("");
    
    Serial.print("Design Capacity: " );
    Serial.println(fetchWord(BATT_SMBUS_DESIGN_CAPACITY));
    
    Serial.print("Design Voltage: " );
    Serial.println(fetchWord(BATT_SMBUS_DESIGN_VOLTAGE));
  
    Serial.print("Serial Number: ");
    Serial.println(fetchWord(BATT_SMBUS_SERIALNUM));
    
    Serial.print("Voltage: ");
    Serial.println((float)fetchWord(BATT_SMBUS_VOLTAGE)/1000);
  
    Serial.print("Full Charge Capacity: " );
    Serial.println(fetchWord(BATT_SMBUS_FULL_CHARGE_CAPACITY));
    
    Serial.print("Remaining Capacity: " );
    Serial.println(fetchWord(BATT_SMBUS_REMAINING_CAPACITY));
  
    Serial.print("Temp: ");
    unsigned int tempk = fetchWord(BATT_SMBUS_TEMP);
    Serial.println((float)tempk/10.0-273.15);
  
    Serial.print("Current (mA): " );
    Serial.println(fetchWord(BATT_SMBUS_CURRENT));

    Serial.print("Device Name: ");
    length_read = i2c_smbus_read_block(DEV_NAME, i2cBuffer, bufferLen);
    Serial.write(i2cBuffer, length_read);
    Serial.println("");

    Serial.print("Chemistry ");
    length_read = i2c_smbus_read_block(CELL_CHEM, i2cBuffer, bufferLen);
    Serial.write(i2cBuffer, length_read);
    Serial.println("");

    String formatted_date = "Manufacture Date (Y-M-D): ";
    int mdate = fetchWord(MFG_DATE);
    int mday = B00011111 & mdate;
    int mmonth = mdate>>5 & B00001111;
    int myear = 1980 + (mdate>>9 & B01111111);
    formatted_date += myear;
    formatted_date += "-";
    formatted_date += mmonth;
    formatted_date += "-";
    formatted_date += mday;
    Serial.println(formatted_date);

    Serial.print("Specification Info: ");
    Serial.println(fetchWord(SPEC_INFO));
   
    Serial.print("Cycle Count: " );
    Serial.println(fetchWord(CYCLE_COUNT));
  
    Serial.print("Relative Charge(%): ");
    Serial.println(fetchWord(RELATIVE_SOC));
    
    Serial.print("Absolute Charge(%): ");
    Serial.println(fetchWord(ABSOLUTE_SOC));
    
    Serial.print("Minutes remaining for full charge: ");
    Serial.println(fetchWord(TIME_TO_FULL));
  
    // These aren't part of the standard, but work with some packs.
    // They don't work with the Lenovo and Dell packs we've tested
    Serial.print("Cell 1 Voltage: ");
    Serial.println(fetchWord(CELL1_VOLTAGE));
    Serial.print("Cell 2 Voltage: ");
    Serial.println(fetchWord(CELL2_VOLTAGE));
    Serial.print("Cell 3 Voltage: ");
    Serial.println(fetchWord(CELL3_VOLTAGE));
    Serial.print("Cell 4 Voltage: ");
    Serial.println(fetchWord(CELL4_VOLTAGE));
    
    Serial.print("State of Health: ");
    Serial.println(fetchWord(STATE_OF_HEALTH));
  
    Serial.print("Battery Mode (BIN): 0b");
    Serial.println(fetchWord(BATTERY_MODE),BIN);
  
    Serial.print("Battery Status (BIN): 0b");
    Serial.println(fetchWord(BATTERY_STATUS),BIN);
  
    Serial.print("Charging Current: ");
    Serial.println(fetchWord(CHARGING_CURRENT));
    
    Serial.print("Charging Voltage: ");
    Serial.println(fetchWord(CHARGING_VOLTAGE));
    
    Serial.print("Current (mA): " );
    Serial.println(fetchWord(CURRENT));
      
    Serial.println(".");
    delay(20000);
}



CODE FOR WRITING

#include <Wire.h>

#define I2C_ADDRESS 0x58   // address della batteria 
#define REG_CURRENT 0x17   // register address

void setup() {
  Wire.begin();     

  delay(100);     

  byte currentValue = 0x01;

  Wire.beginTransmission(I2C_ADDRESS);
  Wire.write(REG_CURRENT);           
  Wire.write(currentValue);         
  Wire.endTransmission();           
}

void loop() {}

Please post a link that has the schematic you are working from.

I don't know why you would pull the I2C lines to 3.3 volts if the Arduino board is an UNO.

a7

I beg your pardon I forgot to enclose the schematic, it is really simple.
I do not see how to attach pictures, let me write it:

  • SCL pin from DJI battery to A5 Arduino
  • SDA pin from DJI battery to A4 Arduino
  • GND pin from DJI battery to GND Arduino
  • two resistors from SDA and SCL pins tied together to +Vcc Arduino.
    I can not explain why but this is the note I found in the sketch lines 12 to 14.
    "Configured for Arduino UNO R3 you will need to use external pull up resistors of
    4.7k-ohm to pull the SDA and SCL lines up to 3.3v "

This is the image-upload button...

thanks


LM25066 seems to be good for 2.9vdc to 17vdc

Input Voltage Range: 2.9 V to 17 V 

I am sorry I am not sure about what you say. I am still a newbye.
0x58 in the writing code is actually an example meaning the battery address.
The reading code scan the addresses and when it finds the battery address it shows OK close to the location...I hope I made myself clear. My doubt is not about reading parameters as the code is working properly. I wonder if the following code for writing will actually write the new value in the proper location 0x17.

#include <Wire.h>

#define I2C_ADDRESS 0xB   // battery address taken from reading code  
#define REG_CURRENT 0x17   // register address

void setup() {
  Wire.begin();     

  delay(100);     

  byte currentValue = 0x01;

  Wire.beginTransmission(I2C_ADDRESS);
  Wire.write(CYCLE_COUNT) //     (REG_CURRENT);           
  Wire.write(currentValue);         
  Wire.endTransmission();           
}

void loop() {}

Okay. I guessed it was a power management bus. Sorry.

How are you making the connections to the battery ?

My fault... not clear enough. Now back to the question...I wonder if the WRITING code will actually work. If so I will update teh reading code with an ption to write!

As said, SCL -> A5; SDA -> A4; GND -> GND; SDA and SCL each resistor 4,7 kohm to GND

Sorry, I was not clear enough. How are you physically connecting the wires to the battery ?

The sockets in the battery connector are quite small and I would worry that they may be damaged if something too large were put in them

Based on this code for reading a word from a register

int fetchWord(byte func)
{
  i2c_start(deviceAddress << 1 | I2C_WRITE);              //Initiates a transfer to the slave device with the (8-bit) I2C address addr.
  //Alternatively, use i2c_start_wait which tries repeatedly to start transfer until acknowledgment received
  //i2c_start_wait(deviceAddress<<1 | I2C_WRITE);
  i2c_write(func);                                        //Sends a byte to the previously addressed device. Returns true if the device replies with an ACK.
  i2c_rep_start(deviceAddress << 1 | I2C_READ);           //Sends a repeated start condition, i.e., it starts a new transfer without sending first a stop condition.
  byte b1 = i2c_read(false);                              //i2c_read Requests to receive a byte from the slave device. If last is true,
  //then a NAK is sent after receiving the byte finishing the read transfer sequence.
  byte b2 = i2c_read(true);
  i2c_stop();                                             //Sends a stop condition and thereby releases the bus.
  return (int)b1 | ((( int)b2) << 8);
}

You can try this code to write a word to a register

void writeWord(byte func, int data)
{
  i2c_start(deviceAddress << 1 | I2C_WRITE);
  i2c_write(func);

  i2c_write((data >> 8) & 0xFF)); //hi byte
  i2c_write(data & 0xFF); //loByte

  i2c_stop();

}

If you read the cycles with

Serial.print("Cycle Count: " );
Serial.println(fetchWord(CYCLE_COUNT));

The I think you would use
writeWord(CYCLE_COUNT, 0)//reset cycle count to 0

As said I am a newbye, so it is not easy to completely understand your explanation. I have to study every single command and then go back to the code.
I have a few questions:

  • do I have to "put" the battery/chip in a "writing" mode or it would be possible just to make the connection and run the code?
  • is there the risk of locking/damaging the battery/chip?
  • do I have to put the whole code in a sketch?
  • Thank you for your help.

I don't know if the battery/chip is write protected or not. If it is enabled so that it can be written to, then you should be able to connect to the chip and write to it.

is there the risk of locking/damaging the battery/chip?

If you don't have the registers correct, and you write to a wrong location you could possibly mess something up.

EDIT: Since you have marked this code as a solution, then I guess you didn't brick the chip :wink:

do I have to put the whole code in a sketch?

You can certainly add the wrtiteWord() function to the code you provided and only call it if you want to reset the cycle count.

You may be able to create a more simple sketch to just write, but I'm not clear about how exactly to do that. The sketch you provided uses the SoftI2CMaster.h library to communicate with the chip. I'm not at all clear about why the sketch author went that route instead of using the Arduino wire library.

I'm not at all familiar with your battery or its chip. I used this reference for the SMBUS and I think it should be compatible with the Arduino wire library.
https://www.nxp.com/docs/en/application-note/AN4471.pdf

Thank you for your reply. It helps me to better understanding.
As you noted the code is not mine, I would have liked to know how to do it.
Reading smart battery docs and the running the code, the 0x17 address is the cycle_count, so this seems to be ok. I donot know the reason not using wire library and I would be be able to rewrite te sketch. Are there adavntages using wire instead of I2C?

The Wire library allows hardware I2C to be used which is likely to be more robust than a software implementation of I2C

How can I fix this? Is it complicated to rewrite the code?

If you are asking this question, it means you are not familiar with the i2c bus and the wire library code to use it with an external chip like an eeprom or your battery chip.

One of the best tutorials is by Nick Gammon.
https://www.gammon.com.au/i2c

It is worth your review, and there is plenty of other material on line.

Yeah, I was clear about that...newbye