This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

BQ27742-G1: BQ27742-G1: Command Codes Example Implementation in C

Part Number: BQ27742-G1

I just wanted to follow up regarding a now locked thread here:

https://e2e.ti.com/support/power-management-group/power-management/f/power-management-forum/1198554/bq27742-g1-command-codes?tisearch=e2e-sitesearch

That was never sufficiently answered in my opinion. I was still guessing with some trial and error until I figured out the order of operations. For those in a similar situation, please see my code that worked as expected. I added some notes in the code about what I tried first, and what worked and what did not. I hope it is helpful to others.

static void bq27742_g1_control(uint16_t subcommand_code){
    memset(send_buf, 0, sizeof send_buf);
    memset(return_buf, 0, sizeof return_buf);
    send_buf[0] = 0x00;
    //send_buf[1] = 0x01; // Apparently this is interpreted as the LSB for the SUBCOMMAND.
    // LSB first (i.e. send 0x02 then 0x00)
    send_buf[1] = (subcommand_code & 0x00FF);
    send_buf[2] = (subcommand_code & 0xFF00) >> 8;
    // This is wrong as it is MSB first
    //send_buf[2] = (subcommand_code & 0xFF00) >> 8;
    //send_buf[3] = (subcommand_code & 0x00FF);
    //
    // void i2c_write_error_handling(i2c_inst_t *i2c,
    // uint8_t addr,
    // const uint8_t *src,
    // size_t len, // number of bytes to send
    // bool nostop);
    //
    // This does not work for some unknown reason. I need to write a 0x00 as a 4th byte?
    // Why and how would I know to do this from the guide or datasheet??
    //i2c_write_error_handling(i2c0, BQ27742_G1_ADDR, send_buf, 3, true);
    i2c_write_error_handling(i2c0, BQ27742_G1_ADDR, send_buf, 4, true);
    // Move pointer back to Control register for reading
    send_buf[0] = 0x00;
    i2c_write_error_handling(i2c0, BQ27742_G1_ADDR, send_buf, 1, true);
    i2c_read_error_handling(i2c0, BQ27742_G1_ADDR, return_buf, 2, false);
}

void bq27742_g1_fw_version_check(){
    bq27742_g1_control(0x0002); // Read FW Version
    printf("FW Version: 0x%02x%02x\n", return_buf[1], return_buf[0]);
}